JUnit Test Fails in Jenkins - java

My JUnit Tests run fine in eclipse and pass locally however in Jenkins my 1st test passes and 2nd one fails and I know why but cant figure out the solution. The Jenkins output says it cant find the element however it is there and I have the proper waits.
Here is the 1st test case that passes.
#Before
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\eclipse\\chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
homeUrl = "https://myadp.adpcorp.com/wps/myportal/main/myADP_new";
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(homeUrl);
}
//Full test
#Test
public void fullTest() throws Exception {
step01_VerifyHomePage();
}
// Go to Home page and verify title
public void step01_VerifyHomePage() throws Exception {
driver.getTitle().contains(homeTitle);
Thread.sleep(3000);
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
This is the one that fails in Jenkins
//Full test
#Test
public void fullTest() throws Exception {
step02_HoverMyWorkSpace();
}
public void step02_HoverMyWorkSpace() throws Exception {
WebDriverWait wait = new WebDriverWait(driver,20000);
wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("DivMyWorkList"))));
if ( driver.findElement(By.id("DivMyWorkList")).isDisplayed()){
System.out.println("DivMyWorkList Visiable");
}else{
System.out.println("DivMyWorkList NOT Visiable");
}
// Assert.assertEquals(true, workspace.isDisplayed());
// Thread.sleep(3000);
}
#After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
Jenkins Output
The problem i think is that Jenkins cant find the element because the page is not coming up in what ever environment it is using. When I use
wait.until(ExpectedConditions.visibilityOfElementLocated((By.id("DivMyWorkList")))); Jenkins test hangs and times out.
If this is the case is there a way around this? It dose not make sense that the 1st test passes by using the same url and the 2nd one uses same url to the exact same page and it can find the title but not the web element.
I am using a VM and created a free style project on it.
I have been working on this for the past week any help would be appreciated.

Related

I've got a problem with error: org.openqa.selenium.NoSuchWindowException: no such window: window was already closed

When i clicked on loginBtn, then first site is closing and another opening. Then if I try to find something by any selector->org.openqa.selenium.NoSuchWindowException: no such window: window was already closed:/
I've no idea what's going on.I was trying to find iFRame, switch to another window and still getting error.
That's the flow of test:
#Test
public void LogIn() throws AWTException, InterruptedException {
MainPage mainPage= new MainPage();
mainPage.logIn()
.loginToProfile()
.writeNameAndPassword()
.clickLoginBtn()
.clickWizyty();<--here is the problem
}}
this is method which goes to another page:
public OnMySite clickLoginBtn() {
loginBtn.click();
return new OnMySite();
}
and here is the problem, on everything i getting exception
public void clickWizyty() throws InterruptedException {
Thread.sleep(3000);
//WebElement iFrame= DriverManager.getWebDriver().findElement(By.tagName("iframe"));
//DriverManager.getWebDriver().switchTo().frame(iFrame);
String currentTabHandle = DriverManager.getWebDriver().**getWindowHandle()**;
String newTabHandle = DriverManager.getWebDriver().getWindowHandles()
.stream()
.filter(handle -> !handle.equals(currentTabHandle))
.findFirst()
.get();
WaitForElement.waitUntilElemembtIsVisible(wizyty);
wizyty.click();
}}'''
even here: getWindowHandle(); its showing "no such window... "

chrome browser doesn't closes after finishing test

#BeforeMethod
public void setup ()
{
System.setProperty("webdriver.chrome.driver", "src\\test\\resources\\chromedriver.exe");
driver = new ChromeDriver(); // Opens Chrome browser
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
//driver.manage().window().maximize();
}
#AfterMethod
public void tearDown() throws Exception
{
Thread.sleep(5*1000);
driver.quit();
}
#Test
public void browserCommandsTests()
{
try {
driver.get("http://www.costco.com");
Thread.sleep(3 * 1000);
driver.navigate().to("http://www.facebook.com");
Thread.sleep(3 * 1000);
driver.navigate().back();
Thread.sleep(3 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
When i'm testing this code after opening the urls it doesn't closes the
testing browser.
it opens only 1 link and closes browser when i'm getting only 1 url. but as soon as i try to get 2nd url it opens 2nd url and after it doesn't closes the browser under test.
It is your tag #AfterMethod change it to AfterClass (also you can try close instead of quit):
#AfterClass
public void tearDown() throws Exception
{
driver.close();
}
Also, friendly advice, using the thread.sleep will cause you more grief than joy. The alternative is to use driver.findElement() in a try/catch and depending on the result (true/false) direct your flow from there.
Best of luck!
#AfterMethod
public void tearDown() throws Exception
{
driver.quit();
}

Leanft Custom Framework HTML reports are not generated

Below is my SAMPLE code in which i am trying to create a simple report using the Leanft in which i am getting on result xml file.
#Test
public void Google() throws Exception {
Reporter.init();
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
Thread.sleep(4000);
if( driver.getTitle().equalsIgnoreCase("google")){
Reporter.reportEvent("test", "test",Status.Failed);
}
Reporter.generateReport();
driver.quit();
}
As specified in the docs, if you're going to use a custom framework you'll also need to initialize the SDK (SDK.init() and SDK.cleanup())
E.G.
public static void main(String [] args){
// initialize the SDK and report only once per process
try{
ModifiableSDKConfiguration config = new ModifiableSDKConfiguration();
config.setServerAddress(new URI("ws://myServerAddress:5095"));
SDK.init(config);
Reporter.init();
//put your test code here.
//Generate the report and cleanup the SDK usage.
Reporter.generateReport();
SDK.cleanup();
} catch(Exception e){
}
}
I see nothing wrong with your code as the report generates as you told it too.
However, I believe you were wanting something more like this to show that it PASSES when it finds the Google title:
#Test
public void Google() throws Exception {
Reporter.init();
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com");
Thread.sleep(4000);
if( driver.getTitle().equalsIgnoreCase("google")){
Reporter.reportEvent("test", "test",Status.Passed);
} else {
Reporter.reportEvent("test","test",Status.Failed);
}
Reporter.generateReport();
driver.quit();
}

Selenium WebDriver using TestNG and Excel

Hello i really need help with Selenium WebDriver using TestNG and Excel
i try to get data from excel to open browser and navigate URL. its work successfully and terminal and testng report showing test pass but its not open browser or doing anything its just run its self and show report
Config File
public void openBrowser(String browser){
try {
if (browser.equals("Mozilla")) {
driver = new FirefoxDriver();
} else if(browser.equals("IE")){
driver = new InternetExplorerDriver();
} else if(browser.equals("Chrome")){
System.setProperty("webdriver.chrome.driver", "\\Applications\\Google Chrome.app\\Contents\\MacOS\\Google Chrome ");
driver = new ChromeDriver();
}
} catch (Exception e) {
}
}
public void navigate(String baseUrl){
try {
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.navigate().to(baseUrl);
} catch (Exception e) {
}
}
And Test Execute File
public class NewTest {
public String exPath = Config.filePath;
public String exName = Config.fileName;
public String exWrSheet = "Logiin Functional Test";
public Config config;
#BeforeTest
public void openBrowser() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String browser = Excel.getCellData(1, 2);
config.openBrowser(browser);
}
#BeforeMethod
public void navigate() {
config = new Config();
Excel.setExcelFile(exPath+exName, exWrSheet);
String baseUrl = Excel.getCellData(2, 2);
config.navigate(baseUrl);
}
#Test
public void test() {
System.out.println("Test");
}
#AfterTest
public void closeBroser() {
//Config.tearDown();
}
I don't have quite enough rep to make a comment, which I would prefer here, but if you aren't getting any sort of exception, I'd suspect the value you're getting for the browser variable is not matching anything in your if-then-else in openBrowser and then falling through. Step through the code with a debugger or just add:
String browser = Excel.getCellData(1, 2);
System.out.println("Browser value from Excel =" + browser);
config.openBrowser(browser);
to see what you're reading from the file.
1 - TestNg is always pass because you are using "void" method and you catch "all" exception
2 - No browser opened because in openBrowser(String browser), NullPointException throws and you already catch it.
-> you need to init an instance of WebDriver and pass it through your test.

Test Upload File In Selenium

Currently i wish to run selenium for test excel upload. And my coding didn't working as expected. Just wondering how to resolve it. Every time i run the automated testing my code will be stop at this step:sendKeys("C:\Users\user\Desktop\JSPL Style Excel Upload.csv") without any error message show. Any suggestion?
#Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://localhost:7101/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
public void testJava() throws Exception {
driver.get(baseUrl + "SCM_Telephone_Accounting_2-ViewController-context-root/faces/Main_Menu");
driver.findElement(By.id("np2:cni10")).click();
driver.findElement(By.id("r1:1:if1::content")).click();
driver.findElement(By.id("r1:1:if1::content")).clear();
driver.findElement(By.id("r1:1:if1::content")).sendKeys("C:\\Users\\user\\Desktop\\JSPL Style Excel Upload.csv");
driver.findElement(By.id("r1:1:cb1")).click();
}

Categories