I am trying to code in Eclipe/Java/com.googlecode.json-simple where I can read from a JSON file so I can use the values to build my WebElements, but I cannot get it to read from the same file where I was able to read using Selenium with VS/C#. How can I do the same in Java Selenium using the same JSON file?
I have an example of my JSON file further below, where I have 2 web pages and each pages has at least 1 webelement.
All the examples I found on the web show where the value/key pair is at the 1st level and/or the values to retrieve come in sets in an array (ex: city and state are repeated for the same customer name). But i have 2 levels: 1-Page, 2- WebElement in the page.
Here is the main code I created so far from my research, but it still does not work:
JSONParser jasonparser = new JSONParser();
FileReader reader = new FileReader(".\\OR\\OR1.json");
Object obj = jasonparser.parse(reader);
JSONObject objectJason = (JSONObject)obj;
String value = (String) objectJason.get("FirstName");
Here is the main code I used to make it work in C#:
JObject ORJsonData = JObject.Parse(File.ReadAllText(ORFilePath));
string ORObjJson = ORJsonData[PageName][objName].ToString();
JObject ORObjData = JObject.Parse(ORObjJson);
var ORObjParam = new Dictionary<string, string>();
ORObjParam.Add("objClass", (string)ORObjData.SelectToken("objClass"));
ORObjParam.Add("objAttribute", (string)ORObjData.SelectToken("objAttribute"));
ORObjParam.Add("objValue", (string)ORObjData.SelectToken("objValue"));
And here is a sample of my JSON file:
{
"Payment_Page1": {
"FirstName": {
"objClass": "WebEdit",
"objAttribute": "id",
"objValue": "name"
},
"CardType": {
"objClass": "WebList",
"objAttribute": "id",
"objValue": "card_type"
},
"CardNumber": {
"objClass": "WebEdit",
"objAttribute": "Xpath",
"objValue": "//input[@id='card_number']"
},
"PlaceOrder": {
"objClass": "WebButton",
"objAttribute": "class",
"objValue": "btn-primaryX"
}
},
"Confirmation_Page2: {
"Confirm_button": {
"objClass": "WebButton",
"objAttribute": "Xpath",
"objValue": "//a[@id='wsb-button']"
}
}
}
So what I do in C# is pass in the name of the Page and the
objName name (ex: "Payment_Page1" and "FirstName" ) in order to retrieve the values of its objClass, objAttribute, objValue.
In C# I can do this:
string ORObjJson = ORJsonData[PageName][objName].ToString();
But not sure how to do it in Java. Please help :(
PVP