Selenium Common API
Selenium is a tool of testing web applications.
Selenium-testing runs in the browser directly, just like a real user is operating.
Selenium supports multiple browsers, include ie (7, 8, 9, 10, 11), Mozilla, Firefox, Safari, Google Chrome, opera, edge, etc.
The main functions of this tool include: testing compatibility of browsers - testing your applications to see if they work well on different browsers and operating systems.
Testing system function - create regression test to test software function and user requirement.
It supports automatic recording of actions and automatic generation of test scripts in .Net, Java, Perl and other languages.
Installation
1.install via jar
Click the link of Selenium download, you will see the introduction of Selenium Standalong Server:
The Selenium Server is needed in order to run Remote Selenium WebDriver. Selenium 3.X is no longer capable of running Selenium RC directly, rather it does it through emulation and the WebDriverBackedSelenium interface.
Download version 3.4.0
After download complete, we will get selenium-server-standalone-3.4.0.jar file.
2.install via Maven
Open pom.xml and config Selenium
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.4.0</version>
</dependency>
3.Hello Selenium
We wrote a simple Selenium Sample to verify if the enviroment has been installed succuffully.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Itest {
public static void main(String[] args) {
WebDriver driver = new ChromeDriver();
driver.get("http://www.itest.info");
String title = driver.getTitle();
System.out.printf(title);
driver.close();
}
}
If the execution reports an error, please refer to the next section, selenium 3 browser driver.
Browser driver
1.download browser driver
After selenium was upgraded to v3.0, different browser drivers were standardized.
If you want to use selenium to drive different browsers, you must download and set up different browser drivers separately.
Download address of each browser:
Firefox: geckodriver
Chrome: chromedriver
IE: IEDriverServer
Edge: MicrosoftWebDriver
Opera: operadriver
PhantomJS: phantomjs
2.set browser driver
It is very easy to set the browser driver address.
We can manually create a directory to store the browser driver, such as C: \driver, and put the browser driver files (such as chromedriver and geckodriver) into this directory.
Then click on My computer –> properties –> system settings –> advanced –> environment variables –> system variables –> path, and add the directory (e.g. C:\Driver) to the value of path.
3.verify browser driver
Verify if each browser driver can work properly.
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.opera.OperaDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
……
WebDriver driver = new ChromeDriver(); //Chrome浏览器
WebDriver driver = new FirefoxDriver(); //Firefox浏览器
WebDriver driver = new EdgeDriver(); //Edge浏览器
WebDriver driver = new InternetExplorerDriver(); // Internet Explorer浏览器
WebDriver driver = new OperaDriver(); //Opera浏览器
WebDriver driver = new PhantomJSDriver(); //PhantomJS
……
Element location
1.locating methods
Selenium provides 8 kind of locating methods
- id
- name
- class name
- tag name
- link text
- partial link text
- xpath
- css selector
The corresponding functions in Java as below:
- findElement(By.id())
- findElement(By.name())
- findElement(By.className())
- findElement(By.tagName())
- findElement(By.linkText())
- findElement(By.partialLinkText())
- findElement(By.xpath())
- findElement(By.cssSelector())
2.usage of locating functions
Given we have a web page, we can see the attributes of an element through a front-end tool (e.g. FireBug) as below:
<html>
<head>
<body link="#0000cc">
<a id="result_logo" href="/" onmousedown="return c({'fm':'tab','tab':'logo'})">
<form id="form" class="fm" name="f" action="/s">
<span class="soutu-btn"></span>
<input id="kw" class="s_ipt" name="wd" value="" maxlength="255" autocomplete="off">
Our goal is to locate the input box.
by id
driver.findElement(By.id("kw"))
by name
driver.findElement(By.name("wd"))
by class name
driver.findElement(By.className("s_ipt"))
by tag name
driver.findElement(By.tagName("input"))
by xpath, there are many ways to write the code:
driver.findElement(By.xpath("//*[@id='kw']")) driver.findElement(By.xpath("//*[@name='wd']")) driver.findElement(By.xpath("//input[@class='s_ipt']")) driver.findElement(By.xpath("/html/body/form/span/input")) driver.findElement(By.xpath("//span[@class='soutu-btn']/input")) driver.findElement(By.xpath("//form[@id='form']/span/input")) driver.findElement(By.xpath("//input[@id='kw' and @name='wd']"))
by css, there are many ways to write the code:
driver.findElement(By.cssSelector("#kw") driver.findElement(By.cssSelector("[name=wd]") driver.findElement(By.cssSelector(".s_ipt") driver.findElement(By.cssSelector("html > body > form > span > input") driver.findElement(By.cssSelector("span.soutu-btn> input#kw") driver.findElement(By.cssSelector("form#form > span > input")
Next, we have a set of text links in our page.
<a class="mnav" href="http://news.baidu.com" name="tj_trnews">新闻</a>
<a class="mnav" href="http://www.hao123.com" name="tj_trhao123">hao123</a>
by link text
driver.findElement(By.linkText("新闻") driver.findElement(By.linkText("hao123")
by partial link text
driver.findElement(By.partialLinkText("新") driver.findElement(By.partialLinkText("hao") driver.findElement(By.partialLinkText("123")
Control browser
1.control the browser window size
Sometimes we hope to be able to open browser with a specific size, and visit the pages in this size.
For example, we need to set the browser to the size of the mobile terminal (480 * 800), and then visit the mobile site to evaluate its style.
Webdriver provides one function named manage().window().setSize() to set the size of the browser.
- maximize() to maximize the browser
- setsize() to set the browser width and height
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Browser {
public static void main(String[] args) throws InterruptedException {
WebDriver driver= new ChromeDriver();
driver.get("https://www.baidu.cn");
driver.manage().window().maximize();
Thread.sleep(2000);
driver.get("https://m.baidu.cn");
driver.manage().window().setSize(new Dimension(480, 800));
Thread.sleep(2000);
driver.quit();
}
}
Usually, when we run automatic-test scripts on PC, the browser is expected to be open in full screen mode.
So we can use maximize() function to make the open browser display in full screen.
Its usage is the same as that of setSize() function, but it does not need any parameters.