【问题标题】:A Jasmine spec timed out. Resetting the WebDriver Control Flow - when redirect to new pageJasmine 规范超时。重置 WebDriver 控制流 - 重定向到新页面时
【发布时间】:2015-08-27 13:28:26
【问题描述】:

我是 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

UPD 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();
});

所有这些都不起作用:( 但是当我不使用 browserelement - 它起作用了。例如:

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);
});

所以我猜这是重定向后使用 browserelement 时的问题。

有什么想法吗?

【问题讨论】:

    标签: javascript angularjs protractor angularjs-e2e e2e-testing


    【解决方案1】:

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

    所以问题是我如何等待我的页面重新加载并检查 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!');
    });
    

    【讨论】:

    • 嗨@Dziamid,我已经更新了问题。敬请期待。也许你有什么想法?
    【解决方案2】:

    好的,我自己解决了这个问题,但不知道为什么 elementbrowser 不起作用。 我变了

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

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

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

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

    UPD

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

    【讨论】:

    • 我花了一整天的时间来尝试简单的登录工作,但做不到。只是我只有在看到你的帖子后才能让它工作。非常感谢朋友。
    【解决方案3】:

    Jasmine 对每个规范都有超时,即 jasmine 框架中的每个 it。因此,如果任何规范 (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
    

    更多关于timeouts is here的信息

    【讨论】:

      【解决方案4】:
      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();
      
      • 如果数组中的正文中有不止一次相同的文本,那么

      【讨论】:

      • 正确的格式会增加您的建议的可读性。
      【解决方案5】:
       jasmineNodeOpts: { defaultTimeoutInterval: 260000 } 
      

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

      【讨论】:

        猜你喜欢
        • 2019-10-31
        • 1970-01-01
        • 2015-05-14
        • 1970-01-01
        • 1970-01-01
        • 2018-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多