转自:http://www.51testing.com/html/36/489136-856022.html
一.安装JDK,设置JAVA_HOME的环境变量
下载地址:http://www.java.com
1. 计算机->属性->高级系统设置->高级->环境变量,逐层进入,如下图设置环境变量
2. 打开cmd窗口,用java –version命令验证是否设置成功
二.安装Eclipse
下载地址:http://www.eclipse.org/downloads/
三.在Eclipse中安装TestNG
1. 点击eclipse中的Help->Install New Software
2. 点击Add按钮,输入相应的地址;勾选加载出来的TestNG选项,点击Install,完成TestNG的安装。
Eclipse 3.4或更高版本,安装地址为http://beust.com/eclipse.
Eclipse 3.3或更低版本,安装地址为http://beust.com/eclipse1.
四.Selenium2
下载地址:http://code.google.com/p/selenium/downloads/list
仅考虑支持FireFox的话,下载selenium-server-<version>.zip即可。如要对其他浏览器进行支持,需要下载对应的Driver
五.创建Project
1. 新建一个Java Project即可
2. 添加TestNG Library。Build Path->Add Libraries
3. 添加Selenium的jar文件。Build Path->configure build path->add jars,将上面下载的selenium-server-<version>.zip中的selenium-server-<version>.jar添加进去,并且添加这个zip包中lib目录下面的jar文件
4. 新建一个TestNG Class以及对应的XML文件
5. 在新建的class中添加简单的code,如例子中,在baidu中对Selenium关键字进行搜索
Java Code如下:
packagetest.sample;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.testng.annotations.Test;
publicclassSample {
@Test
publicvoidsimpleTest() {
String testURL ="http://www.baidu.com";
WebDriver driver =newFirefoxDriver();
driver.get(testURL);
WebElement keyword = driver.findElement(By.id("kw"));
keyword.sendKeys("Selenium");
WebElement searchBtn = driver.findElement(By.id("su"));
searchBtn.click();
}
}
同时,testng.xml中会生成对应的内容,来执行刚才创建的Class
<?xmlversion="1.0"encoding="UTF-8"?>
<suitename="Suite"parallel="false">
<testname="Test">
<classes>
<classname="test.sample.Sample"/>
</classes>
</test><!-- Test -->
</suite><!-- Suite -->
6. 执行检验结果。右键testng.xml, run as TestNG Suite.