A, the Eclipse environment to build and selenium
1, the installation JDK1.6
Configure the environment variables:
JAVA_HOME = E: \ the Java \ the Java \ jdk1.7.0_15
the PATH =% JAVA_HOME% \ bin
. The CLASSPATH =;% JAVA_HOME% \ lib \ dt.jar;% JAVA_HOME% \ lib \ tools.jar
After configuring the environment after variable, CMD command line, type: java -version, return the result version, the installation was successful.
2, install Eclipse
Directly after extracting it.
3, before installing Firefox 32 version
1) Tools - Options - Advanced - Update: choose not to check for updates
2) Tools - Add-ons: Install SeleniumIDE, Firebug, Firepath components
4. Record
1) Tools -SeleniumIDE: record a script
2) Export .java named script: SeleniumIDE- the Test-the Java file -Export / JUnit 4 / WebDriver
3) Open Eclipse: Create a project, copy the script to the project to the next.
4) introducing selenium-server-standalone-2.39.0.jar package
5) running
Second, the use of selenium webdirver browser module operates
Driver = new new FirefoxDriver WebDriver ();
String baseUrl = "http://www.maiziedu.com/";
driver.get (baseUrl); // Open wheat Academy
driver.getTitle () // get title driver of the current page. getTitle (). indexOf ( "wheat")! = -1
driver.getCurrentUrl () // get url driver.getCurrentUrl (). indexOf ( " maizi") of the current page! = -1
driver.manage (). window ( ) .maximize (); // browser maximize
driver.navigate () to ( "http://www.baidu.com" );. // open Baidu
driver.navigate () refresh ();. // refresh browser
driver.navigate () back ();. // browser back
driver.navigate () forward ();. // browser forward
driver.close (); // close the current page
driver.quit (); / / Close all windows
截图操作:
driver.get(""http://www.baidu.com"");
File srcFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcFile, new File(""c:\\1.png""));
Analog mouse:
driver.get ( "" http://www.baidu.com "");
the Actions the Actions Action new new = (Driver);
action.contextClick (driver.findElement (By.id ( "" kW "") .)) perform ();
to kill Windows browser process
WindowsUtils.tryToKillByName ( "" firefox.exe "") ;
Three, webdriver web UI module operating element
Links:
WebElement Element = driver.findElement (By.linkText ( "" small tanks ""));
element.click ();
Input box:
. Driver.findElement (By.id ( "" the userid "")) Clear (); // clear input box
. Driver.findElement (By.id ( "" userid "")) sendKeys ( "" test111111 " "); // input content
driver.findElement (By.id (" "userid" ").) getAttribute (" "value" "); // get the contents of the input box"
Button:
String = XPath "" // input [@ value = 'Add'] ""; // value is "add" input element
WebElement element = driver.findElement (By.xpath (xpath )); // Get element
element.click (); // click
element.isEnabled (); // determine whether the button is available
Is selected from the drop-down box by:
the Select the Select new new SELECT = (driver.findElement (By.id ( "" proAddItem "")));
select.selectByIndex (2); // third option, index starts from 0
select.selectByValue ( "" 18 ""); // select option value = 18
select.selectByVisibleText ( "" type AA ""); // select the text is "type AA" options
List <WebElement> options = select.getOptions ( ); // get all the options
Radio button:
String xpath = "" // input [@ of the type = 'Radio'] [@ value = 'the Apple'] ""; // Taype = Radio, the Apple value = input element
WebElement apple = driver.findElement ( By.xpath (xpath)); // Get element
apple.click (); // select checkbox
apple.isSelected (); // check box has been selected
apple.getAttribute ( "" value "") ; / / Gets the element attributes
Checkbox:
String xpath = "" // input [@ of the type = 'the CheckBox'] [@ value = 'the Apple'] ""; // Taype = the CheckBox, the Apple value = input element
String xpath1 = "" // input [@ type = 'checkbox' ] [@ value = 'Banana'] ""; // taype = checkbox, value = Apple input element
WebElement apple = driver.findElement (By.xpath (xpath )); // Get element
WebElement Banana = driver.findElement (By.xpath (xpath1 )); // Get element
apple.click ();
Banana.click ();
apple.isSelected (); // check box has been selected
Banana.isSelected ( ); // check box has been selected
apple.getAttribute ( "" value "") ; // Gets the element attributes
Banana.getAttribute ( "" value "") ; // Gets the element attributes
Four, XPath positioning (page once the structure is changed, the absolute path also will fail, must be re-so do not recommend using an absolute path)
Positioning relative path: // input [@ value = "query"] to find value = "Search" input element
index number Positioning: // input [2] page to find a third input elements, the subscript starts at 0
Page Attributes positioning: // img [@ alt = ' div1-img1'] Find alt = 'div1-img1' img element
starts-with fuzzy keyword targeting: // img [starts-with ( @ alt, 'div')] Find the alt attribute of the img element from the beginning div
contains fuzzy keyword targeting: // img [contains (@ alt , 'g1')] to verify alt attribute contains 'g1' img element
text () function text positioning: // * [ text () = 'Baidu search'] finds all text "element Baidu search for"
// a [the contains (text (), 'Search')] check all text as "search" hyperlink
Fifth, the multi-window switch
Set <String> allWindowsId = driver.getWindowHandles ( ); // all open windows
for (String windowId: allWindowsId) {
.. IF (driver.switchTo () window (windowId) .getTitle () the contains ( "" blog Park " ")) {
driver.switchTo () window (windowId);. // switch to the target window
BREAK;
}
}
String parentWindowId driver.getWindowHandle = (); // get the current window
driver.switchTo () window (parentWindowId). ; // switch back to the parent window
Sixth, pop-up box
WebElement alertButton = driver.findElement(By.xpath(""//input[@value='alert']""));
Alert alert = driver.switchTo().alert();
driver.accept();