javascript - Protractor 等待页面完全加载

标签 javascript node.js angular jasmine protractor

在转到 https://docs.angularjs.org/tutorial 后,我正在尝试单击教程下的“0 - Bootstrapping” ,但是 Protractor 没有等待页面完全加载,所以我得到一个错误

Failed: Cannot read property 'click' of undefined

我尝试使用

手动等待页面加载

browser.driver.sleep(10000);

但我仍然遇到同样的错误。

conf.js

exports.config = {
// seleniumAddress: 'http://localhost:4444/wd/hub',
getPageTimeout : 12000000,
capabilities: {
    'browserName': 'chrome'
},
specs: ['todo-spec.js'],
jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 1440000,
}
};

tod​​o-spec.js

describe('angularjs homepage', function () {
it('should greet the named user', function () {
    var dropdown = element.all(by.css('.dropdown')).first(),
        dropdownToggle = dropdown.element(by.css('.dropdown-toggle')),
        dropdownMenu = dropdown.element(by.css('.dropdown-menu')),
        menuItem = dropdownMenu.all(by.tagName('li')).first();

    browser.get('http://www.angularjs.org');
    dropdownToggle.click();
    menuItem.click();

    expect(browser.getCurrentUrl()).toEqual('https://docs.angularjs.org/tutorial');
    //waiting 10 seconds for the page to fully load
    browser.driver.sleep(10000);
    browser.waitForAngular();
});
it('', async function () {

    /*  tried waiting for the element to be present
    var EC = protractor.ExpectedConditions;
    var e = element.all(by.css('.nav-index-listing'))[0];
    browser.wait(EC.presenceOf(e), 10000);

    Failed: Cannot read property 'isPresent' of undefined
    */

    //clicking '0 - Bootstrapping' under Tutorial
    element.all(by.css('.nav-index-listing'))[0].click();
    expect(element(by.cssContainingText('.pln', 'npm install')).isEnabled()).toBe(true);
    // Failed: Cannot read property 'click' of undefined

});
});

编辑

已替换

element.all(by.css('.nav-index-listing'))[0].click();
    expect(element(by.cssContainingText('.pln', 'npm install')).isEnabled()).toBe(true);

通过

        element.all(by.css('.nav-index-listing')).get(0).click();
    browser.wait(protractor.ExpectedConditions.urlIs("https://docs.angularjs.org/tutorial/step_00"), 5000);
    expect(browser.getCurrentUrl()).toEqual('https://docs.angularjs.org/tutorial/step_00');
    expect(element(by.cssContainingText('.pln', 'npm install')).isEnabled()).toBe(true);

出现错误

Failed: Wait timed out after 5001ms

如果我删除 browser.wait 我得到

Expected 'https://docs.angularjs.org/tutorial' to equal 'https://docs.angularjs.org/tutorial/step_00'.

最佳答案

首先 browser.driver.sleep(10000) 不是必需的。对于页面加载,您可以使用 protractor.ExpectedConditions。如果页面已经加载,它将减少不必要的等待 10 秒。

其次,您的代码中存在一个小错误,这是问题的根本原因。

改变这一行

element.all(by.css('.nav-index-listing'))[0].click();

为此

 element.all(by.css('.nav-index-listing')).get(0).click();

更新:

  1. element.all(by.css('.nav-index-listing')) 只会给你 li 将通过测试的元素。但是单击它不会将您带到所需的页面。
    将其更改为:

    element.all(by.css('.nav-index-listing a')).get(0).click();  
    
  2. 在这种情况下,[0] 将不起作用,因为返回值不是数组。它是 ElementArrayFinder 类型。所以,这就是为什么需要 .get(0)

  3. 如果您也检查以下条件,以进行测试会更好。

    browser.wait(EC.urlIs("https://docs.angularjs.org/tutorial/step_00"), 5000);    
    expect(browser.getCurrentUrl()).toEqual('https://docs.angularjs.org/tutorial/step_00');  
    

    这些条件检查您当前的 URL 是否为 https://docs.angularjs.org/tutorial/step_00

关于javascript - Protractor 等待页面完全加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53466646/

相关文章:

javascript - Jasmine 测试 AngularJS $on

javascript - 所请求的 URL 不允许使用该方法。 flask

javascript - sinon chai - 在全局范围内定义的期望值

javascript - 允许 electron `requires` 在 Vite 中原封不动地通过

node.js - 如何在 Node.js 中的 azure Active Directory 上获取登录用户的 token

angular - flex 容器中的响应列

javascript - 我无法安装 puppeteer

node.js - 尝试从 github 创建示例项目时出现 ionic beta 错误

Angular - 如何为可选输入属性设置默认值

javascript - Angular 8 : ternary operation for img src attribute?