3

With below mentioned code,if the test case is pass-screenshot captured successfully and displayed in report.But when the test is failed--screenshot is not displayed.Even screenshot hyperlink is not displayed in report.Anybody can sort out the mistake in code?

package listeners; import java.io.File; import java.io.IOException; import java.text.Format; import java.text.SimpleDateFormat; import java.util.Date; import org.apache.commons.io.FileUtils; import org.openqa.selenium.By; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.ITestResult; import org.testng.Reporter; import org.testng.annotations.Listeners; import org.testng.annotations.Test; import org.testng.ITestResult; import org.testng.Reporter; import org.testng.TestListenerAdapter; import java.util.logging.Logger; @Listeners public class CountryChoserLayer extends TestListenerAdapter { @Test(priority=1) public void choseCountry() throws Exception{ driver.findElement(By.id("intselect")).sendKeys("India"); driver.findElement(By.xpath(".//*[@id='countryChooser']/a/img")).click(); //window.onbeforeunload = null; Date date=new Date(); Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss"); File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png"); FileUtils.copyFile(scrnsht, new File(NewFileNamePath)); System.out.println(NewFileNamePath); Reporter.log("<a href=\"" + NewFileNamePath + "\">Passed Screenshot</a>"); System.out.println("---------------------------------------"); System.out.println("Country choser layer test case-Success"); System.out.println("---------------------------------------"); } public String baseurl="http://www.sears.com/shc/s/CountryChooserView?storeId=10153&catalogId=12605"; public WebDriver driver; public int Count = 0; @Test(priority=0) public void openBrowser() { driver = new FirefoxDriver(); driver.manage().deleteAllCookies(); driver.get(baseurl); } @Test(priority=2) public void closeBrowser() { driver.quit(); } @Override public void onTestFailure(ITestResult result){ Reporter.log("Fail"); System.out.println("BBB"); //Reporter.setCurrentTestResult(result); Date date=new Date(); Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss"); File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); //File scrFile = ((TakesScreenshot) WebDriver.globalDriverInstance).getScreenshotAs(OutputType.FILE); String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png"); //System.out.println("AAA" + NewFileNamePath); try { //System.out.println("CCC"); FileUtils.copyFile(scrnsht,new File(NewFileNamePath)); System.out.println(NewFileNamePath); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("DDD"); e.printStackTrace(); } Reporter.log("<a href=\"" + NewFileNamePath + "\">Failed Screenshot</a>"); Reporter.setCurrentTestResult(null); System.out.println("---------------------------------------"); System.out.println("Country choser layer test case Failed"); System.out.println("---------------------------------------"); } @Override public void onTestSkipped(ITestResult result) { // will be called after test will be skipped Reporter.log("Skip"); } @Override public void onTestSuccess(ITestResult result) { // will be called after test will pass Reporter.log("Pass"); } } 

1 Answer 1

2

Your onTestFailure method is not being called because you didn't specify listener for your test class. You are missing a value in @Listeners annotation. It should be something like

@Listeners({CountryChoserLayer.class}) 

You can find more ways of specifying a listener in official TestNg's documentation.

Another problem you are likely to encounter would be NullPointerException while trying to take screenshot in onTestFailure method. The easiest workaround for that would be changing the declaration of driver field to static. I run the code with those fixes and I got the report with screenshot.

I must add that in my opinion putting both test and listener methods into one class is not a good practice.

Sign up to request clarification or add additional context in comments.

1 Comment

you can try this project - github.com/yev/seleniumMvnScreenshot. By adding the annotation @org.testng.annotations.Listeners(FailTestScreenshotListener.class) to your selenium tests, you will have the screenshot automatically in your maven target folder

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.