zoukankan      html  css  js  c++  java
  • Selenium2学习-003-WebUI自动化实战实例-001-百度搜索

    此文主要通过百度搜索功能,进行 Selenium2 的实战实例讲解,文中所附源代码于 2015-01-16 02:01 亲测通过,敬请亲们阅览。希望能对初学 Selenium2 UI 自动化测试编程的亲们有所帮助。若有不足之处,敬请大神指正,不胜感激!

    脚本实现功能步骤如下所示:

    1. 启动 Chrome 浏览器
    2. 打开百度网址:www.baidu.com
    3. 输入搜索项:范丰平 博客园
    4. 获取搜索结果的第一项,并打开
    5. 关闭 Chrome 浏览器(为显示打开效果,已将此项注释)

    夜已深了,鬼话少述,直接上源代码了。Selenium UI 自动化功能测试脚本:百度搜索 源代码如下所示:

      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 UI 自动化功能测试脚本第 001 篇顺利完结,希望此文能够给初学 Selenium 的您一份参考。

    最后,非常感谢亲的驻足,希望此文能对亲有所帮助,热烈欢迎亲点评,非常感谢! ^_^

  • 相关阅读:
    Integer值判断是否相等问题
    Java连接Redis
    oracle 10G 没有 PIVOT 函数怎么办,自己写一个不久有了
    前端修炼(第三天)函数
    前端 JS 修炼(第一天)包装对象、作用域、创建对象
    linux oracle 启动全过程
    「android」webview中文乱码
    「dos」bat单条命令跨多行
    「股票」东方财富网公式-缩量
    「android」as javadoc乱码
  • 原文地址:https://www.cnblogs.com/fengpingfan/p/4227604.html
Copyright © 2011-2022 走看看