如何在硒中使用Ashot为整个网页拍摄屏幕截图

时间:2016-05-26 04:32:23

标签: java maven selenium junit

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

  

java.lang.VerifyError :(类:junereleasemain / NewTest,方法:   testFirstResult signature :()V)函数的不兼容参数   java.lang.Class.getDeclaredMethods0(Native Method)at   java.lang.Class.privateGetDeclaredMethods(Unknown Source)at   java.lang.Class.privateGetPublicMethods(未知来源)at   java.lang.Class.getMethods(未知来源)at   org.testng.internal.TestNGClassFinder。(TestNGClassFinder.java:63)     在org.testng.TestRunner.initMethods(TestRunner.java:424)at   org.testng.TestRunner.init(TestRunner.java:247)at   org.testng.TestRunner.init(TestRunner.java:217)at   org.testng.TestRunner。(TestRunner.java:169)at   org.testng.remote.support.RemoteTestNG6_9_10 $ 1.newTestRunner(RemoteTestNG6_9_10.java:28)     在   org.testng.remote.support.RemoteTestNG6_9_10 $ DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_9_10.java:61)     在   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 at   org.testng.TestNG.createSuiteRunners(TestNG.java:1287)at at   org.testng.TestNG.runSuitesLocally(TestNG.java:1141)at at   org.testng.TestNG.runSuites(TestNG.java:1075)at   org.testng.TestNG.run(TestNG.java:1047)at   org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)     在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

我的 脚本

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);

}

}

2 个答案:

答案 0 :(得分:1)

试试这个。

 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 :(得分:0)

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

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());
}