10

我是 e2e 测试的初学者并且遇到了问题。当我登录时 - 我从 login.php 重定向到 index.php 页面。但我的测试失败并出现以下错误:

..A Jasmine spec timed out. Resetting the WebDriver Control Flow.
F

Failures:
1) Login Page should login and redirect to overview page with CR Operators rights.
  Message:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  Stack:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

3 specs, 1 failure

我的代码:

it('should login and redirect to overview page with CR Operators rights', function(sync) {
    element(by.model('username')).clear().sendKeys('testuser');
    element(by.model('password')).clear().sendKeys('test');
    element(by.css('[type="submit"]')).click();
    expect(browser.getLocationAbsUrl()).toMatch('/overview');
});

所以问题是我如何等待我的页面重新加载并检查 url?

UPD

我发出 Ajax POST 请求,如果登录/密码正确,我会重定向到 /index.php

更新 2

我用 browser.wait (browser.driver.wait) 尝试了几种结构,但没有任何成功:

it('should login and redirect to overview page with CR Operators rights', function(done) {
    element(by.model('username')).clear().sendKeys('testuser');
    element(by.model('password')).clear().sendKeys('test');
    element(by.css('[type="submit"]')).click();

    setTimeout(function() {
        expect(element(by.css('body')).getText()).toContain('Welcome Test User');
        done();
    }, 1000);
});

it('should login and redirect to overview page with CR Operators rights', function(done) {
    element(by.model('username')).clear().sendKeys('testuser');
    element(by.model('password')).clear().sendKeys('test');
    element(by.css('[type="submit"]')).click();

    browser.driver.wait(function() {
            return browser.driver.getCurrentUrl().then(function(url) {
                console.log(url);
                return (/overview/).test(url);
            });
        }, 5000);
    expect(browser.getLocationAbsUrl()).toMatch('/overview');
    done();
});

所有这些都不起作用:(但是当我不使用浏览器元素时-它起作用了。例如:

it('should login and redirect to overview page with CR Operators rights', function(done) {
    element(by.model('username')).clear().sendKeys('testuser');
    element(by.model('password')).clear().sendKeys('test');
    element(by.css('[type="submit"]')).click();

    setTimeout(function() {
        expect(true).toBe(true);
        done();
    }, 1000);
});

所以我想这是重定向后使用浏览器元素时的问题。

有什么想法吗?

4

5 回答 5

11

好的,我自己解决了这个问题,但不知道为什么元素浏览器不起作用。我改变了

expect(element(by.css('body')).getText()).toContain('Welcome Test User');

expect(browser.driver.findElement(by.css('body')).getText()).toContain('Welcome Test User');

所以我将元素更改为browser.driver.findElement并且一切正常。

我不知道为什么会这样,但它有效:)

UPD

据我了解,browser.driver.findElement - 不是角度方式,如果我们使用非角度页面,我们应该使用它。尽管我使用 angular,但似乎元素浏览器没有重新初始化。我在这里找到了一些信息 http://engineering.wingify.com/posts/angularapp-e2e-testing-with-protractor/#test-both-angularjs-and-non-angularjs-based-pages

于 2015-06-15T11:37:54.600 回答
7

Jasmine 对每个规范都有超时,即每个it在 jasmine 框架中。因此,如果任何规范 ( it) 超过了 jasmine 规范的默认超时,那么它就会失败该规范。

解决方案:要覆盖单个规范的 jasmine 规范超时,请将第三个参数传递给它: it(description, testFn, timeout_in_millis)

it('description of test case', function() {
   /*your test 
               steps
                    here*/
},120000);//120 seconds timeout for this spec

有关 超时的更多信息在这里

于 2016-10-27T11:21:17.650 回答
4

您正在超时,因为您已将参数注入it回调并且从未调用过(在此处阅读更多信息)。您应该删除参数,或在测试结束时调用它。基本上,你不做任何自定义异步的东西,所以你不需要它。

所以问题是我如何等待我的页面重新加载并检查 url?

我建议您等待index.php页面上的特定元素:

it('should login and redirect to overview page with CR Operators rights', function() {
    element(by.model('username')).clear().sendKeys('testuser');
    element(by.model('password')).clear().sendKeys('test');
    element(by.css('[type="submit"]')).click();
    browser.wait(protractor.until.elementLocated(by.css('Log out'));
    expect(element(by.css('body')).getText()).toContain('Hello, you are Logged In!');
});
于 2015-06-12T10:41:16.483 回答
1
 jasmineNodeOpts: { defaultTimeoutInterval: 260000 } 

对我来说,在 conf.js 中包含这个之后就可以了

于 2018-10-23T10:44:45.517 回答
0
expect(element(by.cssContainingText('body', 'Hello, you are Logged In!')).isPresent()).toBeTruthy();

expect(element.all(by.cssContainingText('body', 'Hello, you are Logged In!')).get(2).isPresent()).toBeTruthy();
  • 如果您在数组中的正文中有不止一次相同的文本,那么
于 2017-09-21T23:16:14.363 回答