zoukankan      html  css  js  c++  java
  • 菜鸟学自动化测试(九)WebDirver

     

    关于什么是WebDirver,上一节做了简单的描述,环境也在上一章中搭建完成。

    下面我们拷贝了官网提供的一个实例。让其在我们的eclipse中运行。

    Selenium WebDirver 代码如下:

     

    package MySel20Proj;

    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.htmlunit.HtmlUnitDriver;
    import org.openqa.selenium.support.ui.ExpectedCondition;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class Selenium2Example {
    public static void main(String[] args) {
    // 用Firefox driver创建一个新的的实例
    //注意:其他的代码依赖于界面
    //不执行

    System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" );
    WebDriver driver = new FirefoxDriver();// 这里我们可以使用firefox来运行测试用例
    //WebDriver driver = new ChromeDriver(); //这是chrome浏览器的驱动
    //WebDriver driver = new InternetExplorerDriver(); //这是IE浏览器的驱动
    // WebDriver driver = new HtmlUnitDriver(); //这是一个无界面测试模式,不用打开浏览器,通过后台输入来判断测试用例是否通过

    // 现在用这个来访问谷歌
    driver.get("http://www.google.com");
    // 也可以用下面的方式访问谷歌
    // driver.navigate().to("http://www.google.com");

    // 找到文本输入元件的名字
    WebElement element = driver.findElement(By.name("q"));

    // 在搜索框内输入“cheese!”
    element.sendKeys("Cheese!");

    // 现在递交表格. WebDriver会发现我们的形式元素
    element.submit();

    // 后台打印输出,检查网页的标题
    System.out.println("Page title is: " + driver.getTitle());

    // 谷歌的搜索是渲染过的动态JavaScript. 等待页面加载,暂停10秒
    (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver d) {
    return d.getTitle().toLowerCase().startsWith("cheese!");
    }
    });

    // Should see: "cheese! - Google Search"
    System.out.println("Page title is: " + driver.getTitle());

    //关闭浏览器
    driver.quit();
    }
    }

     

    运行时报出了错误;

     

    Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed. OS appears to be: XP

     

    Build info: version: '2.16.1', revision: '15405', time: '2012-01-05 12:30:12'

     

     

     

    我们只要在WebDriver driver = new FirefoxDriver(); 前面指定我们浏览器的具体信息即可:

     

    System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" );

     

    WebDriver driver = new FirefoxDriver();

     

    WebDirver 的实现:

     

     

    驱动名称

    对操作系统的支持

    调用的接口

    FireFox Driver

    ALL

     org.openqa.selenium.firefox.FirefoxDriver 

    Chrome Driver

    ALL

    org.openqa.selenium.chrome.ChromeDriver 

    InternetExplorer Driver

    Windows

    org.openqa.selenium.ie.InternetExplorerDriver 

    HtmlUnit Driver

    ALL

    org.openqa.selenium.htmlunit.HtmlUnitDriver 

     

     

    什么情况下选用WebDirver ?

     

     

     

    (1)Selenium-1.0不支持的浏览器功能。 
    2multiple frames, multiple browser windows, popups, and alerts. 
    3)页面导航。 
    4)下拉。 
    5)基于AJAXUI元素。 

     

     

    同样,我们的selenium IDE也支持WebDriver类型脚本的导出。

     

    将我们录制好的脚本 导出为junit(WebDriver) 类型

     

     

    下面代码是我录制的一个google搜索“selenium”关键安的操作:

     

    package com.test.hzh;

    import java.util.regex.Pattern;
    import java.util.concurrent.TimeUnit;
    import org.junit.*;
    import static org.junit.Assert.*;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.Select;

    public class Test1 {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
    System.setProperty ( "webdriver.firefox.bin" , "E:/Program Files/Mozilla Firefox/firefox.exe" );
    driver = new FirefoxDriver();
    baseUrl = "http://www.google.com.hk/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void test() throws Exception {
    driver.get(baseUrl + "/");
    driver.findElement(By.id("lst-ib")).clear();
    driver.findElement(By.id("lst-ib")).sendKeys("selenium");
    driver.findElement(By.name("btnK")).click();
    }

    @After
    public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
    fail(verificationErrorString);
    }
    }

    private boolean isElementPresent(By by) {
    try {
    driver.findElement(by);
    return true;
    } catch (NoSuchElementException e) {
    return false;
    }
    }
    }

     

     

  • 相关阅读:
    threed+handler解析gson文件,并且上拉加载,下拉刷新
    异步交互解析xml文件
    开启事务
    Android判断网路是否畅通加权限
    按两次返回键,退出程序,或者按home键退出程序
    SQL数据库的增删改查
    adb常用的命令
    avd和ddms和项目打包
    activity和fragment之间的交互
    activity的开启和关闭数据返回
  • 原文地址:https://www.cnblogs.com/fnng/p/2345187.html
Copyright © 2011-2022 走看看