1

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 Current result

Expected result Expected result

frianH
  • 7,295
  • 6
  • 20
  • 45
  • Not with Selenium, it can only interact with html. You can take desktop screenshot https://stackoverflow.com/questions/58305/is-there-a-way-to-take-a-screenshot-using-java-and-save-it-to-some-sort-of-image – Guy Jun 19 '19 at 05:04
  • Yes I know it can use desktop screenshots, but in cases where the server is not where the script was executed, I can't use desktop screenshots. The problem is my script uses `RemoteWebDriver`. Any other suggestions? – frianH Jun 19 '19 at 05:12

2 Answers2

0

No you can't, the screenshot functionality in Selenium takes an image of the rendered DOM. The browser chrome (i.e. 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.

Ardesco
  • 7,281
  • 26
  • 49
  • 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. – frianH Jun 19 '19 at 08:41
  • @Frian You can get the number of tabs by using `getWindowHandles` method and do assertions on that. – Fenio Jun 19 '19 at 09:15
  • @Fenio yes right, i see, but that for the script. I need capture screen for ui interface as evidence, this will be seen by others – frianH Jun 19 '19 at 09:22
  • @Frian The enough evidence should be the assertion. – Fenio Jun 19 '19 at 09:24
  • @Fenio sorry, i need evidence in the form of capture :) – frianH Jun 19 '19 at 09:26
  • Sadly you are out of luck. You can capture an image showing what is rendered in the DOM and you can pull back the current URL and print it out in your logs. You can't get selenium to take a image of the desktop though. You could use something like a Java robot class to take an image of the desktop of the machine that the test code is running on, that will not help you if you are using a remotewebdriver though. And depending upon what else is running and what's in. the foreground it may not help that much. – Ardesco Jun 19 '19 at 09:42
  • @ardesco right, with java robot i can capture my desktop, it's fine work if i execute in localhost, the problem my server available on other machine, initialize driver with `RemoteWebDriver`. Okay, now i'm understand this issue is complicated, thanks all for this disccussion – frianH Jun 20 '19 at 01:37
0

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());
}
Santosh
  • 874
  • 11
  • 21
  • I can't achieve with `Robot` class because this is only for client screen, can't shot the server screen. `Robot` class fine work if the server there is at localhost, while our server is not on localhost. Thanks for your reply. – frianH Jan 21 '21 at 07:14