selenium2读书笔记(一)启动Chrome

1.安装并引用Selenium2
下载网址 http://seleniumhq.org/download/ 选一个下载。
这里写图片描述
之后解压。其中ThoughtWorks.Selenium.Core.dll—–Selenium 1的主要API文件; WebDriver.dll—–Selenium2的主要API文件。

2.开始建项目
Eclipse–new–Java project; new class
添加引用:单击项目,选择properties—java build path—libraries—add external jars—把Selenium的libs文件夹中所有与Java相关的jar包选上。
这里写图片描述

3.选择浏览器测试
Selenium支持Firefox, IE, Chrome, Opera, Android, Iphone浏览器。其中Chome, FF, IE是很容易实现的,只需电脑安装相应的浏览器就行。Opera ,C语言和Java的处理方式不同,Android+Iphone测试前需要安装支持软件。现在先讲前3个。

(1) Selenium天然支持Firefox浏览器(呵呵,因为火狐开源啊,大家都能看到代码,selenium团队就把如何操作火狐的代码直接搞好了,不用driver了)
(2)其他的浏览器,Selenium是通过driver来实现的。只需添加XXdriver.exe的路径即可。

ChromeDriver启动本地的Chrome浏览器
需要的是ChromeDriver.exe, 本地安装了Chrome的浏览器, Selenium server jar 文件。
接下来设置Chrome driver 的property,明确地址

System.setProperty("webdriver.chrome.driver", "pathofchromedriver\\chromedriver.exe");

如果path错误,运行就会报错:
Error: The path to the driver executable must be set by the webdriver.chrome.driver system property

完整的打开Chrome的程序

package com.easy;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;//一定要引入,因为要打开Chrome
//同理火狐import org.openqa.selenium.firefox.FirefoxDriver;
//IE浏览器import org.openqa.selenium.ie.InternetExplorerDriver


public class ChromeExample {

 public static String driverPath = "F:/lib/chromedriver/";
 public static WebDriver driver;

    public static void main(String []args) {
        System.out.println("launching chrome browser");
        System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
        driver = new ChromeDriver();
        driver.navigate().to("http://google.com");
    }
}

用TestNg来执行的代码

package com.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestChromeBrowser {

    static String driverPath = "path to chrome driver";
    public WebDriver driver;

    @BeforeClass
    public void setUp() {
        System.out.println("*******************");
        System.out.println("launching chrome browser");
        System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
    }

    @Test
    public void testGooglePageTitleInChromeBrowser() {
        driver.navigate().to("http://www.google.com");
        String strPageTitle = driver.getTitle();
        System.out.println("Page title: - "+strPageTitle);
        Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");
    }

    @AfterClass
    public void tearDown() {
        if(driver!=null) {
            System.out.println("Closing chrome browser");
            driver.quit();
        }
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值