• Selenium Video Tutorials

Selenium Java 教程



Selenium 用於自動化測試 web 應用的測試用例。它支援多種程式語言,例如 Java、PythonC# 等。

如何使用 Java 設定 Selenium?

步驟 1 - 從以下連結下載並安裝 Java:Java 下載

有關如何設定 Java 的更詳細檢視,請參閱以下連結:Java 環境設定

成功安裝 Java 後,我們可以透過在命令提示符中執行命令:java 來確認其安裝。

C:\java 

步驟 2 - 接下來,我們將透過執行命令:java –version 來確認已安裝的 Java 版本。

java –version

它將顯示以下輸出:

openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment Homebrew (build 17.0.9+0)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.9+0, mixed mode, sharing)

步驟 3 - 使用以下連結在我們的系統中安裝 Maven:下載 Apache Maven

接下來,我們將透過執行以下命令來確認已安裝的Maven版本:

透過執行命令:mvn –version 來確認已安裝的 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。

步驟 4 - 安裝名為IntelliJ的程式碼編輯器來編寫和執行 Selenium 測試。

步驟 5 - 在 Main.java 檔案中新增以下程式碼。

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class Main {
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);
      
      // URL launch 
      driver.get("https://tutorialspoint.tw/selenium/practice/resizable.php");

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

在 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>SeleniumJava</artifactId>
   <version>1.0-SNAPSHOT</version>

   <properties>
      <maven.compiler.source>16</maven.compiler.source>
      <maven.compiler.target>16</maven.compiler.target>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   </properties>
   
   <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
   <dependencies>
      <dependency>
         <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>4.19.0</version>
      </dependency>
   </dependencies>
</project>

步驟 6 - 右鍵單擊並選擇“執行‘Main.main()’”選項。等待執行完成。

步驟 7 - 應該啟動 Chrome 瀏覽器,控制檯中顯示的訊息應為 - 瀏覽器標題:Selenium 實踐 - 可調整大小

最後,收到訊息程序已完成,退出程式碼為 0,表示程式碼已成功執行。

此外,Chrome 瀏覽器啟動時,頂部會顯示訊息Chrome 受自動化測試軟體控制

使用 Selenium Java 啟動瀏覽器並退出驅動程式

我們可以使用 driver.get() 方法啟動瀏覽器並開啟應用程式,最後使用 close() 方法關閉瀏覽器。

package org.example;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.edge.EdgeDriver;
import java.util.concurrent.TimeUnit;

public class CloseBrow{
   public static void main(String[] args) throws InterruptedException {

      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 12 secs
      driver.manage().timeouts().implicitlyWait(12, TimeUnit.SECONDS);

      // URL launch and get the browser title
      driver.get("https://tutorialspoint.tw/selenium/practice/droppable.php");
      System.out.println( "Browser title obtained : " + driver.getTitle());

      // close browser
      driver.close();
   }
}

它將顯示以下輸出:

Browser title obtained: Browser Title: Selenium Practice - Droppable

Process finished with exit code 0

在上面的示例中,我們首先啟動了 Chrome 瀏覽器,然後獲取了瀏覽器標題,然後關閉了瀏覽器,在控制檯中收到了訊息 - 獲取的瀏覽器標題:Selenium 實踐 - 學生登錄檔單

如何使用 Selenium Java 識別元素並檢查其功能?

啟動應用程式後,使用者會與頁面上的 web 元素互動,例如單擊連結或按鈕、在輸入框中輸入文字等,以建立自動化測試用例。

第一個任務是找到元素。Selenium 中有多個定位器,即 id、類、類名、名稱、連結文字、部分連結文字、標籤名、css 和 xpath。它們與 Java 中的 findElement() 方法一起使用。

例如,findElement(By.name(“name”)) 將識別具有名稱屬性值的第一個 web 元素。如果名稱屬性值相同的元素為零,則應丟擲 NoSuchElementException

讓我們看看下圖中突出顯示的同一輸入框的 html 程式碼:

Selenium Java Tutorial 2
<input name="name" id="name" type="text" class="form-control" placeholder="First Name">

上圖中突出顯示的編輯框具有名稱屬性,其值為name。讓我們在識別它之後將文字Selenium輸入到此編輯框中。

package org.example;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;

public class LocatorsName {
   public static void main(String[] args) throws InterruptedException {
      
      // Initiate the Webdriver
      WebDriver driver = new ChromeDriver();

      // adding implicit wait of 20 secs
      driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

      // Opening the webpage where we will identify edit box enter text
      driver.get("https://tutorialspoint.tw/selenium/practice/selenium_automation_practice.php");

      // Identify the search box with name locator to enter text
      WebElement i = driver.findElement(By.name("name"));
      i.sendKeys("Selenium");
      
      // Get the value entered
      String text = i.getAttribute("value");
      System.out.println("Entered text is: " + text);
      
      // Closing browser
      driver.quit();
   }
}

它將顯示以下輸出:

Entered text is: Selenium 

Process finished with exit code 0

輸出顯示訊息 - 退出程式碼為 0 的程序,這意味著上述程式碼已成功執行。此外,在控制檯中列印了從 getAttribute 方法獲得的編輯框中輸入的值 - Selenium

結論

本教程總結了 Selenium Java 教程。我們首先介紹瞭如何使用 Java 設定 Selenium,如何使用 Selenium Java 啟動瀏覽器並退出會話,以及如何使用 Selenium Java 識別元素並檢查其功能。這使您掌握了 Selenium Java 教程的深入知識。明智的做法是繼續練習您所學的內容,並探索與 Selenium 相關的其他內容,以加深您的理解並拓寬您的視野。

廣告