0

Following the same principle of this answered question: Reading parameter values from Testng.xml file in cucumber stepdefs I need to be able to read the data coming from a @dataprovider class instead of the testng.xml

@Test(dataProvider = "allUsers", dataProviderClass = ***.automated_testing.DataProviders.DataProviderClass.class)
    @Given("^user is sucessfuly logged into *** website$")
    public void user_is_sucessfuly_logged_into_***_website(String userName, String passWord) throws Throwable {
        homePage.logIn(userName, passWord);
    }

    @Given("^User clicks Overview tab$")
    public void user_clicks_Overview_tab() throws Throwable {
        overviewPage = homePage.goToOverviewPage();
    }

In this case only the first method (which has the @Test annotation) is working, which makes total sense. But how can I use the same @Test for all the methods of the Cucumber framework?

**EDITED: I think the only reason right now for me to be using the @DataProvider is for scenarios I want to use different users with different roles. With dataprovider it creates one test for each of the objects I pass in that class, if there's another way of doing so without the need of @DataProvider would be helpful too.

Community
  • 1
  • 1
Ricardo M.
  • 21
  • 1
  • 7
  • you can check [qaf gherkin client](https://qmetry.github.io/qaf/latest/gherkin_client.html) where you can use different data-providers. You can set `Examples:{'datafile':'resources/${my.test.data.file}.txt'}` to support different data-file for different environment or you can use XML data provider by providing xml data key like `Examples:{'key':'my.test.data'}` where xml resources loaded as per environment by [resource configuration](https://qmetry.github.io/qaf/latest/managing_resources_for_different_env.html). – user861594 Nov 07 '16 at 04:40

2 Answers2

1

You are right that it is risky to have user name and password specified in an example. The risk you are facing is when you have to change a user name or something similar.

It is also so that login as a specific user is not really the interest of the example. Business representatives are usually interested in the event and login is usually not at the core. Therefore, I tend to make sure that when I setup the system under tests for the specific scenario, I allow the Given step to find username and passwords if they are needed. The result is that I have some code that looks up username and password and uses it. This leads to one place to change if needed, not many places in many scenarios.

My solution would look something like this:

@Given("^user is sucessfuly logged into *** website$")
    public void user_is_sucessfuly_logged_into_***_website() throws Throwable {
        String userName = helperClass.getUserName();
        String passWord = helperClass.getPassword();

        homePage.logIn(userName, passWord);
    }

The helperClass is an instance of something that knows how to retrieve the values from the @Dataprovider you are using.

This doesn't really answer your original question, but it may point you in a direction where you are able to find a solution that works for you.

Thomas Sundberg
  • 4,098
  • 3
  • 18
  • 25
  • True, this doesn't answer the question but it really helped me in thinking of some other options in dealing with this. I won't mark this as answered so maybe someone have a direct way of using @DataProvider but your answer is very helpful. Thank you. – Ricardo M. Sep 19 '16 at 12:58
0

Ricardo, that it's not the way you should get access to data in cucumber. You should use scenario outline and special keyword: Examples

Here is an example of how 'examples' work :)

Scenario Outline: feeding a suckler cow
  Given the cow weighs <weight> kg
  When we calculate the feeding requirements
  Then the energy should be <energy> MJ
  And the protein should be <protein> kg

  Examples:
    | weight | energy | protein |
    |    450 |  26500 |     215 |
    |    500 |  29500 |     245 |
    |    575 |  31500 |     255 |
    |    600 |  37000 |     305 |

Meaning this particular test will be executed 4 times using data from each row described in examples table. For more examples please refer to cucumber-jvm docs: https://cucumber.io/docs/reference

Also, there is no need to mark your tests with @Test annotation, tests in cucumber described in feature file format.

Mikhail
  • 665
  • 4
  • 16
  • Thank you for your answer but this is not exactly what I am looking for here. I don't want to have to specify Login Users (for each role) in every scenario that I have to log in (all of them), in the case I need to change users in the future I would have to edit all of those manually. – Ricardo M. Sep 16 '16 at 13:00