我正在使用 selenium 网格进行驾驶测试,我需要从 web 表中提取搜索结果并将其插入到 java 表或列表中,以便将其与 JSON 响应进行比较:
这是我的 JSON 响应的形式:
{
"facets":
{
"application":
[
{
"name": "38",
"distribution": 1
}
],
"node":
[
{
"name": "frstlwardu03_05",
"distribution": 1
}
],
"area":
[
{
"name": "x",
"distribution": 1
}
],
"company":
[
{
"name": "war001",
"distribution": 1
}
]
},
"duObjects":
[
{
"id": "TASK|TSK(ZRM760J)(000)(ZRM760JU00)(000)|ZSRPSRM000",
"name": "TSK(ZRM760J)(000)(ZRM760JU00)(000)",
"mu": "ZSRPSRM000",
"label": "",
"session": "ZRM760J|000",
"sessionLabel": "SAP SRM Achats frais generaux execution",
"uprocHeader": "ZRM760JU00|000",
"uprocHeaderLabel": "Header for SRM760J",
"uprocHeaderType": "CL_INT",
"domain": "M",
"domainLabel": "",
"application": "38",
"applicationLabel": "magasin",
"highlightResult":
{
"name": "name",
"word": "TSK"
}
}
],
"totalCount": 1,
"pageSize": 10,
"pageCurrent": 1,
"pageNb": 1
}
在这里,我们从网站上得到了什么:
<table class="table table-striped table-hover table-condensed table-bordered">
<tbody>
<tr class="ng-scope" ng-repeat="object in objects">
<td>
<a target="_blank" href="#/en/object/TASK|TSK(IAM720JUA5)(000)|BIST_CE891">
<p style="font-size:11px;">
</td>
</tr>
<tr class="ng-scope" ng-repeat="object in objects">
<td>
<a target="_blank" href="#/en/object/TASK|TSK(ITP191HF10)(000)|RITP_BDDH1">
<p style="font-size:11px;">
</td>
</tr>
</tbody>
</table>
我想要做的是从 json 对象或数组中提取 duobject 名称值并将其与 HTML 表中的值进行比较。
到目前为止,这是我编写的代码,但我对如何遍历该 json 对象和 html 表并比较结果有点困惑:
此代码将读取 API URL 并提取 json duObjects 名称
public static String getresults (String URL) throws IOException, JSONException
{
JSONObject json = readJsonFromUrl(URL);
return json.getJSONArray("duObjects").getJSONObject(0).getString("name");
}
此代码将驱动 selenium 在网页中输入搜索关键字并使用用于 json 请求的相同 API 进行搜索:
driver.get(TestURL);
WebElement input1 =
driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/input[1]"));
input1.sendKeys("guest");
WebElement input2 =
driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/input[2]"));
input2.sendKeys("guest");
WebElement btn =
driver.findElement(By.xpath("html/body/div[1]/div/div[2]/div/form/button"));
btn.click();
WebElement w1 =
driver.findElement(By.xpath("html/body/header/nav/div[1]/form/div/input"));
w1.sendKeys( "tsk");
WebElement w2 = driver.findElement(By.xpath("(//button[@type='button'])[2]"));
w2.click();
我想使用这段代码来检查我的前端结果与我的 JSON 响应的一致性。