selenium-webdriver 常用API 命令和操作

上一篇 / 下一篇  2012-12-12 23:34:39 / 个人分类:selenium

selenium rc是先启动控制台再来控制浏览器运行的
webdriver是通过浏览器的内建自动化支持组建来直接启动浏览器的
*访问页面
driver.get(“http://www.baidu.com”)
或者可以使用driver.navigate().to("http://www.baidu.com")
两者使用上没有任何不同,唯一不同之处就是,前者比后者更容易输入一些
navigate接口还可以来模拟浏览器前进和后退
driver.navigate().forward();
driver.navigate().back();
*定位界面元素
和qtp的object一样,所有元素都被当做是一个webelement

Z$T sxd)P/IW:e.J0
by id 使用id属性
51Testing软件测试网~NT d$~)~F
WebElement element = driver.findElement(By.id("kw"));

b8u%@T8Z;c'iE7NM0

;O2k Qpp-nY;v Qj0
by classname
通过clss属性 通常使用findelements来获取一组元素

.m6h'{7Hg4Lm4E.K0
51Testing软件测试网"{S c F;@pA9_
by tagname
通过html标记
51Testing软件测试网|K"NL5xnX b;l{
by name
name属性
51Testing软件测试网"o@D I^
by linktext 链接的可见文字
by partiallinktext 链接的部分可见文字

4hC0?1U3oc0
by css
使用cssSelector
WebElement cheese = driver.findElement(By.cssSelector(""));
51Testing软件测试网AT q;^5B*Xg1e
BY XPATH
WebElement sinput = driver.findElement(By.xpah("//input[@id='kw']"));
51Testing软件测试网:n9I]"^ E!v&cuu%y
*下拉框操作
可以使用click来操作下拉框
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(); 

^ dj)[j&J6ym0
使用select类的方法更有效率,如
Select select = new Select(driver.findElement(By.tagName("select")));
select.deselectAll();
select.selectByVisibleText("Edam");
51Testing软件测试网n J7G l9Q|chpz9P.\
*表单提交
element.submit();
每个元素都有submit方法,如果调用该方法,WebDriver将会在DOM中查找,直到找到其所在的表单,然后提交。如果元素并不是在一个表单中,那么将抛出NoSuchElementException异常。 

Ge:} _ }#B!l,z0
*在Windows和Frames中移动 
switchTo()在窗口和frame之间移动
driver.switchTo().window("windowName");
Alert alert = driver.switchTo().alert();访问讲alert,同样适用于alerts, confirms, and prompts.

w,[XB`#d2?"Wa0
*webdriver实现
HtmlUnit Driver
运行速度最快,没有UI,无法模拟javascript的行为
51Testing软件测试网x&oYans1D2H,Z
firefoxdriver
运行速度介于htmlunit和Internet Explorer Driver 之间,模拟浏览器,可以在unix,mac及windows中运行

|)r8~aR o?j@ g#Q0
Internet Explorer Driver 
运行速度最慢,且对xpath及css支持不是很好,只能运行在windows中

8V5b]{%g&r0

TAG:

 

评分:0

我来说两句

Open Toolbar