[英]Overloading Selenium WebDriver Test Cases with DataProvider
Just as the title says I have been attempting to overload a Selenium test case with no success so far. 就像标题所说的那样,到目前为止,我一直在尝试重载Selenium测试用例,但没有成功。 Here is my code: 这是我的代码:
public class testClass {
@Test(dataProvider="testProvider")
public void testFunc(String value) throws Exception{
System.out.println("The only value provided was a string");
}
@Test(dataProvider="testProvider")
public void testFunc(String value, int myValue) throws Exception{
System.out.println("The two values provided are a string and an integer");
}
@DataProvider(name="testProvider")
public static Object[][] testProvider(){
return new Object[][]{ {"someString"}, {"someString", 5} };
}
}
When I run this code, the first test case (when only "someString" is passed) succeeds, but the second test case (when both "someString" and 5 is passed) fails with this message: 当我运行此代码时,第一个测试用例(仅通过“ someString”通过时)成功,但是第二个测试用例(当同时通过“ someString”和5时)失败,并显示以下消息:
org.testng.TestNGException: The data provider is trying to pass 2 parameters but the method [project structure removed].testFunc takes 1 org.testng.TestNGException:数据提供者试图传递2个参数,但方法[已删除项目结构] .testFunc需要1个
I have run out of ideas...it is entirely possible that this simply cannot be done but it seems like a very basic issue and I must be missing something! 我的想法已经用完了...这完全有可能根本无法完成,但是这似乎是一个非常基本的问题,我必须丢失一些东西!
Thanks for any help! 谢谢你的帮助!
I think here you can use the reflect.Method concept. 我认为在这里您可以使用reflect.Method概念。 I have copied the TestNG documentation, Please refer http://testng.org/doc/documentation-main.html#parameters-dataproviders for more details 我已经复制了TestNG文档,有关更多详细信息,请参阅http://testng.org/doc/documentation-main.html#parameters-dataproviders
If you declare your @DataProvider as taking a java.lang.reflect.Method as first parameter, TestNG will pass the current test method for this first parameter. 如果您声明@DataProvider以java.lang.reflect.Method作为第一个参数,则TestNG将通过该第一个参数的当前测试方法。 This is particularly useful when several test methods use the same @DataProvider and you want it to return different values depending on which test method it is supplying data for. 当多个测试方法使用相同的@DataProvider且您希望它根据为其提供数据的测试方法返回不同的值时,此功能特别有用。 For example, the following code prints the name of the test method inside its @DataProvider: 例如,以下代码在其@DataProvider中打印测试方法的名称:
@DataProvider(name = "dp") @DataProvider(名称=“ dp”)
public Object[][] createData(Method m) { public Object [] [] createData(Method m){
System.out.println(m.getName()); 的System.out.println(m.getName()); // print test method name //打印测试方法名称
return new Object[][] { new Object[] { "Cedric" }}; 返回new Object [] [] {new Object [] {“ Cedric”}}; } }
@Test(dataProvider = "dp") @Test(dataProvider =“ dp”)
public void test1(String s) { } 公共无效test1(String s){}
@Test(dataProvider = "dp") @Test(dataProvider =“ dp”)
public void test2(String s) { } 公共无效test2(String s){}
and will therefore display: 并因此显示:
view sourceprint? 查看原图?
test1 TEST1
test2 TEST2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.