angularjs - 为什么多个 GET 请求无法通过 Protractor 测试?

标签 angularjs phantomjs protractor

我正在尝试使用 PhantomJS 浏览器在 Amazon EC2 环境中运行 Protractor 测试。我知道不建议使用 Protractor 来使用 PhantomJS,但这是我现在唯一的选择。测试始终在 Win7 环境下工作。

当测试看起来像这样它总是工作正常

    describe('Portal Login End 2 End test', function() {
    it('should automatically redirect to login /form', function() {
        browser.get('');
        expect(browser.getLocationAbsUrl()).toMatch("/login");
    });

    it('should automatically redirect to dashboard page after successful login', function() {
        element(by.model('user.username')).sendKeys('admin');
        element(by.model('user.password')).sendKeys('admin');
        element(by.buttonText('Sign in')).click();
        expect(browser.getLocationAbsUrl()).toMatch("dashboard/home");
    });
});

但是当像这样在 beforeEach 函数中发出 GET 请求时
    describe('Portal Login End 2 End test', function() {
    beforeEach(function() {
        browser.get('');
    }
    it('should automatically redirect to login', function() {
        expect(browser.getLocationAbsUrl()).toMatch("/login");
    });

    it('should automatically redirect to dashboard page after successful login', function() {
        element(by.model('user.username')).sendKeys('admin');
        element(by.model('user.password')).sendKeys('admin');
        element(by.buttonText('Sign in')).click();
        expect(browser.getLocationAbsUrl()).toMatch("dashboard/home");
    });
});

大多数情况下测试失败,但并非总是如此,并出现以下错误

UnknownError: Error communicating with the remote browser. It may have died. Build info: version: '2.43.1', revision: '5163bce', time: '2014-09-10 16:27:33' System info: host: 'ip-10-249-98-182', ip: '10.249.98.182', os.name: 'Linux', os.arch: 'amd64', os.version: '2.6.32-504.el6.x86_64', java.version: '1.6.0_45' Driver info: driver.version: EventFiringWebDriver



所以当 两个 GET 请求是在同一个测试中发出的,它最常失败,但当只发出一个请求时,它总是有效。正如我上面写的,两个 GET 请求在 Win7 环境中可以正常工作,但在 Linux 中则不行。

我看过一些帖子有这个错误,但没有看到如何修复它。有人有解决方案吗?

最佳答案

如果你有问题,正如你所说,是因为浏览器没有等待 beforeEach 中的 get,添加 browser.waitForAngular();

describe('Portal Login End 2 End test', function() {
beforeEach(function() {
    browser.get('');
}
it('should automatically redirect to login', function() {
    browser.waitForAngular();
    expect(browser.getLocationAbsUrl()).toMatch("/login");
});

it('should automatically redirect to dashboard page after successful login', function() {
    element(by.model('user.username')).sendKeys('admin');
    element(by.model('user.password')).sendKeys('admin');
    element(by.buttonText('Sign in')).click();
    expect(browser.getLocationAbsUrl()).toMatch("dashboard/home");
});
});

因为 it 测试按顺序运行,所以您不必在每个 it 块中调用它。

您也可以尝试在 beforeEach 函数中调用 browser.waitForAngular(),尽管我更喜欢在 it 函数中进行调用。

关于angularjs - 为什么多个 GET 请求无法通过 Protractor 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26976547/

相关文章:

angularjs - 在多个 http 调用中处理 Spinner

javascript - 在发送之前过滤 $http 请求数据

html - 从 url 获取图像文件对象

javascript - 使用 UnCSS 传递表单数据

docker - 当在 docker 和 chrome 浏览器中运行的 Protractor 处于 headless 模式时,然后得到 "element not visible"

javascript - Protractor :失败:row.findElement 不是函数

javascript - 将动态 CSS 样式应用于谷歌地图的标记

javascript - 无法使用 casperjs 评估和 __doPostBack 函数导航

javascript - CasperJS 和警告框

javascript - Protractor 断言元素不可见