繁体   English   中英

如何在 selenium 中使用 Ashot 为整个网页截屏

[英]How to use Ashot in selenium to take screen shot for entire webpage

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

java.lang.VerifyError: (class: junereleasemain/NewTest, method: testFirstResult signature: ()V) java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Unknown Source) 函数的参数不兼容在 java.lang.Class.privateGetPublicMethods(Unknown Source) 在 java.lang.Class.getMethods(Unknown Source) 在 org.testng.internal.TestNGClassFinder.(TestNGClassFinder.java:63) 在 org.testng.TestRunner.initMethods(TestRunner) .java:424) 在 org.testng.TestRunner.init(TestRunner.java:247) 在 org.testng.TestRunner.init(TestRunner.java:217) 在 org.testng.TestRunner.(TestRunner.java:169) 在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_9_10.java:28) at (SuiteRunner.java:594) at 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) 在 org.testng.TestNG.runSuites(TestNG.java:1075) 在 org.testng.TestNG.run(TestNG.java:1047) 在 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);

}

}

试试这个。

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

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

这就是我一直在使用 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());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM