java selenium webdriver第三讲 helloWord

第一步:建立Maven项目

Selenium 支持 maven 工程,这会让你的工作更加简便。

Eclipse 建个 Maven 的工程,建成后,修改 pom.xml

 

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.2</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
         <artifactId>selenium-java</artifactId>
         <version>2.53.0</version>
    </dependency>
    <dependency>
        <groupId>com.opera</groupId>
        <artifactId>operadriver</artifactId>
    </dependency>
    <dependency>
         <groupId>org.apache.commons</groupId>
         <artifactId>commons-exec</artifactId>
         <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
  </dependencies>
  <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.opera</groupId>
                <artifactId>operadriver</artifactId>
                <version>0.16</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.seleniumhq.selenium</groupId>
                        <artifactId>selenium-remote-driver</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
        </dependencies>
    </dependencyManagement>

 

第二步:启动浏览器

① 使用IE

需要从http://docs.seleniumhq.org/download/

下载一个webDriver连接ie浏览器的驱动程序

其中:C:\\Users\\Administrator\\Desktop\\IEDriverServer_x64_2.53.1\\IEDriverServer.exe是下载的驱动路径

public class test {

    static WebDriver driver;
     @BeforeClass
     public static void init() {
        System.out.println("init...");
        System.setProperty("webdriver.ie.driver", "C:\\Users\\Administrator\\Desktop\\IEDriverServer_x64_2.53.1\\IEDriverServer.exe");
        driver = new InternetExplorerDriver();
     }
     /**
      * 测试入口
      */
    @Test
    public void testMain(){
        driver.get("http://www.baidu.com");
    }
   
   @AfterClass
   public static void destory() {
       System.out.println("destory...");
    driver.quit();
   }
}

 

②使用火狐

firefox浏览器不像ie一样需要下载连接浏览器的驱动文件,你只需要有浏览器就行

D:\\software\\firefox\\firefox.exe不是驱动程序,而是firefox的启动程序

public class TestCommon {
    static WebDriver driver;
     @BeforeClass
     public static void init() {
         System.out.println("init...");
         // 如果你的 FireFox 没有安装在默认目录,那么必须在程序中设置
         System.setProperty("webdriver.firefox.bin", "D:\\software\\firefox\\firefox.exe");
         // 创建一个 FireFox 的浏览器实例
         driver = new FirefoxDriver();
         driver.manage().window().maximize();
     }
     
     /**
      * 测试入口
      */
     @Test
     public void testMain(){
         driver.get("http://www.baidu.com");
     }
    
       @AfterClass
       public static void destory() {
           System.out.println("destory...");
           driver.quit();
       }
}

 

③使用chrome浏览器

在chrome浏览器上进行测试,需要从http://chromedriver.storage.googleapis.com/index.html?path=2.9/

下载webDriver操作chrome浏览器的驱动程序

public class TestMain {
    static WebDriver driver;
     @BeforeClass
     public static void init() {
         System.out.println("init...");
         System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
         // 创建一个 ChromeDriver 的接口,用于连接 Chrome,
         //必须要有chromedriver.exe文件,selenium默认不能启动chrome
         // 创建一个 Chrome 的浏览器实例
         driver = new ChromeDriver();
         driver.manage().window().maximize();
     }
     
     /**
      * 测试入口
      */
     @Test
     public void testMain(){
          driver.get("http://www.baidu.com");
          driver.findElement(By.id("kw")).sendKeys("魔兽");
          driver.findElement(By.id("su")).click();
     }
    
       @AfterClass
       public static void destory() {
           System.out.println("destory...");
        //driver.quit();
       }
}

 

 

 

转载于:https://www.cnblogs.com/itliucheng/p/5578672.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值