javascript - browser.navigate().back();导致 "document unloaded while waiting for result"错误

标签 javascript automated-tests protractor

你好, 我已经阅读了平台上的所有问题/答案,但没有一个能帮助我解决我的问题,问题是我有以下测试脚本:

it('Verify user sees the home page correctly when hitting back button', function () {
    browser.get('/flight/los-angeles/');
    expect(testUtils.getTextFromField(topSearchBarPage.airportDestLocator)).toContain("LAX");
    element.all(by.css(".day-droppable.ui-droppable")).get(30).click();
    element(by.css(".btn-primary")).click();
    browser.navigate().back();
    expect(browser.getTitle()).toMatch('page title');
    expect(browser.getCurrentUrl()).toContain("/flight/los-angeles/");
});

然后我收到此错误:

UnknownError: javascript error: document unloaded while waiting for result

我可以看到(因为我已经设置了 sleep ),在执行 navigate.back() 后,测试立即尝试重新加载页面,并且当它尝试执行期望时,它抛出错误。

这是应用程序在导航后的样子。

reload

我已在 GitHub 中发布了该问题,以下是最重要的信息:

I had removed that step (the navigate back) and the expect function works, I had placed a browser.sleep(5000) right after the navigate.back() and that's how I found that it's trying to execute the next "expecs" on an "empty page".

然后,sjelin 添加了这个

I'm pretty sure the issue is actually just that element(by.css(".btn-primary")).click(); hasn't finished before browser.navigate().back(); happens. Try adding a browser.waitForAngular()

最后我评论了这一点。

@sjelin I've tried what you told and it does not work, in fact, the clicking action does happens and finishes when it should since I'm seeing the action being done while the test runs... so, I think that the problem here is the navigate().back() is opening or refreshing without URL, and the next actions are the ones throwing the errors.

最佳答案

如果您明确等待 click() 完成会怎样:

element(by.css(".btn-primary")).click().then(function () {
    browser.navigate().back();

    expect(browser.getTitle()).toMatch('page title');
    expect(browser.getCurrentUrl()).toContain("/flight/los-angeles/");
});

关于javascript - browser.navigate().back();导致 "document unloaded while waiting for result"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33111081/

相关文章:

javascript - 如何从 javascript 发送多个单选按钮组值

selenium - Katalon 工作室无法设置文本(根本原因 : Element is not currently interactable and may not be manipulated

javascript - 使用 nightwatchjs 测试包含 CodeMirror 组件的页面

css - 将样式注入(inject)测试页面

javascript - Protractor 不能使用以此声明的本地属性。 .then 语句内

javascript - Angular 仅显示结果数据数组的一部分

javascript - 带有 Java 后端的 meanJS

javascript - 未捕获的类型错误 : undefined is not a function

javascript - 我们可以将正则表达式放在 Protractor 的 exports.config 的 seleniumServerJar 中吗

protractor - 在 Protractor 中按顺序运行测试用例