【问题标题】:Error protractor and selenium : StaleElementReferenceError: stale element reference: element is not attached to the page document误差量突和硒:StaleElementRefectionError:陈旧元素参考:元素未连接到页面文档
【发布时间】:2018-08-21 04:29:00
【问题描述】:

我的测试 e2e 有问题。我正在阅读很多量角器和硒,但没有找到解决方案。

我的测试 e2e 是:

    it('Recorro tabla de MCSS, verificando que existan datos', function () {
        browser.sleep(1000);
        browser.ignoreSynchronization = true;
        logger.info("Recorro tabla de MCSS, verificando que existan datos");

        utils.awaitElement(element.all(by.repeater('item in alertsMCSS'))).then(function (rows) {
            rows.forEach(function (row) {
                row.all(by.tagName('td')).then(function (columns) {
                    utils.awaitElement(columns[0].getAttribute('innerText')).then(function (instancia) {
                        logger.info('MCSS: ' + instancia);
                        expect(instancia !== "");
                    });
                    utils.awaitElement(columns[1].getAttribute('innerText')).then(function (valor) {
                        logger.info('Valor: ' + valor);
                        expect(valor !== "");
                    });

                });
            });
        });
        browser.ignoreSynchronization = false;
    });

函数 utils.awaits(我找到了这个函数 here )但我不知道是否真的有效:

var awaitElement = function (item) {

    var EC = protractor.ExpectedConditions;
    var ee = item;
    browser.wait(EC.presenceOf(ee), 5000);
    expect(ee.isPresent()).toBeTruthy();
    return ee;
};

当我运行测试时,有时一切正常,有时测试失败。失败的描述是:

    StaleElementReferenceError: stale element reference: element is not attached to the page document 
 at WebDriverError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:27:5)
        at StaleElementReferenceError (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:227:5)
        at Object.checkLegacyResponse (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:505:15)
        at parseHttpResponse (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:509:13)
        at doSend.then.response (/usr/local/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/http.js:440:13)
        at process._tickCallback (internal/process/next_tick.js:109:7)
    From: Task: WebElement.getAttribute(innerText)

有什么问题?我该如何解决?

【问题讨论】:

标签: selenium testing protractor e2e-testing


【解决方案1】:

我会尝试使用一些内置的量角器功能来解决这个问题。我的猜测是您正在引用存在但因引用而丢失的元素。

代码片段

下面是我尝试使用一些内置的 Protractor API 重构上面的代码。

it('Recorro tabla de MCSS, verificando que existan datos', function () {
  browser.sleep(1000);
  browser.ignoreSynchronization = true;
  logger.info("Recorro tabla de MCSS, verificando que existan datos");

  // element all by repeater should only be used for AngularJS and not Angular
  // see element.all each methods link below.
  element.all(by.repeater('item in alertsMCSS')).each(function(rowElement) {

    // after we get each row, we could get all table data for that row
    columnElements = row.all(by.tagName('td');

    // use the element.all get method to get the first and second elements
    instanciaElement = columnElements.get(0);
    valorElement = columnElement.get(1);

    // instead of using innerHtml, think about using getText
    instanciaElement.getText().then(function(text) {
      logger.info('MCSS: ' + text);

      // use jasmine not tobe method. see link below.
      // expect(text !== "") is not a valid jasmine statement
      expect(text).not.toBe("");
    });
    valorElement.getText().then(function(text) {
      logger.info('Valor: ' + text);
      expect(text).not.toBe("");
    });
  });

  browser.ignoreSynchronization = false;
});

使用量角器 API

与其使用forEach,不如考虑使用element.all each 方法。循环浏览每一行后,您可以获得所有表数据元素的句柄。然后您可以通过索引将element.all get 用于特定元素。在我们有了想要记录和测试的元素之后,我们可以使用内置的getText 方法。由于这会返回一个 Promise,我们需要解决该 Promise 以获取文本。从那里我们应该考虑使用有效的茉莉花语句。请参阅期望 jasmine 文档here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    相关资源
    最近更新 更多