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.support.events.WebDriverEventListener
import org.openqa.selenium.firefox.FirefoxDriver
import org.apache.commons.io.FileUtils
import org.openqa.selenium.TakesScreenshot
import java.io.File


fun main(args: Array) {
    System.setProperty("webdriver.gecko.driver", "D:\\Eclipse progs\\driverserver\\geckodriver.exe")
    val driver = FirefoxDriver()
    driver.get("https://chercher.tech/practice/practice-pop-ups-selenium-webdriver")

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

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
You can also share knowledge by Adding a topic here


Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group