【软件测试】Java中Selenium Webdriver的使用

Java中Selenium Webdriver的使用 

第一步:

进入Selenium 的官网http://docs.seleniumhq.org/,点击“Download”进入如下界面。



第二步:

在Download页面中的如下部分下载语言包,由于我使用的是Java语言,故使用的是Java语言包,点击“download”即可下载语言包。


第三步:

在Download页面中的如下部分下载浏览器驱动,由于实验中我使用的是Chrome浏览器,故下载的是Chrome Driver驱动。


第四步:

将下载好的Selenium Java语言包Build Path到项目中,将下载好的Chrome Driver驱动放到项目下,项目结构如图所示。




测试代码:

package com.tju.junit;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.concurrent.TimeUnit;

import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverTest {
	private WebDriver wd;
	private String url;
	
	@Before
	public void setUp() throws IOException {
		/*Chrome*/
		String chDriver = new File(new File(".").getCanonicalPath()+"\\" + "driver/chromedriver.exe").getCanonicalPath();
		System.setProperty("webdriver.chrome.driver", chDriver);
		System.setProperty("webdriver.chrome.bin", "C:\\Program Files (x86)"
				+ "\\Google\\Chrome\\Application\\chrome.exe");
		url = "http://www.ncfxy.com/index.html";
		wd = new ChromeDriver();
		wd.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		
		/*Firefox*/
		/*wd=new FirefoxDriver();
		url = "www.baidu.com";*/
	}
	
	@Test 
	public void testWebDriver(){
		/*测试*/
		String email=null;

	    wd.get(url);
	    wd.findElement(By.id("name")).sendKeys("admin");
		wd.findElement(By.id("pwd")).sendKeys("123");
	    wd.findElement(By.id("submit")).click();
	
		email=wd.findElement(By.xpath("//*[@id='table-main']/tr[1]/td[2]")).getText();
		System.out.println(email);
		wd.quit();     
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值