如何使用selenium 4和java编写一个通用的base64屏幕截图方法,并将其附加到测试失败的扩展报告中当前ATR



方法

public static String getScreenshot(String screenshotName) throws IOException {
String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
TakesScreenshot ts = (TakesScreenshot) Base.getDriver();
File source = ts.getScreenshotAs(OutputType.FILE);
String destination = System.getProperty("user.dir") + "/FailedTestsScreenshots/" + screenshotName + dateName
+ ".png";
File finalDestination = new File(destination);
FileUtils.copyFile(source, finalDestination);
return destination;
}

TestListener.java

@Override
public synchronized void onTestFailure(ITestResult result) {
System.out.println((result.getMethod().getMethodName() + " failed!"));
test.get().fail(result.getThrowable());
if (result.getStatus() == ITestResult.FAILURE) {
try {
String imgPath = Utilities.getScreenshot(result.getName());
test.get().addScreenCaptureFromPath(imgPath);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

我想得到基本64格式的屏幕截图,这样它就可以很容易地分享给任何人。我使用的是硒版本4.0.0和扩展报告版本3.1.5

我有一个类似的要求,即在base64中存储屏幕截图,因为报告将与我的团队中的多个利益相关者共享。以下解决方案对我来说效果很好。

屏幕截图.java

public static String getScreenshot(WebDriver driver) {
String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
return screenshotBase64;
}

TestListener.java

@Override
public void onTestFailure(ITestResult result) {
test.fail("Test case Failed");
test.fail("Failed step: ",MediaEntityBuilder.createScreenCaptureFromBase64String("data:image/png;base64,"+Screenshot.getScreenshot(driver)).build());
test.fail(result.getThrowable());
}

以下是我为此使用的一些方法(Joe Walnes的Base64编码:https://github.com/jenkinsci/xstream/blob/master/xstream/src/java/com/thoughtworks/xstream/core/util/Base64Encoder.java(:

File full_scrn = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
full_scrn.deleteOnExit();

以后我会使用这些方法。。。

private static final char[] SIXTY_FOUR_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray();
public String FileToBase64(String ThisScreenshotPath)
{
String Base64String = "";
Path screen_path = Paths.get(ThisScreenshotPath);
byte[] screenshot_in_bytes = null;
try
{
screenshot_in_bytes = Files.readAllBytes(screen_path);
}
catch (Exception ex)
{
System.out.println("Exception reading screenshot file: " + ex.toString()); 
}
Base64String = encode(screenshot_in_bytes);   
return Base64String;
}
public String encode(byte[] input) {
StringBuffer result = new StringBuffer();
int outputCharCount = 0;
for (int i = 0; i < input.length; i += 3) {
int remaining = Math.min(3, input.length - i);
int oneBigNumber = (input[i] & 0xff) << 16 | (remaining <= 1 ? 0 : input[i + 1] & 0xff) << 8 | (remaining <= 2 ? 0 : input[i + 2] & 0xff);
for (int j = 0; j < 4; j++) result.append(remaining + 1 > j ? SIXTY_FOUR_CHARS[0x3f & oneBigNumber >> 6 * (3 - j)] : '=');
if ((outputCharCount += 4) % 76 == 0) result.append('n');
}
return result.toString();
}

对于HTML输出:

"<img src="data:image/png;base64," + ThisScreenshotBase64String + ""...

我不使用Extent报告,所以在这方面没有帮助。

最新更新