为什么多个GET请求失败了量角器测试?

Ola*_*Ola 6 phantomjs angularjs protractor

我正在尝试使用PhantomJS浏览器在Amazon EC2环境中运行量角器测试.我知道不建议使用带有量角器的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");
    });
});
Run Code Online (Sandbox Code Playgroud)

但是当像这样在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");
    });
});
Run Code Online (Sandbox Code Playgroud)

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

UnknownError:与远程浏览器通信时出错.它可能已经死了.构建信息:版本:'2.43.1',修订版:'5163bce',时间:'2014-09-10 16:27:33'系统信息:主机:'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.version:EventFiringWebDriver

因此,当在同一测试中发出两个 GET请求时,它通常会失败,但是当只有一个请求发出时它始终有效.正如我上面所写,两个GET请求在Win7环境中运行良好,但在Linux中运行不正常.

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

小智 0

如果您遇到问题,正如您所说,是因为浏览器没有等待 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");
});
});
Run Code Online (Sandbox Code Playgroud)

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

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