webdriver入門-Java


如何用webdriver打開一個瀏覽器,我們常用的瀏覽器有firefox和IE兩種,firefox是selenium支持得比較成熟的瀏覽器,很多新的特性都會在firefox中體現。但是做頁面的測試,啟動速度比較慢,啟動以后運行速度還是可以接受的。

啟動firefox瀏覽器

新建一個firefoxDriver
如果火狐瀏覽器沒有默認安裝在C盤,需要制定其路徑

System.setProperty("webdriver.firefox.bin", "D:/Program Files/Mozilla firefox/firefox.exe"); 
WebDriver driver = newFirefoxDriver(); 

啟動IE瀏覽器

//Create a newinstance of the Internet Explorer driver
WebDriver driver = newInternetExplorerDriver ();

啟動HtmlUnit瀏覽器

//Createa new instance of the HtmlUnit driver
WebDriverdriver = new HtmlUnitDriver();

啟動Chrome瀏覽器

          下載ChromeDriver.exe請點 這里

//Createa new instance of the Chromedriver

System.setProperty(“webdriver.chrome.driver”, bsPath);
WebDriverdriver = new ChromeDriver();

對web頁面進行測試,首先要打開被測試頁面的地址(如:http://www.baidu.com),web driver 提供的get方法可以打開一個頁面:
// And now use thedriver to visit Google
driver.get(“http://www.baidu.com“);
//也可以調用以下方法
driver.navigate().to(“http://www.baidu.com“);
測試腳本如下
package com.test.ui.demo;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestWebDriver {
private WebDriver driver = null;
private String url = “http://www.baidu.com“;
//每個用例執行前會執行該方法
@BeforeMethod
public void startUp(){
//如果firefox沒有安裝在c盤需要執行下面這句,否則請注釋掉
System.setProperty(“webdriver.firefox.bin”, “D:/Program Files/Mozilla firefox/firefox.exe”);
driver = new FirefoxDriver();
}
//每個用例執行后會執行該方法
@AfterMethod
public void tearDown(){
//退出操作
driver.quit();
driver = null;
}
@Test
public void startTest(){
//打開新窗口
driver.get(url);
}
}

各種瀏覽器比較↓

Webdirver對瀏覽器的支持HtmlUnit Driver優點:HtmlUnit Driver不會實際打開瀏覽器,運行速度很快。對於用FireFox等瀏覽器來做測試的自動化測試用例,運行速度通常很慢,HtmlUnit Driver無疑是可以很好地解決這個問題。
缺點:它對JavaScript的支持不夠好,當頁面上有復雜JavaScript時,經常會捕獲不到頁面元素。
使用:
WebDriver driver = new HtmlUnitDriver();

FireFox Driver優點:FireFox Dirver對頁面的自動化測試支持得比較好,很直觀地模擬頁面的操作,對JavaScript的支持也非常完善,基本上頁面上做的所有操作FireFox Driver都可以模擬。
缺點:啟動很慢,運行也比較慢,不過,啟動之后Webdriver的操作速度雖然不快但還是可以接受的,建議不要頻繁啟停FireFox Driver。
使用:
WebDriver driver = new FirefoxDriver();
Firefox profile的屬性值是可以改變的,比如我們平時可能需要通過代理上網,可以這樣修改:
FirefoxProfile profile = new FirefoxProfile();

//使用profile

    ProfilesIni allProfiles = new ProfilesIni();
    FirefoxProfile profile = allProfiles.getProfile("default");
    driver = new FirefoxDriver(profile);

// 使用代理

profile.setPreference(“network.proxy.type”, 1);

// http協議代理配置

profile.setPreference(“network.proxy.http”, proxyIp);
profile.setPreference(“network.proxy.http_port”, proxyPort);

// 所有協議公用一種代理配置,如果單獨配置,這項設置為false,再類似於http的配置

profile.setPreference(“network.proxy.share_proxy_settings”, true);

// 對於localhost的不用代理,這里必須要配置,否則無法和webdriver通訊

profile.setPreference(“network.proxy.no_proxies_on”, “localhost”);

// 以代理方式啟動firefox

FirefoxDriver ff  = new FirefoxDriver(profile);

InternetExplorer Driver優點:直觀地模擬用戶的實際操作,對JavaScript提供完善的支持。
缺點:是所有瀏覽器中運行速度最慢的,並且只能在Windows下運行,對CSS以及XPATH的支持也不夠好。
使用:
WebDriver driver = new InternetExplorerDriver();

元素操作↓

查找元素

使用操作如何找到頁面元素Webdriver的findElement方法可以用來找到頁面的某個元素,最常用的方法是用id和name查找。下面介紹幾種比較常用的方法。
By ID假設頁面寫成這樣:
<input type=”text” name=”userName”  id=”user” />
那么可以這樣找到頁面的元素:
通過id查找:
WebElement element = driver.findElement(By.id(“user”));
By Name或通過name查找:
WebElement element = driver.findElement(By.name(“userName”));
By XPATH或通過xpath查找:
WebElement element =driver.findElement(By.xpath(“//input[@id='user']“));
By Class Name假設頁面寫成這樣:

<div class=”top”><span>Head</span></div><divclass=”top”><span>HeadName</span></div>
可以通過這樣查找頁面元素:
List<WebElement>top= driver.findElements(By.className(“top”));

By Link Text假設頁面元素寫成這樣:
<a href=”http://www.baidu.com”>baidu</a>>
那么可以通過這樣查找:
WebElement baidu=driver.findElement(By.linkText(“baidu”));

輸入框傳值

輸入框(text field or textarea)   找到輸入框元素:
WebElement element = driver.findElement(By.id(“passwd-id”));
在輸入框中輸入內容:
element.sendKeys(“test”);
將輸入框清空:
element.clear();
獲取輸入框的文本內容:
element.getText();

下拉菜單

下拉選擇框(Select)找到下拉選擇框的元素:
Select select = new Select(driver.findElement(By.id(“select”)));
選擇對應的選擇項:select.selectByVisibleText(“testName”);

select.selectByValue(“name”);
不選擇對應的選擇項:
select.deselectAll();
select.deselectByValue(“name”);
select.deselectByVisibleText(“姓名”);
或者獲取選擇項的值:
select.getAllSelectedOptions();
select.getFirstSelectedOption();

單選框

單選項(Radio Button)找到單選框元素:
WebElement sex=driver.findElement(By.id(“sex”));

選擇某個單選項:

sex.click();
清空某個單選項:
sex.clear();

判斷某個單選項是否已經被選擇:

sex.isSelected();

復選框

多選項(checkbox)多選項的操作和單選的差不多:
WebElement area =driver.findElement(By.id(“area .”));
area .click();
area .clear();
area .isSelected();
area .isEnabled();

按鈕

按鈕(button)找到按鈕元素:
WebElement saveButton = driver.findElement(By.id(“save”));

點擊按鈕:

saveButton.click();

判斷按鈕是否enable:

saveButton.isEnabled ();

左右選擇框也就是左邊是可供選擇項,選擇后移動到右邊的框中,反之亦然。例如:

Select name= new Select(driver.findElement(By.id(“name”)));
name.selectByVisibleText(“hellen”);
WebElement addName=driver.findElement(By.id(“addButton”));
addName.click();

彈出框

彈出對話框(Popup dialogs)Alert alert = driver.switchTo().alert();
alert.accept();
alert.dismiss();
alert.getText();

表單提交

表單(Form)Form中的元素的操作和其它的元素操作一樣,對元素操作完成后對表單的提交可以:
WebElement sub= driver.findElement(By.id(“sub”));
sub.click();

sub.submit();//只適合於表單的提交

上傳附件

上傳文件 (Upload File)上傳文件的元素操作:
WebElement picFile = driver.findElement(By.id(“picFile ”));
String filePath = “d:\\report\\600x600x0.jpg”;
picFile .sendKeys(filePath);

多窗口切換

Windows 或 Frames之間的切換

首先切換到默認的frame
driver.switchTo().defaultContent();
切換到某個frame:
driver.switchTo().frame(“leftFrame”);
從一個frame切換到另一個frame:
driver.switchTo().frame(“mainFrame”);
切換到某個window:
driver.switchTo().window(“windowName”);

導航

導航 (Navigationand History)打開一個新的頁面:
driver.navigate().to(“http://www.baidu.com”);

通過歷史導航返回原頁面:
driver.navigate().forward();
driver.navigate().back();

以上為簡單介紹了一下webDriver中常遇到的操作,有問題可以查閱官方的API文檔


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM