您的位置:首页 > 编程语言 > Java开发

ava selenium webdriver实战 helloWord

2016-10-19 11:19 309 查看
第一步:建立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();
}
}


 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息