• selenium2(WebDriver)环境搭建


    1、安装jdk并配置环境变量:

    jdk安装
    jdk下载地址: http://www.oracle.com/technetwork/java/javase/downloads/index.html
    环境变量配置,如:
    CLASSPATH=.;%JAVA_HOME%libdt.jar;%JAVA_HOME%lib ools.jar
    JAVA_HOME=D:Program FilesJavajdk1.6.0_10
    PATH=%JAVA_HOME%in

    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资料,不知具体作者是谁哈~)

  • 相关阅读:
    codeforces 733D
    HDU2647
    匈牙利算法 DFS模板(了解度+1)
    HDU1532 网络流:最大流之福德福克森算法
    mysql5 解压版 安装 建用户授权采坑
    Spring Boot 相关随笔
    Spring Boot 项目jar包 构建运行
    随笔小结
    war包 jar包理解(记录)
    vue axios异步请求及回调函数(前台)
  • 原文地址:https://www.cnblogs.com/splvxh/p/4341770.html
Copyright © 2020-2023  润新知