Sanjeev Kannan
Sanjeev Kannan

Reputation: 27

Selenium Web Driver Eclipse: Passing the username and password from json file or xml file

I need to test a login page with multiple username and password and need to pass the username and password from JSON file or XML file. is there a way to achieve it?

driver.findElement(By.id("usernameS")).sendKeys(username); driver.findElement(By.id("passwordS")).sendKeys(password);

How should i reference the values in the sendkeys function in selenium web driver

Upvotes: 0

Views: 3153

Answers (2)

user861594
user861594

Reputation: 5908

You can check QAF. Where you can use properties in xml file and use in code. For example:

driver.findElement(By.id("usernameS")).sendKeys(getBundle().getString("default.username"));
driver.findElement(By.id("passwordS")).sendKeys(getBundle().getString("default.password"));

Properties in xml file may look like below:

<testdata>
   <default>
      <username>user1</username>
      <password>user1pwd</password>
      <!-- other default properties-->
   </default>
   <admin>
      <username>adminUser</username>
      <password>adminpwd</password>
   </admin>
</testdata>

Furthermore for secure values you can have encrypted values in the properties file. for example:

<testdata>
   <default>
      <username>user1</username>
   </default>
   <encrypted>
      <default>
         <password>encrypted value</password>
   </default>
   </encrypted>
</testdata>

In above case, for password, you have provided encrypted.default.password in property. So when you use default.password you will get decrypted value.

In case of data driven test where you want to run your test with different data set you can use inbuilt data-providers that feeds data form XML/JSON/CSV/EXCEL/DB.

There are many more features for web, mobile and web-services test automation. For web and mobile, you can take benefit of features like:

and many more.

Upvotes: 0

HARDI
HARDI

Reputation: 394

The below answer is based on assumption that the JSON is of below format:

{
    "testData": [{
        "userName": "test1",
        "password": "password1"
    }, {
        "userName": "test2",
        "password": "password2"
    }]
}

I would suggest using Jackson to deserialize the data. You will need jackson-core, jackson-annotations, jackson-databind jars of same versions in your classpath.

Create Class User as below:

public class User {
    private String userName;
    public String getUserName() {
        return userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    private String password;
}

Create Class TestData as below:

public class TestData {
    List<User> testData;

    public List<User> getTestData() {
        return testData;
    }

    public void setTestData(List<User> testData) {
        this.testData = testData;
    }
}

Deserializing will be done as below:

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {
        WebDriver driver = new ChromeDriver();
        // You can read the json from property file/service call and store to string.
        String jsonString = "{\"testData\": [{\"userName\": \"test1\",\"password\": \"pwd1\"}, {\"userName\": \"test2\", \"password\": \"pwd2\" }]}";
        ObjectMapper mapper = new ObjectMapper();
        TestData obj = mapper.readValue(jsonString,TestData.class);
        for (User data : obj.getTestData()) {
             driver.findElement(By.id("usernameS")).sendKeys(data.getUserName());
             driver.findElement(By.id("passwordS")).sendKeys(data.getPassword());
        }
}

Upvotes: 0

Related Questions