Ist es möglich, einen Screenshot mit Selenium WebDriver zu erstellen?
(Anmerkung: Nicht Selenium-Fernbedienung )
Ist es möglich, einen Screenshot mit Selenium WebDriver zu erstellen?
(Anmerkung: Nicht Selenium-Fernbedienung )
Ja, es ist möglich, mit Selenium WebDriver einen Schnappschuss von einer Webseite zu machen.
El getScreenshotAs()
Methode, die von der WebDriver-API bereitgestellt wird, übernimmt die Arbeit für uns.
Syntax: getScreenshotAs(OutputType<X> target)
Art der Rückgabe: X
Parameter: target
- Prüfen Sie die Optionen, die von OutputType
Anwendbarkeit: Nicht spezifisch für ein DOM-Element
Beispiel:
TakesScreenshot screenshot = (TakesScreenshot) driver;
File file = screenshot.getScreenshotAs(OutputType.FILE);
Weitere Einzelheiten finden Sie in dem folgenden Codeausschnitt.
public class TakeScreenShotDemo {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get(“http: //www.google.com”);
TakesScreenshot screenshot = (TakesScreenshot) driver;
File file = screenshot.getScreenshotAs(OutputType.FILE);
// Creating a destination file
File destination = new File(“newFilePath(e.g.: C: \\Folder\\ Desktop\\ snapshot.png)”);
try {
FileUtils.copyFile(file, destination);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Besuchen Sie Schnappschuss mit WebDriver um weitere Details zu erhalten.
IWebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/");
File scrFile = ((ITakesScreenshot)driver).GetScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
/**
* Take a screenshot and move to the given folder location.
*
* @param driver
* @param folderLocation
* @return screenShotFilePath
*/
public static String captureScreenshot(WebDriver driver, String folderLocation) {
// Variable to store screenshot's file path.
String screenShotFilePath = null;
// Generate unique id for screen shot name.
String uniqueId = UUID.randomUUID().toString().substring(31);
if (driver != null) {
// Generate screenshot as a file
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
// New screenshot file path with having file name
screenShotFilePath = folderLocation + File.separator + uniqueId + ".png";
// Move file to the destination location.
FileUtils.moveFile(scrFile, new File(screenShotFilePath));
}
return screenShotFilePath;
}
So wird die Selenid Projekt macht es einfacher als jede andere Methode:
import static com.codeborne.selenide.Selenide.screenshot;
screenshot("my_file_name");
Für JUnit:
@Rule
public ScreenShooter makeScreenshotOnFailure =
ScreenShooter.failedTests().succeededTests();
Für TestNG:
import com.codeborne.selenide.testng.ScreenShooter;
@Listeners({ ScreenShooter.class})
CodeJaeger ist eine Gemeinschaft für Programmierer, die täglich Hilfe erhalten..
Wir haben viele Inhalte, und Sie können auch Ihre eigenen Fragen stellen oder die Fragen anderer Leute lösen.