File Handling with JSON and return values to the function. unable to proceed further using demo shown for JSON | Selenium Forum
V
Vipin Posted on 29/07/2020

i would like to pass the data from DataProvider to method doLogin() as mention in below code. so that username and password can be taken from JSON file and then can be send to application.

Question - how should i write return statement to pass the Set values to the function as key and values

below are the JSON and JAVA files

--------------------------------------------------------------------------------------------------------------------

package com.Yelp.Hybrid.suiteA;

import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.Yelp.Hybrid.base.YelpBaseTest;

import io.github.bonigarcia.wdm.WebDriverManager;

public class YelpLoginTest extends YelpBaseTest {

WebDriver driver;

@BeforeClass
public void initbrowser() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.MILLISECONDS);

}

@Test(dataProvider = "getData")
public void doLogin(String key, String value) {

System.out.println("Running login Test");
driver.get("http://localhost:3000/login");
driver.findElement(By.xpath(envProp.getProperty("username"))).sendKeys(key);
driver.findElement(By.xpath(envProp.getProperty("password"))).sendKeys(value);
driver.findElement(By.xpath(envProp.getProperty("login"))).click();

}

@DataProvider(name = "getData")
public Object[][] getData() throws IOException, ParseException {
String fs = (System.getProperty("user.dir") + "//src//main//java//com//Yelp//Hybrid//util//YelpLoginTest.json"); // get
FileReader reader = new FileReader(fs); // reader object
parser = new JSONParser(); // parser object
json = (JSONObject) parser.parse(reader); // parse reader object
JSONArray testdata = (JSONArray) json.get("logindata");

for (int i = 0; i < testdata.size(); i++) {
JSONObject testdetails = (JSONObject) testdata.get(i);
String testname = (String) testdetails.get("testName");
System.out.println(testname);

JSONArray data = (JSONArray) testdetails.get("data");
for (int j = 0; j < data.size(); j++) {

JSONObject currentTestData = (JSONObject) data.get(j);
Set<String> keys = currentTestData.keySet();
Iterator<String> it = keys.iterator();
while(it.hasNext()) {
String key = it.next();
String value = (String) currentTestData.get(key);
}


/*String username = (String) currentTestData.get("username");
String password = (String) currentTestData.get("password");
System.out.println(username + "--" + password);
*/

}

}
return Keys,value; // wanted to pass the keys to above function so it can be taken from json

}
}

 

----------------------------------------------------------------------------------------------------------------------------------------------

JSON file

 

{
"logindata": [
{
"testName": "LoginTest",
"data": [
{
"username": "mytest1",
"password": "password"
},
{
"username": "mytest2",
"password": "password"
},
{
"username": "mytest3",
"password": "password"
}
]
}
]
}

 


V
Vipin Replied on 30/07/2020

 

 


V
Vipin Replied on 30/07/2020

Hi

Please could you have a look at once and assist on the above query

thanks