• Selenium Video Tutorials

Kotlin Selenium 教程



Selenium 可以與多種語言一起使用,例如 Java、Python、Kotlin、JavaScript、Ruby 等。Selenium 廣泛用於 Web 自動化測試。Selenium 是一款開源且可移植的自動化軟體測試工具,用於測試 Web 應用程式。它能夠跨不同瀏覽器和作業系統執行。Selenium 不僅僅是一個工具,而是一套工具,可以幫助測試人員更有效地自動化基於 Web 的應用程式。

使用 Kotlin 設定 Selenium

步驟 1 - 使用以下連結在我們的系統中安裝 Maven:https://maven.apache.org/download.cgi

要更詳細地瞭解如何設定 Maven,請參閱以下連結:Maven 環境

透過執行以下命令確認已安裝的Maven版本:

mvn –version

它將顯示以下輸出

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"

執行的命令的輸出表明系統中安裝的 Maven 版本為 Apache Maven 3.9.6。

步驟 2 - 從以下連結下載並安裝程式碼編輯器 IntelliJ,以編寫和執行 Selenium 測試:https://www.jetbrains.com/idea/

要更詳細地瞭解如何設定 IntelliJ,請參閱以下連結:Selenium IntelliJ

步驟 3 - 啟動 IntelliJ 並單擊“新建專案”按鈕。

Selenium Kotlin Tutorial 1

步驟 4 - 輸入專案名稱、位置,然後選擇 Kotlin 作為語言,Maven 作為構建系統。

Selenium Kotlin Tutorial 2

步驟 5 - 從“構建”選單構建專案。

Selenium Kotlin Tutorial 3

步驟 6 - 從以下連結新增 Selenium Maven 依賴項:https://mvnrepository.com/artifact/

步驟 7 - 儲存包含所有依賴項的 pom.xml 並更新 maven 專案。

步驟 8 - 在 Maven 專案 SeleniumKotlin 中,右鍵單擊 test 資料夾中的 kotlin 資料夾,然後建立一個包,例如 TestCases。

步驟 9 - 右鍵單擊 TestCases 包,然後選擇“新建”選單,然後單擊“Kotlin 類/檔案”選單。

Selenium Kotlin Tutorial 4

步驟 10 - 在“新建 Kotlin 類/檔案”欄位中輸入檔名,例如 MyTest,然後按 Enter 鍵。

Selenium Kotlin Tutorial 5

步驟 11 - 在 MyTest.kt 檔案中新增以下程式碼。

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://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");

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

在 pom.xml 檔案中新增的整體依賴項:

<?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>

在此示例中遵循的專案結構:

Selenium Kotlin Tutorial 6

步驟 12 - 右鍵單擊並選擇“執行‘MyRun’”選項。等待執行完成。

它將顯示以下輸出

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

在上面的示例中,我們首先啟動了 Edge 瀏覽器並啟動了一個應用程式,然後檢索了瀏覽器標題,並在控制檯中收到了訊息 - 瀏覽器標題:Selenium 實踐 - 學生登錄檔單

控制檯中的結果顯示已執行的總測試數:1,因為只有一個方法帶有 @Test 註釋 - launchBrow()。

最後,收到了訊息透過:1程序已完成,退出程式碼為 0,表明程式碼已成功執行。

使用 Selenium Kotlin 識別元素並檢查其功能

導航到網頁後,我們必須與頁面上可用的 Web 元素進行互動,例如單擊連結/按鈕、在編輯框中輸入文字等,以完成我們的自動化測試用例。

為此,我們的首要任務應該是識別元素。我們可以使用連結的連結文字進行識別,並使用方法 findElement(By.linkText("<連結文字的值>"))。這樣,應該返回具有匹配連結文字值的第一個元素。

如果找不到具有匹配連結文字值的元素,則應丟擲 NoSuchElementException。

讓我們看看上面圖片中討論的連結的 html 程式碼:

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

上面圖片中突出顯示的連結 - 已移動 的連結文字值為 Moved。讓我們在識別它之後單擊它。單擊“已移動”連結後,文字 - 連結已返回狀態 301 和狀態文字“永久移動” 將出現在網頁上。最後,我們將退出瀏覽器。

Selenium Kotlin Tutorial 8

示例

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://tutorialspoint.tw/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()
   }
}

它將顯示以下輸出

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

在上面的示例中,我們首先啟動了 Edge 瀏覽器並啟動了一個應用程式,然後單擊了該連結。單擊連結後,我們獲得了文字,並在控制檯中收到了訊息 - 獲得的文字是:連結已返回狀態 301 和狀態文字“永久移動”

控制檯中的結果顯示已執行的總測試數:1,因為只有一個方法帶有 @Test 註釋 - accessLnkGetText()

最後,收到了訊息透過:1程序已完成,退出程式碼為 0,表明程式碼已成功執行。

這總結了我們對 Selenium - Kotlin 教程的全面講解。我們首先介紹瞭如何使用 Kotlin 設定 Selenium,如何使用 Selenium Kotlin 啟動瀏覽器和退出會話,以及如何識別元素並使用 Selenium Kotlin 檢查其功能。

這使您掌握了 Selenium - Kotlin 教程的深入知識。明智的做法是繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。

廣告