首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用selenium中的Ashot为整个网页拍摄屏幕快照

如何使用selenium中的Ashot为整个网页拍摄屏幕快照
EN

Stack Overflow用户
提问于 2016-05-26 04:32:22
回答 2查看 10.1K关注 0票数 3

我无法在selenium web驱动程序中使用testng和maven获得结果,它显示在控制台中

java.lang.VerifyError:(班级:军政府/新考试,方法: java.lang.Class.privateGetDeclaredMethods(Unknown签名:()V)不兼容的参数,作用在java.lang.Class.getDeclaredMethods0(原生方法)在java.lang.Class.privateGetPublicMethods(Unknown源)在java.lang.Class.getMethods(未知源)在org.testng.internal.TestNGClassFinder.(TestNGClassFinder.java:63) at org.testng.TestRunner.initMethods(TestRunner.java:424) at org.testng.TestRunner.init( org.testng.TestRunner.init(TestRunner.java:217) at org.testng.TestRunner.(TestRunner.java:169)在org.testng.remote.support.RemoteTestNG6_9_10$1.newTestRunner(RemoteTestNG6_9_10.java:28) at org.testng.remote.support.RemoteTestNG6_9_10$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_9_10.java:61) at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:594) )org.testng.SuiteRunner.init(SuiteRunner.java:168) at org.testng.SuiteRunner.(SuiteRunner.java:117) at org.testng.TestNG.createSuiteRunner(TestNG.java:1300) at org.testng.TestNG.createSuiteRunners(TestNG.java:1287) at org.testng.TestNG.runSuitesLocally(TestNG.java:1141) at org.testng.TestNG.runSuites(TestNG.java:1075) at org.testng.TestNG.run(TestNG.java:1047)在org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

我的脚本

代码语言:javascript
复制
package junereleasemain;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.screentaker.ViewportPastingStrategy;

public class NewTest {

WebDriver driver;

@BeforeTest
public void setUp() {
driver = new FirefoxDriver();
driver.manage().window().maximize();
}

@AfterTest
public void tearDown() {
driver.quit();
}
@Test
public void testFirstResult() throws InterruptedException, IOException
{
driver.get("http://www.vpl.ca");
//take the screenshot of the entire home page and save it to a png file
Screenshot screenshot = new AShot().shootingStrategy(new    ViewportPastingStrategy(100)).takeScreenshot(driver);
ImageIO.write(screenshot.getImage(), "PNG", new File("c:\\temp\\home.png"));

WebElement searchField =   driver.findElement(By.xpath("//input[@id='globalQuery']"));
searchField.click();
searchField.sendKeys("java");
WebElement searchButton =    driver.findElement(By.xpath("//input[@class='search_button']"));
searchButton.click();

Thread.sleep(3000);

//take the screenshot of the entire results page and save it to a png file
screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(100)).takeScreenshot(driver);
ImageIO.write(screenshot.getImage(), "PNG", new   File("c:\\temp\\results.png"));

WebElement searchResultLink = driver.findElement(By.xpath("(//a[@testid='bib_link'])[2]"));
searchResultLink.click();
Thread.sleep(3000);

//take the screenshot of the entire details page and save it to a png file
screenshot = new AShot().shootingStrategy(new ViewportPastingStrategy(100)).takeScreenshot(driver);
ImageIO.write(screenshot.getImage(), "PNG", new   File("c:\\temp\\details.png"));

WebElement bookTitleElement =    driver.findElement(By.xpath("//h1[@id='item_bib_title']"));
String bookTitleValue = bookTitleElement.getText();

assertEquals(bookTitleElement.isDisplayed(), true);
assertTrue(bookTitleValue.length()> 0);

}

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-01 10:02:31

试试这个。

代码语言:javascript
复制
 public class Screenshot {

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    FirefoxDriver driver = new FirefoxDriver();
    driver.get("http://yahoo.com");
    driver.manage().window().maximize();

    File scrFile = (driver.getScreenshotAs(OutputType.FILE));  
    FileUtils.copyFile(scrFile, new File("d:\\Selenium\\screenshot2.png"));
    }
}

如果您有任何疑问,请告诉我。:-)

票数 1
EN

Stack Overflow用户

发布于 2019-12-18 16:36:24

这就是我一直使用aShot的方式。这很好,因为它捕获了整个页面的图像,而不仅仅是当前窗格的视图。

代码语言:javascript
复制
private void writeToFile(Path path) {
    try {
        path.toFile().mkdirs();

        Files.deleteIfExists(path);
        Files.createFile(path);

        LOGGER.debug("Writing screenshot to: {}", path);

        writeSsWithAShot(path);

    } catch (IOException e) {
        throw new RuntimeException(
            "Unable to capture and write screenshot to " + path.toString(),
            e
        );
    }
}

private void writeSsWithAShot(Path path) throws IOException {

    ru.yandex.qatools.ashot.Screenshot aShot = new AShot()
        .shootingStrategy(ShootingStrategies.viewportPasting(1500))
        .takeScreenshot(driver);

    BufferedImage image = aShot.getImage();
    ImageIO.write(image, "png", path.toFile());
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37451676

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档