I want my test case to run once and test n number of inputs at various positions. Suppose If I want to check a login screen for different members like manager,user,admin,e.t.c then instead of running multiple test case for each category with specific input credentials, How can I parameterize the test so that in just one run it would evaluate all cases.
4 Answers
Selenium has no built in support to execute a test case, or collection of test cases, or ability to execute a parameterized test case. Selenium is simply a framework to interact with a web UI. The test execution engine you use to actually kick off your selenium tests varies depending on which language you are using, and even for a specific language there are often multiple competing test execution engines. For example, with Java some popular choices are JUnit and TestNG. For C#, popular choices are Nunit and MSTest. There are other options for ruby, python, etc.
Each one of these test execution engines handles parameterized or data driven test cases differently. You would need to refer to the documentation for whichever tool you are using.
-
-
Funny that correct answer is considered helpful by OP, but different incorrect answer is accepted. Such is life of experts... Commented May 12, 2014 at 23:32
Using TestNG's Dataprovider annotation is a very good way of doing this
This link gives a simple example of how the dataprovider annotation can be used using hardcoded values. You could also have the @dataprovider read an external file, store, and pass the values to your tests.
I'm not sure if I get the question correctly, but I would do login function that takes login and password as parameters, and some role-checking function that takes role as parameter. Then I would make list of dicts like
users = [{'login':'login1','pass':'password1', 'role':'admin'},{'login':'login2','pass':'password2', 'role':'user'},(...)]
And finally create tes-case which iterates through users list and calls login and role-checking function for each. It would be something like
for i in users:
login(i['login,],i['password'])
check_role(i['role'])
Tricky thing is making good role-checking function. This depends on how you can distinguish roles. I would use some element that is diffrent for each role (e.g. role written under username, or some links that are present to certain roles only) and check if it is in the right state.
package parametricJunitTests;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(Parameterized.class)
public class ParameterizedTests extends DriverHandler {
private String username;
private String password;
public ParameterizedTests(String username, String password){
this.username = username;
this.password = password;
}
@Parameters(name="{0}")
public static Collection<Object[]> data(){
Object[][] data = new Object[][] {{"[email protected]", "howdy123"}, {"[email protected]", "howdy456"}};
return Arrays.asList(data);
}
@Test
public void abLogIn(){
Page_Objects login = new Page_Objects(driver);
login.userName(username);
login.password(password);
login.loginButtonClick();
}
}
This is one way of creating parametric data and using (with JUnit) it for testing. Ideally you use data from another document, an excel cheet maybe. But like this you may code it.
This particular example has 2 other classes: one which defines and and contains the logic to use the relevant page objects, and another class which handles drivers using @Before and @After