量角器在页面对象中获取陈旧元素引用

时间:2018-04-24 22:06:42

标签: javascript for-loop protractor iteration

我正在验证页面中的过滤器选项。每当我选择一个选项时,我都会提交过滤器,然后页面会刷新新结果。我再次单击过滤器按钮,需要选择下一个单选按钮。但我得到陈旧的元素引用错误。

filterButton.click().then(function () {
  filteroptions.count().then(function (totalradios) {
    console.log("before start" + totalradios);
    filteroptions.each(function (radiooption, index) {
      radiooption.getText()  //getting error at this place on the second iteration
       .then(function (radioName) {
        console.log(radioName, index);
      })
      radiooption.click().then(() => {
        filterSubmit.click().then(function () {
          console.log(totalradios, ' ', index)
          if (index <= totalradios) {
            filterButton.click();
          }
        })
      })
    })
  })
})

从每个(功能)更新为For循环

 var filteroptions = element.all(by.css("md-radio-group.ng-pristine.ng-untouched.ng-valid._md.layout-row.ng-not-empty md-radio-button")); 

filterButton.click().then(function () {
  filteroptions.count().then(function (totalradios, async) {
   console.log("before start" + totalradios);
   for (var i = 0; i < totalradios; i++) {
   console.log("recalulated true " + totalradios);
   (function (index) {
    console.log("select filter option");
    filteroptions.get(index).click();
    console.log("click submit button");
    filterSubmit.click();
    console.log("click filter button");
    filterButton.click();
    console.log("get new totalcounts");
    let setradios = filteroptions.count().then((value) => {
    return value;
   })
   console.log("recalulated totalradios" + setradios);
   totalradios = setradios;
   })(i);
  }
  })
})

试图在单独的函数中读取列表

var getfiltercount = function () {
  filteroptions = element.all(by.css("md-radio-group.ng-pristine.ng-untouched.ng-valid._md.layout-row.ng-not-empty md-radio-button")); // filtertext
  return filteroptions.count().then(function (addradios) {
  console.log("addradios is " + addradios);
 })
  return addradios;
}

1 个答案:

答案 0 :(得分:1)

因为硒也会在下面的情况下进入新页面:

  • 页面整个刷新,甚至页面网址都不会更改
  • 页面部分刷新,甚至页面网址都不会更改

一旦selenium检测到进入新页面,前一页上找到的元素参考无效,如果您仍在脚本中使用它们,selenium将报告 StaleReferenceException。

要解决此问题,很容易在新页面上再次找到该元素以获取新参考以供日后使用。

//选择yes单选按钮

时要测试的功能
function testYesRaidoButton() {
    // click `yes` radio button
    element(<locator of `yes` raido button>).click();
    // find how many new radio buttons will appear after choose `yes`
    element.all(<locator of sub radio buttons of `yes`>).count()

    .then(function (totalradios) {
        console.log("before start " + totalradios);

        for(var i=0;i<totalradios;i++) {

            (function(index) {

                // click one of new radio button of `yes`
                element.all(<locator of sub radio buttons of `yes`>).get(index).click();

                // click `submit` button
                element(<locator of filterSubmit>).click();

                // click `yes` radio button on `new page` again
                element(<locator of `yes` raido button>).click();

            })(i);

        }
    });
}

//选择no单选按钮

时要测试的功能
function testNoRadioButton() {
    // click `no` radio button
    element(<locator of `no` raido button>).click();
    // find how many new radio buttons will appear after choose `no`
    element.all(<locator of sub radio buttons of `yes`>).count()

    .then(function (totalradios) {
        console.log("before start " + totalradios);

        for(var i=0;i<totalradios;i++) {

            (function(index) {

                // click one of new radio button of `no`
                element.all(<locator of sub radio buttons of `no`>).get(index).click();

                // click `submit` button
                element(<locator of filterSubmit>).click();

                // click `no` radio button on `new page` again
                element(<locator of `no` raido button>).click();

            })(i);

        }
    }); 
}

实际上,这两个函数有许多相似的代码行, 您可以优化并合并为一个函数以接受不同的参数值来存档相同的目的。

通过使用两个循环来存档某些目标的替代解决方案,但不建议这样做,因为代码很难阅读和理解,非常不友好。

// iterate `yes` and 'no'
for (var j = 0; j < 2; j++) {

  (function(__index) {

    element.all(<locator of `yes` and `no` radio button>).get(__index).click();

    element.all(<locator of new radio buttons>).count()

    .then(function(totalradios) {
      console.log("before start " + totalradios);

      // iterate new radio buttons
      for (var i = 0; i < totalradios; i++) {

        (function(index) {

          // click one of new radio buttons
          element.all(<locator of new radio buttons>).get(index).click();

          // click `submit` button
          element(<locator of filterSubmit> ).click();

          // click `yes` or `no` radio button on `new page` again
          element.all(<locator of `yes` and `no` radio button>).get(__index).click();

        })(i);

      }
    });

  })(j)

}