Selenium2學習-003-Selenium2 WebUI自動化實戰執行個體-001-百度搜尋

來源:互聯網
上載者:User

標籤:

此文主要通過百度搜尋功能,進行 Selenium2 的實戰執行個體講解,文中所附原始碼於 2015-01-16 02:01 親測通過,敬請親們閱覽。希望能對初學 Selenium2 使用者介面自動化測試編程的親們有所協助。若有不足之處,敬請大神指正,不勝感激!

指令碼實現功能步驟如下所示:

  1. 啟動 Chrome 瀏覽器
  2. 開啟百度網址:www.baidu.com
  3. 輸入搜尋項:範豐平 部落格園
  4. 擷取搜尋結果的第一項,並開啟
  5. 關閉 Chrome 瀏覽器(為顯示開啟效果,已將此項注釋)

夜已深了,鬼話少述,直接上原始碼了。Selenium 使用者介面自動化功能測試指令碼:百度搜尋 原始碼如下所示:

  1 /**  2  * Aaron.ffp Inc.  3  * Copyright (c) 2014-2015 All Rights Reserved.  4  */  5 package main.java.aaron.selenium;  6   7 import java.util.concurrent.TimeUnit;  8   9 import org.openqa.selenium.By; 10 import org.openqa.selenium.WebDriver; 11 import org.openqa.selenium.WebElement; 12 import org.openqa.selenium.chrome.ChromeDriver; 13  14 /** 15  * UI自動化功能測試指令碼:百度搜尋 16  *  17  * 實現的百度搜尋功能步驟描述如下: 18  *   1.啟動 Chrome 瀏覽器 19  *   2.開啟百度網址:www.baidu.com 20  *   3.輸入搜尋項:範豐平 部落格園 21  *   4.擷取搜尋結果的第一項,並開啟 22  *   5.關閉 Chrome 瀏覽器(為顯示開啟效果,已將此項注釋) 23  * @author Aaron.ffp 24  * @version $Id: BaiduSearch.java, v 0.1 2015年1月16日 上午1:10:08 Aaron.ffp Exp $ 25  */ 26 public class BaiduSearch { 27     private static WebDriver cd; 28     private static String baseUrl;          // 百度首頁網址 29     private static WebElement txt_search;   // 搜尋錄入框 30     private static WebElement btn_search;   // 搜尋按鈕 31     private static WebElement lnk_first;    // 第一條搜尋結果 32     private static String searchContent;    // 搜尋內容 33      34     /** 35      * 測試主入口 36      * @param args 37      * @throws InterruptedException 38      */ 39     public static void main(String[] args) throws InterruptedException{ 40         chromeStart(); 41         searchBaidu(); 42 //        chromeQuit(); 43     } 44      45     /** 46      * 百度搜尋 47      *  48      * @throws InterruptedException 49      */ 50     public static void searchBaidu() throws InterruptedException{ 51         /* 開啟百度首頁 */ 52         cd.get(baseUrl); 53          54         /* 擷取頁面元素:搜尋錄入框 */ 55         txt_search = cd.findElement(By.id("kw")); 56         /* 擷取頁面元素:搜尋按鈕 */ 57         btn_search = cd.findElement(By.id("su")); 58          59         /* 清空搜尋方塊, 並輸入搜尋項 */ 60         txt_search.clear(); 61         txt_search.sendKeys(searchContent); 62          63         /* 點擊搜尋按鈕 */ 64         btn_search.click(); 65          66         /* 等待 1s, 待網頁載入完成再操作頁面元素, 否則錄入框元素尋找時會報錯:No such element */ 67         TimeUnit.SECONDS.sleep(1); 68          69         /* 擷取頁面元素:第一條搜尋結果*/ 70         lnk_first = cd.findElement(By.xpath("//div[@id=‘1‘]/h3/a")); 71          72         System.out.println(lnk_first.getText()); 73         /* 點擊第一條搜尋結果連結 */ 74         lnk_first.click(); 75     } 76      77     /** 78      * Chrome WebDriver 設定, 網址及搜尋內容初始化, 開啟 Chrome 瀏覽器 79      */ 80     public static void chromeStart(){ 81         /* 設定 chrome webdirver 的位置 */ 82         System.setProperty("webdriver.chrome.driver", "C:/Windows/System32/chromedriver.exe"); 83         /* 百度首頁網址賦值 */ 84         baseUrl = "http://www.baidu.com/"; 85         /* 搜尋內容賦值 */ 86         searchContent = "範豐平 部落格園"; 87         /* 啟動 chrome 瀏覽器 */ 88         cd = new ChromeDriver(); 89     } 90      91     /** 92      * 關閉並退出 Chrome 93      */ 94     public static void chromeQuit(){ 95         /* 關閉 chrome */ 96         cd.close(); 97         /* 退出 chrome */ 98         cd.quit(); 99     }100 }
View Code

 

至此,Selenium2 使用者介面自動化功能測試指令碼第 001 篇順利完結,希望此文能夠給初學 Selenium 的您一份參考。

最後,非常感謝親的駐足,希望此文能對親有所協助,熱烈歡迎親點評,非常感謝! ^_^

Selenium2學習-003-Selenium2 WebUI自動化實戰執行個體-001-百度搜尋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.