简体   繁体   中英

Testing multiple environments with different data - best practice

I need to be able to run the same tests on different environments (max 3) but with different data for each one.

I have a test method:

@Test (groups = "core", description = "Login: Valid log in")
public void validLogin() {
    User user = UserData.user_2();
    loginPage.logOn(user);
}

In the UserData class I have:

public static User user_2() {
    return new User().newUser("user2", "password");
}

"user2" does not exist on all environments. I may not be able to change the data that is available on all of the environments to match the test data.

The tests will be executed either using Maven and TestNg so I can send in the parameter for the execution environment.

My initial thought is to use:

public static User user_2() {
    switch(env) {
    case "env1": return new User().newUser("user2", "password"); break;
    case "env2": return new User().newUser("user2Z", "password"); break;
    case "env3": return new User().newUser("user2X", "password"); break;
}

I have a limited number of data classes and methods (<100) but several thousand tests.

What is the best way of setting up and handling the data required for testing against the different environments?

When it comes to different users , You always wish that all my test cases should remain as it is and with minimal change. So this is what i follow. I create a file lets say username.properties file in eclipse.

username=xyz@gmail.com password=passswd1

You can create multiple users here with name ie

rohan=rohan@gmail.com rohan's password: rohan

Now we need to call this file in our class. See below example.

Main test

SignInPage.SendkeysMethodForSignInPAgeForPropertyFile(driver, By.cssSelector("input[id='Email']") , "username" );

SignInPage.SendkeysMethodForSignInPAgeForPropertyFile(driver, By.cssSelector("input[id='Passwd'][type='password']"), "password"); 

So here username will be taken from properties file.

This will go to SendkeysMethodForSignInPAgeForPropertyFile which is:

public class SignInPage {

public void SendkeysMethodForSignInPAgeForPropertyFile(WebDriver driver, By by, String Text) {
    WebUtils.SendkeysForPropertyFile(driver,by, Text);
  }
}

Which will go to SendkeysForPropertyFile method as:

 public static void SendkeysForPropertyFile(WebDriver driver, By by, String Text) {
    ReadFileData File = new ReadFileData();
    Properties Values = File.ReadFile();
        WebElement Element = driver.findElement(by);
        Element.clear();
        if (Text == "username"){
        Element.sendKeys(Values.getProperty("username"));
        }
        else {
        Element.sendKeys(Values.getProperty("password"));
        }

Which will read from ReadFileData() class which is:

  public class ReadFileData {
  public Properties ReadFile() {
      File file = new File("D:\\Selenium\\Gmail_Web_UI\\Loginproperty.properties");

        FileInputStream fileInput = null;
        try {
            fileInput = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Properties prop = new Properties();

        //load properties file
        try {
            prop.load(fileInput);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return prop;
  }

This helps to keep our username and password safe in a single file. Reply to me for further query. Happy Learning :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM