我有一个测试方法,在下面检查空密码:
@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));
}
1条答案
按热度按时间carvr3hs1#
创建一个新类(截图),使用方法“capturescrenshot”来截图
然后在测试用例中调用该类
希望这会有帮助。