Java 如何使用selenium中的Ashot拍摄整个网页的屏幕截图
我无法使用testng和maven在selenium web驱动程序中获得结果 它在控制台中显示为 java.lang.VerifyError:(类:junReleaseMain/NewTest,方法: testFirstResult签名:()V)处函数的参数不兼容 位于的java.lang.Class.getDeclaredMethods0(本机方法) java.lang.Class.privateGetDeclaredMethods(未知源代码)位于 java.lang.Class.privateGetPublicMethods(未知源代码)位于 java.lang.Class.getMethods(未知源代码)位于 TestNGClassFinder.java:63) 位于org.testng.TestRunner.initMethods(TestRunner.java:424) org.testng.TestRunner.init(TestRunner.java:247)位于 org.testng.TestRunner.init(TestRunner.java:217)位于 TestRunner.(TestRunner.java:169)位于 RemoteTestNG6_9_10$1.newTestRunner(RemoteTestNG6_9_10.java:28) 在 org.testng.remote.support.RemoteTestNG6_9_10$delegatingestrunnerfactory.newestrunner(RemoteTestNG6_9_10.java:61) 在 org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:594) 位于org.testng.SuiteRunner.init(SuiteRunner.java:168) org.testng.SuiteRunner.(SuiteRunner.java:117)位于 org.testng.testng.createSuiteRunner(testng.java:1300)位于 org.testng.testng.createSuiteRunners(testng.java:1287)位于 org.testng.testng.runSuitesLocal(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) 我的脚本是Java 如何使用selenium中的Ashot拍摄整个网页的屏幕截图,java,maven,selenium,junit,Java,Maven,Selenium,Junit,我无法使用testng和maven在selenium web驱动程序中获得结果 它在控制台中显示为 java.lang.VerifyError:(类:junReleaseMain/NewTest,方法: testFirstResult签名:()V)处函数的参数不兼容 位于的java.lang.Class.getDeclaredMethods0(本机方法) java.lang.Class.privateGetDeclaredMethods(未知源代码)位于 java.lang.Class.priv
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());
}
您想拍摄网页截图或日志截图吗?stacktrace表明,失败可能是因为使用的库的两个版本存在冲突,但存在一些差异。您是否可以共享
pom.xml
以了解您使用的依赖项的版本。@KishanPatel,想拍摄网页截图吗?请参见答案Rajesh如何使用selenium webdriver在网页/表格中查找重复的文本/记录?我的qyuestoin是,现在我指向一个网页,它包含n个文本,我怎么能发现网页包含重复的文本,,,,比如在该网页中显示/列出了2次文本(斑马),它应该发现斑马是重复的,就像我想要上面场景的脚本,如果网页有数千条文本,我是否只需要找到重复的记录,我无法单独搜索对吗。。。。。我只想打印重复的记录,你能详细说明一下吗?那些记录/文本是什么?当你说斑马作为文本时,请告诉我。它在哪里,我是说它是如何显示的?它只是一个文本还是某个字段?你能分享一个小截图吗?我需要知道它是什么类型的文本。