selenium2(WebDriver)环境搭建

系统 2031 0

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

官方 UserGuide http://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资料,不知具体作者是谁哈~)

selenium2(WebDriver)环境搭建


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论