溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

Selenium 2.0中文

發(fā)布時(shí)間:2020-04-10 15:39:33 來(lái)源:網(wǎng)絡(luò) 閱讀:67 作者:360982886 欄目:web開(kāi)發(fā)

Selenium2.0

第1章 Webdirver基礎(chǔ)

1.1   下載selenium2.0的lib包

http://code.google.com/p/selenium/downloads/list

官方UserGuidehttp://seleniumhq.org/docs/

1.2   用webdriver打開(kāi)一個(gè)瀏覽器

我們常用的瀏覽器有firefoxIE兩種,firefoxselenium支持得比較成熟的瀏覽器。但是做頁(yè)面的測(cè)試,速度通常很慢,嚴(yán)重影響持續(xù)集成的速度,這個(gè)時(shí)候建議使用HtmlUnit,不過(guò)HtmlUnitDirver運(yùn)行時(shí)是看不到界面的,對(duì)調(diào)試就不方便了。使用哪種瀏覽器,可以做成配置項(xiàng),根據(jù)需要靈活配置。

 

打開(kāi)firefox瀏覽器:

       //Create a newinstance of the Firefox driver

       WebDriver driver = newFirefoxDriver();

 

打開(kāi)IE瀏覽器

       //Create a newinstance of the Internet Explorer driver

       WebDriver driver = newInternetExplorerDriver ();

 

打開(kāi)HtmlUnit瀏覽器

       //Createa new instance of the Internet Explorer driver    

       WebDriverdriver = new HtmlUnitDriver();

 

1.3   打開(kāi)測(cè)試頁(yè)面

對(duì)頁(yè)面對(duì)測(cè)試,首先要打開(kāi)被測(cè)試頁(yè)面的地址(如:http://www.google.com,web driver 提供的get方法可以打開(kāi)一個(gè)頁(yè)面:

       // And now use thedriver to visit Google

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

1.4   GettingStarted

package org.openqa.selenium.example;

import org.openqa.selenium.By;

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 Selenium2Example {

    public staticvoidmain(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("Pagetitle 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("Pagetitle is: " + driver.getTitle());

       

       //Close the browser

       driver.quit();

    }

}

第2章 Webdirver對(duì)瀏覽器的支持

2.1   HtmlUnitDriver

優(yōu)點(diǎn):HtmlUnitDriver不會(huì)實(shí)際打開(kāi)瀏覽器,運(yùn)行速度很快。對(duì)于用FireFox等瀏覽器來(lái)做測(cè)試的自動(dòng)化測(cè)試用例,運(yùn)行速度通常很慢,HtmlUnit Driver無(wú)疑是可以很好地解決這個(gè)問(wèn)題。

缺點(diǎn):它對(duì)JavaScript的支持不夠好,當(dāng)頁(yè)面上有復(fù)雜JavaScript時(shí),經(jīng)常會(huì)捕獲不到頁(yè)面元素。

使用:

WebDriver driver = new HtmlUnitDriver();

 

2.2   FireFoxDriver

優(yōu)點(diǎn):FireFoxDirver對(duì)頁(yè)面的自動(dòng)化測(cè)試支持得比較好,很直觀地模擬頁(yè)面的操作,對(duì)JavaScript的支持也非常完善,基本上頁(yè)面上做的所有操作FireFox Driver都可以模擬。

缺點(diǎn):?jiǎn)?dòng)很慢,運(yùn)行也比較慢,不過(guò),啟動(dòng)之后Webdriver的操作速度雖然不快但還是可以接受的,建議不要頻繁啟停FireFox Driver。

使用:

WebDriver driver = new FirefoxDriver();

Firefox profile的屬性值是可以改變的,比如我們平時(shí)使用得非常頻繁的改變useragent的功能,可以這樣修改:

FirefoxProfile profile = newFirefoxProfile();
profile.setPreference("general.useragent.override", "someUAstring");
WebDriver driver = new FirefoxDriver(profile);

2.3   InternetExplorerDriver

優(yōu)點(diǎn):直觀地模擬用戶(hù)的實(shí)際操作,對(duì)JavaScript提供完善的支持。

缺點(diǎn):是所有瀏覽器中運(yùn)行速度最慢的,并且只能在Windows下運(yùn)行,對(duì)CSS以及XPATH的支持也不夠好。

使用:

WebDriver driver = newInternetExplorerDriver();

 

第3章 使用操作

3.1  如何找到頁(yè)面元素

WebdriverfindElement方法可以用來(lái)找到頁(yè)面的某個(gè)元素,最常用的方法是用idname查找。下面介紹幾種比較常用的方法。

3.1.1 By ID

假設(shè)頁(yè)面寫(xiě)成這樣:

<input type="text"name="passwd"id="passwd-id" />

 

那么可以這樣找到頁(yè)面的元素:

通過(guò)id查找:

WebElement element =driver.findElement(By.id("passwd-id"));

3.1.2 By Name

或通過(guò)name查找:

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

3.1.3 By XPATH

或通過(guò)xpath查找:

WebElement element=driver.findElement(By.xpath("http://input[@id='passwd-id']"));

3.1.4 By Class Name

假設(shè)頁(yè)面寫(xiě)成這樣:

 

<divclass="cheese"><span>Cheddar</span></div><divclass="cheese"><span>Gouda</span></div>

可以通過(guò)這樣查找頁(yè)面元素:

List<WebElement>cheeses =driver.findElements(By.className("cheese"));

 

3.1.5 By Link Text

假設(shè)頁(yè)面元素寫(xiě)成這樣:

<a>cheese</a>>

 那么可以通過(guò)這樣查找:

WebElement cheese=driver.findElement(By.linkText("cheese"));

 

 

 

3.2  如何對(duì)頁(yè)面元素進(jìn)行操作

找到頁(yè)面元素后,怎樣對(duì)頁(yè)面進(jìn)行操作呢?我們可以根據(jù)不同的類(lèi)型的元素來(lái)進(jìn)行一一說(shuō)明。

3.2.1輸入框(text field or textarea)

   找到輸入框元素:

WebElement element =driver.findElement(By.id("passwd-id"));

在輸入框中輸入內(nèi)容:

element.sendKeys(“test”);

將輸入框清空:

element.clear();

獲取輸入框的文本內(nèi)容:

element.getText();

 

3.2.2下拉選擇框(Select)

找到下拉選擇框的元素:

Select select = newSelect(driver.findElement(By.id("select")));

 

  選擇對(duì)應(yīng)的選擇項(xiàng):

select.selectByVisibleText(“mediaAgencyA”);

select.selectByValue(“MA_ID_001”);

 

不選擇對(duì)應(yīng)的選擇項(xiàng):

select.deselectAll();

select.deselectByValue(“MA_ID_001”);

select.deselectByVisibleText(“mediaAgencyA”);

或者獲取選擇項(xiàng)的值:

select.getAllSelectedOptions();

select.getFirstSelectedOption();

 

3.2.3單選項(xiàng)(Radio Button)

找到單選框元素:

WebElement bookMode=driver.findElement(By.id("BookMode"));

選擇某個(gè)單選項(xiàng):

bookMode.click();

清空某個(gè)單選項(xiàng):

bookMode.clear();

判斷某個(gè)單選項(xiàng)是否已經(jīng)被選擇:

bookMode.isSelected();

3.2.4多選項(xiàng)(checkbox)

多選項(xiàng)的操作和單選的差不多:

WebElement checkbox=driver.findElement(By.id("myCheckbox."));

checkbox.click();

checkbox.clear();

checkbox.isSelected();

checkbox.isEnabled();

3.2.5按鈕(button)

找到按鈕元素:

WebElement saveButton =driver.findElement(By.id("save"));

點(diǎn)擊按鈕:

saveButton.click();

判斷按鈕是否enable:

 

saveButton.isEnabled ();

3.2.6左右選擇框

也就是左邊是可供選擇項(xiàng),選擇后移動(dòng)到右邊的框中,反之亦然。例如:

Select(driver.findElement(By.id("languages")));

lang.selectByVisibleText(“English”);

WebElement addLanguage=driver.findElement(By.id("addButton"));

addLanguage.click();

3.2.7彈出對(duì)話(huà)框(Popup dialogs)

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

alert.accept();

alert.dismiss();

alert.getText();

3.2.8表單(Form)

Form中的元素的操作和其它的元素操作一樣,對(duì)元素操作完成后對(duì)表單的提交可以:

WebElement approve =driver.findElement(By.id("approve"));

approve.click();

approve.submit();//只適合于表單的提交

3.2.9上傳文件 (Upload File)

上傳文件的元素操作:

WebElement adFileUpload =driver.findElement(By.id("WAP-upload"));

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

adFileUpload.sendKeys(filePath);

3.2.10 Windows 和 Frames之間的切換

一般來(lái)說(shuō),登錄后建議是先:

driver.switchTo().defaultContent();

切換到某個(gè)frame

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

從一個(gè)frame切換到另一個(gè)frame

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

切換到某個(gè)window

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

 

3.2.11 拖拉(Drag andDrop)

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

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

 

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

 

3.2.12 導(dǎo)航 (Navigationand History)

打開(kāi)一個(gè)新的頁(yè)面:

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

 

通過(guò)歷史導(dǎo)航返回原頁(yè)面:

driver.navigate().forward();

driver.navigate().back();

3.3 高級(jí)使用

3.3.1改變user agent

User Agent的設(shè)置是平時(shí)使用得比較多的操作:

FirefoxProfile profile = newFirefoxProfile();

profile.addAdditionalPreference("general.useragent.override","someUA string");

WebDriver driver = newFirefoxDriver(profile);

3.3.2讀取Cookies

我們經(jīng)常要對(duì)的值進(jìn)行讀取和設(shè)置。

增加cookie:

// Now set the cookie. This one's valid forthe entire domain

Cookie cookie = new Cookie("key","value");

driver.manage().addCookie(cookie);

獲取cookie的值:

// And now output all the available cookiesfor the current URL

Set<Cookie> allCookies =driver.manage().getCookies();

for (Cookie loadedCookie : allCookies) {

   System.out.println(String.format("%s-> %s",loadedCookie.getName(), loadedCookie.getValue()));

}

根據(jù)某個(gè)cookiename獲取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();

3.3.3調(diào)用Java Script

Web driver對(duì)Java Script的調(diào)用是通過(guò)JavascriptExecutor來(lái)實(shí)現(xiàn)的,例如:

JavascriptExecutor js = (JavascriptExecutor)driver;

js.executeScript("(function(){inventoryGridMgr.setTableFieldValue('"+inventoryId + "','" + fieldName + "','"

               + value + "');})()");

 

3.3.4 Webdriver截圖

如果用webdriver截圖是:

driver = webdriver.Firefox()
driver.save_screenshot("C:\error.jpg")

3.3.5頁(yè)面等待

因?yàn)?/span>Load頁(yè)面需要一段時(shí)間,如果頁(yè)面還沒(méi)加載完就查找元素,必然是查找不到的。最好的方式,就是設(shè)置一個(gè)默認(rèn)等待時(shí)間,在查找頁(yè)面元素的時(shí)候如果找不到就等待一段時(shí)間再找,直到超時(shí)。

Webdriver提供兩種方法,一種是顯性等待,另一種是隱性等待。

顯性等待:

WebDriver driver =new FirefoxDriver();

driver.get("http://somedomain/url_that_delays_loading");

WebElement myDynamicElement = (newWebDriverWait(driver, 10))

  .until(new ExpectedCondition<WebElement>(){

  @Override

  public WebElement apply(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"));

第4章 RemoteWebDriver

當(dāng)本機(jī)上沒(méi)有瀏覽器,需要遠(yuǎn)程調(diào)用瀏覽器進(jìn)行自動(dòng)化測(cè)試時(shí),需要用到RemoteWebDirver.

4.1 使用RemoteWebDriver

import java.io.File;

import java.net.URL;

 

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

importorg.openqa.selenium.remote.Augmenter;

importorg.openqa.selenium.remote.DesiredCapabilities;

importorg.openqa.selenium.remote.RemoteWebDriver;

 

public class Testing {

   

    public voidmyTest()throws Exception {

       WebDriver driver = newRemoteWebDriver(

                               newURL("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);

    }

}

 

4.2 SeleniumServer

在使用RemoteDriver時(shí),必須在遠(yuǎn)程服務(wù)器啟動(dòng)一個(gè)SeleniumServer:

java -jar selenium-server-standalone-2.20.0.jar-port 4446

4.3 How to setFirefox profileusing RemoteWebDriver

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 = newWebDriverWait(driver,TestConstant.WAIT_ELEMENT_TO_LOAD);

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

 

第5章 封裝與重用

WebDriver對(duì)頁(yè)面的操作,需要找到一個(gè)WebElement,然后再對(duì)其進(jìn)行操作,比較繁瑣:

 // Find the text inputelement by itsname

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

 

// Enter something to search for

element.sendKeys("Cheese!");

我們可以考慮對(duì)這些基本的操作進(jìn)行一個(gè)封裝,簡(jiǎn)化操作。比如,封裝代碼:

    protected voidsendKeys(By by, String value){

       driver.findElement(by).sendKeys(value);

    }

那么,在測(cè)試用例可以這樣簡(jiǎn)化調(diào)用:

sendKeys(By.name("q"),”Cheese!”);

 

看,這就簡(jiǎn)潔多了。

 

類(lèi)似的封裝還有:

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 boolean isWebElementExist(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);

    }

 

第6章 在selenium2.0中使用selenium1.0的API

Selenium2.0中使用WeDriver API對(duì)頁(yè)面進(jìn)行操作,它最大的優(yōu)點(diǎn)是不需要安裝一個(gè)selenium server就可以運(yùn)行,但是對(duì)頁(yè)面進(jìn)行操作不如selenium1.0Selenium RC API那么方便。Selenium2.0提供了使用Selenium RC API的方法:

// You may use any WebDriverimplementation. Firefox is used hereas an example

WebDriver driver = new FirefoxDriver();

 

// A "base url", used by seleniumto resolve relativeURLs

 String baseUrl="http://www.google.com";

 

// Create the Selenium implementation

Selenium selenium = newWebDriverBackedSelenium(driver, baseUrl);

 

// Perform actions with selenium

selenium.open("http://www.google.com");

selenium.type("name=q","cheese");

selenium.click("name=btnG");

 

// Get the underlying WebDriverimplementation back. This willrefer to the

// same WebDriver instance as the"driver" variableabove.

WebDriver driverInstance =((WebDriverBackedSelenium)selenium).getUnderlyingWebDriver();

 

    //Finally, closethebrowser. Call stop on the WebDriverBackedSelenium instance

    //instead ofcallingdriver.quit(). Otherwise, the JVM will continue running after

    //the browser hasbeenclosed.

    selenium.stop();

 

我分別使用WebDriverAPISeleniumRCAPI寫(xiě)了一個(gè)Login的腳本,很明顯,后者的操作更加簡(jiǎn)單明了。

WebDriver API寫(xiě)的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寫(xiě)的Login腳本:

    public void login() {

       selenium.selectFrame("relative=top");

       selenium.selectFrame("mainFrame");

       selenium.type("username","manager@ericsson.com");

       selenium.type("password","manager");

       selenium.click("loginButton");

}

     

  

 

 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI