Xcode UI测试-使用存储的凭据登录/注销 [英] Xcode UI testing - login/logout with stored credentials

查看:62
本文介绍了Xcode UI测试-使用存储的凭据登录/注销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的iOS应用(Xcode 7.2.1)中针对登录过程运行功能(UI)测试.

I want to run functional (UI) tests for the login procedure in my iOS app (Xcode 7.2.1).

该应用的行为是,成功登录后,将存储用户凭据,以便在下次启动时自动登录(不显示登录屏幕).

The app's behaviour is that upon successful login, user credentials are stored in order to automatically login (without showing the login screen) in the next launches.

因此,我在登录屏幕上设置了一系列UI事件,以使该应用程序首次在iOS模拟器中启动时通过登录测试. 但是,下一次我运行测试会失败,因为登录屏幕甚至没有按预期显示.

So I set up a sequence of UI events in the login screen to make the login test pass on the first time the app launches in the iOS Simulator. However, next times I run my tests will fail, since the login screen doesn't even show up as expected.

我在这里看到两个选项,但似乎都不合适:

I see two options here, none of them seem to fit well:

  1. 每次运行测试之前,请使用脚本重置iOS Simulator的内容和设置.我尝试使用xcrun simctl shutdown booted && xcrun simctl erase all && killall "Simulator"在测试目标的Build Phases中添加一个Run Script阶段,但它似乎不起作用(Simulator应用程序无法启动并且测试卡住了).
  2. -(void)tearDown中包含一些代码以清除存储的用户凭据.此选项不好,因为它不仅在每个测试方法之间运行(不是在每次测试启动时都运行),而且似乎我无权访问用于清除用户凭据的AuthManager类.
  1. Reset iOS Simulator's content and settings with a script before each time my tests run. I tried adding a Run Script phase in the test target's Build Phases with: xcrun simctl shutdown booted && xcrun simctl erase all && killall "Simulator", and it doesn't seem to work (Simulator app doesn't launch and tests get stuck).
  2. Include in the -(void)tearDown some code to clear the stored user credentials. This option is not good either as not only it's run between each test method (not per test launch), but also it seems like I don't have access to the AuthManager class that I use to clear user's credentials.

这样的UI测试登录过程时怎么办?

What do you do when UI-testing login procedures like that?

推荐答案

我遇到了相同类型的问题.经过一番抨击之后,我最好的方法是尝试使事情变得更简单.在泪流满面的时候,我总是放松"我可能做过的任何事情.有时它是过大的,但仍然是很好的做法.如果进行某种硬重置,在发现可能会忽略它们的地方时,我发现了许多错误. IE:我将导航回到主页(我的起点),如果我已经登录,那么我只是退出即可.作为记录,在应用程序侧,当用户注销时,其凭据将被剥夺.

I've ran into the same types of issues. After a bunch of bashing around my best approach was to try and keep things a little more simple. In my tear downs I always "unwind" anything that I might have done. Some times it's overkill but it's still good practice. I've found many bugs while unwinding where I might have overlooked them if I did some sort of hard reset. IE: I'll navigate back to the home page (my starting point) and if I've signed in then I'll simply just sign out. For the record, on the app side when a users signs out, their credentials are stripped.

例如,在我的SignInTests.swift类上,我将所有方法放在同一个类文件中的扩展名中.这样,我可以简单地调用SignInTests().signIn()或SignInTests().signOut(),这样我就可以从任何其他可能要调用signOut()的测试类中访问它们.

So for example, on my SignInTests.swift classes I put all my methods in an extension in the same class file. That way I can simply call SignInTests().signIn() or SignInTests().signOut() so that I can access them from whatever other test class I might to called signOut() from.

这是我的理想选择.可能不是您的最佳选择,但我希望它为您指明了正确的方向.

This is my scenario that works perfectly for me. May not be the best option for you but I hope it points you in the right direction.

这篇关于Xcode UI测试-使用存储的凭据登录/注销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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