【问题标题】:Why Protractor can't find element(by.id('mat-input-0'))?为什么量角器找不到元素(by.id('mat-input-0'))?
【发布时间】:2019-10-07 04:28:15
【问题描述】:

我是自动化新手,我正在运行一个非常基本的测试,它运行良好,但最近它停止工作,我不断收到以下错误:

失败:脚本超时:11 秒内未收到结果

我查看了它并进行了一些基本调试,发现 Protractor 没有找到我所指的元素 (element(by.id('mat-input-0')) - 更多信息请参见 spec.js 代码文件。
我正在使用最新版本的 Protractor (5.4.2),以及 Jasmine 最新版本 (3.4.0)。我正在最新的 chrome 浏览器上进行测试。

我尝试了很多在线解决方案(例如使用 async/await,在配置文件中添加长 allScriptsTimeoutdefaultTimeoutInterval(如下所示),使用不同的定位器调用元素(通过 xpath、id、tagName、 ...),尝试browser.waitForAngular();browser.ignoreSynchronization = true,...)但没有奏效,所以我想知道是否有人对可能的解决方案有任何意见? (或者真正的问题是什么?)

元素 HTML

<input _ngcontent-mkt-c2="" class="mat-input-element mat-form-field-autofill-control cdk-text-field-autofill-monitored ng-pristine ng-invalid ng-touched" formcontrolname="email" matinput="" name="email" placeholder="Email" required="" type="text" id="mat-input-0" aria-invalid="true" aria-required="true">

Spec.js:

describe('Login', function(){
    it('test 1', async function(){
        await browser.get('the STG URL im testing');


    })
    it('set username', async function(){
        await element(by.id('mat-input-0')).sendKeys('root@user.com');


    })
    it('set password', async function(){
        await element(by.id('mat-input-1')).sendKeys('1234');
    })

})

Conf.js:

exports.config = {  
    seleniumAddress: 'http://localhost:4444/wd/hub',
    allScriptsTimeout: 80000,

    framework: 'jasmine',

    specs: ['spec.js'],

    SELENIUM_PROMISE_MANAGER: false,

    jasmineNodeOpts: {
      defaultTimeoutInterval: 50000
    }
  };

【问题讨论】:

  • browser.waitForAngular() 不等同于browser.ignoreSynchronization 你可能会想到browser.waitForAngularEnabled()。您可以发布您尝试定位的对象的 HTML 吗?
  • @DublinDev 我猜browser.waitForAngular() 会和browser.waitForAngularEnabled(true) 一样吗? - 如果我错了纠正我。这是我试图定位的字段的 HTML:&lt;input _ngcontent-mkt-c2="" class="mat-input-element mat-form-field-autofill-control cdk-text-field-autofill-monitored ng-pristine ng-invalid ng-touched" formcontrolname="email" matinput="" name="email" placeholder="Email" required="" type="text" id="mat-input-0" aria-invalid="true" aria-required="true"&gt;
  • 我确实通过使用browser.waitForAngularEnabled(false) 解决了这个问题,但我读到不建议使用它,并且之后的测试会变得不稳定......所以我正在研究另一种解决方案......我想知道我如何知道我的页面上尚未稳定的内容? (它超过了Angular 7)。 @都柏林开发
  • waitForAngularEnabled(true) 告诉 Protractor 你希望它等到页面上的所有 Angular 调用都完成并且页面在 every 操作之前稳定。 Mu 的理解是,如果设置了waitForAngular(),将在尝试任何交互之前调用。
  • 您对禁用waitForAngularEnabled 的缺点是正确的。根据您的最后评论,您的问题似乎与量角器没有变得“稳定”有关。你能在 chrome 开发工具上打开控制台并运行以下命令getAllAngularTestabilities()。当您展开结果时,您可能会看到属性hasPendingMacrotasks: falsehasPendingMicrotasks: true。是这样吗?

标签: javascript jasmine protractor


【解决方案1】:

您收到错误可能有很多原因,但正如您所说,您是量角器的新手。我会说首先采取简单的步骤。

在全球安装量角器后。您的机器必须在 %appdata% 中有文件夹。转到目录路径 AppData\Roaming\npm\node_modules\protractor\example。在此目录中,您必须看到两个文件 conf.js 和 example_spec.js 。在规范文件中,将 url 替换为您拥有的 url,并对测试执行相同操作。

运行配置文件而不对其进行任何更改。并检查它是否工作正常然后有一些配置问题。

请通过Protractor learning了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多