Hey everybody has a go and this is Caleb with slopes dot com.
And in this video we're going to talk about UI testing navigation.
OK.
There's a lot of different things we can do to test navigation.
And I want to show you the app that is the start of project for what we're going to be learning.
So I'm going to pull it up here in the simulator it's called bored to death and hopefully you're not
bored to death while watching this but basically it's a simple app with an onboarding experience get
it bored to death.
Onboarding anyway and basically as you swipe through these different cards there are different onboarding
experiences that just kind of describes the app what they're going to be doing.
And then when they click Done it goes ahead and it dismisses the onboarding experience.
Then of course if you were to come back into this and open it back up the user defaults save that you've
seen that you've read through it and the onboarding no longer shows up.
So let's go ahead and let's take a quick look through the code before we get into adding our testing
target and adding some tests.
Basically it is a UI page view controller already and we create a couple of instances we add them in
to our page view controller using the proper delegate methods.
We set up the indexes to basically show The View controllers in an ordered way and then essentially
our onboarding page ADC when the done button is pressed on an instance of the onboarding page Visi like
here.
When I click the done button what it does is it sets our user defaults to allow our onboarding completed
boolean to be set to true and therefore when we load up our view controller when this VC pops up if
the onboarding View Controller property which is actually stored in this extension k it has been set
to true.
We're basically going to only present it if it's false.
So once it's true it no longer presents.
That's that so pretty easy stuff.
Basic little onboarding experience.
And if you want.
You can use this code for your own onboarding experience in your app.
It's up to you.
But we're going to go ahead and add a testing target.
So go ahead and click on File new target and we're going to be adding an iOS UI testing but also double
click on that.
And bored to death you eye test is fine.
Click Finish.
And there we go we've got ourselves a unit testing bundle.
So go ahead and open up the board to death test file here and we're going to set it up just like we
did in a previous video to essentially create an instance of our application and we're going to set
it up so that when we run our app it automatically will launch and basically do a quick little test
of navigation.
So obviously as we are testing our onboarding experience we're testing the navigation of swiping through
a particular experience.
And I want to show you how to do that.
So what I'm going to do is I'm going actually get rid of all these comments and random stuff get rid
of that and then get rid of this sample test and to begin.
I'm going to go ahead and create a variable called app and it's going to be of type C UI application.
Then in our setup function we're going to go ahead and instantiate it.
App equals X application then we're going to set the property continue after failure to be false because
like best practices state if we had a single test that does not pass we should not be doing this so
we're going to actually fail the entire thing if one test fails.
So that's set up in the tear down function we're going to set up to be equal to nil so that every single
time a new test runs we get a fresh instance of our application.
OK now we are going to talk in a future video about App State and we're going to talk about how we can
actually reset our app state every time because user defaults if we set up an instance of our application
if we run it again those user defaults will still be there.
So we're going to set it up so that we can actually essentially like delete the app and then reinstall
it with fresh user defaults.
It's pretty cool.
We'll talk about it later but for now let's go ahead and write our first test for this application.
So type phunk test and we're testing the onboarding so we can say that then we're going to say what
we're testing.
Well the swiping we're going to test the swiping on the page controller so just say when swiped.
That's what we're testing is when it is swiped.
What's it supposed to do.
It should load the next controller.
So go ahead and just type next VC loads.
OK.
Now this is where we launch our application.
Every test we should launch our instance of X-C UI application so type app launch and there goes it
launches and it'll start you know displaying in the console here.
We've set that up.
But it launches Aryeh.
And now what we need to do is we need to verify that a something from our screen that we know will be
there.
Whoops let me delete this actually because you know user defaults saved that.
I've already gone through onboarding.
There is a label on the first screen that's displayed and this should be displaying every time it says
welcome to bored to death.
So what I'm going to do is I'm going to check that that is indeed showing by typing ex-city assert.
True.
I'm going to assert that the label welcome to bored to death is showing or that it exists on the screen.
So type app OK and we're going to access a property called static texts K and static text basically
is a way that we can query all of the static texts or the labels on the screen and I can search for
a particular content of a label like welcome to bored to death and make sure that you're typing that
case sensitive.
It's probably best to put this inside of a constant file like I have done here.
These are basically just non string identifiers for string properties so that I can't type them wrong
but I'm just going to go ahead and leave that as it is.
OK.
Now we're going to go ahead and check in the static text for this particular label and type.
Dot exists.
OK.
And what that's going to return is a Boolean if it exists it will return true if it doesn't it will
return false.
So it should return true.
And that means our test will pass.
If it does exist.
So let's actually go ahead and let's just run this first to make sure that it works.
Click the little diamond there and the test will run.
Our application will pop open and our test should pass.
But of course our test is not actually testing whether or not this label is there.
We're testing to make sure the next Visi loads.
Now of course our test passes.
Now if I were to remove that label obviously the test would fail if I were to change that label.
The test would fail which is why testing is so amazing because it ensures that things stay exactly the
way they were when everything was good.
Okay that's perfect.
So now we want to test navigation.
We want to make sure that we are able to actually swipe through our apps so all we have to do to type
that are all we have to do to make that happen is type app dot and check it out.
I can call swipe swipe left swipe up down or right and you can specifically set where a touchpoint goes
down on the screen and to where.
Swipes but just for a simple swipe left gesture this function works just fine.
So if we came into our app and swipe left obviously welcome to bored to death no longer exists.
But now there's new content so we can actually go ahead and type ex-city assert true.
And we can check to see if our new label shows up.
So app dot static texts like so put some square brackets there and we're going to go ahead and search
for custom puppy content.
And of course I typed that incorrectly.
Thankfully our test would catch this but we're going to check to see if that label exists and if it
does that means that our test is good.
It means when swiped the next VC is loading because this content is exclusive to that controller.
So let's go ahead and run our test.
Click that little diamond shape.
It should run our test and you'll actually see it swipe on the screen which is pretty cool.
So it's going to go ahead and load it.
It will crash and rebuild it.
We get a swipe action here or we should.
Anyway here we go.
The app launched we checked to see if it existed.
And the test fails so failure requesting automation session for Stoltze apps waiting for Target to check
in.
OK so if this happens just go ahead and actually delete the instance of the application UI.
Testing is a bit buggy.
Or it can be.
So you'll go ahead and try it one more time if you get that issue.
And if you don't then you should be just fine.
It should run.
It should pass because what we've done has basically proven that this works OK.
Did you see that it successfully swiped.
So sometimes it has a hard time getting a hold of the instance of the app that we made.
But what happened was it opened our app it checked for the starting label swipe left and it checked
for the new label so our test passes.
We've successfully navigated through our application and we have verified that a certain label exists.
This is awesome guys this is so so cool.
Now there is a more elegant way to do this and we're actually going to talk about that in the next video.
But for now this is a great intro to how to navigate through your application in Iowa UI testing.
So we're going to move on to the next video and we're going to basically test to see if particular views
are visible and we're going to actually learn a little bit more elegant of a way to see if something
is on the screen or not.
So let's head over to the next video.
I'll see there.