Protractor e2e 测试登录重定向

2023-06-15前端开发问题
6

本文介绍了Protractor e2e 测试登录重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

目前有一个输入用户名/密码并点击登录"的部分端到端测试.

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.

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

New to this project but we are using OAuth.

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

更多细节:

spec.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('test@user.com');
      userPassField.sendKeys('1234');

      // Ensure fields contain what we've entered
      expect(userNameField.getAttribute('value')).toEqual('test@user.com');
      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.

编辑 - 稍微总结一下:

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

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

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

推荐答案

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

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

我怀疑您的感谢您已登录"页面会在超时后重定向 javascript.

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

所以当你点击登录"后,浏览器会加载谢谢你登录"页面,并且由于 .then() 的第二个参数什么都不做,browser.waitForAngular() 失败,因为该页面上没有角度.

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.

您应该尝试使用类似 browser.driver.wait() 之类的东西并设置合理的超时时间来检测 url 更改(此处描述:https://github.com/angular/protractor/issues/610) 并在浏览器之后触发 browser.waitForAngular()进入 /success 页面.

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 /success page.

这篇关于Protractor e2e 测试登录重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5