Java 当我的测试失败时,在chrome浏览器关闭之前(@After),如何截屏chrome浏览器
我已经运行了这段代码,在chrome浏览器关闭(@after)后截图 如果我把CloseBrowser()注释掉;截图被捕获,但chromebrowser保持打开状态。 我想截图捕捉失败的测试,然后关闭浏览器 总之 屏幕截图当前在浏览器关闭后捕获,它只是一个空白的.png 我希望在浏览器关闭前测试失败时捕获屏幕截图 谢谢Java 当我的测试失败时,在chrome浏览器关闭之前(@After),如何截屏chrome浏览器,java,maven,selenium,junit,selenium-chromedriver,Java,Maven,Selenium,Junit,Selenium Chromedriver,我已经运行了这段代码,在chrome浏览器关闭(@after)后截图 如果我把CloseBrowser()注释掉;截图被捕获,但chromebrowser保持打开状态。 我想截图捕捉失败的测试,然后关闭浏览器 总之 屏幕截图当前在浏览器关闭后捕获,它只是一个空白的.png 我希望在浏览器关闭前测试失败时捕获屏幕截图 谢谢 public class TestClass extends classHelper//has BrowserSetup(); and CloseBrowser(); {
public class TestClass extends classHelper//has BrowserSetup(); and CloseBrowser(); {
@Rule
public ScreenshotTestRule my = new ScreenshotTestRule();
@Before
public void BeforeTest()
{
BrowserSetup();// launches chromedriver browser
}
@Test
public void ViewAssetPage()
{
//My test code here//And want to take screenshot on failure
}
@After
public void AfterTest() throws InterruptedException
{
CloseBrowser();//closes the browser after test passes or fails
}
}
class ScreenshotTestRule implements MethodRule {
public Statement apply(final Statement statement, final FrameworkMethod frameworkMethod, final Object o) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
try {
statement.evaluate();
} catch (Throwable t) {
captureScreenshot(frameworkMethod.getName());
throw t; // rethrow to allow the failure to be reported to JUnit
}
}
public void captureScreenshot(String fileName) {
try {
new File("target/surefire-reports/").mkdirs(); // Insure directory is there
FileOutputStream out = new FileOutputStream("target/surefire-reports/screenshot-" + fileName + ".png");
out.write(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));
out.close();
} catch (Exception e) {
// No need to crash the tests if the screenshot fails
}
}
};
}
}
您可以实现TestNG侦听器,以便在测试之前或之后执行代码 或者测试失败或成功等 如下面所示实现它,并将您的屏幕截图放在我显示的方法中
public class Listeners implements ITestListener {
Methods…
And put the screenshot code inside the method below:
@Override
public void onTestFailure(ITestResult result) {
code for screenshot
}
}您可以实现TestNG侦听器,以便在测试之前或之后执行代码 或者测试失败或成功等 如下面所示实现它,并将您的屏幕截图放在我显示的方法中
public class Listeners implements ITestListener {
Methods…
And put the screenshot code inside the method below:
@Override
public void onTestFailure(ITestResult result) {
code for screenshot
}
}因此我找到了一种实现屏幕截图的方法。我创建了一个截图方法。我对测试代码进行了尝试和捕获,捕获了一个异常,并调用了该方法来截图
public class TestClass extends classHelper//has BrowserSetup(); and CloseBrowser(); {`
@Rule
public ScreenshotTestRule my = new ScreenshotTestRule();
@Before
public void BeforeTest()
{
BrowserSetup();// launches chromedriver browser
}
@Test
public void ViewAssetPage()
{
try
{
//My test code here//And want to take screenshot on failure
}
catch(Exception e)
{
//print e
takeScreenShot();
}
}
@After
public void AfterTest() throws InterruptedException
{
CloseBrowser();//closes the browser after test passes or fails
}
}
///////////////////////////////////////////
void takeScreenShot()
{
try
{
int num = 0;
String fileName = "SS"+NAME.getMethodName()+".png";//name of file/s you wish to create
String dir = "src/test/screenshot";//directory where screenshots live
new File(dir).mkdirs();//makes new directory if does not exist
File myFile = new File(dir,fileName);//creates file in a directory n specified name
while (myFile.exists())//if file name exists increment name with +1
{
fileName = "SS"+NAME.getMethodName()+(num++)+".png";
myFile = new File(dir,fileName);
}
FileOutputStream out = new FileOutputStream(myFile);//creates an output for the created file
out.write(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));//Takes screenshot and writes the screenshot data to the created file
//FileOutputStream out = new FileOutputStream("target/surefire-reports/" + fileName);
out.close();//closes the outputstream for the file
}
catch (Exception e)
{
// No need to crash the tests if the screenshot fails
}
所以我找到了一种实现屏幕截图的方法。我创建了一个截图方法。我对测试代码进行了尝试和捕获,捕获了一个异常,并调用了该方法来截图
public class TestClass extends classHelper//has BrowserSetup(); and CloseBrowser(); {`
@Rule
public ScreenshotTestRule my = new ScreenshotTestRule();
@Before
public void BeforeTest()
{
BrowserSetup();// launches chromedriver browser
}
@Test
public void ViewAssetPage()
{
try
{
//My test code here//And want to take screenshot on failure
}
catch(Exception e)
{
//print e
takeScreenShot();
}
}
@After
public void AfterTest() throws InterruptedException
{
CloseBrowser();//closes the browser after test passes or fails
}
}
///////////////////////////////////////////
void takeScreenShot()
{
try
{
int num = 0;
String fileName = "SS"+NAME.getMethodName()+".png";//name of file/s you wish to create
String dir = "src/test/screenshot";//directory where screenshots live
new File(dir).mkdirs();//makes new directory if does not exist
File myFile = new File(dir,fileName);//creates file in a directory n specified name
while (myFile.exists())//if file name exists increment name with +1
{
fileName = "SS"+NAME.getMethodName()+(num++)+".png";
myFile = new File(dir,fileName);
}
FileOutputStream out = new FileOutputStream(myFile);//creates an output for the created file
out.write(((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES));//Takes screenshot and writes the screenshot data to the created file
//FileOutputStream out = new FileOutputStream("target/surefire-reports/" + fileName);
out.close();//closes the outputstream for the file
}
catch (Exception e)
{
// No need to crash the tests if the screenshot fails
}
这可能有助于:
使用新的“TestRule”更改了规则执行的顺序这可能有助于:
使用新的“TestRule”更改了规则执行的顺序