Everybody has a go and this is Caleb with Debb slopes and in this video we're going to be testing views
to determine whether or not they have presented or have stopped presenting.
And we're also going to test you alerts because those you know can be kind of pesky And we're going
to I'm going to show you how to you I test views as well as alerts.
So pull up in your project and what we're going to do is we're going to go ahead and basically go into
our onboarding Visi or no.
Rather our onboarding page AVC and that is the one that when you open the app when you come into here
this is one of the pages but the done button when we tap done obviously user defaults completes it and
it dismisses.
I'm going to go ahead and comment that out for now and I'm going to add a UI alert controller so let.
Alert equals UI alert controller.
Whoops.
There we go.
And we're going to go ahead and give this thing a title saying you did it.
Yay.
Congratulations.
You survived our onboarding experience and of course the style will be an alert.
OK so next we need to add an alert action to give it a button.
So type alert action.
Give this one a title as well.
This is what the button actually says so it's going to just say awesome.
The style will be default and we don't need a handler so just make that call.
Next we need to go ahead and add the action to the alert by typing alert an action and type an alert
action and then we're going to present this just like any other view controller present alert.
And of course animated is true and completion.
We don't really care about.
So if I actually build and run this before testing it we're going to just go ahead and see that when
we get to the very end we will tap the done button and it will present an alert.
So check it out.
Boom an alert pops up.
We tap.
Awesome.
It dismisses k.
Very cool stuff.
So now let's go ahead and let's test it to make sure that that actually works the way that we want.
So let's write a new test phunk test onboarding and we are testing when done button tapped.
Presents alert.
So we're going to go ahead and launch the app after launch because remember every test gets a new instance
of our app.
So we're going to go ahead and just swipe to the left two times.
OK then we need to tap on the done button and to do that we're going to type app dot buttons wups app
dot buttons and we can just go ahead and put some brackets here.
And then in quotes we can go ahead and type what the name of the or the title of the button is and in
this case it's done and to tap it.
We just simply call DOT tap and that will tap the button.
Very cool stuff.
Very easy.
And then of course that will actually go ahead and present our alert View Controller.
And so then of course we need to check that it's actually there.
So go into onboarding page AVC.
And what we're going to do is we're going to go ahead and set alert.
OK.
Just alert dot accessibility identifier x ability let's go find the accessibility identifier.
It's in here somewhere says Bill that he identifies.
Oh you know what.
I'm sorry.
We need to go into alert dot view that accessibility identifier and we're going to set this just to
be well I guess alert.
OK.
Now since I'm only using that once I'm not going to make a constant but I'm going to go into my tests
now and what I can do is I can type LCT assert true and I can verify that it actually is there by typing
app dot alerts.
OK.
And I can go ahead and actually search for a particular alert.
And what did I name it.
I named it alert.
OK.
And then we can type dot exists and if we run this test we should get a pass because it should show
us that our alert exists.
And so we're going to go ahead and pop open our application here.
It's going to run that test.
It will.
Here we go.
Launch our app.
It'll swipe left twice swipe swipe.
It will tap the done button and the test passes.
OK.
So that alert exists.
Very very cool.
Now we're going to go ahead and continue on.
We know that the alert exists and let's make sure.
Oh no.
You know what.
That actually that's our test.
We're testing to make sure that it exists.
So that test is now finished.
Now in the previous video I told you that I would show you a more elegant way to check whether or not
a certain view exists.
OK.
And there's a really cool way to do it.
And I'm going to make some more space here to show you.
But essentially we're going to create an extension of X-C UI application and we're going to create a
boolean variable that is set depending on whether or not a certain view is visible.
So go ahead and type extension X-C UI application and go ahead and type.
Var is displaying onboarding of type bool OK.
And then inside of this we're going to go ahead and return basically the onboarding view if the onboarding
view controller is showing up if it exists.
We will return the boolean.
Otherwise if it doesn't exist it will return false.
Now onboarding page visi.
That is one of the pages that is shown right when when the onboarding is being displayed.
And so what we're going to do is we're going to go into the UI page view controller and we're going
to go ahead and take a look at what I've done here.
Now I've set an accessibility identifier a view ID on board view.
Now of course if you look it's really onboarding view and all I've done is set an accessibility identifier
and that will basically allow us to check hey if a certain view with that name is visible we're going
to return true so to access that what we need to type is other elements.
That's kind of weird but there's not actually a property specifically for views.
But what we can do is we can type in that identifier.
But actually it's not a string.
I can just take the name here view Id onboard view OK and I can type that in here view Id let's build
it so we can actually access it on board view.
OK.
And then after that I can go ahead and type datt exists and we can build it and see if that works.
Now of course we're not getting a problem here.
It's having some trouble building and verifying and it's saying view use of unresolved identifier view
Id onward view.
The reason for that is we never imported our project so go ahead type at testable import.
And the name is bored to death.
OK now build and of course it can access that.
My bad.
So there we go.
Basically what is happening is other elements checks through all the other elements that are not the
ones that are prescribed like we could have checked for buttons we could have checked for static texts
which checks all instances of label but other elements is for views.
So this will be set to Boole whether or not this will be set to True or false depending on if the view
is visible.
So we have our identifiers set up we have where were we we are yet in our tests so we are basically
going to say phunk test.
We're going to test to make sure onboarding is visible.
So go ahead and type phunk test and we're going to go ahead and type onboarding OK when finished Visi
or let's say onboard Visi dismisses.
OK.
And what we can do is we can call app don't launch to launch the app from beginning.
But then what we're going to do is we're going to check to see hey at the beginning is it showing.
Then we'll go through the process to dismiss it and then we'll check again.
If it shows.
So go ahead and type ex-city assert true app dot and since we wrote an extension we can just type app
dot is displaying onboarding and it's going to check for us then we're going to go ahead and go through
the process of swiping through the onboarding tapping done and then checking to see if the test passes.
So app dot swipe left app dot swipe left.
Then we to tap on are done button.
So app dot buttons and of course the name of that button is done.
The title from the storyboard and then we're going to check again.
Ex-city assert false.
OK.
App dot is displaying onboarding because at that point it should be false right.
Let's go ahead and let's build this or test Diamond shows up and click on it to test it.
And now let's see what happens.
Oh look at that.
We get a weird problem here.
Let's go ahead and clean with a shift command K and then command B to build.
That should fix us up here.
Let's see if it'll build successfully.
Looks like it is.
Yep.
OK so now let's go ahead and click the diamond to run just that test alone.
But it's given me a weird problem it's saying that we are referencing it from something that's not linked.
So you know what I'm going to actually go ahead and I'm going to save this and I'm going to actually
quit out of X code and go back in just a second.
Let's see if that'll fix our problem here.
All right let's try that one more time.
The onboarding should dismiss when I build this and it's given me a really weird error.
I have not seen this before.
So let's go look at what's going on.
It's saying symbols not found for architecture.
Xx 86 64.
That's really bizarre.
So let's see we imported our project.
We have our application here let's clean it one more time.
Yes that belongs to that.
This all belongs to the proper project bored to death.
UI tests.
We have an extension that looks good.
We have one test.
Let's go ahead and do command you.
Let's see if it's going to allow us to do it.
Still saying no failed with exit code 2 555.
So let's go ahead and let's quit the simulator here.
Let's try that one more time.
Really weird bugs here.
Building OK it's exceeded.
So that's good.
The simulator is opening again.
Hopefully that means we'll be able to run our tests very soon.
Of course there are bugs even in you know really nice applications like X code there can still be issues.
And let's see if we're going to run.
OK.
So it runs.
Now our testing should work as well.
So click test and we're still getting that error that is so weird.
So you know what let's go ahead let's go back to our tests here.
We're referencing the proper file here.
You know what.
Let me try just changing this back to the actual onboarding view.
And let's see if that lets us test
that is so weird.
So for some reason it does not like referencing this Constance file even though it's public and it's
linked it doesn't like that I was using that Constans files.
That's the weirdest thing I've ever seen.
OK.
So we're testing our first test here we tap the button.
The alert exists.
Awesome that passes we're testing the onboarding when it's finished.
The onboard Visi should dismiss.
Here we go tap the button and we get a test failed because the onboarding is still displaying.
So let's go back into I believe it was onboarding page visi.
And we're actually going to go ahead and comment this out.
For now we're going to go ahead and re or I guess re-enable this code.
And now we're going to go run our tests once more.
So let's go ahead and run them again.
Now of course you know it's going to happen here.
This test will now fail because there's no alert.
And the top 10 or the bottom test will pass because our display will be you know dismissed.
So there we go first test passes which is awesome or sorry first test fails.
Second test is going to also fail.
OK.
Now you might be thinking wait a minute our code is good.
Everything should be working.
And that's exactly what brings us into needing to know about the apps state in the next video.
We're going to go ahead and set up a little function in our app delegate that's basically going to delete
the user defaults for every single time that we go in to our tests.
Every test we run we should clear out the user defaults so we get a fresh app state every single time.
Let's head over to the next video and let's fix this problem.
I'll see you there.