![](/img/trans.png)
[英]How to automatically take full screen shot of a webpage include fixed element using java selenium webdriver ashot firefox
[英]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.