亚洲好骚综合-亚洲黄色录像-亚洲黄色网址-亚洲黄色网址大全-99久久99久久-99久久99久久精品国产

您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Selenium
菜鳥學自動化測試??selenium系列全集
作者:網絡轉載 發布時間:[ 2013/4/3 14:43:08 ] 推薦標簽:

菜鳥學自動化測試(九)----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不支持的瀏覽器功能。
(2)multiple frames, multiple browser windows, popups, and alerts.
(3)頁面導航。
(4)下拉。
(5)基于AJAX的UI元素。

同樣,我們的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;
        }
    }
}

上一頁1234567891011下一頁
軟件測試工具 | 聯系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網站地圖
滬ICP備07036474 2003-2017 版權所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd
主站蜘蛛池模板: 二区在线视频 | 成人精品一区二区激情 | a一级免费 | 一级毛片区 | 国产成人综合日韩精品无 | 亚洲 日本 欧美 日韩精品 | 九九99国产精品视频 | 日韩欧美亚洲一区二区综合 | 成人影视频 | 免费成人福利视频 | 天天干天天色综合 | 中文国产成人精品久久一区 | 国产麻豆精品免费视频 | 免费看黄色片视频 | 天天摸天天看天天做天天爽 | 成人黄色小视频 | 欧美成人看片黄a免费看 | 天天做夜夜做 | 久草福利网| 国产成人一区二区三中文 | 日本福利片在线观看 | www.91成人 | 亚洲美女一级片 | 亚洲日本三级 | 国产 在线 | 日韩 | 天天综合天天看夜夜添狠狠玩 | 成年人在线网站 | 久久久久avav久久久 | 久久精品全国免费观看国产 | 日日操日日插 | 免费一级毛片女人图片 | 黄在线观看www免费看 | 国产精品视频一区二区三区经 | 亚洲欧美日韩综合二区三区 | 亚洲高清专区 | 九九热免费视频 | 成人短视频在线观看视频 | 欧美日韩无线码免费播放 | 久久午夜一区二区 | 两性色午夜视频免费播放 | 午夜视频十八嗯嗯啊免费 |