qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

          查找條件對象By—Selenium自動化測試指南(3)

          51Testing軟件測試網獲人民郵電出版社和作者授權連載本書部分章節。任何個人或單位未獲得明確的書面許可,不得對本文內容復制、轉載或進行鏡像,否則將追究法律責任。)

            程序清單5-12  Java代碼

          package Project1;
          import org.openqa.selenium.*;
          import org.openqa.selenium.WebDriver.*;
          import org.openqa.selenium.firefox.*;
          public class Project1Class {
          public static void main(String[] args) {
          //如果啟動出現問題,可以使用System.setProperty指出firefox.exe的路徑
          //System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
          WebDriver driver = new FirefoxDriver();
          Navigation navigation = driver.navigate();
          navigation.to("http://www.baidu.com");
          navigation.to("http://www.google.com.hk");
          try {
          Thread.sleep(3000);
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          navigation.back();
          try {
          Thread.sleep(3000);
          } catch (InterruptedException e) {
          e.printStackTrace();
          }
          navigation.forward();
          }
          }

            程序清單代碼執行后可以發現,程序共打開了兩個頁面:百度谷歌。然后,頁面先后退到了第一個頁面(百度),再前進到了第二個頁面(谷歌)。

            5.4.3  Refresh()

            使用該方法將刷新整個頁面(類似于按F5的效果),多用于執行某些操作后需要刷新的情況(例如登錄后頁面未自動刷新),代碼如程序清單5-13或程序清單5-14所示。

            程序清單5-13  C#代碼

          using System;
          using OpenQA.Selenium;
          using OpenQA.Selenium.Firefox;
          namespace ConsoleApplication1
          {
          class Program
          {
          static void Main(string[] args)
          {
          IWebDriver driver = new FirefoxDriver();
          INavigation navigation = driver.Navigate();
          navigation.GoToUrl("http://www.baidu.com");
          navigation.Refresh();
          }
          }
          }

            程序清單5-14  Java代碼

          package Project1;
          import org.openqa.selenium.*;
          import org.openqa.selenium.WebDriver.*;
          import org.openqa.selenium.firefox.*;
          public class Project1Class {
          public static void main(String[] args) {
          //如果啟動出現問題,可以使用System.setProperty指出firefox.exe的路徑
          //System.setProperty("webdriver.firefox.bin","D:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
          WebDriver driver = new FirefoxDriver();
          Navigation navigation = driver.navigate();
          navigation.to("http://www.baidu.com");
          navigation.refresh();
          }
          }


          5.5  查找條件對象By

            在導航到對應頁面后,就可以對頁面上的元素進行操作了。然而,在進行操作之前,必須要找到相應的元素。如何才能找到這些元素呢?需要使用查找條件對象“By”進行查找。

            根據HTML的不同,查找條件也各有不同。例如,可以按HTML元素的ID進行查找,也可以按Name屬性查找,或者直接按HTML標簽查找,接下來將列舉常用的查找條件。

            5.5.1  Id(idToFind)

            可以按照HTML元素的ID屬性進行查找。例如,百度首頁有一個搜索文本框,如圖5-26所示。

            圖5-26  百度搜索文本框

            其HTML代碼如下:

            <input id="kw" class="s_ipt" type="text" maxlength="100" name="wd" autocomplete="off">

            如要操作該文本框,則可以通過ID(id="kw")作為查找條件獲取該對象,代碼如程序清單5-15或程序清單5-16所示。

            程序清單5-15  C#代碼

          IWebDriverdriver=newFirefoxDriver();
          INavigationnavigation=driver.Navigate();
          navigation.GoToUrl(http://www.baidu.com);
          IWebElementbaiduTextBox=driver.FindElement(By.Id("kw"));
          baiduTextBox.SendKeys("找到文本框");

            程序清單5-16  Java代碼

          WebDriver driver = new FirefoxDriver();
          Navigation navigation = driver.navigate();
          navigation.to(http://www.baidu.com);
          WebElement baiduTextBox = driver.findElement(By.id("kw"));
          baiduTextBox.sendKeys("找到文本框");

            代碼driver.FindElement(By.Id("kw"));表示尋找ID為“kw”的元素。

            找到文本框之后,執行“baiduTextBox.SendKeys("找到文本框");”,在搜索文本框中輸入“找到文本框”。

            代碼清單5-15和5-16的執行結果如圖5-27所示。

           





            5.5.2  Name(nameToFind)

            Name方法按Name進行查找與按ID進行查找類似,例如百度首頁上面有“登錄”超級鏈接,如圖5-28所示。

            圖5-28 “登錄”超級鏈接

            其HTML代碼如下:

            <a name="tj_login" href=http://passport.baidu.com/?login&tpl=mn>登錄</a>

            注意,它的name屬性為“tj_login”,可以用其作為查找條件來獲取登錄鏈接對象,使用方法如程序清單5-17或程序清單5-28所示。

            程序清單5-17  C#代碼

          IWebElement loginButton= driver.FindElement(By.Name("tj_login"));

            程序清單5-18  Java代碼

          WebElement loginButton= driver.findElement(By.name("tj_login"));

            5.5.3  LinkText(linkTextToFind)

            LinkText方法按鏈接的文本進行查找。例如,百度首頁上有“登錄”超級鏈接,如圖5-29所示。

            圖5-29 “登錄”超級鏈接

            它的鏈接文本為屬性為“登錄”,可以用它作為查找條件來獲取登錄鏈接對象。先打開百度頁面,然后單擊“登錄”,代碼如程序清單5-19或程序清單5-20所示。

            程序清單5-19  C#代碼

          IWebDriver driver = new FirefoxDriver();
          INavigation navigation = driver.Navigate();
          navigation.GoToUrl(http://www.baidu.com);
          IWebElement baiduLogin = driver.FindElement(By.LinkText("登錄"));
          baiduLogin.Click();

            程序清單5-20  Java代碼

          WebDriver driver = new FirefoxDriver();
          Navigation navigation = driver.navigate();
          navigation.to(http://www.baidu.com);
          WebElement baiduLogin = driver.findElement(By.LinkText("登錄"));
          baiduLogin.click();


           5.5.4  PartialLinkText(partialLinkTextToFind)

            PartialLinkText方法按鏈接的文本進行模糊查找。例如,百度首頁上有“登錄”超級鏈接,如圖5-30所示。

            圖5-30 “登錄”超級鏈接

            它的鏈接文本屬性為“登錄”。PartialLinkText可用于模糊查詢,它可以用“錄”字作為查找條件來獲取“登錄”鏈接對象。先打開百度頁面,然后單擊“登錄”超級鏈接,代碼如程序清單5-21或程序清單5-22所示。

            程序清單5-21  C#代碼

          IWebDriver driver = new FirefoxDriver();
          INavigation navigation = driver.Navigate();
          navigation.GoToUrl(http://www.baidu.com);
          IWebElement baiduLogin = driver.FindElement(By.PartialLinkText("錄"));
          baiduLogin.Click();

            程序清單5-22  Java代碼

          WebDriver driver = new FirefoxDriver();
          Navigation navigation = driver.navigate();
          navigation.to(http://www.baidu.com);
          WebElement baiduLogin = driver.findElement(By.partialLinkText("錄"));
          baiduLogin.click();

            5.5.5  ClassName(classNameToFind)

            ClassName方法按鏈接的文本進行模糊查找。例如,百度貼吧上有“貼吧搜索”超級鏈接,如圖5-31所示。

            圖5-31 “貼吧搜索”超級鏈接

            使用FireBug查看其HTML代碼,如圖5-32所示。

            圖5-32  HTML代碼

           

            其Class屬性為“j_global_search”,可以用其作為查找條件來獲取“貼吧搜索”鏈接。先打開貼吧頁面,然后單擊“貼吧搜索”鏈接,代碼如程序清單5-23或程序清單5-24所示。

            程序清單5-23  C#代碼

          IWebDriver driver = new FirefoxDriver();
          INavigation navigation = driver.Navigate();
          navigation.GoToUrl(http://tieba.baidu.com/index.html);
          IWebElement tiebaSearch = driver.FindElement(By.ClassName("j_global_search"));
          tiebaSearch.Click();




           程序清單5-24  Java代碼

          WebDriver driver = new FirefoxDriver();
          Navigation navigation = driver.navigate();
          navigation.to(http://www.baidu.com);
          WebElement tiebaSearch = driver.findElement(By.className("j_global_search"));
          tiebaSearch .click();

            5.5.6  TagName(TagNameToFind)

            TagName方法按標記名稱進行查找,并返回第一個匹配項。例如,百度首頁有“搜索設置”超級鏈接,如圖5-33所示。

            圖5-33  登錄按鈕

            使用FireBug查看其HTML代碼,可以發現它是整個頁面的第一個“a”標記,如圖5-34所示。

            圖5-34  HTML代碼

            因此,可以用它的標記名稱“a”作為查找條件來獲取“搜索設置”鏈接。先打開百度主頁,然后單擊“搜索設置”超級鏈接,代碼如程序清單5-25和程序清單5-26所示。

            程序清單5-25  C#代碼

          IWebDriver driver = new FirefoxDriver();
          INavigation navigation = driver.Navigate();
          navigation.GoToUrl(http://tieba.baidu.com/index.html);
          IWebElement searchSetting = driver.FindElement(By.TagName("a"));
          searchSetting .Click();

            程序清單5-26  Java代碼

          WebDriver driver = new FirefoxDriver();
          Navigation navigation = driver.navigate();
          navigation.to(http://www.baidu.com);
          WebElement searchSetting = driver.findElement(By.tagName("a"));
          searchSetting .click();

          本文選自《Selenium自動化測試指南》第五章節,本站經人民郵電出版社和作者的授權,近期將進行部分章節的連載,敬請期待!

          版權聲明:51Testing軟件測試網獲人民郵電出版社和作者授權連載本書部分章節。

          任何個人或單位未獲得明確的書面許可,不得對本文內容復制、轉載或進行鏡像,否則將追究法律責任。

          相關文章:

          選擇瀏覽器開始測試—Selenium自動化測試指南(2)

          操作頁面元素WebElement—Selenium自動化測試指南(4)


          posted on 2013-08-29 16:46 順其自然EVO 閱讀(580) 評論(0)  編輯  收藏 所屬分類: selenium and watir webdrivers 自動化測試學習

          <2013年8月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 紫阳县| 德惠市| 双鸭山市| 深水埗区| 凤翔县| 南昌县| 筠连县| 博客| 会昌县| 西吉县| 淳安县| 泰来县| 姜堰市| 申扎县| 镇巴县| 拉孜县| 天全县| 桂东县| 策勒县| 毕节市| 龙门县| 梅河口市| 家居| 澄迈县| 渑池县| 平湖市| 浪卡子县| 临沧市| 嘉定区| 紫阳县| 上犹县| 特克斯县| 广元市| 北京市| 凤凰县| 潞城市| 海林市| 东乡县| 潜江市| 万山特区| 昌平区|