使用selenium从Web表中提取数据,并将其与JSON响应进行比较

时间:2013-10-31 09:01:37

标签: java json selenium selenium-webdriver

我正在使用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响应的一致性。

1 个答案:

答案 0 :(得分:0)

我有类似的疑问。 我希望使用Appium在我的自动化上比较JSON和本机应用程序。

我需要验证应用程序的结构是否与JSON一致。