• <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>
  • Selenium WebDriver 介紹(2)

    發表于:2014-04-08來源:博客園作者:思勉點擊數: 標簽:
    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; impor

      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 void main(String[] args) {

      // 創建一個FirefoxDriver實例

      // 這個類依賴于接口而不是接口的實現

      WebDriver driver = new FirefoxDriver();

      // 使用get方法訪問Google

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

      // 使用下面這個方法也能夠達到訪問Google的目的

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

      // 找到html輸入框的name

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

      // 輸入要查找的內容

      element.sendKeys("Cheese!");

      // 提交表單,WebDriver會自動找到我們需要提交的元素所在的表單

      element.submit();

      // 打印網頁的標題

      System.out.println("Page title is: " + driver.getTitle());

      // Google的搜索網頁會通過JS動態渲染

      // 等待頁面加載完畢,超時時間為10秒

      (new WebDriverWait(driver, 10)).until(new ExpectedCondition() {

      public Boolean apply(WebDriver d) {

      return d.getTitle().toLowerCase().startsWith("cheese!");

      }

      });

      // 控制臺上將打印如下信息: "cheese! - Google Search"

      System.out.println("Page title is: " + driver.getTitle());

      // 關閉瀏覽器

      driver.quit();

      }

      }

      在本章節的接下來篇幅,我們將學習如何使用WebDriver操作你的瀏覽器,如何使用框架和窗口來測試Web網站。當然,我們將提供更加翔實的論述和舉例。

      7.Selenium-WebDriver API詳解

      7.1獲取Web頁面

      我們第一件要做的事是通過WebDriver取得Web頁面的控制權,一般情況下使用get方法

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

      在某些情況下,比如操作系統和瀏覽器的穿插組合,WebDriver有可能不會等待Web頁面加載完成,這種情況下WebDriver會返回錯誤或者直接運行下一步操作。為了保證程序的健壯性,你需要等待頁面中某個元素加載完成后再進行下一步操作,請參考Explicit and Implicit Waits。

      7.2定位UI元素

      我們可以通過WebDriver實例或者WebElement類來定位UI元素。我們為每種編程語言都提供了兩種方法:“Find Element”和“Find Elements”。第一種方法返回的一個WebElement,找不到則拋出異常。第二個方法返回一個WebElement鏈表(List),在找不到任何DOM元素的情況下會返回空的鏈表。

      Find方法會使用類似探測器的類,類名叫做By。下面列舉By的一些常用方法:

      By ID

      當我們定位一個UI 元素,這個是最有效也是最好的方法。不過這個方法不是萬能的,有的前端開發在設計UI元素時會遺漏ID或者使用動態ID,這兩種情況下都要避免使用這個方法。這時候使用獲取class名稱方法比By ID更合適。

      示例:如何使用該方法定位元素

      

    ...

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

      By Class Name

      在這種場景下,我們引用DOM元素的屬性。實際情況是很多元素都有一樣的Class Name,因此找到多個有相同Class Name的元素,比找到第一個擁有這個Class Name的元素來的更重要。

      示例:如何使用該方法定位元素

      

    Cheddar
    Gouda

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

      By Tag Name

      DOM元素Tag的名稱。

      示例:如何使用該方法定位元素

      

      WebElement frame = driver.findElement(By.tagName("iframe"));

      By Name

      找到與Name屬性相同的Input元素。

      示例:如何使用該方法定位元素

      

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

      By Link Text

      找到與Text屬性精確匹配的超鏈接。

      示例:如何使用該方法定位元素

      cheese

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

      By Partial Link Text

      找到與Text屬性模糊匹配的超鏈接。

      示例:如何使用該方法定位元素

      search for cheese

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

      By CSS

      這個方法名稱意味著它是一個CSS探測器。前提是瀏覽器默認支持這種方法,建議根據W3C的標準文檔構建CSS選擇器。如果瀏覽器不支持CSS選擇器,可以使用Sizzle。IE6,7和FireFox3.0就是使用Sizzle作為CSS查詢引擎。

    原文轉自:hhttp://www.cnblogs.com/keepsilent/p/3530825.html

    老湿亚洲永久精品ww47香蕉图片_日韩欧美中文字幕北美法律_国产AV永久无码天堂影院_久久婷婷综合色丁香五月

  • <ruby id="5koa6"></ruby>
    <ruby id="5koa6"><option id="5koa6"><thead id="5koa6"></thead></option></ruby>

    <progress id="5koa6"></progress>

  • <strong id="5koa6"></strong>