selenium—将java robot屏幕截图保存到带有扩展报告的testng侦听器中

t40tm48m  于 2021-06-26  发布在  Java
关注(0)|答案(1)|浏览(521)

我有一个测试方法,在下面检查空密码:

@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无法截图,因此我使用robot框架。我正试图通过下面的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;
    }

在我的listener类中,在我的ontestsuccess方法中,如果它根据testname检测到方法为“emptypassword”,它将在我的listener类正在扩展的另一个类中的另一个方法getpasswordalert()中以不同方式保存屏幕截图:

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

    }

carvr3hs

carvr3hs1#

创建一个新类(截图),使用方法“capturescrenshot”来截图

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());

希望这会有帮助。

相关问题