Selenium
Selenium resources: http://seleniumhq.org/download/
Selenium IDE
Selenium server
Selenium client drivers and java doc: http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html
Selenium WebDriver: API Commands and Operations http://docs.seleniumhq.org/docs/03_webdriver.jsp
Fetching a Page:
org.openqa.selenium.WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
Locating UI Elements (WebElements):
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
WebElement cheese = driver.findElement(By.name("cheese"));
User Input - Filling In Forms
//input and submit
ebElement element = driver.findElement(By.name("q"));
element.sendKeys("Cheese!");
element.submit();
//option
WebElement select = driver.findElement(By.tagName("select"));
List<WebElement> allOptions = select.findElements(By.tagName("option"));
for (WebElement option : allOptions) {
System.out.println(String.format("Value is: %s", option.getAttribute("value")));
option.click();
}
Select select = new Select(driver.findElement(By.tagName("select")));
select.deselectAll();
select.selectByVisibleText("Edam")
//button
driver.findElement(By.id("submit")).click();
Moving Between Windows and Frames
driver.switchTo().window("windowName");
for (String handle : driver.getWindowHandles()) {//iterate over every open window
driver.switchTo().window(handle);// pass a “window handle” to the “switchTo().window()” method
}
driver.switchTo().frame("frameName");
driver.switchTo().frame("frameName.0.child");//access subframes
Popup Dialogs
Alert alert = driver.switchTo().alert();
Navigation: History and Location
driver.navigate().to("http://www.example.com");//“navigate().to()” and “get()” do exactly the same thing
driver.navigate().forward();
driver.navigate().back();
Cookies
Drag And Drop
WebElement element = driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element, target).perform();
Driver Specifics and Tradeoffs
WebDriver driver = new FirefoxDriver();
WebDriver driver = new InternetExplorerDriver();
WebDriver driver = new ChromeDriver();
WebDriver: Advanced Usage
Explicit Waits
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("myDynamicElement"));
}});
Implicit Waits
WebDriver driver = new FirefoxDriver();
//The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
Set browser driver
System.setProperty("webdriver.chrome.driver","D:/selenium-server/drivers/chromedriver.exe");
Actions
org.openqa.selenium.interactions.Actions;
Actions: Actions action=new Actions(driver);
action.click();
click(WebElement)
clickAndHold(WebElement)
doubleClick(WebElement)
dragAndDrop(WebElement source, WebElement target)
contextClick(WebElement)
keyDown(Keys)
keyUp(Keys)
release(WebElement)
sendKeys()
moveByOffset
moveToElement(WebElement)
switchTo()
driver.switchTo().window(nameOrHandle);
driver.switchTo().frame(nameOrId);
driver.switchTo().alert();
quit vs close
webDriver.close() - closes the current window
webDriver.quit() - Quits this driver, closing every associated window.
If there is just one window open the close() and quit() technically do the same.
org.openqa.selenium.By static methods
By.className(java.lang.String className) Finds elements based on the value of the "class" attribute.
By.cssSelector(java.lang.String selector) Finds elements via the driver's underlying W3 Selector engine.
By.id(java.lang.String id)
By.linkText(java.lang.String linkText)
By.name(java.lang.String name)
By.partialLinkText(java.lang.String linkText)
By.tagName(java.lang.String name)
By.xpath(java.lang.String xpathExpression)
abstract java.util.List<WebElement> findElements(SearchContext context) Find many elements.
Get information of the current page
driver.getCurrentUrl();
driver.getTitle();
driver.getPageSource();
org.openqa.selenium.WebDriver.Navigation
back()
forward()
refresh()
to(String)
Wait
org.openqa.selenium.support.ui.WebDriverWait wait = new WebDriverWait(driver, 5));
org.openqa.selenium.support.ui.ExpectedCondition e = new ExpectedCondition<Boolean>(){
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("customer");
}
};
wait.until(e);
//org.openqa.selenium.support.ui.ExpectedConditions
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_search_textbox")));
(new WebDriverWait(driver, 5)).until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle().toLowerCase().startsWith("customer");
}
});
WebElement myDynamicElement = (new WebDriverWait(driver, 10)).until(new ExpectedCondition<WebElement>(){
@Override
public WebElement apply(WebDriver d) {
return d.findElement(By.id("myDynamicElement"));
}});
Questions
http://blog.csdn.net/shandong_chu/article/category/610439
- 如何打开一个测试浏览器
- 如何打开1个具体的url
- 如何关闭浏览器
- 如何返回当前页面的url
- 如何返回当前页面的title
- 如何执行一段js脚本
- 定位单个对象
- 层级定位
- WebElement element = driver.findElement(By.name("q")).findElement(By.id("1"));
- 如何定位frame中的元素
- driver.switchTo().frame(nameOrId);
- 如何捕获弹出窗口
- 使用windowhandles方法获取所有弹出的浏览器窗口的句柄,然后使用windowhandle方法来获取当前浏览器窗口的句柄,将这两个值的差值就是新弹出窗口的句柄。在获取新弹出窗口的句柄后,使用switch_to.window(newwindow_handle)方法,将新窗口的句柄作为参数传入既可捕获到新窗口了。
- 如何处理alert和confirm
- driver.switchTo().alert().accept();
- 使用Page Object设计模式
- 将一些测试对象以及操作这些测试对象的动作或步骤封装在1个类中,代码的灵活性和适用性将会更强。
- 如何操作select下拉框
- 如何智能的等待页面加载完成
1 package selenium.example; 2 3 import org.openqa.selenium.By; 4 5 import org.openqa.selenium.WebDriver; 6 import org.openqa.selenium.WebElement; 7 import org.openqa.selenium.chrome.ChromeDriver; 8 import org.openqa.selenium.firefox.FirefoxDriver; 9 import org.openqa.selenium.interactions.Actions; 10 import org.openqa.selenium.support.ui.ExpectedCondition; 11 import org.openqa.selenium.support.ui.ExpectedConditions; 12 import org.openqa.selenium.support.ui.WebDriverWait; 13 14 public class Selenium2Example { 15 public static void main(String[] args) { 16 System.setProperty("webdriver.chrome.driver","D:/selenium-server/drivers/chromedriver.exe"); 17 // Create a new instance of the Firefox driver 18 // Notice that the remainder of the code relies on the interface, 19 // not the implementation. 20 WebDriver driver = new ChromeDriver(); 21 22 // And now use this to visit Google 23 driver.get("http://www.google.com"); 24 // Alternatively the same thing can be done like this 25 // driver.navigate().to("http://www.google.com"); 26 27 // Find the text input element by its name 28 WebElement element = driver.findElement(By.name("q")); 29 //WebElement element = driver.findElement(By.name("q")).findElement(By.id("1")); 30 31 // Enter something to search for 32 element.sendKeys("Cheese!"); 33 34 // Now submit the form. WebDriver will find the form for us from the element 35 element.submit(); 36 37 // Check the title of the page 38 System.out.println("Page title is: " + driver.getTitle()); 39 40 // Google's search is rendered dynamically with JavaScript. 41 // Wait for the page to load, timeout after 10 seconds 42 (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { 43 public Boolean apply(WebDriver d) { 44 return d.getTitle().toLowerCase().startsWith("cheese!"); 45 } 46 }); 47 48 // Should see: "cheese! - Google Search" 49 System.out.println("Page title is: " + driver.getTitle()); 50 51 //Close the browser 52 driver.quit(); 53 //driver.close(); 54 55 //driver.switchTo().window(nameOrHandle); 56 //driver.switchTo().frame(nameOrId); 57 driver.switchTo().alert().accept(); 58 driver.navigate();//An abstraction allowing the driver to access the browser's history and to navigate to a given URL. 59 60 Actions action=new Actions(driver); 61 //action.dragAndDrop(source, target); 62 63 driver.getCurrentUrl(); 64 driver.getTitle(); 65 driver.getPageSource(); 66 driver.getWindowHandle(); 67 68 WebDriverWait wait= new WebDriverWait(driver, 5); 69 ExpectedCondition e= new ExpectedCondition<Boolean>(){ 70 public Boolean apply(WebDriver d) { 71 return d.getTitle().toLowerCase().startsWith("customer"); 72 } 73 }; 74 wait.until(e); 75 76 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_search_textbox"))); 77 } 78 }
import org.openqa.selenium.WebElement; import org.openqa.selenium.ie.InternetExplorerDriver; public class WebTableExample { public static void main(String[] args) { WebDriver driver = new InternetExplorerDriver(); driver.get("http://localhost/test/test.html"); WebElement table_element = driver.findElement(By.id("testTable")); List<WebElement> tr_collection=table_element.findElements(By.xpath("id('testTable')/tbody/tr")); System.out.println("NUMBER OF ROWS IN THIS TABLE = "+tr_collection.size()); int row_num,col_num; row_num=1; for(WebElement trElement : tr_collection) { List<WebElement> td_collection=trElement.findElements(By.xpath("td")); System.out.println("NUMBER OF COLUMNS="+td_collection.size()); col_num=1; for(WebElement tdElement : td_collection) { System.out.println("row # "+row_num+", col # "+col_num+ "text="+tdElement.getText()); col_num++; } row_num++; } } }