我正在尝试在 Selenium 中捕获屏幕截图,但是出现错误 - File source = ts.getScreenshotAs(OutputType.FILE); 我已经在网上搜索了代码,他们已经按原样给出了它。现在我不知道应该用这段代码替换什么。我得到的错误是 - 文件无法解析或不是字段。我的完整代码是:
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import com.aventstack.extentreports.Status;
import com.google.common.io.Files;
import com.mongodb.MapReduceCommand.OutputType;
public class UtilityMethods extends ExtentReportBaseClass{
static WebDriver driver;
public static WebDriver openBrowser(String browsers) {
System.out.println("initiated browser is " + browsers);
if (browsers.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.firefox.marionette", "");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("marionatte", false);
FirefoxOptions opt = new FirefoxOptions();
opt.merge(dc);
driver = new FirefoxDriver(opt);
}
else if (browsers.equalsIgnoreCase("Chrome")) {
// String driverPath = System.getProperty("user.dir") +
// "\\src\\Drivers\\chromedriver";
// System.setProperty("webdriver.chrome.driver", driverPath);
System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
return driver;
}
public static void launchWebsite(String URL) {
driver.get(URL);
}
public static By locateByElement(String locator, String locatorValue) {
By by = null;
switch (locator) {
case "id":
by = By.id(locatorValue);
break;
case "name":
by = By.name(locatorValue);
break;
case "linktext":
by = By.linkText(locatorValue);
break;
case "partialLinkText":
by = By.partialLinkText(locatorValue);
break;
case "className":
by = By.className(locatorValue);
break;
case "tagName":
by = By.tagName(locatorValue);
break;
case "cssSelector":
by = By.cssSelector(locatorValue);
break;
case "xpath":
by = By.xpath(locatorValue);
break;
}
System.out.println("value of by is" + by);
return by;
}
public void sendDataById(String locatorValue, String userData) {
driver.findElement(By.id(locatorValue)).sendKeys(userData);
}
public void sendData(String element) {
String[] var = element.split("###");
String loctype = var[0];
String locval = var[1];
// System.out.println(locval);
String uservalue = var[2];
// System.out.println(locval);
// System.out.println(uservalue);
By locator = locateByElement(loctype, locval);
System.out.println(loctype);
System.out.println(locval);
driver.findElement(locator).sendKeys(uservalue);
}
public WebDriver getdriver() {
if (driver == null) {
driver = new FirefoxDriver();
return driver;
} else {
return driver;
}
}
public static String capture(WebDriver driver,String screenShotName) throws IOException
{
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
String dest = System.getProperty("user.dir") +"/TestCasesScreenshot"+screenShotName+".png";
File destination = new File(dest);
Files.copy(source, destination);
return dest;
}
public void clickElement(String locElem) throws IOException {
try {
System.out.println(locElem);
String[] var = locElem.split("###");
String loctype = var[0];
String locval = var[1];
System.out.println(loctype);
System.out.println(locval);
By locator = locateByElement(loctype, locval);
driver.findElement(locator).click();
}
catch(Exception e)
{
System.out.println("Catched Exception, Element not found");
String screenShotPath = UtilityMethods.capture(driver, "screenShotName");
test.log(Status.FAIL, "Snapshot below: " + test.addScreenCaptureFromPath(screenShotPath));
}
}
}