http://code.google.com/p/selenium/downloads/listcss
官方UserGuide:http://seleniumhq.org/docs/java
咱們經常使用的瀏覽器有firefox和IE兩種,firefox是selenium支持得比較成熟的瀏覽器。可是作頁面的測試,速度一般很慢,嚴重影 響持續集成的速度,這個時候建議使用HtmlUnit,不過HtmlUnitDirver運行時是看不到界面的,對調試就不方便了。使用哪一種瀏覽器,能夠 作成配置項,根據須要靈活配置。web
//Create a newinstance of the Firefox driver瀏覽器
WebDriver driver = newFirefoxDriver();服務器
//Create a newinstance of the Internet Explorer drivercookie
WebDriver driver = newInternetExplorerDriver ();app
//Createa new instance of the Internet Explorer driver dom
WebDriverdriver = new HtmlUnitDriver();ide
對頁面對測試,首先要打開被測試頁面的地址(如:http://www.google.com),web driver 提供的get方法能夠打開一個頁面:測試
// And now use thedriver to visit Google
driver.get("http://www.google.com");
package org.openqa.selenium.example; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class Selenium2Example { public static voidmain(String[] args) { // Create a newinstance of the Firefox driver // Notice that theremainder of the code relies on the interface, // not the implementation. WebDriver driver = newFirefoxDriver(); // And now use this tovisit Google driver.get("http://www.google.com"); // Alternatively thesame thing can be done like this // driver.navigate().to("http://www.google.com"); // Find the text inputelement by its name WebElement element =driver.findElement(By.name("q")); // Enter something tosearch for element.sendKeys("Cheese!"); // Now submit the form.WebDriver will find the form for us from the element element.submit(); // Check the title ofthe page System.out.println("Page title is: " + driver.getTitle()); // Google's search isrendered dynamically with JavaScript. // Wait for the pageto load, timeout after 10 seconds (newWebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Booleanapply(WebDriver d) { returnd.getTitle().toLowerCase().startsWith("cheese!"); } }); // Should see:"cheese! - Google Search" System.out.println("Page title is: " + driver.getTitle()); //Close the browser driver.quit(); } }
優勢:HtmlUnit Driver不會實際打開瀏覽器,運行速度很快。對於用FireFox等瀏覽器來作測試的自動化測試用例,運行速度一般很慢,HtmlUnit Driver無疑是能夠很好地解決這個問題。
缺點:它對JavaScript的支持不夠好,當頁面上有複雜JavaScript時,常常會捕獲不到頁面元素。
使用:
WebDriver driver = new HtmlUnitDriver();
優勢:FireFox Dirver對頁面的自動化測試支持得比較好,很直觀地模擬頁面的操做,對JavaScript的支持也很是完善,基本上頁面上作的全部操做FireFox Driver均可以模擬。
缺點:啓動很慢,運行也比較慢,不過,啓動以後Webdriver的操做速度雖然不快但仍是能夠接受的,建議不要頻繁啓停FireFox Driver。
使用:
WebDriver driver = new FirefoxDriver();
Firefox profile的屬性值是能夠改變的,好比咱們平時使用得很是頻繁的改變useragent的功能,能夠這樣修改:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("general.useragent.override", "some UAstring");
WebDriver driver = new FirefoxDriver(profile);
優勢:直觀地模擬用戶的實際操做,對JavaScript提供完善的支持。
缺點:是全部瀏覽器中運行速度最慢的,而且只能在Windows下運行,對CSS以及XPATH的支持也不夠好。
使用:
WebDriver driver = new InternetExplorerDriver();
Webdriver的findElement方法能夠用來找到頁面的某個元素,最經常使用的方法是用id和name查找。下面介紹幾種比較經常使用的方法。
假設頁面寫成這樣:
<input type="text" name="passwd"id="passwd-id" />
那麼能夠這樣找到頁面的元素:
經過id查找:
WebElement element = driver.findElement(By.id("passwd-id"));
或經過name查找:
WebElement element = driver.findElement(By.name("passwd"));
或經過xpath查找:
WebElement element =driver.findElement(By.xpath("//input[@id='passwd-id']"));
假設頁面寫成這樣:
<div class="cheese"><span>Cheddar</span></div><divclass="cheese"><span>Gouda</span></div>
能夠經過這樣查找頁面元素:
List<WebElement>cheeses = driver.findElements(By.className("cheese"));
假設頁面元素寫成這樣:
<ahref="http://www.google.com/search?q=cheese">cheese</a>>
那麼能夠經過這樣查找:
WebElement cheese =driver.findElement(By.linkText("cheese"));
找到頁面元素後,怎樣對頁面進行操做呢?咱們能夠根據不一樣的類型的元素來進行一一說明。
找到輸入框元素:
WebElement element = driver.findElement(By.id("passwd-id"));
在輸入框中輸入內容:
element.sendKeys(「test」);
將輸入框清空:
element.clear();
獲取輸入框的文本內容:
element.getText();
找到下拉選擇框的元素:
Select select = new Select(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();
找到單選框元素:
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);
通常來講,登陸後建議是先:
driver.switchTo().defaultContent();
切換到某個frame:
driver.switchTo().frame("leftFrame");
從一個frame切換到另外一個frame:
driver.switchTo().frame("mainFrame");
切換到某個window:
driver.switchTo().window("windowName");
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();
User Agent的設置是平時使用得比較多的操做:
FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override","some UA string");
WebDriver driver = new FirefoxDriver(profile);
咱們常常要對的值進行讀取和設置。
//增長cookie: // Now set the cookie. This one's valid for the entire domain Cookie cookie = new Cookie("key", "value"); driver.manage().addCookie(cookie); //獲取cookie的值: // And now output all the available cookies for the current URL Set<Cookie> allCookies = driver.manage().getCookies(); for (Cookie loadedCookie : allCookies) { System.out.println(String.format("%s -> %s",loadedCookie.getName(), loadedCookie.getValue())); } //根據某個cookie的name獲取cookie的值: driver.manage().getCookieNamed("mmsid"); //刪除cookie: // You can delete cookies in 3 ways // By name driver.manage().deleteCookieNamed("CookieName"); // By Cookie driver.manage().deleteCookie(loadedCookie); // Or all of them driver.manage().deleteAllCookies();
Web driver對Java Script的調用是經過JavascriptExecutor來實現的,例如:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("(function(){inventoryGridMgr.setTableFieldValue('"+ inventoryId + "','" + fieldName + "','"
+ value + "');})()");
若是用webdriver截圖是:
driver = webdriver.Firefox()
driver.save_screenshot("C:\error.jpg")
由於Load頁面須要一段時間,若是頁面還沒加載完就查找元素,必然是查找不到的。最好的方式,就是設置一個默認等待時間,在查找頁面元素的時候若是找不到就等待一段時間再找,直到超時。
Webdriver提供兩種方法,一種是顯性等待,另外一種是隱性等待。
顯性等待:
WebDriver driver =new FirefoxDriver(); driver.get("http://somedomain/url_that_delays_loading"); WebElementmyDynamicElement = (new WebDriverWait(driver, 10)) .until(newExpectedCondition<WebElement>(){ @Override public WebElementapply(WebDriver d) { returnd.findElement(By.id("myDynamicElement")); }});
隱性等待:
WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://somedomain/url_that_delays_loading"); WebElement myDynamicElement =driver.findElement(By.id("myDynamicElement"));
當本機上沒有瀏覽器,須要遠程調用瀏覽器進行自動化測試時,須要用到RemoteWebDirver.
import java.io.File; import java.net.URL; import org.openqa.selenium.OutputType; import org.openqa.selenium.TakesScreenshot; import org.openqa.selenium.WebDriver; import org.openqa.selenium.remote.Augmenter; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; public class Testing { public void myTest()throws Exception { WebDriver driver = newRemoteWebDriver( new URL("http://localhost:4446/wd/hub"), DesiredCapabilities.firefox()); driver.get("http://www.google.com"); // RemoteWebDriverdoes not implement the TakesScreenshot class // if the driver doeshave the Capabilities to take a screenshot // then Augmenter willadd the TakesScreenshot methods to the instance WebDriveraugmentedDriver = new Augmenter().augment(driver); File screenshot =((TakesScreenshot)augmentedDriver). getScreenshotAs(OutputType.FILE); } }
在使用RemoteDriver時,必須在遠程服務器啓動一個SeleniumServer:
java -jar selenium-server-standalone-2.20.0.jar -port 4446
profile = new FirefoxProfile(); profile.setPreference("general.useragent.override",testData.getUserAgent()); capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("firefox_profile", profile); driver = new RemoteWebDriver(new URL(「http://localhost:4446/wd/hub」),capabilities); driverWait = new WebDriverWait(driver,TestConstant.WAIT_ELEMENT_TO_LOAD); driver.get("http://www.google.com");
WebDriver對頁面的操做,須要找到一個WebElement,而後再對其進行操做,比較繁瑣:
// Find the text inputelement by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
咱們能夠考慮對這些基本的操做進行一個封裝,簡化操做。好比,封裝代碼:
protected void sendKeys(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; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.support.ui.WebDriverWait; import com.drutt.mm.end2end.data.TestConstant; public class WebDriverAction { //protected WebDriverdriver; protected RemoteWebDriverdriver; protected WebDriverWaitdriverWait; protected booleanisWebElementExist(By selector) { try { driver.findElement(selector); return true; } catch(NoSuchElementException e) { return false; } } protected StringgetWebText(By by) { try { return driver.findElement(by).getText(); } catch (NoSuchElementException e) { return "Textnot existed!"; } } protected voidclickElementContainingText(By by, String text){ List<WebElement>elementList = driver.findElements(by); for(WebElement e:elementList){ if(e.getText().contains(text)){ e.click(); break; } } } protected StringgetLinkUrlContainingText(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 void click(Byby){ driver.findElement(by).click(); driver.manage().timeouts().implicitlyWait(TestConstant.WAIT_ELEMENT_TO_LOAD,TimeUnit.SECONDS); } protected StringgetLinkUrl(By by){ return driver.findElement(by).getAttribute("href"); } protected void sendKeys(Byby, String value){ driver.findElement(by).sendKeys(value); }
Selenium2.0中使用WeDriver API對頁面進行操做,它最大的優勢是不須要安裝一個selenium server就能夠運行,可是對頁面進行操做不如selenium1.0的Selenium RC API那麼方便。Selenium2.0提供了使用Selenium RC API的方法:
// You may use any WebDriver implementation. Firefox is used hereas an example WebDriver driver = new FirefoxDriver(); // A "base url", used by selenium to resolve relativeURLs String baseUrl ="http://www.google.com"; // Create the Selenium implementation Selenium selenium = new WebDriverBackedSelenium(driver, baseUrl); // Perform actions with selenium selenium.open("http://www.google.com"); selenium.type("name=q", "cheese"); selenium.click("name=btnG"); // Get the underlying WebDriver implementation back. This willrefer to the // same WebDriver instance as the "driver" variableabove. WebDriver driverInstance = ((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver(); //Finally, close thebrowser. Call stop on the WebDriverBackedSelenium instance //instead of callingdriver.quit(). Otherwise, the JVM will continue running after //the browser has beenclosed. 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"); }