没有从json读取第二个数据

时间:2017-04-05 03:42:14

标签: json protractor

我正在尝试从json(2个周期)读取数据并添加一个条目。第一次,数据成功添加,但在第二个周期,它失败了。以下是详细信息。当使用页面对象运行而不从json读取时也可以工作;

" test_pom_fromjson"添加成功,但是当运行test_pom_fromjson_first时,它失败并显示错误,说明没有使用element(by.id('s2id_autogen10')).click();找到元素,这是位置列的定位器。

请参阅页面的屏幕截图和html代码 - https://pastebin.com/ZXyRx1tv add page



mo.ts: pageobject for the below spec file

    var mo = function () {
this.createbutton = function () {
        var createb = element(by.css('button[title="Add Master Obligation"]'))
        //var createbutton = element(by.buttonText('↵ ↵ Add Master Obligatio...↵ '))
        browser.executeScript("arguments[0].scrollIntoView();arguments[1].click();", createb, createb);


    }


    this.MasterObligationName = function (value) {


        element(by.model('obligation.ObligationName')).sendKeys(value);
    }

    this.Module = function (value) {


        element(by.id('s2id_TaxProcess')).click();
        element.all(by.repeater('item in obligation.TaxProcess.list')).get(value).click();
    }

    this.Location = function (value) {


        element(by.id('s2id_autogen10')).click();
        element.all(by.repeater('item in obligation.Jurisdiction.list')).get(value).click();
    }

    this.CentralObligation = function (value) {


        element.all(by.model('obligation.CentralObligation')).get(value).click();
    }
    this.Type = function (value) {


        element(by.id('s2id_txtReturnType')).click();
        element.all(by.repeater('item in obligation.ReturnType.list')).get(value).click();
    }
    this.Years = function (value) {


        element(by.id('s2id_txtTaxYear')).click();
        element.all(by.repeater('item in obligation.TaxYears.list')).get(value).click();
    }
    this.Periods = function (value) {


        element(by.id('s2id_txtPeriod')).click();
        element.all(by.repeater('tem in obligation.Periods.list')).get(value).click();
    }
    this.Forms = function (value) {


        element.all(by.id('s2id_txtForms')).get(0).click();
        element.all(by.repeater('item in obligation.Forms.list')).get(value).click();
    }
    this.Reports = function (value) {


        element.all(by.id('s2id_txtForms')).get(1).click();
        element.all(by.repeater('item in obligation.Reports.list')).get(value).click();
    }

    this.savebutton = function () {


        var saveb = element(by.css('button[title="Save"]'))
        browser.executeScript("arguments[0].scrollIntoView();arguments[1].click();", saveb, saveb);


    }

    module.exports = new mo();




   
spec.ts:                                                                         var mo = require("../page/mo.ts")
    var testData = require('../testdata/testdata.json');

    testData.forEach(function (data) {
        it('create and save master obligation', function () {
            browser.sleep(10000);
            mo.createbutton();
            browser.sleep(10000);
            mo.MasterObligationName(data.Master_Obligation_Name)
            mo.Module(data.Module);
           mo.Location(data.Location);
            mo.CentralObligation(data.Central_Obligation);
            mo.Type(data.Type);
            mo.Years(data.Years);
            mo.Periods(data.Periods);
            mo.Forms(data.Forms);
            mo.Reports(data.Reports);
            mo.savebutton();
        })

    })  
testdata.json: [{ "Master_Obligation_Name": "test_pom_fromjson", "Module": "2", "Location": "1", "Central_Obligation": "0", "Type": "2", "Years": "1", "Periods": "1", "Forms": "1", "Reports": "0" }, { "Master_Obligation_Name": "test_pom_fromjson_first", "Module": "2", "Location": "1", "Central_Obligation": "0", "Type": "2", "Years": "1", "Periods": "1", "Forms": "1", "Reports": "0" }]

1 个答案:

答案 0 :(得分:1)

您可以尝试两种选择:

1.在点击之前等待一段时间

2.每个ID号都是自动生成的,因此每次都可以更改,所以请使用其他定位器。

如果id是自动生成的,那么找一些其他定位器:

element.all(by.id('s2id_Jurisdiction')).get(1).click(); 
element.all(by.repeater('item in obligation.Jurisdiction.list')).get(value).click();
  

或者您也可以尝试以下方式:

var module = element(by.xpath("//input[starts-with(@id, 's2id_autogen')]")).get(1).click();

var location= element(by.xpath("//input[starts-with(@id, 's2id_autogen')]")).get(2).click();

var type = element(by.xpath("//input[starts-with(@id, 's2id_autogen')]")).get(3).click();