【问题标题】:Saving java robot screenshot into testng listener with Extent Reports使用范围报告将 java 机器人屏幕截图保存到 testng 侦听器中
【发布时间】:2021-04-11 20:17:05
【问题描述】:

我有一个测试方法可以检查空密码:

@Test(priority = 4,description = "Empty password validation")
public void emptyPassword(Method method, ITestContext context) {
        ExcelData ex = new ExcelData();
        try {
            ISuite suite = context.getSuite();
            String env = (String)suite.getAttribute("Env");

            String [] envLinks = ex.getEnvLinks(env);
            homePage.loadLMSLoginPage(envLinks[0]);
            ExtentTestManager.startTest(method.getName(), "Empty Password Validation");
            loginPage
                    .verifyEmptyPassword("USERNAME","",context);
        }catch(Exception e){
            e.printStackTrace();
        }
    }

这是密码验证的方法,因为密码为空时系统会弹出警告,selenium webdriver无法截图,所以我使用机器人框架。我正在尝试通过下面的 ITestContext 将此屏幕截图传递给我的 Listener 类:

public LoginPage verifyEmptyPassword(String id, String pswd, ITestContext context) throws AWTException {

        try {

            writeText(userId, id);
            writeText(userPwd, pswd);
            click(logInButtonId);
            Alert passwordAlert = driver.switchTo().alert();
            String passText = passwordAlert.getText();

            Robot robot = new Robot();
            Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
            BufferedImage image = robot.createScreenCapture(screenRect);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            context.setAttribute("PasswordAlert", passText);
            ImageIO.write(image, "PNG", out);
            out.flush();
            byte[] encodeBase64 = Base64.encodeBase64(out.toByteArray());
            context.setAttribute("AlertScreenshot", encodeBase64);

            passwordAlert.accept();

            Assert.assertTrue(passText.contains("Enter Password !"));
        }catch (IOException e){
            e.printStackTrace();;
        }
        return this;
    }

在我的侦听器类中,在我的 onTestSuccess 方法中,如果它根据测试名称检测到该方法是“emptyPassword”,它将以不同的方式将屏幕截图保存在另一个方法 getPasswordAlert() 中,该方法位于我的 Listener 类正在扩展的另一个类中:

public void onTestSuccess(ITestResult iTestResult) {
        System.out.println("Test" + getTestMethodName(iTestResult) + " passed");
        //ExtentReports log operation for passed tests.
        Object testClass = iTestResult.getInstance();
        ITestContext context = iTestResult.getTestContext();
        ExtentTest extent = ExtentTestManager.getTest();
        WebDriver webDriver = ((BaseTest) testClass).getDriver();
        String base64Screenshot = "data:image/png;base64," + ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BASE64);
          if (getTestMethodName(iTestResult).equals("emptyPassword")) {
            getPasswordAlert(context,extent);
        } else {
            ExtentTestManager.getTest().log(LogStatus.PASS, "Test passed",
                    ExtentTestManager.getTest().addBase64ScreenShot(base64Screenshot));
        }
    }

这是我尝试从上下文中获取屏幕截图并保存的地方,但它在 ExtentReports 中出现如下所示的空白,一直在尝试调试此问题但找不到答案。有人可以帮忙吗?

public void getPasswordAlert(ITestContext context, ExtentTest extent) {
        String message = (String) context.getAttribute("PasswordAlert");
        byte [] imgByte = (byte[]) context.getAttribute("AlertScreenshot");
        String img = new String(imgByte);
        extent.log(LogStatus.PASS, "Test passed, alert message:" + message,
                extent.addBase64ScreenShot(img));

    }

【问题讨论】:

  • 您正在使用 Java 的 Robot Framework?我无法将robot.createScreenCapture(screenRect) 映射到任何东西,你确定robotframework 标记在问题上是正确的吗?

标签: java selenium testing robotframework extentreports


【解决方案1】:

使用“captureScreenshot”方法创建一个新类(屏幕截图)以截取屏幕截图

 try{
            projectPath = System.getProperty("user.dir");
            TakesScreenshot ts=(TakesScreenshot)driver;
            File source=ts.getScreenshotAs(OutputType.FILE);
            FileUtils.copyFile(source, new File("./screenshots/"+ screenshotName+".png"));
            System.out.println("Screenshot taken successfully");
            }
            catch (Exception e){
                System.out.println("Exception while taking screenshot "+e.getMessage());
            }
} 

然后在您的测试用例中调用该类

screenshot.captureScreenshot();
test1.log(Status.FAIL, "Test failed.", MediaEntityBuilder.createScreenCaptureFromPath(projectPath+ "/screenshots/imagename.png").build());

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 2018-06-20
    • 2017-04-25
    • 2013-07-10
    • 2021-08-22
    相关资源
    最近更新 更多