【发布时间】: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