请问selenium+TestNG如何实现参数化
第一次在这里发帖,初涉自动化测试半个月,对TestNG还不是很了解,要实现参数化,请问XML文件具体命名,放在工程哪个位置,和固定格式应该怎么写.网上虽然有例子,但是说的太简单,没有成功.另外我是用java写的测试代码,代码中该在哪个位置,如何注释.希望有经验的朋友帮忙指点一下. 请问XML文件具体命名,放在工程哪个位置,和固定格式应该怎么写.
你是要问testng.xml文件吗?给你一个Sample吧,
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="selenium test">
<parameter name="title" value="updated title"></parameter>
<testname="nnnnn"annotations="JDK">
<classes>
<class name="com.www.Test_TestNG"/>
</classes>
</test>
<testname="nnnn2"annotations="JDK">
<classes>
<class name="com.www.Test_TestNG2"/>
</classes>
</test>
</suite> Running:
D:\workspace\WorldColor TestNG\temp-testng-customsuite.xml
FAILED: basicSearch
org.testng.TestNGException:
Parameter 'keyword' is required by @Test on method basicSearch
but has not been marked @Optional or defined in D:\workspace\WorldColor TestNG\temp-testng-customsuite.xml
为什么要temp-testng-customsuite.xml
回复 2# 的帖子
这个testng.xml该放在什么位置呢,java代码中用@parameter就可以得到值了? testng.xml can be placed in any place. tmp testng config file is generated by TestNG for current test run. You can ignore it.The usage of parameter is:
@Parameters( { "title" })
@Test
public void setUpForClass(String title) {
...
Show me your test case if still experience the problem
回复 5# 的帖子
FAILED: basicSearchorg.testng.TestNGException:
Method basicSearch requires 1 parameters but 0 were supplied in the @Test annotation.
... Removed 21 stack frames
运行失败,这是Exception.下面是我一个小脚本的开头
import ...
@Parameters({"keyword"})
@Test
public class BasicSearch extends Login{
publicvoid basicSearch(String keyword) throws Exception {
login();
....... 你把@Parameters({"keyword"})
放到方法的前面,不要放在类定义的前面试试
回复 7# 的帖子
还是有错....org.testng.TestNGException:
Parameter 'keyword' is required by @Test on method basicSearch
but has not been marked @Optional or defined in D:\workspace\WorldColor TestNG\temp-testng-customsuite.xml
是不是说parameter要在temp-testng-customsuite.xml里定义
[ 本帖最后由 rockygenius 于 2010-7-22 13:23 编辑 ] 你是从哪个XML运行TESTNG的?
从ERROR看像是从D:\workspace\WorldColor TestNG\temp-testng-customsuite.xml运行的,如果是的话,你把keyword 定义到这个文件中试试
回复 9# 的帖子
哦,原来我运行的都是XXX.java 运行XML文件的话没有报错.但我没有使用parameter的话XXX.java是可以运行的,这是为什么...
回复 10# 的帖子
单个运行java文件时,你看你的控制台输出,会有类似于Running:
C:\Users\XXXX\AppData\Local\Temp\testng-eclipse\testng-customsuite.xml
这样的字段,说明TESTNG为你这个JAVA文件自动创建了一个XML文件,然后还是以这个XML文件启动JAVA文件的。 you specify your parameter in xml, should start the test from xml, otherwise testng generates a temporary xml and start from it. The paramter is missing in the temp one.
回复 11# 12# 的帖子
有点明白了,非常感谢. 我也是刚弄这个,也碰到了同样的问题。。。弱弱的问一句,怎样运行XML文件啊。。。 ok,know了~xml文件直接右键run as就可以了,晕 为什么Eclipse总是自己创建默认的xml文件,向里面添加<Paramater>还不行,自己写的xml文件也不管用,求高手指点啊、、、、、 回复 12# goal1860为什么Eclipse总是自己创建默认的xml文件,向里面添加<Paramater>还不行,自己写的xml文件也不管用,求高手指点啊、、、、、 求指点啊啊啊 回复goal1860
为什么Eclipse总是自己创建默认的xml文件,向里面添加还不行,自己写的xml文件也 ...
xinghui6666 发表于 2011-12-26 14:30 http://bbs.51testing.com/images/common/back.gif
同样的问题,求指点 回复 1# rockygenius
在testNg xml中定义参数,不灵活,可以自定义DataProvider.比如:
@DataProvider(name = "excel")
public static Object[][] getExcelData(Method method) throws IOException, SQLException{
String[][] testData =TestDataProvider.getData(method.getName().toString());
return testData;
}
这样可以自由获取Excel或者DB数据库中的测试数据
页:
[1]
2