selenium2(WebDriver)環境搭建


1、安裝jdk並配置環境變量:

jdk安裝
jdk下載地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html
環境變量配置,如:
CLASSPATH=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
JAVA_HOME=D:\Program Files\Java\jdk1.6.0_10
PATH=%JAVA_HOME%\bin

2、安裝Firefox,Selenium IDE,Firebug和xpahter

安裝FireFox

Firefox版本有一定限制,需要和selenium IDE相匹配。
下載地址: http://www.firefox.com.cn/download/

 

安裝Selenium IDE
Selenium IDE是基於FIREFOX瀏覽器的一個插件,提供GUI界面來運行Selenium測試。Selenium IDE提供腳本錄制和回放功能,可以將用戶在瀏覽器中執行的操作記錄下來,生成各種形式的腳本,可以將這些腳本保存供selenium使用。Selenium IDE主要是用在Selenium 1.0中,在Selenium 2.0中基本不使用。
1)下載Selenim IDE
下載地址:http://seleniumhq.org/projects/ide/

2)安裝:直接把下載的Selenium IDE文件拖到FireFox瀏覽器窗口中,按提示操作即可安裝成功。

 

安裝Firebug
1)打開Firefox瀏覽器
2)點擊菜單“工具(T)”,下拉列表中選擇“附加組件”。
3)“獲取附加組件”
4)在搜索里輸入“firebug”,稍等即可。
5)點擊“添加至Firefox”
6)OK,重啟瀏覽器即可。

 

安裝xpahter
1)打開Firefox瀏覽器
2)點擊菜單“工具(T)”,下拉列表中選擇“附加組件”。
3)“獲取附加組件”
4)在搜索里輸入“xpahter”,稍等即可。
5)點擊“添加至Firefox”
6)OK,重啟瀏覽器即可。

 

安裝xpath checker

1)打開Firefox瀏覽器
2)點擊菜單“工具(T)”,下拉列表中選擇“附加組件”。
3)“獲取附加組件”
4)在搜索里輸入“xpath checker”,稍等即可。
5)點擊“添加至Firefox”
6)OK,重啟瀏覽器即可。

 

3、安裝eclipse

安裝eclipse

 

4、安裝selenium webdriver

1)下載地址: http://code.google.com/p/selenium/downloads/list

官方UserGuidehttp://seleniumhq.org/docs/

2)下載:selenium-server-standalone-2.44.0.jar和selenium-java-2.44.0.zip(使用java語言的下載該包)。 
3)解壓下載的selenium-java-2.44.0.zip文件

 

5、selenium2使用:

1)在Eclipse里新建一個project,然后引用selenium-java-2.44.0.zip解壓后的selenium-java-2.44.0.jar,及libs下的jar包

2新建一個class“Seleniumcn”把代碼貼進去,如果代碼沒錯誤就可以運行了。例如下面:

package com.hxh.test;

import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.BeforeClass;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;

public class SeleniumTest{
    private WebDriver driver;
    @BeforeMethod
    @BeforeClass
    public void setUp(){
        System.setProperty("webdriver.ie.driver", "C:\\Program Files (x86)\\Internet Explorer\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
    }
    
    @Test(invocationCount=3)
    public void testLogic(){
        driver.get("http://www.baidu.com/");
        System.out.println("打開鏈接——>");
        WebDriverWait wait = new WebDriverWait(driver, 100);
        WebElement element = wait.until(new ExpectedCondition<WebElement>() {
            @Override
            public WebElement apply(WebDriver d) {
                return d.findElement(By.id("kw"));
            }
        });
        if(element!=null){
            System.out.println("成功打開連接~~~~~~~~O(∩_∩)O~");
        }
    }
    
    @AfterMethod
    public void tearDown(){
        if(driver!=null){
            driver.quit();
        }
    }
}
View Code

正常運行后,這幾行代碼將會打開IE瀏覽器,然后轉跳到百度首頁。

並關閉IE瀏覽器。

 

(以上內容參考群共享某word資料,不知具體作者是誰哈~)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM