我正在尝试自动化一个测试用例,我必须截取不同网站中存在的特定屏幕的屏幕截图。具体来说,我正在尝试测试一个特定的复选框是否对齐。下面是我的脚本,我正在使用 Ashot 截屏。脚本登录到三个系统,然后单击我想要的链接点击它,但是最后一个 URL 的屏幕截图与每个 URL 的屏幕截图相比只有一个屏幕截图。请帮助我解释如何迭代 Ashot,以便它为每个网站截取屏幕截图,而不是它现在正在做什么。本质上,除了截取屏幕截图外,所有步骤都被迭代,我希望脚本也能遍历屏幕截图。
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.*;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;
public class checkboxAlignment {
String driverPath = "C:\\Users\\xxx\\Desktop\\Work\\chromedriver.exe";
public WebDriver driver;
public String expected = null;
public String actual = null;
@BeforeTest
public void launchBrowser() {
System.out.println("launching chrome browser");
System.setProperty("webdriver.chrome.driver", driverPath);
driver = new ChromeDriver();
}
@Test(dataProvider = "URLprovider")
private void notePrice(String url) throws IOException {
driver.get(url);
System.out.println(driver.getCurrentUrl());
WebElement email = driver.findElement(By.xpath("//input[@id='Email']"));
WebElement password = driver.findElement(By.xpath("//input[@id='PWD']"));
WebElement submit = driver.findElement(By.xpath("//button[@type='submit']"));
email.sendKeys("xxx@xxx.com");
password.sendKeys("xxx");
submit.click();
System.out.println(driver.getTitle());
driver.manage().window().maximize();
//click on the PI tab
driver.findElement(By.id("IDpi")).click();
// This doesnot iterate, only one screenshot is taken by Ashot
Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000)).takeScreenshot(driver);
ImageIO.write(fpScreenshot.getImage(),"PNG",new File("C://Users//dir//eclipse-workspace//someDir//screenshots//checkbox.jpg"));
}
@DataProvider(name = "URLprovider")
private Object[][] getURLs() {
return new Object[][] { { "http://www.someURL.com/A" }, { "http://www.someurl.com/B" },
{ "http://www.someurl.com/C" } };
}
}