selenium:
selenium2 (WebDriver) API
1.1 download the selenium2.0 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
Open firefox browser:
WebDriver driver = new FirefoxDriver();
Open Internet Explorer
WebDriver driver = new InternetExplorerDriver ();
Open the HtmlUnit browser
WebDriver driver = new HtmlUnitDriver();
Open chrome browser
WebDriver driver = new ChromeDriver ();
1.2.2 Maximize the browser
WebDriver driver = new FirefoxDriver ();
driver.manage (). Window (). Maximize ();
1.2.3 Close the browser
WebDriver driver = new FirefoxDriver();
driver.close();
driver.quit();
1.3 Open the test page
driver.get("http://www.google.com");
driver.navigate().to("http://www.baidu.com/");
The PSnavigate method will generate a Navigator object, which encapsulates some methods related to navigation, such as forward and backward
1.4 page element positioning
Webdriver provides the following two methods to locate page elements. The parameter is By object, and the most commonly used is By.id and By.name search.
findElement locates an element, an exception will be thrown if no element is found: NoSuchElementException
findElements locates a group of elements
For example, you need to locate the following elements:
<input class="input_class" type="text" name="passwd" id="passwd-id" />
By.id:
WebElement element = driver.findElement(By.id("passwd-id"));
By.name:
WebElement element = driver.findElement(By.name("passwd"));
By.xpath:
WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));
By.className
WebElement element = driver.findElement(By.className("input_class"));
By.cssSelector
WebElement element = driver.findElement(By.cssSelector(".input_class"));
By.linkText:
// The popular point is precise query
WebDriver driver = new FirefoxDriver();
driver.get("http://www.baidu.com/");
WebElement element = driver.findElement(By.linkText("百科"));
By.partialLinkText:
// This method is to fuzzy query
WebDriver driver = new FirefoxDriver ();
driver.get ("http://www.baidu.com/");
WebElement element = driver.findElement (By.partialLinkText ("hao"));
By.tagName:
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 operate on page elements
1.5.1 Input field (text field or textarea)
WebElement element = driver.findElement(By.id("passwd-id"));
element.sendKeys ("test"); // Enter the content 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 whether 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 button
WebElement btn= driver.findElement(By.id("save"));
btn.click (); // Click the button
btn.isEnabled (); // Determine whether the button is enabled
1.5.7 Popup dialogs
Alert alert = driver.switchTo().alert();
alert.accept (); // determine
alert.dismiss (); // cancel
alert.getText (); // get text
1.5.8 Form
The operations of the elements in the Form are the same as those of other elements. After the element operations are completed, the form can be submitted:
WebElement approve = driver.findElement(By.id("approve"));
approve.click();
or
approve.submit (); // Only suitable for form submission
1.5.9 Upload files
Element operations for uploading files:
WebElement adFileUpload =driver.findElement(By.id("WAP-upload"));
String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys (filePath);
1.6 Switch between Windows and Frames
driver.switchTo (). defaultContent (); // Return to the topmost frame / iframe
driver.switchTo (). frame ("leftFrame"); // Switch to a certain frame:
driver.switchTo (). window (" windowName "); // Switch to a window
1.7 Call Java Script
The Web driver calls Java Script through JavascriptExecutor, for example:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript ("JS script");
1.8 timeout setting
WebDriver driver = new FirefoxDriver();
driver.manage (). timeouts (). implicitlyWait (10, TimeUnit.SECONDS); // Timeout time when identifying elements
driver.manage (). timeouts (). pageLoadTimeout (10, TimeUnit.SECONDS); // Page load Time-out time of
driver.manage (). Timeouts (). SetScriptTimeout (10, TimeUnit.SECONDS); // Timeout time of asynchronous script