如何在量角器测试中使用 browser.getCurrentUrl()? [英] How to use browser.getCurrentUrl() in a protractor test?

查看:106
本文介绍了如何在量角器测试中使用 browser.getCurrentUrl()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天一直在为这些 Protractor 代码行苦苦挣扎:

I have been struggling with these lines of Protractor code today:

element(by.linkText("People")).click();
browser.waitForAngular();        
var url = browser.getCurrentUrl();
...

似乎 getCurrentUrl 放在 waitForAngular() 语句之后时总是失败.

It appears that getCurrentUrl always fails when placed after a waitForAngular() statement.

错误输出太模糊:

UnknownError:javascript 错误:文档在等待结果时已卸载

UnknownError: javascript error: document unloaded while waiting for result

那么,单击超链接并检查新网址的正确方法是什么?

So, what is the correct way to click on a hyperlink and check the new url?

这是我的测试:

如果我在点击链接之前getCurrentUrl()

If I getCurrentUrl() before the link is clicked,

it('can visit people page', function () {
    var url = browser.getCurrentUrl();
    element(by.linkText("People")).click();
    expect(true).toBe(true);
});

测试会通过.

如果我在点击链接后getCurrentUrl()

it('can visit people page', function () {
    var url = browser.getCurrentUrl();
    element(by.linkText("People")).click();
    expect(true).toBe(true);
    url = browser.getCurrentUrl();
});

Protractor 中抛出一个错误,上面的 UnknownError 输出.出了什么问题?

An error is thrown in Protractor with the UnknownError output above. What went wrong?

推荐答案

代替waitForAngular()调用,等待URL改变:

browser.wait(function() {
  return browser.getCurrentUrl().then(function(url) {
    return /index/.test(url);
  });
}, 10000, "URL hasn't changed"); 

最初由 @juliemr 在 UnknownError: javascript error: document unloaded while waiting for result 提出.

Originally suggested by @juliemr at UnknownError: javascript error: document unloaded while waiting for result.

这篇关于如何在量角器测试中使用 browser.getCurrentUrl()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆