(三) 命令和操作
這一部分將介紹一下WebDriver的一些具體操作和命令,實際操作中,我們需要兩大工具來幫助我們:FireBug和Xpath工具,這兩者都是Firefox上的插件。接下來我們所講解的都是以FirefoxDriver為基礎(chǔ)的,且基于WebDriver driver = new FirefoxDriver();創(chuàng)建的一個driver實例:
a) 訪問一個頁面
第一件你想使用WebDriver做的事情肯定是訪問一個頁面,最基礎(chǔ)的方法是調(diào)用“get”方法:
driver.get("http://www.google.com");
同樣我們可以使用:
driver.navigate().to("http://www.google.com");
WebDriver會自動等待到該頁面完全加載才執(zhí)行接下來的測試和腳本的執(zhí)行。但是如果你的頁面存在很多的AJAX加載,此時WebDriver是無法知道是否完成加載。檢查此類頁面是否加載完成,那么我們就需要Explicit和Implicit Wait(這兩個將在后面文章解釋)。
b) 定位UI元素
WebDriver可以通過WebDriver實例來定位元素,任何語言庫都含有“Find Element”和“Find Elements”的方法。第一個方法返回一個WebElement或者拋出異常。后者返回所有WebElement的列表,或者空列表。
獲取和定位元素我們調(diào)用“By”方法。下面具體解釋下“By”方法:
By ID
這是一個極為有效定位元素的方法。普遍的現(xiàn)狀是UI工程師在實際編寫頁面時很少寫id或者自動生產(chǎn)一個ID,這些都是需要避免的。對于一個頁面Element來說,class比自動生產(chǎn)的id更好。
通過id定位元素的例子:
<div id="coolestWidgetEvah">...</div>
WebElement element = driver.findElement(By.id("coolestWidgetEvah"));
By Class Name
這里的class指的是DOM中的元素,在實際使用過程中,我們也會發(fā)現(xiàn)很多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
DOM的Tag元素
用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
根據(jù)鏈接的部分文字
例子:
<a href="http://www.google.com/search?q=cheese">search for cheese</a>>
WebElement cheese = driver.findElement(By.partialLinkText("cheese"));
By CSS
從名字上看,這是根據(jù)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支持的瀏覽器,我們提供自己的實現(xiàn)方式。但是三個Driver有一定的區(qū)別。
例子:
<input type="text" name="example" />
<INPUT type="text" name="other" />
List<WebElement> inputs = driver.findElements(By.xpath("http://input"));
查找結(jié)果:
HTML元素有時并不需明確聲明,因為他們將默認(rèn)為已知值的屬性。例如,input標(biāo)簽,就不需要設(shè)置type為text,默認(rèn)屬性就是text,經(jīng)驗原則:WebDriver在使用中的XPath時,不應(yīng)該期望能夠?qū)@些隱含屬性相匹配。
使用javascript
您可以執(zhí)行任意JavaScript找到一個元素,只要你返回一個DOM元素,它會自動轉(zhuǎn)換到一個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標(biāo)簽
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);
提交:
Submit在form中
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();
Windows和Frames之間的切換
一些web應(yīng)用程序有許多Frames或多個Windows。 WebDriver支持使用“switchTo”的方法實現(xiàn)的窗口之間切換。
driver.switchTo().window("windowName");
所有對driver的調(diào)用都會指向特定的窗口,但是我們怎么知道窗口的名字呢?我們可以查看javascript代碼和打開他的鏈接:
<a href="somewhere.html" target="windowName">Click here to open a new window</a>
另外,還可以通過“window handle”去調(diào)用“switchTo().window()”,通過這個,我們就遍歷來找到所有打開的窗口:
for (String handle : driver.getWindowHandles()) {
driver.switchTo().window(handle);
}
Switch同樣支持frame:
driver.switchTo().frame("frameName");
同樣可以使用他訪問subframe,找frameName的第一個subframe中叫做child的frame:
driver.switchTo().frame("frameName.0.child");
彈出框:
從selenium2.0開始,已經(jīng)支持對彈出框的獲取
Alert alert = driver.switchTo().alert();
這個方法會返回當(dāng)前被打開打警告框,你可以進(jìn)行統(tǒng)一,取消,讀取提示內(nèi)容,后則進(jìn)入到提示,這個同樣使用alerts,confirms,prompts。
Navigation:History and Location
之前我們就可以通過get方法來打開一個網(wǎng)頁,像我們所看到的,WebDriver同樣還有許多小接口,Navigation就是其中一個小接口:
driver.navigate().to("http://www.");
navigate().to和get()其實作用是一樣的,但是navigate還可以進(jìn)行瀏覽器的前進(jìn)后退操作:
driver.navigate().forward();
driver.navigate().back();