简体   繁体   中英

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. 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:

org.testng.TestNGException: The data provider is trying to pass 2 parameters but the method [project structure removed].testFunc takes 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. I have copied the TestNG documentation, Please refer http://testng.org/doc/documentation-main.html#parameters-dataproviders for more details

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. 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. For example, the following code prints the name of the test method inside its @DataProvider:

@DataProvider(name = "dp")

public Object[][] createData(Method m) {

System.out.println(m.getName()); // print test method name

return new Object[][] { new Object[] { "Cedric" }}; }

@Test(dataProvider = "dp")

public void test1(String s) { }

@Test(dataProvider = "dp")

public void test2(String s) { }

and will therefore display:

view sourceprint?

test1

test2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM