P.Schultz
P.Schultz

Reputation: 13

Selenium WebDriver: Variable for different countries

Hi I'm new to Java and Selenium and I am trying to figure out some things. And please forgive my English.

Let's assume I have 2 or more countries and I only have one "testclass" for logging in.

For Country1 I have the username:"user1" and the password "pass1". For country2 it is "user2" and "password2". I do not want to hardcode the username and the password into the testclass so I created another one which holds the variable e.g
public static final String iUsername = "user1";

and

public static final String mUsername = "user2";.

In the testclass I would call it e.g

driver.findElement(By.id("username")).sendKeys(Constant.iUsername);.

My Question is how can I select before I start the test the other username (mUsername) without always changing the code at the testclass.

Like I said I'm new and I am not so good at explain things.

Thanks

Upvotes: 1

Views: 678

Answers (2)

Tom
Tom

Reputation: 1411

This is a pseudo-code that shows a possible design for your test:

  1. Create a class for Users:

    public class User {
        public String username;
        public String password;
    
        public Users(String username, String password) {
            this.username = username;
            this.password = password;
        }
    }
    
  2. Create a class to store their information (similar to your static class):

    public class UserFixture {
        public static User userUSA = new User("userUSA", "passUSA");
        public static User userUK = new User("userUK", "passUK");
        public static User userChina = new User("userChina", "passChina");
        public static User userFrance = new User("userFrance", "passFrance");
    }
    
  3. Create a HashMap with the list of countries you want to test:

    public class AccessTest {
        List<String> countries = Arrays.asList(
            "www.website.com", 
            "www.website.co.uk", 
            "www.website.ch", 
            "www.website.com.fr");
    
        Map<Users, List<String>> userCountries = new HashMap<Users, List<String>>();
        userCountries.put(UserFixture.userUSA, countries);
        userCountries.put(UserFixture.userUK, countries);
        userCountries.put(UserFixture.userChina, countries);
        userCountries.put(UserFixture.userFrance, countries);
    
        public static test() {
            // u = user; c = country 
            userCountries.forEach((u, c) ->
                c.forEach( country ->
                    goToUrl(country);
                    login(u);
                    // Write the rest of the test below
                )
            )
        }
    }
    

Upvotes: 1

vv88
vv88

Reputation: 173

There are many ways to do this, but easiest one is -

You can have one properties file containing your user ids and passwords and one extra parameter (say Choice = 1 or 2) defining which user you want to use. This way you will not need to have the class holding constants. Then you can have one class for reading parameter file. Create a login function where you can pass the login data. Then in your test class you can use ids and their passwords based on the value of Choice.

if(properties.getProperty("Choice") == 1)
{ Login(properties.getProperty("User1Id"),properties.getProperty("Pass1"));
else if (properties.getProperty("Choice") == 2)
{ Login(properties.getProperty("User2Id"),properties.getProperty("Pass2"));

} else //some error

This way you just have to change the value of choice variable in your properties file.

Upvotes: 0

Related Questions