Reputation: 87
I am using selenium webdriver 2.47.1 & TestNG for automation process. In my code I have 12 different tests & at end of each and every test I checked whether the test is pass or not using if else loop. sample code is given below..
@Test(priority = 0)
public void Login() {
String UserName = ex.getExcelValue(scenarioName, 2, 4);
cd.findElement(By.xpath("//body/center/form/table/tbody/tr/td/div/div[2]/table/tbody/tr[1]/td[2]/input")).sendKeys(UserName);
String PassWord = ex.getExcelValue(scenarioName, 3, 4);
cd.findElement(By.xpath("//body/center/form/table/tbody/tr/td/div/div[2]/table/tbody/tr[2]/td[2]/input")).sendKeys(PassWord);
cd.findElement(By.xpath("//body/center/form/table/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td[2]/input")).click();
cd.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
String ExpTitle = "Demo Practice";
String ActTitle = cd.getTitle();
if(ExpTitle.equals(ActTitle)) {
ex.setExcelValue(scenarioName, 2, 8, "PASSED");
System.out.println("PASSED : User Logged in Successfully");
} else {
ex.setExcelValue(scenarioName, 2, 8, "FAILED");
System.out.println("FAILED : User Not Logged in");
}
}
I am using Jenkins for continuous integration. Now when I am running the above script Jenkins build successfully completed even the test is failed. How to change this?
Upvotes: 0
Views: 427
Reputation: 87
I have used try/finally block like below... It solved my issue
try {
Assert.assertEquals(ExpeNote1, ActeNote1);
ex.setExcelValue(scenarioName, 75, 8, "PASSED");
}
catch(Exception e) {
ex.setExcelValue(scenarioName, 75, 8, "FAILED");
}finally{
cd.switchTo().defaultContent();}
Upvotes: 0
Reputation: 2044
I think what you are looking for can be handled this way. Use testng
Assertions for your tests. That way your continuous integration environment can show your tests failing.
@Test
public void Login() {
String UserName = ex.getExcelValue(scenarioName, 2, 4);
cd.findElement(By.xpath("//body/center/form/table/tbody/tr/td/div/div[2]/table/tbody/tr[1]/td[2]/input")).sendKeys(UserName);
String PassWord = ex.getExcelValue(scenarioName, 3, 4);
cd.findElement(By.xpath("//body/center/form/table/tbody/tr/td/div/div[2]/table/tbody/tr[2]/td[2]/input")).sendKeys(PassWord);
cd.findElement(By.xpath("//body/center/form/table/tbody/tr/td/div/div[2]/table/tbody/tr[3]/td[2]/input")).click();
cd.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
String ExpTitle = "Demo Practice";
String ActTitle = cd.getTitle();
ex.setExcelValue(scenarioName, 2, 8, "FAILED");
assertEquals(ActTitle, ExpTitle, "Expected: "+ExpTitle+" but found: "+ActTitle);
ex.setExcelValue(scenarioName, 2, 8, "PASSED");
}
Few things, I think I would avoid:
priority
in tests. Makes them dependant, you would not be able to make methods run in parallelSystem.out.println()
. Assertions and Loggers are your friend here. Upvotes: 1
Reputation: 1109
As I stated in a comment earlier, you should use assertions. Then you should not have the need to add any custom configuration to your jenkins build, as the tests would actually fail if the result is incorrect.
So instead of the if-else block, you should do:
assertEquals(ActTitle, ExpTitle);
Although there should be an alternative, if you really want to use your if-else, the testNG framework offers a fail() method. I haven't used it myself but if you change your if else block to this, it should also fail the jenkins build:
if(ExpTitle.equals(ActTitle)) {
ex.setExcelValue(scenarioName, 2, 8, "PASSED");
System.out.println("PASSED : User Logged in Successfully");
} else {
ex.setExcelValue(scenarioName, 2, 8, "FAILED");
fail("FAILED : User Not Logged in");
}
}
More info on testng assertions: http://testng.org/javadoc/org/testng/Assert.html
I would recommend going with the first provided solution.
Upvotes: 0