当我的测试用例断言失败时,我试图在selenium webdriver中拍摄一个屏幕截图。请找到以下代码供您参考。
public abstract class TestChromeDriver {
public WebDriver driver;
public WebDriver getDriver(){
return driver;
}
@Parameters("browser")
@BeforeTest
public void browserInitialize(String browser){
if(browser.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver", "D:\\Aishu\\Test_Java\\TestNG Groups\\External JAR's\\chromedriver.exe");
driver = new ChromeDriver();
}
}
@AfterTest
public void tearDownDriver(){
if(driver != null){
driver.quit();
}
}
}
public class TestClass extends TestChromeDriver{
@Test
public void tc1_functionality(){
driver.navigate().to("https://www.google.com");
driver.manage().window().maximize();
driver.findElement(By.name("q")).sendKeys("Selenium");
driver.findElement(By.name("q")).sendKeys(Keys.ENTER);
Assert.assertEquals("test", driver.getCurrentUrl().toLowerCase().contains("selenium"));
}
}
public class TestListener extends TestListenerAdapter{
@Override
public void onTestFailure(ITestResult result){
Object currentClass = result.getInstance();
WebDriver driver = ((TestChromeDriver)currentClass).getDriver();
File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(file, new File("D:\\Aishu\\Test_Java\\TestNG Groups\\Images"+result.getName()+"_"+System.currentTimeMillis()+".png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
断言在我的测试方法中失败了。但即使屏幕截图没有用于失败的断言。不确定我在TestNG.XML
文件甚至代码级别中做错了什么
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="suite1">
<listeners>
<listener class-name = "org.test.chromeDriver.TestListener"/>
</listeners>
<test name="chromeTest">
<parameter name="browser" value="chrome"/>
<classes>
<class name = "org.test.chromeDriver.TestChromeDriver"/>
<class name = "org.test.chromeDriver.TestClass"/>
</classes>
</test>
</suite>
非常感谢任何帮助。