最近自己通过网上的资料安装了eclipse+selenium+testng+ant,发现网上的资料不是很完整,对于初学者来说,配置起来不是很舒畅,我把我配置的详细过程写下来,希望能给一部分人提供帮助。
1、在eclipse中安装Testng插件。(testng和junit的比较稍后)
安装Testng有几种方法,我推荐使用eclipse
marketplace
A. 点击help—>eclipse marketplace,弹出如下界面:
B. 在find输入框中输入testng,然后按回车,出现如上图所示的结果(由于我已经安装过了,所以没有install的按钮);
C. 点击第一项后面的install按钮,一直下一步即可安装成功。
2、在eclipse上配置selenium
A. 首先先下载最新的selenium的jar包,下载地址为:
http://selenium.googlecode.com/files/selenium-server-standalone-2.24.1.jar
目前最新的版本是2.24(selenium
2.X和selenium
1.X的区别这里就不提了)
B. 在eclipse下新建一个工程,导入jar包的方法,我这里就不提了。
C. 新建一个testng class
D.
简单写下代码,代码具体含义先不介绍,以后章节会
packagecom.liu.testng;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.firefox.FirefoxDriver;
importorg.testng.annotations.Test;
publicclassBaidu {
@Test
publicvoidtest() {
WebDriver dr =newFirefoxDriver();
dr.get("http://www.baidu.com");
dr.findElement(By.id("kw")).sendKeys("51testing");
dr.findElement(By.id("kw")).submit();
}
}
E. 在代码中右键使用点击run as -->testing test,即可运行。
F. 执行testng
class文件后,会自动生成test-output目录。
3、Eclipse已经集成了ant,所以不需要安装ant
A. 整合ant,在根目录下新建build.xml文件,内容如下:
<projectname="web"basedir=".">
<propertyname="lib.dir"value="lib"/>
<propertyname="test.dir"value="web"/>
<pathid="test.classpath">
<!-- adding the saxon jar to your classpath -->
<filesetdir="${lib.dir}"includes="*.jar"/>
</path>
<targetname="transform">
<xsltin="D:/workspace/web/test-output/testng-results.xml"style="D:/workspace/web/test-output/testng-results.xsl"
out="D:/workspace/web/test-output/index1.html">
<!-- you need to specify the directory here again -->
<paramname="testNgXslt.outputDir"expression="D:/workspace/web/test-output/"/>
<classpathrefid="test.classpath"/>
</xslt>
</target>
</project>
B. 执行build.xml之后,会在test-output文件夹下生成index1.html文件,打开即可看到结果