Javascript 量角器 e2e 测试登录重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34009256/
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-08-23 15:48:03  来源:igfitidea点击:

Protractor e2e Tests Login Redirection

javascriptangularjsoauthprotractore2e-testing

提问by bruh

Currently have a partial end-to-end test that enters a username/password and clicks 'sign in'.

目前有一个部分端到端测试,输入用户名/密码并单击“登录”。

It does that successfully, but concludes at a "thanks you're logged in" page, instead of being redirected to the 'account portal' or 'dashboard', the way it would if I logged in through the browser.\

它成功地做到了这一点,但在“感谢您登录”页面结束,而不是像我通过浏览器登录那样被重定向到“帐户门户”或“仪表板”。\

New to this project but we are using OAuth.

这个项目的新手,但我们正在使用 OAuth。

Main question: Does this sound like a need for http mocking?

主要问题:这听起来像是需要 http 模拟吗?

Further details:

更多细节:

spec.js

规范.js

describe('login page', function() {
    browser.driver.get('http://url.path/login');
    it('should render login page', function() {

      // Checking the current url
      var currentUrl = browser.driver.getCurrentUrl();
      expect(currentUrl).toMatch('/login');
    });
    it('should sign in', function() {

      // Find page elements
      var userNameField = browser.driver.findElement(By.id('username'));
      var userPassField = browser.driver.findElement(By.id('password'));
      var userLoginBtn  = browser.driver.findElement(By.id('loginbtn'));

      // Fill input fields
      userNameField.sendKeys('[email protected]');
      userPassField.sendKeys('1234');

      // Ensure fields contain what we've entered
      expect(userNameField.getAttribute('value')).toEqual('[email protected]');
      expect(userPassField.getAttribute('value')).toEqual('1234');

      // Click to sign in - waiting for Angular as it is manually bootstrapped.
      userLoginBtn.click().then(function() {
        browser.waitForAngular();
        expect(browser.driver.getCurrentUrl()).toMatch('/success');
      }, 10000);
    });
});

If I quickly click on the testing window, I can see it successfully reaches the 'success' page - but it does not redirect to the dashboard (it redirects when you manually sign in, through the browser). How can I continue this test to remain signed in and access the dashboard like a user would?

如果我快速单击测试窗口,我可以看到它成功到达“成功”页面 - 但它不会重定向到仪表板(它会在您通过浏览器手动登录时重定向)。如何继续此测试以保持登录状态并像用户一样访问仪表板?

// New to the project, angular and and protractor.

// 项目的新手,角度和量角器。

EDIT - Summarizing this a bit:

编辑 - 总结一下:

  • I would like protractor to begin tests on the /loginpage
  • Protractor should find and fill out the username and password fields, then click Login
  • Protractor successfully log in, to see a /thankyoupage, then immediately redirect to the user's /dashboardpage
  • Maybe I'm missing a step, do we need to manually redirect in Protractor tests?
  • 我希望量角器在/login页面上开始测试
  • 量角器应该找到并填写用户名和密码字段,然后单击登录
  • 量角器成功登录,看到一个/thankyou页面,然后立即重定向到用户的/dashboard页面
  • 也许我错过了一个步骤,我们是否需要在量角器测试中手动重定向?

(When a user manually logs in through the browser, they don't see the /thankyoupage - it's a quick redirect to /dashboard. Protractor does not reach the dashboard page.

(当用户通过浏览器手动登录时,他们看不到/thankyou页面 - 这是一个快速重定向到/dashboard。量角器不会到达仪表板页面。

采纳答案by Eugene

Your post lacks information but I'll try to make an assumption:

您的帖子缺乏信息,但我会尝试做一个假设:

I suspect that your "thanks you're logged in" page makes javascript redirect after a timeout.

我怀疑您的“感谢您已登录”页面在超时后使 javascript 重定向。

So after you click "Login", the browser loads "thanks you're logged in" page, and since the second parameter to .then()does nothing, browser.waitForAngular()fails because there is no angular on that page.

因此,在您单击“登录”后,浏览器会加载“感谢您已登录”页面,并且由于第二个参数 to.then()什么都不做,因此browser.waitForAngular()失败,因为该页面上没有角度。

You should try to use something like browser.driver.wait()with a reasonable timeout to detect url change (described here: https://github.com/angular/protractor/issues/610) and trigger browser.waitForAngular()after the browser get to /successpage.

您应该尝试使用类似browser.driver.wait()合理超时的方法来检测 url 更改(此处描述:https: //github.com/angular/protractor/issues/610)并browser.waitForAngular()在浏览器到达/success页面后触发。

回答by flaviomeira10

Have you tried using an explicit wait?

您是否尝试过使用显式等待?

    return browser.driver.wait(function() {
        return browser.driver.getCurrentUrl().then(function(url) {
            return /success/.test(url);
        });
    }, 10000);

Your code would be like that:

你的代码是这样的:

  // Click to sign in - waiting for Angular as it is manually bootstrapped.
  userLoginBtn.click();
  return browser.driver.wait(function() {
        return browser.driver.getCurrentUrl().then(function(url) {
            return /success/.test(url);
        });
  }, 10000);