Kotlin selenium open browsers

Launch Internet Explorer : :

Webdriver is an Interface and InternetExplorerDriver() is a class which has implemented Webdriver Interface. You should invoke the InternetExplorerDriver() constructor to open the internet explorer browser in kotlin selenium.

To execute test script on Internet Explorer, you need to download an external .exe file IEDriverServer, a standalone server to execute test scripts on Internet Explorer.

User can download the server from https://www.seleniumhq.org/download/ and keep it in a desired location.

Set the webdriver.ie.driver system’s property to point to the location where the IEDriverServer file is kept. Setting system property is nothing but setting environment variables for the JVM, once the JVM exits these properties will be destroyed.

Choose IEdriverserver based on your work environment because there are two separate zip files for both 32 and 64 bit IE.


fun main(args: Array) {
    // Set path for Internet Explorer server executable
    System.setProperty("webdriver.ie.driver", "C:\\PATH\\IEDriverServer.exe")
    // Launch IE browser
    val driver = InternetExplorerDriver()
    // open the google
    driver.get("https://google.com")
    // close the browser
    driver.quit()
}
Few Observation From IE with Kotlin Selenium: :
  • The IE browser is slower than other browsers, slower than edge
  • IE browser zooming level must be set to 100% otherwise, you will get an exception.
  • You must also check your security settings in IE when you turn on the IE browser in the Selenium, all your areas should be enabled or disabled.
  • IE will have failures because of xpath, so try to avoid xpath and use css selector

Firefox Browser :

Webdriver is an Interface and FirefoxDriver() is a constructor of FirefoxDriver class which has implemented Webdriver Interface.

A proxy for W3C WebDriver-compatible clients to interact with Gecko-based browsers. Geckodriver provides the HTTP API described by the WebDriver protocol, so that Gecko browsers, such as Firefox.

Mozilla runs on Gecko browser engine and Gecko browser engine is developed by Mozilla.

Till firefox 47, selenium has provided inbuilt support for selenium but Firefox 48 onward Firefox has started using gecko engine, so selenium was not able to support it by default.

If you trying to open a Firefox version newer than 47, then you must use Gecko driver to open Firefox.

You can download the latest executable on the GitHub page (https://github.com/mozilla/geckodriver/releases) and make sure to download the correct driver file based on your platform and OS version.

After Downloading set the environment variable using setProperty() method in selenium


import org.openqa.selenium.firefox.FirefoxDriver
fun main(args: Array<String>) {
    // Set geckodriver path
    System.setProperty("webdriver.gecko.driver", "C:\\PATH\\geckodriver.exe")
    // Open browser
    val driver = FirefoxDriver()
    // open google
    driver.get("https://google.com")
    // close firefox browser
    driver.close()
}

Open Google Chrome :

Like Internet Explorer, Google Chrome also requires user to download an external utility ChromeDriver, a standalone server to execute test scripts on Google Chrome.

User can download the ChromeDriver file from https://chromedriver.storage.googleapis.com/index.html . The rest of the process would remain the same as that of Internet Explorer.


import org.openqa.selenium.chrome.ChromeDriver
fun main(args: Array) {
    // Set path for Chrome Driver executable path
    System.setProperty("webdriver.chrome.driver", "C:/lib/chromedriver.exe")
    // Launch ChromeDriver
    val driver = ChromeDriver()
    // Open the web page
    driver.get("https://google.com")
    // close the browser
    driver.quit()
}

Open Safari Browser :

SafariDriver comes along with the Selenium server’s package, so user is not required to download or set any external entity. It is readily available to execute the test scripts on Safari.


import org.openqa.selenium.safari.SafariDriver
fun main(args: Array) {
    // Launch SafariDriver
    val driver = SafariDriver()
    // Open the web page
    driver.get("https://google.com")
    // close the browser and quite the driver
    driver.quit()
}

Get functions in Kotlin Selenium :

get() :

get() function launches a new browser and opens the specified URL in the browser instance. The get() function takes a single string type parameter that is usually a URL of application/Webpage.


							
fun main(args: Array) {
    val driver = FirefoxDriver()
    driver.get("https://chercher.tech")
}
							

close() :

close() function closes the web browser window that the user is currently working on or we can also say the window that is being currently accessed by the kotlin selenium. close() function does not accept any parameter and returns nothing (yes, return type of nothing).


							fun main(args: Array) {
    val driver = FirefoxDriver()
    driver.get("https://chercher.tech")
    driver.close()
}
							

quit() :

quit() function not only closes the current window but closes down all the windows that the program has opened. quit() function in kotlin selenium does not accept any parameter and returns nothing (yes, return type of nothing).


							fun main(args: Array) {
    val driver = FirefoxDriver()
    driver.get("https://chercher.tech")
    driver.quit()
}
							

currentUrl :

The currentUrl property is used to retrieve the URL of the webpage the user is currently opened.


							driver.currentUrl
							

title :

The title property is used to retrieve the title of the webpage the user is currently working on. A null string is returned if the webpage has no title.


							driver.title
							

pageSource :

pageSource property is used to retrieve the page source of the webpage the user is currently opened, pageSource property returns a string value.

pageSource property can be used with various string operations like contains() to assert the presence of the specified string value.


							driver.pageSource
							

Browser Size functions in kotlin selenium

maximize() :

maximize() function in kotlin selenium maximizes a browser if it is not already maximized


							driver.manage().window().maximize();
							

fullscreen() :

fullscreen() function will make the browser into full screen if it s not already in full screen.


							fun main(args: Array) {
    val driver = FirefoxDriver()
    driver.manage().window().fullscreen()
    driver.get("https://chercher.tech")
}
							

position :

position property fetches the position of the browser window, the retrieved position would be relative to the upper left corner of the screen.


							fun main(args: Array) {
    val driver = FirefoxDriver()
    driver.get("https://chercher.tech")
    val position = driver.manage().window().position
    val y = position.y
    val x = position.x
    val Y1 = position.getY()
    val X1 = position.getX()
    println("The y position of window : $y or $Y1")
    println("The x position of window : $x or $X1")
}
							

We can also set the position of the window by assigning a Point class object to the position property in kotlin selenium


							// below line sets the size of the browser
    driver.manage().window().position = Point(200, 200)
							

size :

size property fetches the size of the browser window, the retrieved position would be relative to the upper left corner of the screen. It returns Dimension class object, so we have to get using keys in Dimension class (x and y)


val driver = FirefoxDriver()
    driver.get("https://chercher.tech")
    val size = driver.manage().window().size
    val height = size.height
    val width = size.width
    val height1 = size.getHeight()
    val width2 = size.getWidth()
    println("The height of the window by height variable : $height")
    println("The height of the window by height1 variable : $height1")
    println("by methods")
    println("The width of the window by getWidth() variable : $width")
    println("The width of the window by getWidth() variable : $width2")
							

We can also set the size of the window by assigning a Dimension class object to the size property.


							val driver = FirefoxDriver()
    driver.get("https://chercher.tech")
    // below line sets the size of the browser
    driver.manage().window().size = Dimension(200, 200)
							

Navigate to() function :

to() function opens a web page in the browser window, It allows to a pass a String parameter or URL Type parameter, and its return type is a void.


							driver.navigate().to("https://google.com)
							

Navigate forward page :

navigate().forward() function takes you forward by one page on the browser's history.


							driver.navigate().forward()
							

Navigate back page :

navigate().back() function is used to go back by one page on the browser's history.


							driver.navigate().back()
							

Refresh page :

navigate().refresh() function refreshes the current page.


							driver.navigate().refresh()
							

Complete program for basic commands :


							import java.util.concurrent.TimeUnit
import org.openqa.selenium.firefox.FirefoxDriver
fun main(args: Array) {
    // set the geckodriver.exe property
    System.setProperty("webdriver.gecko.driver", "D:\\PATH\\geckodriver.exe")
    // open firefox
    val driver = FirefoxDriver()
    driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES)
    // open webpage
    driver.get("https://google.com")
    // Storing Title name in String variable 
	// so that we can get the title name
    val btitle = driver.title
    // Printing the Title
    println("Title is : $btitle")
    // Storing Title length in Int variable because 
	// we are dealing with Integers that's why int is used.
    val bTitleLength = driver.title.length
    // Printing Title length
    println("Length of the Title is : $bTitleLength")
    // Storing URL in String variable
    val b1 = driver.currentUrl
    // Printing URL on Console Window
    println("URL is : $b1")
    // Storing URL length
    val b2 = driver.currentUrl.length
    // Printing URL length on console Window
    println("URL length is : $b2")
    driver.navigate().to("https://chercher.tech")
    // COMMAND FOR NAVIGATION THROUGH PAGES.
    //Navigate to previous Page.
    driver.navigate().back()
    Thread.sleep(1000)
    val b5 = driver.title
    // Print the text of the previous page
    println("Text of the previous page is : $b5")
    //Navigate to Next page.
    driver.navigate().forward()
    Thread.sleep(1000)
    // Print the text of the NEXT page
    val b6 = driver.title
    println("Text of the NEXT page is : $b6")
    // COMMAND FOR REFRESHING THE CURRENT PAGE
    driver.navigate().refresh()
    // PAGE SOURCE COMMAND
    // Storing Page Source in String variable.
    val pageSrc = driver.pageSource
    Thread.sleep(1000)
    // Printing the Page Source
    // println("Page source : $pageSrc")
    // Storing Page Source length in Int variable
    val pageSrcLen = driver.pageSource.length
    // Printing the Page Source length
    println("Length of the page source is : $pageSrcLen")
    //close the browser or page currently which is having the focus.
    driver.close()
    //Close all the windows.
    //driver.quit();
}
							

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