菜鸟学自动化测试(九)----WebDirver
  54wKHQuU2AYM 2023年11月05日 100 0

 

关于什么是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不支持的浏览器功能。 
(2)multiple frames, multiple browser windows, popups, and alerts. 
(3)页面导航。 
(4)下拉。 
(5)基于AJAX的UI元素。

 

 

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

 

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

 

菜鸟学自动化测试(九)----WebDirver_firefox

 

下面代码是我录制的一个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;
        }
    }
}

 

 

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
54wKHQuU2AYM