量角器如何首先运行登录测试脚本 [英] Protractor how to run login test script first
问题描述
我正在尝试使用量角器测试Angular Single Page应用程序.我需要先运行登录脚本.只有这样,我才能转到其他路线,因为在更改路线的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() {
})
因此,在运行Login with correct mail
之后,我将获得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.
在量角器样式指南中,它被称为
使测试彼此独立
Make your tests independent from each other
所以我应该使用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.
非常感谢您的帮助.
谢谢.
推荐答案
如量角器中突出显示的常见问题解答,您可以在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屋!