javascript Jasmine 规范超时。重置 WebDriver 控制流 - 重定向到新页面时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30787784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 12:41:46  来源:igfitidea点击:

A Jasmine spec timed out. Resetting the WebDriver Control Flow - when redirect to new page

javascriptangularjsprotractorangularjs-e2ee2e-testing

提问by Gleb

I'm beginner in the e2e testing and have a problem. When I do login - I make redirect from login.php to index.php page. But my test is fails with following errors:

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

My code:

我的代码:

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

So the question is how i can wait when my page will reload and check url?

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

UPD

UPD

I make an Ajax POST request and if login/pass are correct I do redirect to /index.php

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

UPD 2

更新版本 2

I have tried several constructions with browser.wait (browser.driver.wait) but without any success:

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

and

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

everything of these don't work :( BUTwhen I don't use browseror element- it works. E.g.:

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

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

So I guess that here is a problem when use browserand elementafter redirect.

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

Any thoughts?

有什么想法吗?

回答by Gleb

OK, I have resolved this issue by myself, but not sure why elementand browserdon't work. I have changed

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

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

to

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

So I have changed elementto browser.driver.findElementand everything works.

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

I don't know why it happens, but it works :)

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

UPD

UPD

As I understand, browser.driver.findElement- is not angular way and we should use it if we use non-angular pages. Although I use angular it seems that elementand browserdoesn't re-initialize. Some info I found here http://engineering.wingify.com/posts/angularapp-e2e-testing-with-protractor/#test-both-angularjs-and-non-angularjs-based-pages

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

回答by Jlearner

Jasmine has timeout for each spec i.e. each itin jasmine framework. so if any spec (it) exceeds jasmine spec's default timeout then it fails that spec.

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

Solution: To override jasmine spec timeout for one individual spec, pass a third parameter to it: it(description, testFn, timeout_in_millis)

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

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

more information on timeouts is here

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

回答by Dziamid

You are getting timeout because you've injected a param into itcallback and never called that (read more on that here). You should remove the param, or call it when the test is over. Basically, you don't do any custom async stuff, so you don't need it.

您正在超时,因为您已将一个参数注入it回调,但从未调用过它(在此处阅读更多相关信息)。您应该删除该参数,或者在测试结束时调用它。基本上,您不执行任何自定义异步操作,因此您不需要它。

So the question is how I can wait when my page will reload and check url?

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

I'd suggest that you waited for a specific element on index.phppage:

我建议您等待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!');
});

回答by Saurabh Prakash

 jasmineNodeOpts: { defaultTimeoutInterval: 260000 } 

for me this works after including this in conf.js

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

回答by Georgi Ekimov

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();
  • If you have more than one time the same text in body in array then
  • 如果数组中的 body 中有不止一次相同的文本,那么