Multiple Objects in Model

Model could have multiple objects & arrays. Let us see an example.

Define Test data with multiple objects and arrays

Let us assume we have two types of addresses:

  1. Default Address
  2. Secondary Address

Let us define these addresses as shown below:

  1. qa

Let us add data_sets/QA/address.json

{
  "default_address": [
    {
      "firstName": "Hari",
      "city": "Bangalore",
      "pinCode": "11001"
    }
  ],
  "secondary_address": [
    {
      "firstName": "Hari",
      "city": "Mumbai",
      "pinCode": "33003"
    }
  ]
}
  1. uat

Let us add data_sets/UAT/address.json

{
  "default_address": [
    {
      "firstName": "Puja",
      "city": "Delhi",
      "pinCode": "22002"
    }
  ],
  "secondary_address": [
    {
      "firstName": "Puja",
      "city": "Chennai",
      "pinCode": "44004"
    }
  ]
}

As we could see, there are two array of objects - default_address and secondary_address. Let’s see how to read these objects :

Refactor Data Client & Test

Let’s refactor AddressDataClient to get the desired address object:

package dataclients.address;

import com.testvagrant.ekam.commons.data.DataSetsClient;

public class AddressDataClient extends DataSetsClient {

    public Address getDefaultAddress() {
        return getAddress(DataKeys.DEFAULT_ADDRESS);
    }

    public Address getSecondaryAddress() {
        return getAddress(DataKeys.SECONDARY_ADDRESS);
    }

    private Address getAddress(String key) {
        return getValue(key, Address.class);
    }

    private static class DataKeys {
        public static final String DEFAULT_ADDRESS = "default_address";
        public static final String SECONDARY_ADDRESS = "secondary_address";
    }
}

Let’s refactor the test to fetch the desired address:

package ekam.example.web;

import com.google.inject.Inject;
import com.testvagrant.ekam.testBases.testng.WebTest;
import dataclients.address.Address;
import dataclients.address.AddressDataClient;
import org.testng.annotations.Test;

public class DataSetsClientTest extends WebTest {

    @Inject
    private AddressDataClient addressDataClient;

    @Test(groups = "web")
    public void shouldFetchAddresses() {

        Address defaultAddress = addressDataClient.getDefaultAddress();
        System.out.println(defaultAddress.getCity());

        Address secondaryAddress = addressDataClient.getSecondaryAddress();
        System.out.println(secondaryAddress.getCity());
    }
}

Execute test

Execute test against qa environment:

./gradlew clean runWebTests -Dconfig=qa

The output would be

Bangalore
Mumbai

Execute test against uat environment:

./gradlew clean runWebTests -Dconfig=uat

The output would be

Delhi
Chennai