【问题标题】:Cannot access 'element' before initialization in Protractor在量角器中初始化之前无法访问“元素”
【发布时间】:2021-07-08 09:30:36
【问题描述】:

我正在尝试使用 Protractor 为 Angular 应用程序编写端到端测试。我正在尝试创建一个函数,该函数在访问之前等待元素出现在页面上,但出现错误:

ReferenceError: 在初始化之前无法访问“元素”

虽然我确实需要量角器中的元素。

函数在文件global.js:

const { browser, element } = require('protractor');

const global = function () {
  this.getElementAsync = (element, waitTime) =>
    browser.wait(() => element.isPresent(), waitTime)
    .then(() => browser.wait(() => element.isDisplayed(), waitTime));
  }
    
module.exports = new global();

我正在另一个文件中使用它:

const global = require('./global');
const { element } = require('protractor');
    
var someFile = function () {
  this.doWhatever = async function(sec) {
    const element = await global.getElementAsync(element(by.css('.some.css')), 5000); //error is thrown here
    return element.click();
  };
}
    
module.exports = new someFile();

我愿意接受所有建议/建议。

【问题讨论】:

    标签: angular async-await protractor end-to-end


    【解决方案1】:

    你在这一行有一个变量名冲突:

    const element = await global.getElementAsync(element(by.css('.some.css')), 5000); //error is thrown here
    

    element(by.css('.some.css') 正在尝试引用 const element 而不是从量角器导入的 element,因此出现 ReferenceError 消息。

    尝试将您的变量名称更改为其他名称,它应该可以工作。

    E.x:

    const someElement= await global.getElementAsync(element(by.css('.some.css')), 5000);
    

    【讨论】:

    • 是的,我完全忽略了保留字的使用。 :sigh: 谢谢指正和提醒,现在很好用!
    猜你喜欢
    • 2021-07-02
    • 1970-01-01
    • 2020-12-29
    • 2021-09-10
    • 2020-10-23
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多