【问题标题】:How do i screenshot chrome browser when my tests fail and before the chrome browser closes (@After)当我的测试失败和 chrome 浏览器关闭之前,我如何截图 chrome 浏览器(@After)
【发布时间】:2020-01-23 10:47:03
【问题描述】:

我已经运行了这段代码,并且在 chrome 浏览器关闭后截取了屏幕截图 (@After) 如果我注释掉 CloseBrowser();屏幕截图被捕获,但 chromebrowser 保持打开状态。 我希望在测试失败时捕获屏幕截图,然后关闭浏览器。

总结 当前在浏览器关闭后截取的截图,只是一个空白的.png 我希望在浏览器关闭前测试失败时捕获屏幕截图

谢谢

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
                }
            }
        };
    }
}

【问题讨论】:

    标签: java maven selenium junit selenium-chromedriver


    【解决方案1】:

    您可以实现 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
    }
    

    }

    【讨论】:

      【解决方案2】:

      所以我找到了一种实现屏幕截图的方法。我创建了一个截取屏幕截图的方法。我已经尝试并捕获了我的测试代码并捕获了异常并调用该方法来截取屏幕截图。

             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
                  }
      

      【讨论】:

        【解决方案3】:

        这可能会有所帮助: https://github.com/junit-team/junit4/issues/383

        新的“TestRule”改变了规则执行的顺序

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-09-18
          • 1970-01-01
          • 2019-02-03
          • 1970-01-01
          • 1970-01-01
          • 2014-04-14
          相关资源
          最近更新 更多