Screenshots in Kotlin selenium

Selenium Kotlin has built-in functionality to take screenshots across the page and it’s quite easy to use as well.

Kotlin Selenium has the TakesScreenshot interface to capture the screenshots during execution of your Selenium kotlin script.

The TakesScreenshot interface has a method named getScreenshotAs( ) which can capture a screenshot and store it in any location specified by us. With this, we can take a screenshot of pages across various browsers like Chrome, Firefox, IE, Opera etc using their respective web-drivers.

The WebDriver interface extends TakesScreenshot and all browser driver classes like ChromeDriver, FirefoxDriver, InternetExplorerDriver, EdgeDriver, OperaDriver etc, implements TakesScreenshot.

The WebDriver extending TakesScreenshot class and the getScreenshotAs( ) method, capture only the visible area of the web page in all the browsers and not the full page

Why do we need screenshot :

  • Take screenshots when tests fail at the exact moment of failure to help developers debug what might have gone wrong.
  • Take screenshots during your test to capture specific elements in your viewport to test specific layouts.
  • Capture each step of your test case both functionally and with a screenshot.

import org.openqa.selenium.*
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.TakesScreenshot

fun main(args: Array) {
    System.setProperty("webdriver.gecko.driver", "D:\\Eclipse progs\\driverserver\\geckodriver.exe")
    val driver = FirefoxDriver()

    val source_file = (driver as TakesScreenshot).getScreenshotAs(OutputType.FILE)
    FileUtils.copyFile(source_file, File("C:\\screenshot.png"))

About Author

Article is written by Pavan (a) KarthiQ. Well, I am serving notice period in an MNC, Bangalore. I thought to enrich every person knowledge a little, I always have a feeling, when we teach something, we will learn more than what you know. Knowledge is the only thing that doubles when you spend it.

I have also created the reporter for Protractor Jasmine. Use for your projects without any hesitation

You can also share knowledge by Adding a topic here

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions