Java代碼css
import org.openqa.selenium.WebDriver;java
importorg.openqa.selenium.firefox.FirefoxBinary;web
importorg.openqa.selenium.firefox.FirefoxDriver;chrome
importorg.openqa.selenium.ie.InternetExplorerDriver;apache
public class OpenBrowsers {瀏覽器
public static void main(String[] args) {cookie
//打開默認路徑的firefoxapp
WebDriver diver = new FirefoxDriver();ide
//打開指定路徑的firefox,方法1
System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\MozillaFirefox\\firefox.exe");
WebDriver dr = new FirefoxDriver();
//打開指定路徑的firefox,方法2
File pathToFirefoxBinary = newFile("D:\\Program Files\\Mozilla Firefox\\firefox.exe");
FirefoxBinary firefoxbin = newFirefoxBinary(pathToFirefoxBinary);
WebDriver driver1 = newFirefoxDriver(firefoxbin,null);
//打開ie
WebDriver ie_driver = new InternetExplorerDriver();
//打開chrome
System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
System.setProperty("webdriver.chrome.bin",
"C:\\Documents and Settings\\gongjf\\Local Settings"
+"\\ApplicationData\\Google\\Chrome\\Application\\chrome.exe");
}
}
打開一個瀏覽器後,咱們須要跳轉到特定的url下,看下面代碼:
Java代碼
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class OpenUrl {
publicstatic void main(String []args){
Stringurl = "http://www.51.com";
WebDriverdriver = new FirefoxDriver();
//用get方法
driver.get(url);
//用navigate方法,而後再調用to方法
driver.navigate().to(url);
}
}
測試完成後,須要關閉瀏覽器
Java代碼
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class CloseBrowser {
publicstatic void main(String []args){
Stringurl = "http://www.51.com";
WebDriverdriver = new FirefoxDriver();
driver.get(url);
//用quit方法
driver.quit();
//用close方法
driver.close();
}
}
有時候咱們須要返回當前頁面的url或者title作一些驗證性的操做等。代碼以下:
Java代碼
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class GetUrlAndTitle {
publicstatic void main(String []args){
Stringurl = "http://www.google.com";
WebDriverdriver = new FirefoxDriver();
driver.get(url);
//獲得title
Stringtitle = driver.getTitle();
//獲得當前頁面url
StringcurrentUrl = driver.getCurrentUrl();
//輸出title和currenturl
System.out.println(title+"\n"+currentUrl);
}
}
getWindowHandle() 返回當前的瀏覽器的窗口句柄
getWindowHandles() 返回當前的瀏覽器的全部窗口句柄
getPageSource() 返回當前頁面的源碼
從上面代碼能夠看出操做瀏覽器的主要方法都來自org.openqa.selenium.WebDriver這個接口中。看了一下源代碼這些方法都是在org.openqa.selenium.remote.RemoteWebDriver這個類中實現的,而後不一樣瀏覽的driver類繼承RemoteWebDriver。
selenium-webdriver提供了強大的元素定位方法,支持如下三種方法。
單個對象的定位方法
多個對象的定位方法
層級定位
定位單個元素
在定位單個元素時,selenium-webdriver提示了以下一些方法對元素進行定位。
By.className(className))
By.cssSelector(selector)
By.id(id)
By.linkText(linkText)
By.name(name)
By.partialLinkText(linkText)
By.tagName(name)
By.xpath(xpathExpression)
注意:selenium-webdriver經過findElement()\findElements()等find方法調用"By"對象來定位 和查詢元素。By類只是提供查詢的方式進行分類。findElement返回一個元素對象不然拋出異常,findElements返回符合條件的元素 List,若是不存在符合條件的就返回一個空的list。
當所定位的元素具備class屬性的時候咱們能夠經過classname來定位該元素。
下面的例子定位了51.com首頁上class爲"username"的li。
Java代碼
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
public class ByClassName {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.51.com");
WebElement element =driver.findElement(By.className("username"));
System.out.println(element.getTagName());
}
}
輸出結果:
Java代碼
Li
51.com首頁的賬號輸入框的html代碼以下:
Java代碼
<input id="passport_51_user"type="text" value="" tabindex="1" title="用戶名/彩虹號/郵箱"
name="passport_51_user">
在下面的例子中用id定位這個輸入框,並輸出其title,藉此也能夠驗證代碼是否工做正常。
Java代碼
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class ByUserId {
/**
* @param args
*/
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
WebDriverdr = new FirefoxDriver();
dr.get("http://www.51.com");
WebElementelement = dr.findElement(By.id("passport_51_user"));
System.out.println(element.getAttribute("title"));
}
}
Java代碼
用戶名/彩虹號/郵箱
51.com首頁的賬號輸入框的html代碼以下:
Java代碼
<input id="passport_51_user"type="text" value="" tabindex="1" title="用戶名/彩虹號/郵箱"
name="passport_51_user">
使用name定位
Java代碼
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class ByUserId {
/**
* @param args
*/
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
WebDriverdr = new FirefoxDriver();
dr.get("http://www.51.com");
WebElemente = dr.findElement(By.name("passport_51_user")); System.out.println(element.getAttribute("title"));
}
}
輸出結果:
Java代碼
用戶名/彩虹號/郵箱
51.com首頁的賬號輸入框的html代碼以下:
Java代碼
<input id="passport_51_user"type="text" value="" tabindex="1" title="用戶名/彩虹號/郵箱"
name="passport_51_user">
使用css定位
Java代碼
WebElement e1 =dr.findElement(By.cssSelector("#passport_51_user"));
51.com首頁的賬號輸入框的html代碼以下:
Java代碼
<input id="passport_51_user"type="text" value="" tabindex="1" title="用戶名/彩虹號/郵箱"
name="passport_51_user">
經過xpath查找:
Java代碼
WebElement element=driver.findElement(By.xpath("//input[@id=' passport_51_user ']"));
在定位link元素的時候,可使用link和link_text屬性;
另外還可使用tag_name屬性定位任意元素;
上面提到findElements()方法能夠返回一個符合條件的元素List組。看下面例子。
Java代碼
import java.io.File;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FindElementsStudy {
/**
* @author gongjf
*/
publicstatic void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://www.51.com");
//定位到全部<input>標籤的元素,而後輸出他們的id
List<WebElement>element = driver.findElements(By.tagName("input"));
for(WebElement e : element){
System.out.println(e.getAttribute("id"));
}
driver.quit();
}
}
輸出結果:
Java代碼
passport_cookie_login
gourl
passport_login_from
passport_51_user
passport_51_password
passport_qq_login_2
btn_reg
passport_51_ishidden
passport_auto_login
上面的代碼返回頁面上全部input對象
層級定位的思想是先定位父元素,而後再從父元素中精肯定位出其咱們須要選取的子元素。
層級定位通常的應用場景是沒法直接定位到須要選取的元素,可是其父元素比較容易定位,經過定位父元素再遍歷其子元素選擇須要的目標元素,或者須要定位某個元素下全部的子元素。
下面的代碼演示瞭如何使用層級定位class爲"login"的div,而後再取得它下面的全部label,並打印出他們的文本
Java代碼
importjava.util.List;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxBinary;
importorg.openqa.selenium.firefox.FirefoxDriver;
publicclass LayerLocator {
/**
* @author gongjf
*/
public static void main(String[] args){
WebDriver driver = new FirefoxDriver();
driver.get("http://www.51.com");
//定位class爲"login"的div,而後再取得它下面的全部label,並打印出他們的值
WebElement element =driver.findElement(By.className("login"));
List<WebElement> el =element.findElements(By.tagName("label"));
for(WebElement e : el)
System.out.println(e.getText());
}
}
輸出結果:
Java代碼
賬號:
密碼:
隱身
找到頁面元素後,怎樣對頁面進行操做呢?咱們能夠根據不一樣的類型的元素來進行一一說明。
找到輸入框元素:
WebElement element =driver.findElement(By.id("passwd-id"));
在輸入框中輸入內容:
element.sendKeys(「test」);
將輸入框清空:
element.clear();
獲取輸入框的文本內容:
element.getText();
找到下拉選擇框的元素:
Select select = newSelect(driver.findElement(By.id("select")));
選擇對應的選擇項:
select.selectByVisibleText(「mediaAgencyA」);
或
select.selectByValue(「MA_ID_001」);
不選擇對應的選擇項:
select.deselectAll();
select.deselectByValue(「MA_ID_001」);
select.deselectByVisibleText(「mediaAgencyA」);
或者獲取選擇項的值:
select.getAllSelectedOptions();
select.getFirstSelectedOption();
對下拉框進行操做時首先要定位到這個下拉框,new 一個Selcet對象,而後對它進行操做
找到單選框元素:
WebElement bookMode =driver.findElement(By.id("BookMode"));
選擇某個單選項:
bookMode.click();
清空某個單選項:
bookMode.clear();
判斷某個單選項是否已經被選擇:
bookMode.isSelected();
多選項的操做和單選的差很少:
WebElement checkbox=driver.findElement(By.id("myCheckbox."));
checkbox.click();
checkbox.clear();
checkbox.isSelected();
checkbox.isEnabled();
找到按鈕元素:
WebElement saveButton =driver.findElement(By.id("save"));
點擊按鈕:
saveButton.click();
判斷按鈕是否enable:
saveButton.isEnabled ();
也就是左邊是可供選擇項,選擇後移動到右邊的框中,反之亦然。例如:
Select lang = new Select(driver.findElement(By.id("languages")));
lang.selectByVisibleText(「English」);
WebElement addLanguage=driver.findElement(By.id("addButton"));
addLanguage.click();
Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();
後面有具體的例子解釋~
Form中的元素的操做和其它的元素操做同樣,對元素操做完成後對錶單的提交能夠:
WebElement approve =driver.findElement(By.id("approve"));
approve.click();
或
approve.submit();//只適合於表單的提交
上傳文件的元素操做:
WebElement adFileUpload = driver.findElement(By.id("WAP-upload"));
String filePath ="C:\test\\uploadfile\\media_ads\\test.jpg";
adFileUpload.sendKeys(filePath);
WebElement element=driver.findElement(By.name("source"));
WebElement target = driver.findElement(By.name("target"));
(new Actions(driver)).dragAndDrop(element,target).perform();
打開一個新的頁面:
driver.navigate().to("http://www.example.com");
經過歷史導航返回原頁面:
driver.navigate().forward();
driver.navigate().back();
有時候咱們在定位一個頁面元素的時候發現一直定位不了,反覆檢查本身寫的定位器沒有任何問題,代碼也沒有任何問題。這時你就要看一下這個頁面元素是否在一個iframe中,這可能就是找不到的緣由之一。若是你在一個default content中查找一個在iframe中的元素,那確定是找不到的。反之你在一個iframe中查找另外一個iframe元素或default content中的元素,那必然也定位不到。
selenium webdriver中提供了進入一個iframe的方法:
WebDriverorg.openqa.selenium.WebDriver.TargetLocator.frame(String nameOrId)
也提供了一個返回default content的方法:
WebDriver org.openqa.selenium.WebDriver.TargetLocator.defaultContent()
這樣使咱們面對iframe時能夠輕鬆應對。
如下面的html代碼爲例,咱們看一下處現iframe。
Html代碼
main.html
<html>
<head>
<title>FrameTest</title>
</head>
<body>
<divid = "id1">this is a div!</div>
<iframe id = "frame" frameborder="0" scrolling="no"style="left:0;position:absolute;" src ="frame.html"></iframe>
</body>
</html>
frame.html
<html>
<head>
<title>this is a frame!</title>
</head>
<body>
<divid = "div1">this is a div,too!</div>
<label>input:</label>
<inputid = "input1"></input>
</body>
</html>
Java代碼
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class FameStudy {
publicstatic void main(String[] args) {
WebDriverdr = new FirefoxDriver();
Stringurl = "\\Your\\Path\\to\\main.html";
dr.get(url);
//在defaultcontent定位id="id1"的div
dr.findElement(By.id("id1"));
//此時,沒有進入到id="frame"的frame中時,如下兩句會報錯
dr.findElement(By.id("div1"));//報錯
dr.findElement(By.id("input1"));//報錯
//進入id="frame"的frame中,定位id="div1"的div和id="input1"的輸入框。
dr.switchTo().frame("frame");
dr.findElement(By.id("div1"));
dr.findElement(By.id("input1"));
//此時,沒有跳出frame,若是定位defaultcontent中的元素也會報錯。
dr.findElement(By.id("id1"));//報錯
//跳出frame,進入defaultcontent;從新定位id="id1"的div
dr.switchTo().defaultContent();
dr.findElement(By.id("id1"));
}
}
小結:
switch_to方法會new1個TargetLocator對象,使用該對象的frame方法能夠將當前識別的」主體」移動到須要定位的frame上去。
在selenium 1.X裏面獲得彈出窗口是一件比較麻煩的事,特別是新開窗口沒有id、name的時候。在selenium webdriver中獲得新開窗口相對簡單的多,它無關新開窗口的id、name等屬性。如下面的html爲例:
Html代碼
<span style="white-space: normal;">test.html</span>
<html>
<head><title>Test Popup Window</title></head>
<body>
<a id = "51" href = "http://www.51.com/" target ="_blank">Let's go!</a>
</body>
</html>
下面的代碼演示瞭如何去獲得彈出的新窗口
Java代碼
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class PopupWindowTest {
/**
* @author gongjf
*/
publicstatic void main(String[] args) {
System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe");
WebDriverdr = new FirefoxDriver();
Stringurl ="\\Your\\Path\\to\\main.html";
dr.get(url);
dr.findElement(By.id("51")).click();
//獲得當前窗口的句柄
StringcurrentWindow = dr.getWindowHandle();
//獲得全部窗口的句柄
Set<String>handles = dr.getWindowHandles();
Iterator<String>it = handles.iterator();
while(it.hasNext()){
if(currentWindow== it.next()) continue;
dr.switchTo().window(it.next());
}
}
}
輸出結果:
title,url = 51.com 真人配對玩遊戲,http://www.51.com/
小結:
捕獲或者說定位彈出窗口的關鍵在於得到彈出窗口的句柄。(
在上面的代碼裏,使用windowhandle方法來獲取當前瀏覽器窗口的句柄,使用了windowhandles方法獲取全部彈出的瀏覽器窗口的句柄,而後經過排除當前句柄的方法來獲得新開窗口的句柄。
在獲取新彈出窗口的句柄後,使用switchto.window(newwindow_handle)方法,將新窗口的句柄做爲參數傳入既可捕獲到新窗口了。
若是想回到之前的窗口定位元素,那麼再調用1次switchto.window方法,傳入以前窗口的句柄既可達到目的。
alert、confirm、prompt這樣的js對話框在selenium1.X時代也是難啃的骨頭,經常要用autoit來幫助處理。
試用了一下selenium webdriver中處理這些對話框十分方便簡潔
Html代碼
Dialogs.html
<html>
<head>
<title>Alert</title>
</head>
<body>
<input id = "alert" value = "alert" type ="button" onclick = "alert('歡迎!請按確認繼續!');"/>
<input id = "confirm" value= "confirm" type = "button" onclick = "confirm('肯定嗎?');"/>
<inputid = "prompt" value = "prompt" type = "button"onclick = "var name = prompt('請輸入你的名字:','請輸入
你的名字'); document.write(name) "/>
</body>
</html>
以上html代碼在頁面上顯示了三個按鈕,點擊他們分別彈出alert、confirm、prompt對話框。若是在prompt對話框中輸入文字點擊肯定以後,將會刷新頁面,顯示出這些文字。
selenium webdriver 處理這些彈層的代碼以下:
Java代碼
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class DialogsStudy {
/**
* @author gongjf
*/
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe");
WebDriverdr = new FirefoxDriver();
Stringurl = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Dialogs.html";//"/Your/Path/to/main.html"
dr.get(url);
//點擊第一個按鈕,輸出對話框上面的文字,而後叉掉
dr.findElement(By.id("alert")).click();
Alertalert = dr.switchTo().alert();
Stringtext = alert.getText();
System.out.println(text);
alert.dismiss();
//點擊第二個按鈕,輸出對話框上面的文字,而後點擊確認
dr.findElement(By.id("confirm")).click();
Alertconfirm = dr.switchTo().alert();
Stringtext1 = confirm.getText();
System.out.println(text1);
confirm.accept();
//點擊第三個按鈕,輸入你的名字,而後點擊確認,最後
dr.findElement(By.id("prompt")).click();
Alertprompt = dr.switchTo().alert();
Stringtext2 = prompt.getText();
System.out.println(text2);
prompt.sendKeys("jarvi");
prompt.accept();
}
}
小結:
從以上代碼能夠看出dr.switchTo().alert();這句能夠獲得alert\confirm\prompt對話框的對象,而後運用其方法對它進行操做。對話框操做的主要方法有:
getText() 獲得它的文本值
accept() 至關於點擊它的"確認"
dismiss() 至關於點擊"取消"或者叉掉對話框
sendKeys() 輸入值,這個alert\confirm沒有對話框就不能用了,否則會報錯。
Web 測試中咱們常常會接觸到Cookies,一個Cookies主要屬性有」所在域、name、value、有效日期和路徑",下面來說一下怎麼操做Cookies
Java代碼
import java.util.Set;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class CookiesStudy {
/**
* @author gongjf
*/
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe");
WebDriverdr = new FirefoxDriver();
dr.get("http://www.51.com");
//增長一個name ="name",value="value"的cookie
Cookiecookie = new Cookie("name", "value");
dr.manage().addCookie(cookie);
//獲得當前頁面下全部的cookies,而且輸出它們的所在域、name、value、有效日期和路徑
Set<Cookie>cookies = dr.manage().getCookies();
System.out.println(String.format("Domain-> name -> value -> expiry -> path"));
for(Cookiec : cookies)
System.out.println(String.format("%s-> %s -> %s -> %s -> %s",
c.getDomain(),c.getName(), c.getValue(),c.getExpiry(),c.getPath()));
//刪除cookie有三種方法
//第一種經過cookie的name
dr.manage().deleteCookieNamed("CookieName");
//第二種經過Cookie對象
dr.manage().deleteCookie(cookie);
//第三種所有刪除
dr.manage().deleteAllCookies();
}
小結:
上面的代碼首先在頁面中增長了一個cookie,而後遍歷頁面的全部cookies,並輸出他們的主要屬性。最後就是三種刪除cookie的方法。
web的自動化測試中,咱們常常會遇到這樣一種狀況:當咱們的程序執行時須要頁面某個元素,而此時這個元素還未加載完成,這時咱們的程序就會報錯。怎麼辦?等待。等待元素出現後再進行對這個元素的操做。
在selenium-webdriver中咱們用兩種方式進行等待:明確的等待和隱性的等待。
明確的等待
明確的等待是指在代碼進行下一步操做以前等待某一個條件的發生。最很差的狀況是使用Thread.sleep()去設置一段確認的時間去等待。但爲何說最很差呢?由於一個元素的加載時間有長有短,你在設置sleep的時間以前要本身把握長短,過短容易超時,太長浪費時間。selenium webdriver提供了一些方法幫助咱們等待正好須要等待的時間。利用WebDriverWait類和ExpectedCondition接口就能實現這一點。
下面的html代碼實現了這樣的一種效果:點擊click按鈕5秒鐘後,頁面上會出現一個紅色的div塊。咱們須要寫一段自動化腳本去捕獲這個出現的div,而後高亮之。
Html代碼
Wait.html
<html>
<head>
<title>Set Timeout</title>
<style>
.red_box { width = 20%; height: 100px; border:none;}
</style>
<script>
function show_div(){
setTimeout("create_div()", 5000);
}
function create_div(){
d =document.createElement('div');
d.className ="red_box";
document.body.appendChild(d);
}
</script>
</head>
<body>
<button id = "b" onclick ="show_div()">click</button>
</body>
</html>
下面的代碼實現了高亮動態生成的div塊的功能:
Java代碼
import org.openqa.selenium.By;
importorg.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.openqa.selenium.support.ui.ExpectedCondition;
importorg.openqa.selenium.support.ui.WebDriverWait;
public class WaitForSomthing {
/**
* @author gongjf
*/
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe");
WebDriverdr = new FirefoxDriver();
Stringurl = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html";//"/Your/Path/to/Wait.html"
dr.get(url);
WebDriverWaitwait = new WebDriverWait(dr,10);
wait.until(newExpectedCondition<WebElement>(){
@Override
publicWebElement apply(WebDriver d) {
returnd.findElement(By.id("b"));
}}).click();
WebElementelement = dr.findElement(By.cssSelector(".red_box"));
((JavascriptExecutor)dr).executeScript("arguments[0].style.border= \"5px solid yellow\"",element);
}
}
上面的代碼WebDriverWait類的構造方法接受了一個WebDriver對象和一個等待最長時間(10秒)。而後調用until方法,其中重寫了 ExpectedCondition接口中的apply方法,讓其返回一個WebElement,即加載完成的元素,而後點擊。默認狀況下,WebDriverWait每500毫秒調用一次ExpectedCondition,直到有成功的返回,固然若是超過設定的值尚未成功的返回,將拋出異常。
隱性等待
隱性等待是指當要查找元素,而這個元素沒有立刻出現時,告訴WebDriver查詢Dom必定時間。默認值是0,可是設置以後,這個時間將在WebDriver對象實例整個生命週期都起做用。上面的代碼就變成了這樣:
Java代碼
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
importorg.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.openqa.selenium.support.ui.ExpectedCondition;
importorg.openqa.selenium.support.ui.WebDriverWait;
public class WaitForSomthing {
/**
* @author gongjf
*/
publicstatic void main(String[] args) {
//TODO Auto-generated method stub
System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe");
WebDriverdr = new FirefoxDriver();
//設置10秒
dr.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
Stringurl = "file:///C:/Documents and Settings/gongjf/桌面/selenium_test/Wait.html";//"/Your/Path/to/Wait.html"
dr.get(url);
//註釋掉原來的
/*WebDriverWaitwait = new WebDriverWait(dr,10);
wait.until(newExpectedCondition<WebElement>(){
@Override
publicWebElement apply(WebDriver d) {
returnd.findElement(By.id("b"));
}}).click();*/
dr.findElement(By.id("b")).click();
WebElementelement = dr.findElement(By.cssSelector(".red_box"));
((JavascriptExecutor)dr).executeScript("arguments[0].style.border= \"5px solid yellow\"",element);
}
}
小結:
兩種方法任選其一
在自動化測試中經常會用到截圖功能。能夠截取頁面全圖,無論頁面有多長。
下面的代碼演示瞭如何使用webdriver進行截圖:
Java代碼
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
public class ShotScreen {
/**
* @author gongjf
* @throws IOException
* @throws InterruptedException
*/
publicstatic void main(String[] args) throws IOException, InterruptedException {
System.setProperty("webdriver.firefox.bin","D:\\ProgramFiles\\Mozilla Firefox\\firefox.exe");
WebDriverdr = new FirefoxDriver();
dr.get("http://www.51.com");
//這裏等待頁面加載完成
Thread.sleep(5000);
//下面代碼是獲得截圖並保存在D盤下
FilescreenShotFile = ((TakesScreenshot)dr).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenShotFile,new File("D:/test.png"));
}
}
WebDriver對頁面的操做,須要找到一個WebElement,而後再對其進行操做,比較繁瑣:
// Find the text inputelement by itsname
WebElement element =driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
咱們能夠考慮對這些基本的操做進行一個封裝,簡化操做。好比,封裝代碼:
protected voidsendKeys(Byby, String value){
driver.findElement(by).sendKeys(value);
}
那麼,在測試用例能夠這樣簡化調用:
sendKeys(By.name("q"),」Cheese!」);
看,這就簡潔多了。
相似的封裝還有:
package com.drutt.mm.end2end.actions;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
importorg.openqa.selenium.remote.RemoteWebDriver;
importorg.openqa.selenium.support.ui.WebDriverWait;
importcom.drutt.mm.end2end.data.TestConstant;
public class WebDriverAction {
//protected WebDriverdriver;
protectedRemoteWebDriverdriver;
protectedWebDriverWaitdriverWait;
protected booleanisWebElementExist(Byselector) {
try {
driver.findElement(selector);
return true;
} catch(NoSuchElementException e) {
return false;
}
}
protectedStringgetWebText(By by) {
try {
return driver.findElement(by).getText();
} catch (NoSuchElementException e) {
return "Textnot existed!";
}
}
protectedvoidclickElementContainingText(By by, String text){
List<WebElement>elementList = driver.findElements(by);
for(WebElement e:elementList){
if(e.getText().contains(text)){
e.click();
break;
}
}
}
protectedStringgetLinkUrlContainingText(By by, String text){
List<WebElement>subscribeButton = driver.findElements(by);
String url = null;
for(WebElement e:subscribeButton){
if(e.getText().contains(text)){
url =e.getAttribute("href");
break;
}
}
return url;
}
protected voidclick(Byby){
driver.findElement(by).click();
driver.manage().timeouts().implicitlyWait(TestConstant.WAIT_ELEMENT_TO_LOAD,TimeUnit.SECONDS);
}
protectedStringgetLinkUrl(By by){
return driver.findElement(by).getAttribute("href");
}
protected voidsendKeys(Byby, String value){
driver.findElement(by).sendKeys(value);
}
小結:
按照上面的例子你能夠對各個方法進行封裝,使本身的代碼更加簡潔!
Selenium2.0中使用WeDriver API對頁面進行操做,它最大的優勢是不須要安裝一個selenium server就能夠運行,可是對頁面進行操做不如selenium1.0的Selenium RC API那麼方便。Selenium2.0提供了使用Selenium RC API的方法:
// 我用火狐瀏覽器做爲例子
WebDriver driver = newFirefoxDriver();
String baseUrl="http://www.google.com";
Selenium selenium = newWebDriverBackedSelenium(driver, baseUrl);
// 執行selenium命令
selenium.open("http://www.google.com");
selenium.type("name=q","cheese");
selenium.click("name=btnG");
WebDriver driverInstance = ((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();
selenium.stop();
我分別使用WebDriver API和SeleniumRC API寫了一個Login的腳本,很明顯,後者的操做更加簡單明瞭。
WebDriver API寫的Login腳本:
public void login() {
driver.switchTo().defaultContent();
driver.switchTo().frame("mainFrame");
WebElement eUsername= waitFindElement(By.id("username"));
eUsername.sendKeys(manager@ericsson.com);
WebElement ePassword= waitFindElement(By.id("password"));
ePassword.sendKeys(manager);
WebElementeLoginButton = waitFindElement(By.id("loginButton"));
eLoginButton.click();
}
SeleniumRC API寫的Login腳本:
public void login() {
selenium.selectFrame("relative=top");
selenium.selectFrame("mainFrame");
selenium.type("username","manager@ericsson.com");
selenium.type("password","manager");
selenium.click("loginButton");
}