550 Stimmen

Wie kann ich mit Selenium WebDriver einen Screenshot erstellen?

Ist es möglich, einen Screenshot mit Selenium WebDriver zu erstellen?

(Anmerkung: Nicht Selenium-Fernbedienung )

0voto

Aman Sharma Punkte 195

Ich verwende den folgenden Code in C#, um die gesamte Seite oder nur einen Browser-Screenshot zu erstellen

public void screenShot(string tcName)
{
    try
    {
        string dateTime = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
        string screenShotName = @"D:\Selenium\Project\VAM\VAM\bin" + "\\" + tcName + dateTime + ".png";
        ITakesScreenshot screen = driverScript.driver as ITakesScreenshot;
        Screenshot screenshot = screen.GetScreenshot();
        screenshot.SaveAsFile(screenShotName, System.Drawing.Imaging.ImageFormat.Png);
        if (driverScript.last == 1)
            this.writeResult("Sheet1", "Fail see Exception", "Status", driverScript.resultRowID);
    }
    catch (Exception ex)
    {
        driverScript.writeLog.writeLogToFile(ex.ToString(), "inside screenShot");
    }
}

public void fullPageScreenShot(string tcName)
{
    try
    {
        string dateTime = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
        string screenShotName = @"D:\Selenium\Project\VAM\VAM\bin" + "\\" + tcName + dateTime + ".png";
        Rectangle bounds = Screen.GetBounds(Point.Empty);
        using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
        {
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
            }
            bitmap.Save(screenShotName, System.Drawing.Imaging.ImageFormat.Png);
        }
        if (driverScript.last == 1)
            this.writeResult("Sheet1", "Pass", "Status", driverScript.resultRowID);
    }
    catch (Exception ex)
    {
        driverScript.writeLog.writeLogToFile(ex.ToString(), "inside fullPageScreenShot");
    }
}

-2voto

Jagdeep Punkte 101
import java.io.File;
import java.io.IOException;

import org.apache.maven.surefire.shade.org.apache.maven.shared.utils.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
/**
 * @author Jagdeep Jain
 *
 */
public class ScreenShotMaker {

    // take screen shot on the test failures
    public void takeScreenShot(WebDriver driver, String fileName) {
        File screenShot = ((TakesScreenshot) driver)
                .getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(screenShot, new File("src/main/webapp/screen-captures/" + fileName + ".png"));

        } catch (IOException ioe) {
            throw new RuntimeException(ioe.getMessage(), ioe);
        }
    }

}

CodeJaeger.com

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.

Powered by:

X