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
51Testing软件测试网'kT6L6@
J6o/~N%D
by id 使用id属性
)Zl7_ qe0
WebElement element = driver.findElement(By.id("kw"));
51Testing软件测试网%`:z _3Lw6U.m K8n
51Testing软件测试网4U0SfH5zx%W
by classname
通过clss属性 通常使用findelements来获取一组元素
,n&wIofR0
.S*U l QA3~ YBw%E0
by tagname
通过html标记
D+FY.rX Q/E
Y,M1A0
by name
name属性
51Testing软件测试网5C?}X,q Kz s4c
by linktext 链接的可见文字
by partiallinktext 链接的部分可见文字
51Testing软件测试网 Wf KfT6v,^(e
by css
使用cssSelector
WebElement cheese = driver.findElement(By.cssSelector(""));
51Testing软件测试网T O.Le.O.H@drF
BY XPATH
WebElement sinput = driver.findElement(By.xpah("//input[@id='kw']"));
51Testing软件测试网1K ^2`Hqn9k3l2Vu|
*下拉框操作
可以使用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();
}
^5~X-\Nx2l
uWt0
使用select类的方法更有效率,如
Select select = new Select(driver.findElement(By.tagName("select")));
select.deselectAll();
select.selectByVisibleText("Edam");
51Testing软件测试网^Q+RO0ehnK
*表单提交
element.submit();
每个元素都有submit方法,如果调用该方法,WebDriver将会在DOM中查找,直到找到其所在的表单,然后提交。如果元素并不是在一个表单中,那么将抛出NoSuchElementException异常。
(U;mO8TZ0
switchTo()在窗口和frame之间移动
driver.switchTo().window("windowName");
Alert alert = driver.switchTo().alert();访问讲alert,同样适用于alerts, confirms, and prompts.
51Testing软件测试网!z+{4XwR0J
rv/E
*webdriver实现
HtmlUnit Driver
运行速度最快,没有UI,无法模拟javascript的行为
@6ue%\W$V SG0
firefoxdriver
运行速度介于htmlunit和Internet Explorer Driver 之间,模拟浏览器,可以在
unix,mac及windows中运行
51Testing软件测试网Z
oK9J:B
[
Internet Explorer Driver
运行速度最慢,且对xpath及css支持不是很好,只能运行在windows中
"[ru3Ot7a2?@2I;V`0