Selenium2.0之WebDriver学习总结(2)

(三)   命令和操做javascript

这一部分将介绍一下WebDriver的一些具体操做和命令,实际操做中,咱们须要两大工具来帮助咱们:FireBugXpath工具,这二者都是Firefox上的插件。接下来咱们所讲解的都是以FirefoxDriver为基础的,且基于WebDriver driver = new FirefoxDriver();建立的一个driver实例:css

a)         访问一个页面html

第一件你想使用WebDriver作的事情确定是访问一个页面,最基础的方法是调用“get”方法:java

driver.get("http://www.google.com");

一样咱们可使用:web

driver.navigate().to("http://www.google.com");

 

WebDriver会自动等待到该页面彻底加载才执行接下来的测试和脚本的执行。可是若是你的页面存在不少的AJAX加载,此时WebDriver是没法知道是否完成加载。检查此类页面是否加载完成,那么咱们就须要ExplicitImplicit Wait(这两个将在后面文章解释)。浏览器

 

b)         定位UI元素工具

WebDriver能够经过WebDriver实例来定位元素,任何语言库都含有“Find Element”和“Find Elements”的方法。第一个方法返回一个WebElement或者抛出异常。后者返回全部WebElement的列表,或者空列表。学习

获取和定位元素咱们调用“By”方法。下面具体解释下“By”方法:测试

By IDgoogle

这是一个极为有效定位元素的方法。广泛的现状是UI工程师在实际编写页面时不多写id或者自动生产一个ID,这些都是须要避免的。对于一个页面Element来讲,class比自动生产的id更好。

经过id定位元素的例子:

<div id="coolestWidgetEvah">...</div>WebElement element = driver.findElement(By.id("coolestWidgetEvah"));


 

 By Class Name

        这里的class指的是DOM中的元素,在实际使用过程当中,咱们也会发现不少DOM元素含有相同的class名。

经过class name定位元素例子:


<div class="cheese"><span>Cheddar</span></div><div class="cheese"><span>Gouda</span></div>List<WebElement> cheeses = driver.findElements(By.className("cheese"));


By Tag Name

DOMTag元素

Tag name 定位元素的例子:

<iframe src="..."></iframe>WebElement frame = driver.findElement(By.tagName("iframe"));


By Name

例子:

<input name="cheese" type="text"/>WebElement cheese = driver.findElement(By.name("cheese"));


By Link Text

例子:


<a href="http://www.google.com/search?q=cheese">cheese</a>>WebElement cheese = driver.findElement(By.linkText("cheese"));


By Partial Link Text

根据连接的部分文字

例子:


<a href="http://www.google.com/search?q=cheese">search for cheese</a>>WebElement cheese = driver.findElement(By.partialLinkText("cheese"));


By CSS

从名字上看,这是根据CSS来定位元素。

例子:

<div id="food">         <span class="dairy">milk</span>         <span class="dairy aged">cheese</span></div>WebElement cheese = driver.findElement(By.cssSelector("#food span.dairy aged"));


By XPATH

在高级的水平下,WebDriver尽量使用浏览器的原生的XPath能力。在那些没有原生的XPath支持的浏览器,咱们提供本身的实现方式。可是三个Driver有必定的区别。Selenium2.0之WebDriver学习总结(2) - 网易杭州QA - 网易杭州 QA Team

例子:

 <input type="text" name="example" />    <INPUT type="text" name="other" />    List<WebElement> inputs = driver.findElements(By.xpath("//input"));

 

查找结果:

HTML元素有时并不需明确声明,由于他们将默认为已知值的属性。例如,input标签,就不须要设置typetext,默认属性就是text,经验原则:WebDriver在使用中的XPath时,不该该指望可以对这些隐含属性相匹配。

Selenium2.0之WebDriver学习总结(2) - 网易杭州QA - 网易杭州 QA Team 

使用javascript

您能够执行任意JavaScript找到一个元素,只要你返回一个DOM元素,它会自动转换到一个WebElement对象。

例子:

jQuery的页面加载一个简单的例子:

WebElement element = (WebElement) ((JavascriptExecutor)driver).executeScript("return $('.cheese')[0]");

寻求全部的页面上的input元素:

List<WebElement> labels = driver.findElements(By.tagName("label"));List<WebElement> inputs = (List<WebElement>) ((JavascriptExecutor)driver).executeScript(      "var labels = arguments[0], inputs = []; for (var i=0; i < labels.length; i++){" +"inputs.push(document.getElementById(labels[i].getAttribute('for'))); } return inputs;", labels);


用户表单填充

例子:

遍历select标签

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");
上传文件:

WebElement FileUpload =driver.findElement(By.id("upload"));

String filePath = "C:\test\\uploadfile\\media_ads\\test.jpg";

FileUpload.sendKeys(filePath);


提交:

Submitform

driver.findElement(By.id("submit")).click();


submit不在form

WebElement.submit();


拖拽操做:

WebElement element = driver.findElement(By.name("source"));

WebElement target = driver.findElement(By.name("target"));

(new Actions(driver)).dragAndDrop(element, target).perform();



WindowsFrames之间的切换

一些web应用程序有许多Frames或多个Windows WebDriver支持使用“switchTo”的方法实现的窗口之间切换。

driver.switchTo().window("windowName");

 

全部对driver的调用都会指向特定的窗口,可是咱们怎么知道窗口的名字呢?咱们能够查看javascript代码和打开他的连接:

<a href="somewhere.html" target="windowName">Click here to open a new window</a>

 

另外,还能够经过“window handle”去调用“switchTo().window()”,经过这个,咱们就遍从来找到全部打开的窗口:

for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); }


Switch一样支持frame

driver.switchTo().frame("frameName");

 

一样可使用他访问subframe,找frameName的第一个subframe中叫作childframe

driver.switchTo().frame("frameName.0.child");


弹出框:

selenium2.0开始,已经支持对弹出框的获取

Alert alert = driver.switchTo().alert();

 

这个方法会返回当前被打开打警告框,你能够进行统一,取消,读取提示内容,后则进入到提示,这个一样使用alertsconfirmsprompts


NavigationHistory and Location

以前咱们就能够经过get方法来打开一个网页,像咱们所看到的,WebDriver一样还有许多小接口,Navigation就是其中一个小接口:

driver.navigate().to("http://www.example.com");

 

navigate().toget()其实做用是同样的,可是navigate还能够进行浏览器的前进后退操做:

driver.navigate().forward();

driver.navigate().back();

相关文章
相关标签/搜索