1
00:00:05,810 --> 00:00:10,760
Everybody can appear with deps slopes dot.com and in this video we're going to be talking very quickly

2
00:00:11,030 --> 00:00:14,210
about the apps state when you're testing.

3
00:00:14,240 --> 00:00:19,790
Now of course if you have an app that deals with something like core data or user defaults persisting

4
00:00:19,790 --> 00:00:22,280
data to an app of course it persists.

5
00:00:22,280 --> 00:00:25,370
So it stays there even if you quit the app and come back in.

6
00:00:25,370 --> 00:00:30,170
So what we're going to be doing is we're going to be looking at how to actually clear out that persistent

7
00:00:30,170 --> 00:00:34,730
data when testing so that we can have a fresh app state every time.

8
00:00:34,730 --> 00:00:39,140
So pull open your project here this is where we left off last time and I'm actually going to go ahead

9
00:00:39,170 --> 00:00:44,420
and get rid of this test with the alert because we don't actually need that because you know we're not

10
00:00:44,420 --> 00:00:45,780
going to actually have that alert.

11
00:00:45,800 --> 00:00:52,760
And with that in mind we can also get rid of this code so that it just sets user defaults and then dismisses.

12
00:00:52,790 --> 00:00:58,700
Now basically what I have done I already sort of explain this but I've basically set a parameter in

13
00:00:58,700 --> 00:01:02,320
user defaults that is based off of this extension here.

14
00:01:02,720 --> 00:01:05,960
And it's a parameter called onboarding comp..

15
00:01:06,230 --> 00:01:13,490
And basically it's a value that gets the property here from wherever it's called and it's then set in

16
00:01:13,490 --> 00:01:14,370
user defaults.

17
00:01:14,480 --> 00:01:17,170
Using this nice little extension whenever we call it.

18
00:01:17,270 --> 00:01:18,410
Pretty cool.

19
00:01:18,410 --> 00:01:23,560
So we're going to set it to true when the onboarding is done and it's set false by default.

20
00:01:23,600 --> 00:01:29,220
So when we load up our VC here it's going to basically say Hey check it out.

21
00:01:29,270 --> 00:01:34,930
If user defaults onboarding complete is not true will present the onboarding VC.

22
00:01:34,940 --> 00:01:36,550
Otherwise don't worry about it.

23
00:01:36,680 --> 00:01:42,020
So what we're going to do is we're actually going to make sure that every time our app launches we have

24
00:01:42,020 --> 00:01:47,300
a fresh state and to do that we're going to go into the app delegate and we're going to be putting a

25
00:01:47,300 --> 00:01:53,990
little function right here up at the top called reset state.

26
00:01:54,210 --> 00:01:56,900
Now of course that function is not yet written.

27
00:01:57,060 --> 00:02:03,040
But what we're going to do is we're going to actually utilize something called Konsole arguments.

28
00:02:03,090 --> 00:02:04,110
OK.

29
00:02:04,110 --> 00:02:09,030
Now in our tests what we're going to need to do is we're going to actually need to set it up so that

30
00:02:09,030 --> 00:02:16,650
when ever our tests run we can basically append a little message to the end of our test to kind of set

31
00:02:16,650 --> 00:02:21,720
it apart and and give it an identifier so that we can identify that specifically in our app delegate.

32
00:02:21,720 --> 00:02:26,490
Let me show you what I mean and we're basically going to say in the setup function every single time

33
00:02:26,940 --> 00:02:31,290
app dot and there is a property here called Lunch arguments.

34
00:02:31,290 --> 00:02:31,820
OK.

35
00:02:31,980 --> 00:02:35,900
Now these are the arguments that will be passed to the application at launch.

36
00:02:36,060 --> 00:02:41,550
So go ahead and call apt at launch arguments and we're going to append a string to the end of every

37
00:02:41,550 --> 00:02:41,990
argument.

38
00:02:42,000 --> 00:02:45,280
So type dot append and we can add a string.

39
00:02:45,420 --> 00:02:50,480
So what we're going to do is we're going to append this dash dash UI testing.

40
00:02:50,820 --> 00:02:57,870
And that way every time a particular launch or every time our app launches we end up with this appended

41
00:02:58,140 --> 00:03:00,000
to the launch arguments.

42
00:03:00,000 --> 00:03:06,270
And what that is going to allow us to do is in our app delegate every time the app finishes launching

43
00:03:06,570 --> 00:03:14,940
we're going to go ahead and check if command line k that's a class we can access command line dot arguments.

44
00:03:15,100 --> 00:03:22,980
Ok this is where they print out Dom contains and check it out we can type in dot dash dash UI testing.

45
00:03:23,550 --> 00:03:30,090
If the command line arguments ever contain UI testing we know that we need to reset the state and the

46
00:03:30,090 --> 00:03:35,520
cool thing is that every time our tests run this we'll get called once so we'll say hey the app launched

47
00:03:36,040 --> 00:03:38,140
UI testing reset the state.

48
00:03:38,270 --> 00:03:40,470
So every time it launches it'll have a fresh state.

49
00:03:40,470 --> 00:03:43,650
Now let's actually write this function because right now it does nothing.

50
00:03:43,680 --> 00:03:52,720
So down at the bottom of your app delegate here type phunk reset state and let's give some more space

51
00:03:52,720 --> 00:03:54,310
here at the bottom.

52
00:03:54,310 --> 00:03:56,830
Let's make sure that we can actually see everything.

53
00:03:57,100 --> 00:04:02,130
And first we're going to create a property that basically holds our user defaults and then we're going

54
00:04:02,130 --> 00:04:05,330
to go ahead and remove the.

55
00:04:05,890 --> 00:04:11,880
So basically we're going to create a property that has the name for our user defaults the bundle identifier.

56
00:04:12,100 --> 00:04:16,300
And we're going to go into our user default and actually remove everything that we've saved for that

57
00:04:16,300 --> 00:04:21,590
particular bundle ID so type guard let bundle.

58
00:04:21,910 --> 00:04:31,000
Identifier equals bundle dot main dot bundle ID or bundle identifier else.

59
00:04:31,390 --> 00:04:36,460
If that doesn't work then we're going to have to call fatal error and which will basically stop our

60
00:04:36,460 --> 00:04:39,240
application if we can't do that then we shouldn't be continuing.

61
00:04:39,490 --> 00:04:43,360
Next we're going to go ahead and type user defaults dot standard.

62
00:04:43,660 --> 00:04:45,210
That just gets the user defaults.

63
00:04:45,220 --> 00:04:51,820
And we're going to go ahead and type remove persistent domain for a particular name which in this case

64
00:04:51,820 --> 00:04:52,710
is our bundle ID.

65
00:04:52,720 --> 00:04:59,470
So go ahead and type bundles ID and that will basically reset the states the reset the state of our

66
00:04:59,470 --> 00:05:06,220
application by removing all user defaults for this particular apps bundle ID so we can now call reset

67
00:05:06,220 --> 00:05:08,010
state whenever our app launches.

68
00:05:08,020 --> 00:05:15,100
And if I go back into my tests this test should now pass because when this test runs it goes all the

69
00:05:15,100 --> 00:05:16,120
way through.

70
00:05:16,120 --> 00:05:21,640
It checks to make sure that this exists then it's going to go ahead and go through the next test and

71
00:05:21,670 --> 00:05:25,750
it will make sure that it gets a fresh state and that it won't crash.

72
00:05:25,750 --> 00:05:30,330
So to just verify that this actually works I'm going to put a new temporary test in here.

73
00:05:30,340 --> 00:05:36,450
I'll just say test temp and what it's going to do is it's basically going to swipe left and then tap

74
00:05:36,460 --> 00:05:39,560
done which should dismiss the screen there.

75
00:05:39,640 --> 00:05:42,260
We're not actually testing anything here because we're not asserting.

76
00:05:42,340 --> 00:05:47,260
But what it's going to do is it's going to basically go through the process of tapping done setting

77
00:05:47,260 --> 00:05:48,020
user default.

78
00:05:48,040 --> 00:05:52,540
And then when this test runs you'll see what I mean that we get a fresh state every time.

79
00:05:52,540 --> 00:05:58,210
So first call apt at launch I guess we need to actually launch the application in order for it to do

80
00:05:58,210 --> 00:05:58,900
anything.

81
00:05:59,320 --> 00:06:05,350
Then go ahead and click the big diamond up at the very top of the class declaration and we'll run all

82
00:06:05,350 --> 00:06:07,560
of our tests and let's see how we do.

83
00:06:07,600 --> 00:06:08,360
Here we go.

84
00:06:08,620 --> 00:06:17,650
So we have our simulator and here we go the first test is going to run the app will launch we're going

85
00:06:17,650 --> 00:06:19,500
to check is this text here.

86
00:06:19,600 --> 00:06:22,150
Swipe check.

87
00:06:22,260 --> 00:06:25,360
Well I guess it ran the bottom one first so that passed that's good.

88
00:06:25,360 --> 00:06:30,280
Now when we open it we should have a fresh state and look at that it's fresh We have our onboarding

89
00:06:30,280 --> 00:06:31,220
you pop up.

90
00:06:31,240 --> 00:06:32,470
That's awesome.

91
00:06:32,470 --> 00:06:37,410
So now our third test is going to run swipe swipe and dismiss.

92
00:06:37,420 --> 00:06:39,900
And our third test passes that is so cool.

93
00:06:39,910 --> 00:06:46,660
So now every time we are resetting the apps state this is a really really really really good way to

94
00:06:46,660 --> 00:06:51,400
make sure that you have a fresh brand new instance of your application every time.

95
00:06:51,400 --> 00:06:53,750
Of course this is just for user defaults.

96
00:06:53,770 --> 00:06:58,420
If you're using something like core data you would want to clear out all entities of your core data

97
00:06:58,450 --> 00:07:03,480
every single time so that the app launches with a fresh set of data.

98
00:07:03,520 --> 00:07:04,510
Very very cool stuff.

99
00:07:04,510 --> 00:07:11,530
So we have now learned how to actually manage the apps state and reset it so we get a fresh one every

100
00:07:11,530 --> 00:07:12,810
single time.

101
00:07:12,820 --> 00:07:13,960
Very very cool stuff.

102
00:07:13,960 --> 00:07:18,380
Let's go ahead and head over to the next video and keep learning about UI testing.

103
00:07:18,390 --> 00:07:19,100
I'll see you there.
