Autoplay



Transcripted Summary

In this chapter, we will start using the Data-Driven concept or pattern with Appium in our test cases.

In this chapter, we will give an introduction to data-driven tests.

After that, we will use the test data in a JSON file using Appium tests.


# Introduction to Data Driven Tests

Data-driven testing is a procedure when you repeat the same test scenario with different input parameters and then verify the result with the given output values.



All this test data is an important part of automated tests.

Test data is not just the values or the text in the application, but it's about the whole environment you are testing.

The "data driven" means that you are using different data from external data sources to test the application.

So, we're hiding the data from the tests to make our tests more flexible, and it can be run on any test environment, as shown in this image.

In the previous examples, usually, we are using hardcoded data, and the hardcoded data is the data that we added in our test script, like the username, password, URL, for example.

If we have different testing environments with different URLs, we need to add these URLs to one file and read the correct data from this file.

But if we add it inside the test case, it will be hard-coded and can't be configurable.

Another example for using data driven, if we have a registration form and need to test the positive and the negative scenarios, we need to prepare a different set of data for these cases to be able to verify the application behavior.

In this demo, we will add our test data with a JSON file that will be used with our Appium test cases.

I created a project for AppiumDataDriven and in this project, we will use the previous project that we had with PageObject.

Here we have the source folder, we have test and also, we have the testng XML files, and inside our project, we will create a new folder for our file.

We will call it data and in data, we will right-click and then click New File, and add TasksData.json.

Here we will add the task name and the task description to be used with our data-driven demo.

So, in this file, we will start by creating our JSON file and this JSON will contain "Tasks Data" and this will be the title of the JSON array.

We will add our array and inside our array, we will have different data.

Inside we will create the first item which will be the TaskName.

The value of TaskName will be "Finish Appium Course", the text that we used before in our project.

Then we can add a comma and then add the second item, which is TaskDesc and the task description can be "Need to finish my course ASAP".

This is the first one, and maybe I can add another and just change the data.

For example, here, I can say, "Start Learning Espresso" and with the description "Need check the new course".


{
  "Tasks Data": [
    {
      "TaskName" : "Finish Appium Course",
      "TaskDesc" : "Need to finish my course ASAP"
    },
      {
      "TaskName" : "Start Learning Espresso",
      "TaskDesc" : "Need check the new course"
    }
  ]
}

This is our JSON file with task data as an array, and then we need to create a function to read the data from the JSON file and then start using it with our test cases.

In the next step, we need to create a utility class to get the data from the JSON file and convert it to be an object.

After that, we can use it in our test.

So under main, under java with the Page Objects, we can create a new folder PageObjects, and move the classes into this folder.

Under java, we can also create a new package and name it utilities, so it will include anything that we will create for the utilities.

Then we can right-click and select a new Java class and call it JsonReader.java because we need to create a class to read our JSON file.

After that, we can start creating a public static object.

As we know from when we were working with data-driven with testng and Selenium previously, we are using an object with a two-dimensional array to be able to repeat and use different iterations with the same test case.

This is what we will actually do now with our JSON file.

We'll create a static object from a two-dimensional array to get the JSON data, and then after that, we can use it with the @DataProvider annotation with testng and our test cases.

So, here we will create an object with a two-dimensional array and we will name it getJSONData and this will take different parameters - for example, String JSON_path, STRING JSON_Data, int JSON_attributes, which is how many attributes that we need to read, so it can be changed.

We have an object and then we need to start creating.

We still have a syntax error because this should return an Object[][], and it's missing the return statement, so at the end, we will need to add this.


package utils;

public class JsonReader {
    public static Object[][] getJSONdata(String JSON_path, String JSON_data, int JSON_attributes) {

    }
}

Then after that, we need to start parsing the JSON file.

Before we add our parser, we need to add one more dependency for JSON library or JSON dependency, to be able to use it to parse the JSON file.

In the pom.xml file, and under the dependencies, we will add one new dependency, which is from googlecode - "json-simple", which will be used for parsing the JSON file.

So we added the dependency and we already downloaded that dependency in our project.


<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

We will go back to the JSON file and continue.

We will add a new Object object which equals a new JSONParser() from org.json.simple.parser, and then we need to call .parse() and pass the file we need to parse - new FileReader(JSON_path)).

We will need to import the JSONParser, the FileReader, and the exception class because

this has input-output, and then with parse, we need to add an exception in the signature.


package utils;

import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class JsonReader {
    public static Object[][] getJSONdata(String JSON_path, String JSON_data, int JSON_attributes) throws IOException, ParseException {
        Object obj = new JSONParser().parse(new FileReader((JSON_path)));

    }
}

So, this is the idea.

With the object, we need to parse the file that we read from the JSON_path.

In our test cases, we will add the path for our folder that's inside the data folder.

Then, with JSONObject - we need to import this one from json.simple - we will call this jsonObject and set it equal to object but we need to cast it to be a JSONObject.

And then after that, we will add a JSONArray - we also need to import this one - and we will call it jsonArray and set it equal to jsonObject.get(JSON_Data), so the test will read from JSON_Data and then we need to cast it to a JSONArray.

Then after that, we start adding our array.

We will create an object of two-dimensional arrays called arr and this will equal a new array of Strings and this will include jsonArray.size() in the first dimension and the second dimension will be the JSON_attirbutes.


package utils;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileReader;
import java.io.IOException;

public class JsonReader {
    public static Object[][] getJSONdata(String JSON_path, String JSON_Data, int JSON_attributes) throws IOException, ParseException {
        Object object = new JSONParser().parse(new FileReader((JSON_path)));
        JSONObject jsonObject = (JSONObject) object;
        JSONArray jsonArray = (JSONArray) jsonObject.get(JSON_Data);

        Object[][] arr = new String[jsonArray.size()][JSON_attributes];
    }
}

Then we will look inside this one... so for (int i = 0; i < jsonArray.size(), i++) {}.

Then we need to add the logic here.

So with JSONObject, let's say it's obj1, for example, we can say jsonArray.get(i) so it will loop inside the jsonArray and get the data, and we will cast this data also.

Then after that, you will read the first attribute with arr[i][0], and then we are using String.valueOf(obj1.get("")) and we need to get the key, which is TaskName.

When we get("TaskName"), we will get the text inside "TaskName".

For example, if you are getting "TaskName" for the first time, we will get the "Finish Appium Course" value, and for the next one, we will get "Start Learning Espresso".

We also need to do the same thing for the task description, so we will copy this one and add it for the second item, we need to get TaskDesc.

After that, and outside the for loop, we can add our return, and then we need to return the array because an array is a two-dimensional object and the function is also typed with an object of two-dimensional arrays, so now the issue is fixed for that return statement.


package utils;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import java.io.FileReader;
import java.io.IOException;

public class JsonReader {
    public static Object[][] getJSONdata(String JSON_path, String JSON_Data, int JSON_attributes) throws IOException, ParseException {
        Object object = new JSONParser().parse(new FileReader((JSON_path)));
        JSONObject jsonObject = (JSONObject) object;
        JSONArray jsonArray = (JSONArray) jsonObject.get(JSON_Data);

        Object[][] arr = new String[jsonArray.size()][JSON_attributes];
        for (int i = 0; i < js.size(); i++) {
            JSONObject obj1 = (JSONObject) js.get(i);
            arr[i][0] = String.valueOf(obj1.get("TaskName"));
            arr[i][1] = String.valueOf(obj1.get("TaskDesc"));
        }
        return arr;
    }
}

So, this is a symbol library to read the data from a JSON path.

Here, we are just passing the JSON_attributes and here, we have two attributes so we will pass 2.

The JSON_Data will be the TasksData JSON data and here we have 2 attributes.

And then we will load the file path when we are adding the JSON_path.

In the next step, we will call JsonReader with data-driven with our six cases and using testng annotation.



Resources



Quiz

The quiz for this chapter can be found in Chapter 8.2

© 2024 Applitools. All rights reserved. Terms and Conditions Privacy Policy GDPR