0

I am creating a functionality test were selenium will act as a user registering an account on a website, however, the website registration has 16 input fields to fill in. Some Int values and some String. Is there an easier way of doing this?

 @Test(dataProvider = "Reg")
  public void login(String sUsername, String sPassword, String sMemorableWord)  {

I am only passing in 3 values there but with the full reg test completed there will be 16 string or Int parameters. That is going to be a huge list is there another way to do it?

2 Answers 2

1

There is a way easier way of doing this. If you put all your parameters for a test on a single Excel spreadsheet row, then just read all the parameters into a single object. Then, in your dataprovider, return a single object containing all those parameters.

@Test(dataProvider = "Reg")
public void login(WebDriver drvInstance, HashMap params)  {

This is how I used to do it. Then, within the dataprovider, I just create the new driver instance for each test and collect all the params in one object, then return those 2 items. Also, I use Apache MetaModel to read the spreadsheet.

In your dataprovider, do something like this:

@DataProvider(name = "test1")
public Object[][] prepareTestData() {
    Object[][] vals = new Object[columns][rows];
    for each row in spreadsheet {
        get rows from spreadsheet into a hashmap
        create new Capabilities object from hashmap values
        create webdriver from capabilities            
        add driver and hashmap to object[][]
    }
    return vals;
}

The beauty of this, which you might not realize, is that those 2 parameters your passing back to the method are also accessible from the @BeforeMethod phase of configuration. Something that JUnit cant do. Also, doing it this way, TestNG handles your threading for multiple browser instances.

Also, Rudziankoŭ has a point. You can use the Builder pattern within the dataprovider method to construct the objects, but I would do that later after you get it working.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok this looks good :) Still new to selenium and java. What would be the point in passing parameters to the before method when all the test code happens in the main @test method? Could you set up your beforemethod for say a login screen and then Test handles the rest of the test code for the main part of the test? I can make it work by passing in the 15 single parameters I just want to know is there an advantage to doing it the way you have suggested?
@Speedychuck - Yes, you can declare a single Object[] parameter to the BeforeMethod method, and you can extract the String objects from it, or whatever objects you put into it. Yes, I use it to set up the web browser location before the test starts. DataProvider's are pretty awesome.
0

You can take help from DataProvider in testng. In DataProvider, load all values into may be Hashtable from excel and return as Object[][].

Collect that data provider in @Test method, pass Hashtable h, as argument to functional method, then you can get required data from hash table.

below simple example can give you good idea..

 static WebDriver driver;

@Test(dataProvider="getdata")
public void myTestCase(Hashtable<String, String> h){

    driver=new FirefoxDriver();
    driver.get(h.get("url"));
}

@DataProvider(name="getdata")
public Object[][] testdata(){

    Hashtable<String, String> h=new Hashtable<>();

    //collect data from excel sheet
    h.put("url", "http://www.google.com");

    Object[][] obj={{h}};

    return obj;


}

1 Comment

Updated the code. This is currently my dataprovider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.