我们需要浏览器来测试 Web 应用程序. Selenium 自动化浏览器,并帮助我们在不同的浏览器上自动化 Web 应用程序测试。 Selenium API 提供了许多类和界面来与不同类型的浏览器和 HTML 元素工作。
什么是 Selenium WebDriver 接口
Selenium WebDriver是一个界面,定义了一组方法,但是,实施是由浏览器特定的类提供的。一些实现类是AndroidDriver
,ChromeDriver
,FirefoxDriver
,InternetExplorerDriver
,IPhoneDriver
,SafariDriver
等。WebDriver的主要功能是控制浏览器,甚至有助于我们选择HTML页面元素,并在它们上执行操作,如点击,填写表单等。
如果我们想在Firefox浏览器中执行测试案例,我们必须使用FirefoxDriver
类;同样,如果我们想在Chrome浏览器中执行测试案例,我们必须使用ChromeDriver
类。
Selenium WebDriver 方法
SearchContext 是 Selenium API 的顶级界面,它有两种方法 - findElement() 和 findElements()。 Selenium WebDriver 界面有许多抽象的方法,如 get(String url), quit(), close(), getWindowHandle(), getWindowHandles(), getTitle() 等。
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 实例,然后执行特定操作,如点击、提交等。
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 浏览器并打印网站的标题。
1package com.journaldev.selenium.firefox;
2
3import org.openqa.selenium.WebDriver;
4import org.openqa.selenium.firefox.FirefoxDriver;
5
6public class GeckoDriverExample {
7
8 public static void main(String[] args) {
9 //specify the location of GeckoDriver for Firefox browser automation
10 System.setProperty("webdriver.gecko.driver", "geckodriver");
11 WebDriver driver = new FirefoxDriver();
12 driver.get("https://journaldev.com");
13 String PageTitle = driver.getTitle();
14 System.out.println("Page Title is:" + PageTitle);
15 driver.close();
16 }
17}
输出:
11551941763563 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"
21551941764296 addons.webextension.screenshots@mozilla.org WARN Loading extension '[email protected]': Reading manifest: Invalid host permission: resource://pdf.js/
31551941764297 addons.webextension.screenshots@mozilla.org WARN Loading extension '[email protected]': Reading manifest: Invalid host permission: about:reader*
4Can't find symbol 'GetGraphicsResetStatus'.
51551941765794 Marionette INFO Listening on port 61417
61551941765818 Marionette WARN TLS certificate errors will be ignored for this session
7Mar 07, 2019 12:26:05 PM org.openqa.selenium.remote.ProtocolHandshake createSession
8INFO: Detected dialect: W3C
9Page Title is:JournalDev - Java, Java EE, Android, Python, Web Development Tutorials
101551941814652 Marionette INFO Stopped listening on port 61417
您可以从我们的 GitHub 存储库中查阅更多 Selenium 示例。