1.1 Download Selenium2.0 's Package
- Official download package Address: Http://code.google.com/p/selenium/downloads/list
- Official User guide:http://seleniumhq.org/docs/
- Official api:http://selenium.googlecode.com/git/docs/api/java/index.html
1.2.1 Open a browser with Webdriver
Webdriver Driver = new Firefoxdriver ();
Webdriver Driver = new Internetexplorerdriver ();
Webdriverdriver = new Htmlunitdriver ();
Webdriverdriver = new Chromedriver ();
1.2.2 Maximize Browser
Webdriver Driver = new Firefoxdriver ();
Driver.manage (). window (). Maximize ();
1.2.3 Close Browser
Webdriver Driver = new Firefoxdriver ();
- Driver.close ();
- Driver.quit ();
1.3 Opening the test page
- Driver.get ("http://www.google.com");
- Driver.navigate (). to ("http://www.baidu.com/");
The P.s.navigate method produces 1 navigator objects that encapsulate some navigation-related methods, such as forward and backward.
1.4 Page Element Positioning
Webdriver provides the following two ways to locate page elements, parameters are by pair, most commonly by.id and by.name lookups.
- Findelement locates an element and throws an exception if no element is found:nosuchelementexception
- Findelements locating a set of elements
For example, you need to locate the following elements:
<input class= "Input_class" type= "text" name= "passwd" id= "Passwd-id"/>
webelement element = Driver.findelement (By.id ("Passwd-id"));
webelement element = Driver.findelement (By.name ("passwd"));
webelement element =driver.findelement (By.xpath ("//input[@id = ' Passwd-id ']");
webelement element = Driver.findelement (By.classname ("Input_class"));
webelement element = Driver.findelement (By.cssselector (". Input_class"));
The popular point is the exact query
Webdriver Driver = new Firefoxdriver ();
Driver.get ("http://www.baidu.com/");
webelement element = Driver.findelement (By.linktext ("Encyclopedia"));
This method is a fuzzy query
Webdriver Driver = new Firefoxdriver ();
Driver.get ("http://www.baidu.com/");
webelement element = Driver.findelement (By.partiallinktext ("Hao"));
Webdriver Driver = new Firefoxdriver ();
Driver.get ("http://www.baidu.com/");
String test= driver.findelement (by.tagname ("form")). getattribute ("name");
SYSTEM.OUT.PRINTLN (test);
1.5 How to manipulate page elements 1.5.1 input box (text field or textarea)
webelement element = Driver.findelement (By.id ("Passwd-id"));
- Element.sendkeys ("test");// Enter the contents in the Input box:
- Element.clear (); // Empty the input box
- Element.gettext (); Get the text content of the input box:
1.5.2 drop-down selection box (select)
Select select = New Select (Driver.findelement (By.id ("select"));
- Select.selectbyvisibletext ("A");
- Select.selectbyvalue ("1");
- Select.deselectall ();
- Select.deselectbyvalue ("1");
- Select.deselectbyvisibletext ("A");
- Select.getallselectedoptions ();
- Select.getfirstselectedoption ();
1.5.3 single option (Radio Button)
Webelement radio=driver.findelement (by.id ("Bookmode"));
- Radio.click (); Select a single option
- Radio.clear (); Clear a single option
- Radio.isselected (); Determine if a single option has been selected
1.5.4 Multiple options (checkbox)
webelement checkbox = Driver.findelement (By.id ("MyCheckBox."));
- Checkbox.click ();
- Checkbox.clear ();
- Checkbox.isselected ();
- Checkbox.isenabled ();
1.5.5 buttons (button)
Webelement btn= driver.findelement (by.id ("Save"));
- Btn.click (); Click the button
- Btn.isenabled (); Determines whether the button is enable
1.5.7 Popup dialog box (popup dialogs)
Alert alert = Driver.switchto (). alert ();
- Alert.accept (); Are you sure
- Alert.dismiss (); Cancel
- Alert.gettext (); Get text
1.5.8 form (form)
The actions of elements in a form, like other element operations, can be submitted to the form after the element operation is complete:
webelement approve = driver.findelement (By.id ("approve"));
Approve.click ();
Or
Approve.submit ();//only suitable for form submission
1.5.9 Uploading Files
To upload an element of a file operation:
Webelement adfileupload =driver.findelement (by.id ("Wap-upload"));
String FilePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
Adfileupload.sendkeys (FilePath);
1.6 Switching between Windows and frames
- Driver.switchto (). Defaultcontent (); Return to the top-most frame/iframe
- Driver.switchto (). FRAME ("Leftframe"); Switch to a frame:
- Driver.switchto (). Window ("Windowname"); Switch to a window
1.7 Calling Java Script
Web driver calls to Java script are implemented through Javascriptexecutor, for example:
Javascriptexecutor js = (javascriptexecutor) driver;
Js.executescript ("JS script");
1.8 Timeout settings
Webdriver Driver = new Firefoxdriver ();
- Driver.manage (). Timeouts (). implicitlywait (Timeunit.seconds); Time-out when element is recognized
- Driver.manage (). Timeouts (). Pageloadtimeout (Timeunit.seconds); Time-out at page load
- Driver.manage (). Timeouts (). Setscripttimeout (Timeunit.seconds); Time-out for asynchronous scripts
selenium2.0 (Webdriver) API