我們都知道, 在本地創建java項目后,引入selenium-java-2.35.0.jar selenium-support-2.35.0.jar junit-4.8.1.jar等等jar包之后,(或者創建Java maven項目,在pom.xml的<dependency></dependency>中添加依賴, Maven能夠自動下載相應版本的jar包), 之后, 就可以在本地開發selenium自動化腳本
在完全沒有封裝的情況下, 一般的順序是:
1)得到瀏覽器驅動:
WebDriver driver=new FirefoxDriver();
2)通過WebDriver提供的方法打開網頁:
driver.get("urlString"); 或者 driver.navigate.to("urlString")
3)通過driver.findElement(By.***)找到元素
找元素的過程, 需要做等待處理, 可以用Thread.sleep(), 然而為了提高腳本的效率和健壯性, 一般不采用這種等待方式,可以通過采用 WebDriverWait類+ExpectedConditions接口方式來靈活等待元素出現,具體處理可以見“selenium1.0和selenium2.0頁面等待處理詳解”
4)對元素進行操作
上面步驟3得到元素WebElement,通過WebElement提供的方法, 對元素進行操作(click()/sendKeys()/submit()/getText())等等
5)通過打開頁面、找元素、等待元素、對元素操作等一系列模擬人為動作后,第5步就需要做真正要測試的目的,即檢查操作后的結果
比如, 驗證某個元素是否出現, 驗證是否輸出正確的結果, 此時的步驟可以是試圖去找元素,待 wait.until()拋異常后, 還是沒找到元素的話, 判斷為該元素不存在。 在驗證輸出結果時,重復上述3) 4) 5)步驟取出元素, 再通過if String.contains() 或者String.equals()來判斷輸出結果
顯然, 上述是比較繁瑣的過程, 寫出來的腳本, 也會出現很多冗余的相同操作代碼,這樣的腳本,維護起來吃力, 下面,將對上面的過程做一個小小封裝, 這樣可以只調用一個方法, 就可以完成 “查找/等待/操作元素” 、 “查找/等待/判斷元素是否存在” 或者 “查找/等待/判斷輸出內容” 等等
1、
設定允許最長的等待時間:
WebDriverWait wait=new WebDriverWait(driver, 10);
2、查找元素是否存在的過程,放在一個類里面
class ElementExistOrNot implements Function<WebDriver, Boolean>{//第一個參數為apply方法的參數類型,第二個參數為apply方法的返回類型 private By by; private String sign; public ElementExistOrNot(By by, String sign) { this.by = by; this.sign = sign; } @Override public Boolean apply(WebDriver driver) { try{ WebElement e=driver.findElement(by); if(sign.equals("yes")){ return true; }else{ return false; } }catch (Exception e){ if(sign.equals("yes")){ return false; }else{ return true; } } } }
3、進行簡單封裝成方法供調用
//完成對元素的 ”查找+等待+驗證存在性“ 的封裝 public void waitUntilElementExist(By by){ ElementExistOrNot isTrue=new ElementExistOrNot(by, "yes"); try{ wait.until(isTrue); }catch (Exception e){ fail("Element [" + by + "] should presents!"); } }
4、有了這些方法后,再繼承這些方法所在的類, 就可以輕松調用啦~
PS:
另外,還有其他一些 “查找/等待/操作元素” 、 “查找/等待/判斷輸出內容” 等等,思路基本相同, 下面再提供一些封裝方式的完整源碼:
package com.jennifer.functiontest; import org.junit.After; import org.junit.Assert; import org.junit.Before; 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.WebDriverWait; import com.google.common.base.Function; public class jenniferBase extends Assert { protected WebDriver driver; protected WebDriverWait wait; @Before public void init(){ driver=new FirefoxDriver(); wait=new WebDriverWait(driver, 10); } //得到帶有等待機制的WebElement(對“找元素+等待”的封裝) public WebElement e(By by){ try{ waitUntilElementExist(by); return driver.findElement(by); }catch (Exception e) { fail("Can't find element: ["+by+"]"); } return null; } //完成對元素的“查找+等待(直到元素存在)+驗證+點擊 操作”的封裝 public void clickAndWait(By by){ e(by).click(); } //完成對元素的 ”查找+等待+驗證存在性“ 的封裝 public void waitUntilElementExist(By by){ ElementExistOrNot isTrue=new ElementExistOrNot(by, "yes"); try{ wait.until(isTrue); }catch (Exception e){ fail("Element [" + by + "] should presents!"); } } //完成對元素的“查找+等待+驗證不存在性”的封裝 public void waitUntilElementNotExist(By by){ ElementExistOrNot isTrue=new ElementExistOrNot(by, "no"); try{ wait.until(isTrue); }catch (Exception e) { fail("Element [" + by + "] should not presents!"); } } //完成對元素的“查找+等待+驗證其text存在性”的封裝 public void waitUntilElementEqualsText(By by,String s){ waitUntilElementExist(by); ElementEqualsOrNot isTrue=new ElementEqualsOrNot(by, "yes", s, "text"); try{ wait.until(isTrue); }catch (Exception e) { fail("Element ["+by+"] text should equals"+s+"but not it equals"+e(by).getText()); } } //完成對元素的“查找+等待+驗證其value存在性”的封裝 public void waitUntilElementEqualsValue(By by,String s){ waitUntilElementExist(by); ElementEqualsOrNot isTrue=new ElementEqualsOrNot(by, "yes", s, "value"); try{ wait.until(isTrue); }catch (Exception e) { fail("Element ["+by+"] value should equals:"+s+"but not it equals:"+e(by).getValue()); } } @After public void tearDown(){ driver.quit(); } } class ElementExistOrNot implements Function<WebDriver, Boolean>{//第一個參數為apply方法的參數類型,第二個參數為apply方法的返回類型 private By by; private String sign; public ElementExistOrNot(By by, String sign) { this.by = by; this.sign = sign; } @Override public Boolean apply(WebDriver driver) { try{ WebElement e=driver.findElement(by); if(sign.equals("yes")){ return true; }else{ return false; } }catch (Exception e){ if(sign.equals("yes")){ return false; }else{ return true; } } } } class ElementEqualsOrNot implements Function<WebDriver,Boolean>{ private By by; private String sign; private String s; private String textOrValue; public ElementEqualsOrNot(By by, String sign, String s, String textOrValue) { this.by = by; this.sign = sign; this.s = s; this.textOrValue = textOrValue; } @Override public Boolean apply(WebDriver driver) { WebElement e=driver.findElement(by); if(sign.equals("yes")){ //確定存在性 if(textOrValue.equals("text")){ //確定text的存在性 return e.getText().equals(s); }else{ //確定value的存在性 return e.getValue().equals(s); } }else{//確定不存在性 if(textOrValue.equals("text")){ return !e.getText().equals(s); }else{ return !e.getValue().equals(s); } } } }
以上只是個人的一點封裝思路,其他有別的更好封裝方案, 請指教^_^