String Actualvalue= d.findElement(By.xpath("//[@id=\"wrapper\"]/main/div[2]/div/div[1]/div/div[1]/div[2]/div/table/tbody/tr[1]/td[1]/a")).getText(); Assert.assertEquals(Actualvalue, "jumlga"); captureScreen(d, "Fail"); 2 Answers
The assert should not be put before your capture screen. Because it will immediately shutdown the test process so your code
captureScreen(d, "Fail"); will be not reachable
This is how i usually do:
boolean result = false; try { // do stuff here result = true; } catch(Exception_class_Name ex) { // code to handle error and capture screen shot captureScreen(d, "Fail"); } # then using assert Assert.assertEquals(result, true); Comments
1.
A good solution will be is to use a report framework like allure-reports. Read here:allure-reports
2.
We don't our tests to be ugly by adding try catch in every test so we will use Listeners which are using an annotations system to "Listen" to our tests and act accordingly.
Example:
public class listeners extends commonOps implements ITestListener { public void onTestFailure(ITestResult iTestResult) { System.out.println("------------------ Starting Test: " + iTestResult.getName() + " Failed ------------------"); if (platform.equalsIgnoreCase("web")) saveScreenshot(); } } Please note I only used the relevant method to your question and I suggest you read here: TestNG Listeners
Now we will want to take a screenshot built in method by allure-reports every time a test fails so will add this method inside our listeners class
Example:
@Attachment(value = "Page Screen-Shot", type = "image/png") public byte[] saveScreenshot(){ return ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES); } Test example
@Listeners(listeners.class) public class myTest extends commonOps { @Test(description = "Test01: Add numbers and verify") @Description("Test Description: Using Allure reports annotations") public void test01_myFirstTest(){ Assert.assertEquals(result, true) } } Note we're using at the beginning of the class an annotation of @Listeners(listeners.class) which allows our listeners to listen to our test, please mind the (listeners.class) can be any class you named your listeners.
The @Description is related to allure-reports and as the code snip suggests you can add additional info about the test.
Finally, our Assert.assertEquals(result, true) will take a screen shot in case the assertion fails because we enabled our Listener.class to it.