• Selenium Video Tutorials

Selenium WebDriver - 第一個測試指令碼



在開始使用 Selenium 的第一個測試指令碼之前,我們應該確保 Java 和 Maven 已安裝在我們的系統中。

建立第一個測試指令碼的先決條件步驟

步驟 1 - 透過執行命令:java(在命令提示符中)確認 Java 安裝。

步驟 2 - 透過執行命令確認已安裝的 Java 版本

java –version

步驟 3 - 透過執行命令確認已安裝的Maven版本 -

mvn –version

步驟 4 - 從連結下載 Eclipse 技術下載並安裝任何程式碼編輯器,例如 Eclipse。

要獲取有關 Eclipse 及其安裝過程的更多資訊,請訪問連結Eclipse 教程

建立第一個測試指令碼的步驟

步驟 1 - 啟動驅動程式會話並在瀏覽器上執行某些操作,例如使用以下程式碼在 Chrome 瀏覽器上啟動網頁 -

Webdriver driver = new ChromeDriver();

driver.get("https://tutorialspoint.tw/selenium/practice/text-box.php");

步驟 2 - 使用 getCurrentUrl() 方法檢索有關在 Chrome 瀏覽器上啟動的當前 URL 的一些資訊。

driver.getCurrentUrl();

步驟 3 - 新增一些同步技術,以便 Selenium 工具和瀏覽器操作之間的互動保持同步。Selenium 中有幾種同步方法可用,例如隱式、顯式和流暢等待。

雖然隱式等待是一種全域性等待(適用於測試用例中的所有步驟),但顯式和流暢等待應用於瀏覽器上的特定元素。對於我們這裡的第一個測試指令碼,我們只應用隱式等待。

新增 15 秒隱式等待的語法 -

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

要獲取有關 Selenium 中不同等待的更多資訊,請訪問連結Selenium Webdriver 等待支援

步驟 4 - 在同一個 webdriver 會話中對 webelement 執行任何任務,為此,使用任何Selenium 定位器(如 id、name、class、tagname、link text、partial link text、xpath 和 css 選擇器)以及 findElement() 方法唯一地識別該元素。如果有多個元素具有相同的值的定位器,則應僅識別第一個匹配的元素。

使用 xpath 定位器識別元素的語法 -

WebElement e = driver.findElement(By.xpath("<value of xpath>"));

步驟 5 - 使用 sendKeys() 方法對上一步中識別的元素執行任務。輸入文字 - Selenium,要輸入的文字作為引數傳遞給該方法。

語法

e.sendKeys("Selenium");

步驟 6 - 使用 getAttribute() 方法請求輸入框中輸入的值的資訊,並將值作為引數傳遞給該方法。

語法

String text = driver.findElement(By.xpath("<value of xpath>")).getAttribute("value");

步驟 7 - 使用clear()方法清除輸入的文字。

語法

e.clear();

步驟 8 - 再次使用 getAttribute() 方法請求輸入框中值的資訊(以證明在使用 clear() 方法後輸入框中沒有值),並將值作為引數傳遞給該方法。

語法

String text = driver.findElement(By.xpath("<value of xpath>")).getAttribute("value");

步驟 9 - 使用 quit() 方法終止驅動程式會話,這也會關閉已開啟的瀏覽器視窗。

語法

driver.quit();

示例

右鍵單擊下面的網頁,然後在 Chrome 瀏覽器中單擊“檢查”按鈕。要識別頁面上的第一個文字框,請單擊突出顯示的可見 HTML 程式碼頂部的左上箭頭。

Selenium First Test Script 1

一旦我們單擊並指向輸入框(在下圖中突出顯示),其 HTML 程式碼就會出現,反映輸入標籤名稱和型別屬性的值為文字。

Selenium First Test Script 2

讓我們以以上頁面為例,我們首先使用sendKeys()方法在文字框中輸入文字。稍後,我們將使用clear()方法清除文字。

程式碼實現

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 FrstJavaTestScript {
   public static void main(String[] args) throws InterruptedException {

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

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

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

      // getting current URL
      System.out.println("Getting the Current URL: " + driver.getCurrentUrl());

      // Identify the input box with xpath locator
      WebElement e = driver.findElement(By.xpath("//*[@id='fullname']"));

      // enter text in input box
      e.sendKeys("First Test Script");

      // Get the value entered
      String text = e.getAttribute("value");
      System.out.println("Entered text is: " + text);

      // clear the text entered
      e.clear();

      // Get no text after clearing text
      String text1 = e.getAttribute("value");
      System.out.println("Get text after clearing: " + text1);

      // Closing browser
      driver.quit();
   }
}

輸出

Getting the Current URL: 
https://tutorialspoint.tw/selenium/practice/text-box.php
Entered text is: First Test Script
Get text after clearing: 

Process finished with exit code 0

在以上示例中,我們首先獲得了當前 URL,控制檯中的訊息為:獲取當前 URL:Selenium 自動化實踐表單。然後,我們在輸入框中輸入了文字第一個測試指令碼,我們使用訊息 - 輸入的文字為:第一個測試指令碼在控制檯中檢索了輸入的值。接下來,我們清除了輸入的值,因此在同一個文字框中沒有獲得任何值。

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

結論

這結束了我們對 Selenium Webdriver 第一個測試指令碼教程的全面介紹。我們首先描述了建立第一個測試指令碼的先決條件步驟、建立第一個測試指令碼的步驟以及一個示例來說明如何在 Selenium Webdriver 中建立第一個測試指令碼。這使您深入瞭解 Selenium Webdriver - 第一個測試指令碼。明智的做法是不斷練習您學到的知識,並探索與 Selenium 相關的其他知識,以加深您的理解並拓寬您的視野。

廣告