Preface: Selenium and Webdriver is one of the mainstream UI Automation testing framework, Selenium is also known as Selenium RC, the basic principle is JS injection, and Webdriver is directly using the browser native Support (vendor-supported) to operate the browser, so for different browsers, you must rely on a specific browser native component to implement the Webdriver API into the browser's native invoke. When we new a webdriver, selenium first confirms that the browser's native component exists and the version matches (so when using a browser driver, you need to check that the driver version matches the selenium version, the mismatch is not available), Then launch a full set of Web service in the target browser, which uses a protocol defined by selenium's own design, which simulates a user action browser to make a series of actions. For more information, visit the official user's manual: http://www.seleniumhq.org/docs/
First listed under the "WebUI Automated testing Framework" I intend to write a blog, interested in children's shoes welcome the continued attention, which is my actual work in some of the learning and practice records, may be some wrong or imperfect place, welcome children shoes:
WebUI Automated Testing Framework (i): Webdriver Introduction and demo Introduction
WebUI Automated Testing Framework (II): Code layering-Base layer
WebUI Automated Testing Framework (III): Code layering-Object library layer
WebUI Automated test Framework (IV): Code layering-operation layer and use case layer
WebUI Automated test Framework (V): Continuous integration and test report output
Go to the Chase:
First, Environment construction
WebUI automated Environment Building compared to the app is much simpler, there are JDK, selenium jar package, browser-driven is enough.
1. Go to official website download Selenium related jar package: http://www.seleniumhq.org/download/, because Google is wall, can download to my network, version is selenium-java-3.4.0:http:// pan.baidu.com/s/1denrx89
2. Create a new Java project and add the Selenium jar package and the jar package from the Lib directory to the build path.
3.webdriver Common methods:
Element positioning methods: By.id (ID), By.xpath (XPath), By.linktext (LinkText), By.classname (ClassName), By.cssselector (selector);
Click action: Driver.findelement (By.id (ID)). Click ();
Input: Driver.findelement (by.id (ID)). SendKeys ("Selenium");
Get text: Driver.findelement (by.id (ID)). getText ();
Gets the object property value: Driver.findelement (by.id (ID)). getattribute ("attribute");
More ways to access official documents.
4. Next we'll write a demo.
Package com.etyero.testcase;
Import Java.util.concurrent.TimeUnit;
Import Org.openqa.selenium.By;
Import Org.openqa.selenium.WebDriver;
Import Org.openqa.selenium.chrome.ChromeDriver;
Import Org.testng.Assert;
Import Org.testng.annotations.AfterMethod;
Import Org.testng.annotations.BeforeMethod;
Import Org.testng.annotations.Test;
public class Webdriverdemo {private Webdriver driver;
Private String BASEURL = "http://www.baidu.com";
Private StringBuffer verificationerrors = new StringBuffer (); @BeforeMethod public void SetUp () throws Exception {String Browserdriverurl = "D:/work/workplace/webuitest/browserdriv
Er/chromedriver.exe ";//Browser drive path//Start Chrome browser system.setproperty (" Webdriver.chrome.driver ", Browserdriverurl);
Driver = new Chromedriver (); Driver.manage (). window (). Maximize ();//Maximize Browser driver.manage (). Timeouts (). implicitlywait (timeunit.seconds);// Set the Operation timeout length, this setting is global, that is, all operations are the longest waiting for 30s} @Test/** * Search Selenium * * */public void Testlogin () throws Exception {driver.get (BASEURL); Driver.findelement (By.id ("kw")). Clear ();//After the element is found by ID, the element driver.findelement (by.id ("kw") is emptied. SendKeys ("Selenium"); /Enter Selenium driver.findelement (by.id ("su")). Click ();
Click the Search button} @AfterMethod public void TearDown () throws Exception {driver.quit ();
String verificationerrorstring = verificationerrors.tostring (); if (! "".
Equals (verificationerrorstring)) {assert.fail (verificationerrorstring);
}
}
}
At this point, you have entered the gate of Webdriver, Welcome to join ~ ~