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?

Mukesh Takhtani
  • 852
  • 5
  • 15
Speedychuck
  • 400
  • 9
  • 29

2 Answers2

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.

djangofan
  • 28,471
  • 61
  • 196
  • 289
  • 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 Jan 20 '16 at 10:29
  • @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. – djangofan Jan 20 '16 at 16:03
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;


}
murali selenium
  • 3,847
  • 2
  • 11
  • 20