量角器如何先运行登录测试脚本 [英] Protractor how to run login test script first

查看:20
本文介绍了量角器如何先运行登录测试脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用量角器测试 Angular 单页应用程序.我需要先运行登录脚本.只有这样我才能移动到其他路线,因为在路线更改时会检查 localStorage 中的令牌.

I am trying to test an Angular Single Page application with protractor. I need to run the login script first. Only then I can move to other routes since there is a check for token in localStorage on route change.

这种测试方法是否正确?在这种情况下,我需要先运行登录脚本.量角器是否允许控制规范文件的顺序.

Is this testing approach correct?. In that case I need to run the login script first. Does protractor allows to control the spec file order.

或者我应该通过对 localStorage 中的令牌进行硬编码来独立运行每个脚本(我应该在每次测试之前独立进行登录 api 调用吗).

Or should I run the each script independently by hardcoding the token in localStorage (Should I do login api call independently before each test).

我的登录脚本包含以下内容

My login script contains the following

it('Login with wrong email', function() {

})

it('Login with correct email', function() {

})

因此,在运行 使用正确的邮件登录 后,我将获得 accessToken,该 accessToken 将存储在 localStorage 中,我可以继续测试其他路由.这是正确的方法吗.如果不是,我如何通过端到端登录测试单个应用程序.

So after running the Login with correct mail I will get the accessToken which will get stored in localStorage and I can continue testing other routes. Is this the correct approach. If not how do I test a single application with login from end to end.

在量角器样式指南中提到

让您的测试相互独立

所以我应该在每次测试之前使用 beforeAll, beforeEach 来获取访问令牌并存储在 localStorage 中.在这种情况下,请告诉我该怎么做.

So should I use beforeAll, beforeEach to get the access token and store in localStorage before each test. In that case please explain me how to do it.

非常感谢任何帮助.

谢谢.

推荐答案

正如 Protractor 中突出显示的那样 FAQs,您可以在 conf 文件的 onPrepare 部分中指定您的登录代码.这是一个示例.

As highlighted in the Protractor FAQs, you can specify your login code in the onPrepare section of your conf file. Here is an example.

您也可以在 beforeAll 中实现这一点,但这会给您的测试脚本增加不必要的开销.更改 localStorage 是可能的,但不符合 e2e 测试的精神,因为 LS 可能会出错,您最终会想知道是您的应用程序还是您的 LS 修改代码导致了这种情况.

You can also achieve this in beforeAll but that adds unnecessary overhead to your test scripts. Altering the localStorage is possible, but not in the spirit of e2e testing since a lot can go wrong wrt LS, and you will end up wondering if it is your app or your LS modification code that caused this.

这篇关于量角器如何先运行登录测试脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆