目录

软件测试Java中Selenium-Webdriver的使用

目录

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

Java中Selenium Webdriver的使用

第一步:

进入Selenium 的官网 ,点击“Download”进入如下界面。

https://img-blog.csdn.net/20160517165420386?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

第二步:

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

https://img-blog.csdn.net/20160517165444793?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

第三步:

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

https://img-blog.csdn.net/20160517165507332?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

第四步:

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

https://img-blog.csdn.net/20160517165534317?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

测试代码:

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();     
	}
}