我正在从json文件中读取输入数据。我的json文件结构是
`{ "testloginpage":[
{
"username":"hellotest",
"password":"password1234",
},
], "emailtest":[
{
"fromemailaddress":"noreply@test.com",
"testemailserver":"111.11.11.1",
"testusername":"test",
"testpassword":"test",
},
{
"fromemailaddress":"aaaa@test.com",
"testemailserver":"22.22.22.2",
"testusername":"aaaa",
"testpassword":"aaaa",
}, ],
}`
我可以使用以下代码循环播放并能够读取所有数据:
public static void readFromJson() {
JSONParser parser = new JSONParser();
try{
FileReader reader = new FileReader ("<--FilePath--><filenale>.json.txt");
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
JSONArray login = (JSONArray) jsonObject.get("testloginpage");
for (int i = 0; i<login.size();i++){
JSONObject jsonObjectRow = (JSONObject) login.get(i);
UserName = (String) jsonObjectRow.get("username");
Password = (String) jsonObjectRow.get("password");
}
JSONArray emailtestfields = (JSONArray) jsonObject.get("emailtest");
for ( i = 0; i<emailtestfields.size();i++)
{
System.out.println("The "+i+" elements in email test are" +emailtestfields.get(i));
}
Iterator i = emailtestfields.iterator();
while (i.hasNext()){
JSONObject innerObj = (JSONObject) i.next();
System.out.println("From email address is "+innerObj.get("fromemailaddress"));
fromEmailAddressEmailSettings = (String) innerObj.get("fromemailaddress");
System.out.println("test Email Server is "+innerObj.get("testemailserver"));
emailServerEmailSettings = (String) innerObj.get("testemailserver");
System.out.println("User name is "+innerObj.get("testusername"));
testUserNameEmailSettings = (String) innerObj.get("testusername");
System.out.println("test User password is "+innerObj.get("testuserpassword"));
testPasswordEmailSettings = (String) innerObj.get("testpassword");
}
}catch (Exception e) {
System.out.println("Error: "+e);
}
}
我有一个testng类,我在其中创建测试脚本。目前,脚本只将第二组数据输入表单并保存。 我想要实现的场景是我必须从json读取有多少电子邮件测试记录集(例如2),而在我的testng类中,我必须首先获取第一组emailtest数据并输入并保存然后采取第二组并进入网络应用程序并保存。我已经创建了通用函数来将数据输入到正常工作的文本框中。我不知道如何将循环计数带入我的testng测试脚本,而不是首先输入第一个数据集并保存,然后进入第二个设置,输入并保存。
答案 0 :(得分:0)
只需将JSONObject保存在List中并将其传递给数据输入方法,例如
List<JSONObject> lst = new ArrayList..
while (i.hasNext()){
JSONObject innerObj = (JSONObject) i.next();
lst.add(innerObj);
}
for (Jsonobject a : lst) {
putindata(a);
}
public void putindata(JsonObject a) {
//sendKeys(a.getString("fromemailaddress");
//...
}
答案 1 :(得分:0)
我将json格式更改为 { “appLogin”:{ “username” 的: “测试”, “密码”:“测试”, },
"createNewUser":{
"UserName":["test1", "test2"],
"UserPassword":["solution1","solution2"],
"UserConfirmPassword":["solution1","solution2"],
"UserFirstName":["testuser1","testuser2"],
"UserLastName":["test1","Hello2"],
"UserGroup":"localuser",
},
}
这是我正在使用的java代码
static String userName;
static String password;
static String newUserNames[];
static String newUserPasswords[];
static String newUserConfirmPasswords[];
static String newUserFirstNames[];
static String newUserLastNames[];
static String userGroup[];
Public static void readFile() {
JSONParser parser = new JSONParser();
try {
Object obj = parser.parse(new FileReader("<<Location of ur input json data file>>"));
jsonObject = (JSONObject) obj;
/**
* This section is to read login data from input json file.
*/
JSONObject gveLogin = (JSONObject) jsonObject.get("appLogin");
userName = (String) gveLogin.get("userName");
password = (String) gveLogin.get("password");
/**
* This section is to read create new user from input json file.
*/
JSONObject createNewUser = (JSONObject) jsonObject.get("createNewUser");
JSONArray newUserName = null;
Object newUserNameObj = (createNewUser != null &&
createNewUser.containsKey("UserName")) ? createNewUser.get("UserName") : null;
if (newUserNameObj !=null && newUserNameObj instanceof JSONArray){
newUserName = (JSONArray) newUserNameObj;
}
int newUserNameSize = newUserName.size();
newUserNames = new String[newUserNameSize];
int newUserNameCount;
for (newUserNameCount = 0;newUserNameCount<newUserNameSize; newUserNameCount++){
newUserNames [newUserNameCount] = (String) newUserName.get(newUserNameCount);
System.out.println(newUserNames[newUserNameCount]);
}
.... Same for password, confirm password, first name , last name and for userGroup I have used jsonObject as follows:
newUserGroup = (String) createNewUser.get("userGroup");