Selenium WebDriver

时间:2020-02-23 14:41:51  来源:igfitidea点击:

我们需要浏览器来测试Web应用程序。
Selenium使浏览器自动化,并帮助我们跨不同浏览器自动化Web应用程序测试。
Selenium API提供了许多类和接口,可用于不同类型的浏览器和HTML元素。

什么是Selenium WebDriver接口?

Selenium WebDriver是定义一组方法的接口。
但是,实现是由浏览器特定的类提供的。
一些实现类是AndroidDriver,ChromeDriver,FirefoxDriver,InternetExplorerDriver,IPhoneDriver,SafariDriver等。

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具有嵌套的接口,例如Window,Navigation,Timeouts等。
这些嵌套的接口用于执行back(),forward()等操作。

MethodDescription
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等。
每个驱动程序类都对应于一个浏览器。
我们只需创建驱动程序类的对象并使用它们即可。

ClassDescription
ChromeDriverIt helps you to execute Selenium Scripts on Chrome browser.
FirefoxDriverIt helps you to execute Selenium Scripts on Firefox browser.
InternetExplorerDriverIt helps you to execute Selenium Scripts on InternetExplorer browser.

WebElement上的命令列表

Selenium WebElement表示HTML元素。
我们可以使用findElement()方法获取WebElement的实例,然后执行特定的操作,例如单击,提交等。

一些常用的WebElement方法是:

CommandDescriptionSyntax
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.theitroad.selenium.firefox;

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

public class GeckoDriverExample {

	public static void main(String[] args) {
		//specify the location of GeckoDriver for Firefox browser automation
		System.setProperty("webdriver.gecko.driver", "geckodriver");
		WebDriver driver = new FirefoxDriver();
		driver.get("https://theitroad.local");
		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:theitroad - Java, Java EE, Android, Python, Web Development Tutorials
1551941814652	Marionette	INFO	Stopped listening on port 61417