我想运行并行屏幕截图测试,但出现错误。 我提高了 Xmx 和 Xms,它们并没有吃掉我所有的 RAM,我有 32GB。 但在吃掉定义的内存后,它们会在随机点失败。 有内存优化的解决方案吗? 我在某处放置了超过 18 张 PNG 格式的图像,大约需要 200-600-800kb
这是 gradle.properties。
org.gradle.parallel=false
org.gradle.caching=false
org.gradle.console=verbose
org.gradle.jvmargs=-Xmx16g -Xms4g -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
systemProp.junit.jupiter.execution.parallel.enabled=true
systemProp.junit.jupiter.execution.parallel.mode.default=concurrent
systemProp.junit.jupiter.execution.parallel.mode.classes.default=concurrent
systemProp.junit.jupiter.execution.parallel.config.strategy=dynamic
systemProp.junit.jupiter.execution.parallel.config.dynamic.factor=1
我的导入,有最新版本的库。
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.comparison.ImageDiff;
import ru.yandex.qatools.ashot.comparison.ImageDiffer;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
还有一些代码与我发现差异
protected int differenceBetweenPages(String pageName,
String actualUrl,
int scrollTime) throws IOException {
open(actualUrl);
actual = capturePage(scrollTime);
ImageIO.write(actual.getImage(), "png", actualImg(pageName));
attach = new FileInputStream(actualImg(pageName));
Allure.addAttachment("Actual page", "image/png", attach, ".png");
attach.close();
expected = expectedScreenshot(pageName);
diff = new ImageDiffer().makeDiff(expected, actual);
ImageIO.write(diff.getMarkedImage(), "png", diffImg(pageName));
attach = new FileInputStream(diffImg(pageName));
Allure.addAttachment("Diff Page", "image/png", attach, ".png");
attach.close();
return diff.getDiffSize();
}
最佳答案
我在使用 Ashot 库时也遇到了问题。我改用 ShutterBug,它工作正常。 https://github.com/assertthat/selenium-shutterbug
build.gradle 片段
compile 'com.assertthat:selenium-shutterbug:0.9.2'
用于捕获页面屏幕截图并将其附加到 Allure 报告的 API
@Attachment(value = "Page screenshot", type = "image/png")
public static byte[] captureScreenshot(WebDriver driver) {
BufferedImage screenshot = Shutterbug.shootPage(driver, ScrollStrategy.WHOLE_PAGE, true).getImage();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
ImageIO.write(screenshot, "png", outputStream);
} catch (IOException e) {
e.printStackTrace();
}
return outputStream.toByteArray();
}
关于java - Ashot Java 堆空间错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59272317/