• Selenium Video Tutorials

Selenium with Kotlin Tutorial



Selenium can be used with multiple languages like Java, Python, Kotlin, JavaScript, Ruby, and so on. Selenium is used extensively for web automation testing. Selenium is an open-source and a portable automated software testing tool for testing web applications. It has capabilities to operate across different browsers and operating systems. Selenium is not just a single tool but a set of tools that helps testers to automate web-based applications more efficiently.

Setup Selenium with Kotlin

Step 1 − Install Maven in our system using the link − https://maven.apache.org/download.cgi.

To get a more detailed view on how set up Maven, refer to the link − Maven Environment.

Confirm the version of the Maven installed by running the following command −

mvn –version

It will show the following output

Apache Maven 3.9.6 (bc0240f3c744dd6b6ec2920b3cd08dcc295161ae)
Maven home: /opt/homebrew/Cellar/maven/3.9.6/libexec
Java version: 21.0.1, vendor: Homebrew, runtime: /opt/homebrew/Cellar/openjdk/21.0.1/libexec/openjdk.jdk/Contents/Home
Default locale: en_IN, platform encoding: UTF-8
OS name: "mac os x", version: "14.0", arch: "aarch64", family: "mac"

The output of the command executed signified that the Maven version installed in the system is Apache Maven 3.9.6.

Step 2 − Download and install the code editor IntelliJ from the link to write and run the Selenium test − https://www.jetbrains.com/idea/.

To get a more detailed view on how set up IntelliJ, refer to the link − Selenium IntelliJ.

Step 3 − Launch IntelliJ and click on the New Project button.

Selenium Kotlin Tutorial 1

Step 4 − Enter the Project Name, Location, and then select the the Language as Kotlin and Maven as the Build Systems.

Selenium Kotlin Tutorial 2

Step 5 − Build the project from the Build menu.

Selenium Kotlin Tutorial 3

Step 6 − Add the Selenium Maven dependencies from the link − https://mvnrepository.com/artifact/.

Step 7 − Save the pom.xml with all the dependencies and update the maven project.

Step 8 − Within the Maven project SeleniumKotlin, right click on the kotlin folder within the test folder, and create a package, say TestCases.

Step 9 − Right click on the TestCases package, and select the New menu, then click on the Kotlin Class/File menu.

Selenium Kotlin Tutorial 4

Step 10 − Enter a filename, say MyTest within the New Kotlin Class/File field and press Enter.

Selenium Kotlin Tutorial 5

Step 11 − Add the below code in the MyTest.kt file.

package TestCases

import org.openqa.selenium.WebDriver
import org.openqa.selenium.edge.EdgeDriver
import java.time.Duration
import org.testng.annotations.Test

class MyTest {

   @Test
   fun launchBrow() {

      // Initiate Webdriver
      val driver: WebDriver = EdgeDriver();

      // adding implicit wait of 15 seconds
      driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));

      // URL launch
      driver.get("https://www.tutorialspoint.com/selenium/practice/selenium_automation_practice.php");

      // get browser title after browser launch
      System.out.println("Browser title: " + driver.title);
   }
}

Overall dependencies added in the pom.xml file −

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">
   
   <modelVersion>4.0.0</modelVersion>
   <groupId>org.example</groupId>
   <artifactId>SeleniumKotlin</artifactId>
   <version>1.0-SNAPSHOT</version>
   
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <kotlin.code.style>official</kotlin.code.style>
      <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget>
   </properties>
   
   <repositories>
      <repository>
         <id>mavenCentral</id>
         <url>https://repo1.maven.org/maven2/</url>
      </repository>
   </repositories>
   
   <build>
      <sourceDirectory>src/main/kotlin</sourceDirectory>
      <testSourceDirectory>src/test/kotlin</testSourceDirectory>
      <plugins>
         <plugin>
            <groupId>org.jetbrains.kotlin</groupId>
            <artifactId>kotlin-maven-plugin</artifactId>
            <version>1.9.23</version>
            <executions>
               <execution>
                  <id>compile</id>
                  <phase>compile</phase>
                  <goals>
                     <goal>compile</goal>
                  </goals>
               </execution>
               <execution>
                  <id>test-compile</id>
                  <phase>test-compile</phase>
                  <goals>
                     <goal>test-compile</goal>
                  </goals>
               </execution>
            </executions>
         </plugin>
         <plugin>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.2</version>
         </plugin>
         <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.22.2</version>
         </plugin>
         <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <configuration>
               <mainClass>MainKt</mainClass>
            </configuration>
         </plugin>
      </plugins>
   </build>
   
   <dependencies>
      <dependency>
         <groupId>org.jetbrains.kotlin</groupId>
         <artifactId>kotlin-test-junit5</artifactId>
         <version>1.9.23</version>
         <scope>test</scope>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.19.0</version>
      </dependency>
      
      <!-- https://mvnrepository.com/artifact/org.testng/testng -->
      <dependency>
         <groupId>org.junit.jupiter</groupId>
         <artifactId>junit-jupiter-engine</artifactId>
         <version>5.10.0</version>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.jetbrains.kotlin</groupId>
         <artifactId>kotlin-stdlib</artifactId>
         <version>1.9.23</version>
      </dependency>
   </dependencies>
</project>

Project Structure followed in this example −

Selenium Kotlin Tutorial 6

Step 12 − Right click and select Run ‘MyRun’ option. Wait till the run is completed.

It will show the following output

Browser title: Selenium Practice - Student Registration Form

===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

In the above example, we had first launched the Edge browser and launched an application then retrieved the browser title and in the console received the message - Browser title: Selenium Practice - Student Registration Form.

The result in the console shows Total tests run: 1, as there is one method with @Test annotations - launchBrow().

Finally, the message Passes: 1 and Process finished with exit code 0 was received, signifying successful execution of the code.

Identify Element and Check Its Functionality Using Selenium Kotlin

Once we navigate to a webpage, we have to interact with the web elements available on the page like clicking a link/button, entering text within an edit box, and so on to complete our automation test case.

For this, our first job should be to identify the element. We can use the link text for a link for its identification and utilize the method findElement(By.linkText("<value of link text>")). With this, the first element with the matching value of the link text should be returned.

In case there is no element with the matching value of the link text, NoSuchElementException should be thrown.

Let us see the html code of the link as discussed before in the below image −

Selenium Kotlin Tutorial 7
<a href="javascript:void(0)" id="moved" onclick="shide('move')">Moved</a>

The link - Moved highlighted in the above image has the link text value as Moved. Let us click on it after identifying it. The text - Link has responded with status 301 and status text Moved Permanently would appear on the web page on clicking the Moved link. Finally we would quit the browser.

Selenium Kotlin Tutorial 8

Example

package TestCases

import org.openqa.selenium.By
import org.openqa.selenium.WebDriver
import org.openqa.selenium.edge.EdgeDriver
import java.time.Duration
import org.testng.annotations.Test

class MyTest {
   
   @Test
   fun accessLnkGetText() {
   
      // Initiate Webdriver
      val driver: WebDriver = EdgeDriver();
      
      // adding implicit wait of 15 seconds
      driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15));
      
      // URL launch
      driver.get("https://www.tutorialspoint.com/selenium/practice/links.php");
      
      // identify link then click
      val lnk = driver.findElement(By.linkText("Moved"));
      lnk.click();
      
      // identify text the get text
      val txt = driver.findElement(By.xpath("/html/body/main/div/div/div[2]/div[3]"));
      System.out.println("Text Obtained is: " + txt.text);
      
      // quit browser
      driver.quit()
   }
}

It will show the following output

Text Obtained is: Link has responded with status 301 and status text Moved Permanently
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================

Process finished with exit code 0

In the above example, we had first launched the Edge browser and launched an application then clicked on the link. After clicking the link, we had obtained the text and in the console received the message - Text Obtained is: Link has responded with status 301 and status text Moved Permanently.

The result in the console shows Total tests run: 1, as there is one method with @Test annotations - accessLnkGetText().

Finally, the message Passes: 1 and Process finished with exit code 0 was received, signifying successful execution of the code.

This concludes our comprehensive take on the tutorial on Selenium - Kotlin Tutorial. We’ve started with describing how to set up Selenium with Kotlin, how to launch a browser and quit a session using the Selenium Kotlin, and how to identify an element and check its functionality using Selenium Kotlin.

This equips you with in-depth knowledge of the Selenium - Kotlin Tutorial. It is wise to keep practicing what you’ve learned and exploring others relevant to Selenium to deepen your understanding and expand your horizons.

Advertisements