2 回答
TA贡献1826条经验 获得超6个赞
通过添加一个shootingStrategy,我能够在此页面底部仅捕获具有属性 id = "post-form" 的表单元素。
来自https://github.com/pazone/ashot的文档
不同的 WebDriver 截取屏幕截图的方式不同。一些 WebDriver 提供整个页面的屏幕截图,而另一些只处理视口。
...
ShootingStrategies 中有针对不同用例的内置策略。
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
import javax.imageio.ImageIO;
import java.io.File;
public class Main
{
public static void main(String args[]) throws Exception
{
System.setProperty("webdriver.gecko.driver", "./geckodriver");
System.setProperty("webdriver.firefox.bin", "/usr/bin/firefox");
WebDriver driver = new FirefoxDriver();
driver.get("https://stackoverflow.com/questions/54724963/ashot-is-not-taking-screenshot-of-correct-element");
Thread.sleep(2000);
WebElement webElement = driver.findElement(By.id("post-form"));
Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver,webElement);
ImageIO.write(screenshot.getImage(),"PNG",new File("/home/dan/ElementScreenshot.png"));
Thread.sleep(2000);
driver.quit();
}
}
输出:
TA贡献1848条经验 获得超2个赞
Protractor 也可以通过需要像“protractor-image-comparison”这样的 NPM 模块来实现此功能。例如,如果您想捕获侧边栏上的相关帖子,您可以使用以下代码。
注意:我没有用超出浏览器视口范围的大元素测试这个包,所以不能说它们将如何工作。
规格文件
describe('simple test', () => {
it('will save image', async () => {
await browser.get("https://stackoverflow.com/questions/54724963/ashot-is-not-taking-screenshot-of-correct-element");
await browser.driver.sleep(10 * 1000);
let related_questions_sidebar = element(by.className('module sidebar-related'));
await browser.executeScript('arguments[0].scrollIntoView();', related_questions_sidebar);
await browser.driver.sleep(3 * 1000);
// saveElement
await browser.protractorImageComparison.saveElement(related_questions_sidebar, 'sidebar-image');
});
});
Conf.js- 在你的 OnPrepare
onPrepare: async () => {
// await jasmine.getEnv().addReporter(new dbReporter());
const protractorImageComparison = require('protractor-image-comparison');
browser.protractorImageComparison = new protractorImageComparison(
{
baselineFolder: './screen-compare/baselines/',
screenshotPath: './screen-compare/screenshots/'
}
);
);
图片已保存
添加回答
举报