We will go to our test
folder, and under java
we will start with ToDo_Android.java
and here in our test case we can start creating a public object.
With the @DataProvider()
annotation from testng, that helps us to read different data with our test cases and we need to pass a name here.
This will be name = "tasks data"
, for example, or you can use any name.
After that, it will be public Object[][]
with two-dimensional arrays and we will call it passData()
.
Inside this, we can return JsonReader.getJSONdata()
, and after that we need to pass three parameters - the JSON_path, the String JSON_data, and the JSON_attributes.
So for example, we can add System.getProperty()
and we will load it from "user.dir", and "/data/TasksData.json".
After that, we need to pass the JSON_data - in our case, it will be the name of our array "Tasks Data".
Then we need to add the third one, which is the attributes.
We have here two because in our file we have two - TaskName and TaskDescription - and if we add more attributes, we can add the number here.
So this is the calling of JsonReader
and this is the idea.
We need to add an Exception in the signature for IOException
and the ParseException
.
So this is the passed data, and then we need to add the data provider in our test cases.
For example, in the testng annotation, we can add the data here - @Test(dataProvider = "tasks data")
- so we are passing the data provider with the name and the name here is "tasks data".
import PageObjects.CreateTaskPage;
import PageObjects.TasksListPage;
import org.json.simple.parser.ParseException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.JsonReader;
import java.io.IOException;
import java.net.MalformedURLException;
public class ToDo_Android extends TestBase {
CreateTaskPage createTaskPage;
TasksListPage tasksListPage;
@DataProvider(name = "tasks data")
public Object[][] passData() throws IOException, ParseException {
return JsonReader.getJSONdata
(System.getProperty("user.dir") + "/data/TasksData.json"
, "Tasks Data", 2);
}
@Test(dataProvider = "tasks data")
public void test_add_task() throws MalformedURLException {
Android_setUp();
tasksListPage = new TasksListPage(driver);
createTaskPage = new CreateTaskPage(driver);
tasksListPage.clickAddTaskBtn();
createTaskPage.enterTaskName("Finish Appium Course");
createTaskPage.enterTaskDesc("Finishing my course ASAP");
driver.hideKeyboard();
createTaskPage.clickSaveBtn();
tearDown();
}
}
Now we will remove the hard-coded values in our test cases, and then we should read them from the JSON file.
Then we need to add here two parameters, String taskName
and String TaskDescription
and we will replace the hard-coded value with these.
For the first one, we will call TaskName
and for the second one, we will call TaskDescription
.
So here, what will be happening during our test?
We will start with the data provider.
We will start parsing the data, reading the data from this path, start converting it to JSON objects, and then with the data provider, we start looping on that data in the JSON file.
For the first test case, it will be "Finish Appium Course", and this description.
And for the second one, it will be "Start Learning Espresso", and this description.
import PageObjects.CreateTaskPage;
import PageObjects.TasksListPage;
import org.json.simple.parser.ParseException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.JsonReader;
import java.io.IOException;
import java.net.MalformedURLException;
public class ToDo_Android extends TestBase {
CreateTaskPage createTaskPage;
TasksListPage tasksListPage;
@DataProvider(name = "tasks data")
public Object[][] passData() throws IOException, ParseException {
return JsonReader.getJSONdata
(System.getProperty("user.dir") + "/data/TasksData.json"
, "Tasks Data", 2);
}
@Test(dataProvider = "tasks data")
public void test_add_task(String taskName, String TaskDesc) throws MalformedURLException {
Android_setUp();
tasksListPage = new TasksListPage(driver);
createTaskPage = new CreateTaskPage(driver);
tasksListPage.clickAddTaskBtn();
createTaskPage.enterTaskName(taskName);
createTaskPage.enterTaskDesc(TaskDesc);
driver.hideKeyboard();
createTaskPage.clickSaveBtn();
tearDown();
}
}
Let's run our script and check if we need to change anything, or if we have any problems with our scripts.
We will run our test cases.
This is for Android, so we will open the Appium server, and this is the Android emulator and the start.
While we are running our test here, we can see that the data is already added in our test cases.
For the first iteration, this will be "Finish Appium Course" for that first attribute, and this is a task description.
We are now running test_add_task
with this data.
We will wait until we add the first script, and now we have "Finish Appium Course" and added a description, and then we will hide the keyboard, and then click "Save".
Then we close the application and then we will now start with iteration number one, which is iteration number two because we started from zero.
This is for "Start Learning Espresso" with the description "Need check the new course".
Now our system is running and the first one is passed.
So we are waiting for the second one to check our test results.
Here we added the second data, and then we will hide the keyboard and then click "Save".
So then our test case passed, and the data is read from the JSON file.
We will do the same change with the iOS test case.
So for example, we can copy this @DataProvider
and open our iOS_Todo_Test.java
, and then we can add this here and we can import is the required annotations from the JsonReader
, and also IOException
and the ParseException
.
Also, we need to add the annotations - the data provider and the parameters in our test cases.
So this will be the annotation - @Test(dataProvider = "tasks data"
and we need to add these two parameters in our test - (String taskName, String TaskDesc)
- and it will be the same data for iOS.
We copy that TaskName and remove the hard-coded value in our iOS test case, and for the task description as well.
import PageObjects.CreateTaskPage;
import PageObjects.TasksListPage;
import org.json.simple.parser.ParseException;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import utils.JsonReader;
import java.io.IOException;
import java.net.MalformedURLException;
public class iOS_Todo_Test extends TestBase {
CreateTaskPage createTaskPage;
TasksListPage tasksListPage;
@DataProvider(name = "tasks data")
public Object[][] passData() throws IOException, ParseException {
return JsonReader.getJSONdata
(System.getProperty("user.dir") + "/data/TasksData.json"
, "Tasks Data", 2);
}
@Test(dataProvider = "tasks data")
public void test_add_task(String taskName, String TaskDesc) throws MalformedURLException {
iOS_setUp();
tasksListPage = new TasksListPage(driver);
createTaskPage = new CreateTaskPage(driver);
tasksListPage.clickAddTaskBtn();
createTaskPage.enterTaskName(taskName);
createTaskPage.enterTaskDesc(TaskDesc);
driver.hideKeyboard();
createTaskPage.clickSaveBtn();
tearDown();
}
}
Let's run our test case and check if everything is okay.
We will, in this case, open the iOS simulator and open the Appium server.
We opened the application, we will click here, and then we will add the first data.
Also, with the same case, we are running now with the test_add_task
with the first iteration, and the second one now is loaded with "Start Learning Espresso" with the second record in the JSON file.
Now our test case passed with two passed because we have two test cases - one test case, but with different iterations, with different data.