简体   繁体   中英

Take a Screenshot with RemoteWebDriver

Is there a way to get selenium screenshots with headers ? I've tried the code below but the screenshot does not have a header. I have a test case that requires clicking a link and making sure the action must bring to a new tab, so as evidence I have to attach capture there are two tabs.

public static void main (String args[]) throws IOException {
    DesiredCapabilities dc = new DesiredCapabilities();
    RemoteWebDriver driver;

    URL url = new URL("http://localhost:4444/wd/hub");
    dc.setCapability(CapabilityType.BROWSER_NAME, BrowserType.CHROME);
    dc.setCapability(CapabilityType.PLATFORM, "MAC");

    driver = new RemoteWebDriver(url, dc);
    driver.manage().window().maximize();
    driver.get("https://google.com");

    new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.name("q")));

    File getImage = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(getImage, new File("/Users/path/screenshot.jpg"));

    driver.quit();
}

Current result当前结果

Expected result预期结果

No you can't, the screenshot functionality in Selenium takes an image of the rendered DOM. The browser chrome (ie the the UI components of the browser rendered by the OS the browser is running on) is not part of the DOM so Selenium is unaware of it.

The next question is why do you want the browser chrome in your image? If you are just trying to find out the displayed URL (as your question implies) you can use the command driver.getCurrentUrl(); instead.

As @Ardesco suggested, it is not possible to take screenshot.

However i think you can use java.awt.Robot class to capture the screen. It takes the screenshot of the current screen.

Here's an snapshot of code for capturing screenshot using java.awt,

public void getScreenshot(int timeToWait) throws Exception {
    Rectangle rec = new Rectangle(
      Toolkit.getDefaultToolkit().getScreenSize());
    Robot robot = new Robot();
    BufferedImage img = robot.createScreenCapture(rectangle);
    
    ImageIO.write(img, "jpg", setupFileNamePath());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM