Selenium WebDriver

我們需要瀏覽器來測試 Web 應用程序。 Selenium 可自動化瀏覽器,並協助我們自動化跨不同瀏覽器進行 Web 應用程序測試。 Selenium API 提供了許多類和接口來處理不同類型的瀏覽器和 HTML 元素。

什麼是 Selenium WebDriver 介面?

Selenium WebDriver 是一個定義一組方法的接口。但實現是由特定於瀏覽器的類提供的。一些實現類包括 AndroidDriverChromeDriverFirefoxDriverInternetExplorerDriverIPhoneDriverSafariDriver 等等。WebDriver 的主要功能是控制瀏覽器。它甚至幫助我們選擇 HTML 頁面元素並對其執行操作,如點擊、填寫表單字段等。

Selenium WebDriver

如果我們想要在 Firefox 瀏覽器中執行測試用例,我們必須使用 FirefoxDriver 類。同樣,如果我們想要在 Chrome 瀏覽器中執行測試用例,我們必須使用 ChromeDriver 類。

Selenium WebDriver 方法

SearchContext是Selenium API中最頂層的接口,擁有兩個方法 – findElement() 和 findElements()。Selenium WebDriver接口具有許多抽象方法,如get(String url)、quit()、close()、getWindowHandle()、getWindowHandles()、getTitle()等。WebDriver還有嵌套接口,如WindowNavigationTimeouts等。這些嵌套接口用於執行back()、forward()等操作。

Method Description
get(String url) This method will launch a new browser and opens the given URL in the browser instance.
getWindowHandle() It is used to handle single window i.e. main window. It return type is string. It will returns browser windlw handle from focused browser.
getWindowHandles() It is used to handle multiple windows. It return type is Set. It will returns all handles from all opened browsers by Selenium WebDriver.
close() This command is used to close the current browser window which is currently in focus.
quit() This method will closes all the browsers windows which are currently opened and terminates the WebDriver session.
getTitle() This method is used to retrieve the title of the webpage the user currently working on.

實現WebDriver的類列表

WebDriver接口的主要實現類包括ChromeDriver、EdgeDriver、FirefoxDriver、InternetExplorerDriver等。每個驅動程序類對應一個瀏覽器。我們只需創建驅動程序類的對象並與它們一起工作。

Class Description
ChromeDriver It helps you to execute Selenium Scripts on Chrome browser.
FirefoxDriver It helps you to execute Selenium Scripts on Firefox browser.
InternetExplorerDriver It helps you to execute Selenium Scripts on InternetExplorer browser.

WebElement上的命令列表

Selenium的WebElement代表一個HTML元素。我們可以使用findElement()方法獲取WebElement的實例,然後執行特定的操作,如click、submit等。一些常用的WebElement方法包括:

Command Description Syntax
findElement() This method finds the first element within the current web page by using given locator. WebElement element = driverObject.findElement(By.locator(“value”));
sendKeys() This method enters a value in to an Edit Box or Text box. driver.findElement(By.elementLocator(“value”)).sendkeys(“value”);
clear() It clears the Value from an Edit box or Text Box. driverObject.findElement(By.locatorname(“value”)).clear();
click() It clicks an Element (Button, Link, Checkbox) etc. driverObject.findElement(By.ElementLocator(“LocatorValue”)).click();

示例:使用 Selenium WebDriver 打印網站標題

讓我們看一個簡單的例子,使用 Selenium WebDriver 調用 Firefox 瀏覽器並打印網站的標題。

package com.journaldev.selenium.firefox;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class GeckoDriverExample {

	public static void main(String[] args) {
		//指定 GeckoDriver 的位置以進行 Firefox 瀏覽器自動化
		System.setProperty("webdriver.gecko.driver", "geckodriver");
		WebDriver driver = new FirefoxDriver();
		driver.get("https://journaldev.com");
		String PageTitle = driver.getTitle();
		System.out.println("Page Title is:" + PageTitle);
		driver.close();
	}
}

輸出:

1551941763563	mozrunner::runner	INFO	Running command: "/Applications/Firefox.app/Contents/MacOS/firefox-bin" "-marionette" "-foreground" "-no-remote" "-profile" "/var/folders/1t/sx2jbcl534z88byy78_36ykr0000gn/T/rust_mozprofile.t6ZyMHsrf2bh"
1551941764296	[email protected]	WARN	Loading extension '[email protected]': Reading manifest: Invalid host permission: resource://pdf.js/
1551941764297	[email protected]	WARN	Loading extension '[email protected]': Reading manifest: Invalid host permission: about:reader*
Can't find symbol 'GetGraphicsResetStatus'.
1551941765794	Marionette	INFO	Listening on port 61417
1551941765818	Marionette	WARN	TLS certificate errors will be ignored for this session
Mar 07, 2019 12:26:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Page Title is:JournalDev - Java, Java EE, Android, Python, Web Development Tutorials
1551941814652	Marionette	INFO	Stopped listening on port 61417
Selenium WebDriver Example

您可以從我們的 GitHub 存儲庫查看更多 Selenium 示例。

參考:WebDriver GitHub 代碼

Source:
https://www.digitalocean.com/community/tutorials/selenium-webdriver