1

When using DataProviders, on TestNG, my test method has asserts that will fail since the data passed in navigates to a different url. Is there a way to work around this, i.e. a way for the data to only be injected to certain/specific asserts?

@DataProvider(name = "VINNumbers")
public String[][] VINNumbers() {
    return new String[][] {
            {"2T1BU4ECC834670"},
            {"1GKS2JKJR543989"},
            {"2FTDF0820A04457"}
    };
}

@Test(dataProvider = "VINNumbers")
public void shouldNavigateToCorrespondingVinEnteredIn(String VIN) {

    driver.get(findYourCarPage.getURL() + VIN);
    Assert.assertTrue(reactSRP.dealerListingMSRPIsDisplayed());
}

The assert test whether or not the page has an MSRP displayed, but not all dataproviders will have an MSRP displayed so it will fail. The only dataprovider that has it is the first array. Is there a way for dataproviders to be called to specific asserts?

2

In the data provider, pass another string to indicate whether to execute the assert or not. Change your assertTrue to check for that. Something like following:

@DataProvider(name = "VINNumbers")
public String[][] VINNumbers() {
    return new String[][] {
            {"2T1BU4ECC834670", "true" },
            {"1GKS2JKJR543989", "false" },
            {"2FTDF0820A04457", "false "}
    };
}

@Test(dataProvider = "VINNumbers")
public void shouldNavigateToCorrespondingVinEnteredIn(String VIN, String validVIN) {

    driver.get(findYourCarPage.getURL() + VIN);
    Assert.assertTrue("false".equals(validVIN) || reactSRP.dealerListingMSRPIsDisplayed());
}
  • But this way is not convenient/efficient because you still have to create different test methods for it's specific vin number. – Robben Sep 1 '15 at 19:20
  • I did not follow your comment. Why do you need to create different test methods for specific vin number? The data provider gives the information whether you want to execute a test or a part of the test for a number or not. You use the information in the same test for all the VIN numbers. You can even enhance the data provider to give information related to multiple tests also. – Dakshinamurthy Karra Sep 2 '15 at 4:38
  • In my case I have a bunch of data but for each data there is something else I want to test. This is good only if you want to test the same thing but with different data whereas I have to test different things with different data. – Robben Sep 2 '15 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.