I hope that you spend some time trying that exercise and I hope that you've succeeded.
If not it's OK.
It was really tough.
I actually spent about 30 minutes trying to figure out how to automate the situation.
And it's because there are many variables on this page that make it extremely tough.
For example when you first load the page you can't just directly identify the toggle and
try and open it by its locator because it's not visible.
And so you won't be able to click on it.
So that's not going to work.
And what you're going to get is element not clickable exception and that's some other
element is going to receive your click.
So in that case what you need to do is to place focus on the actual element so that
it's visible now is going to be available for you to click on.
There are several methods to do this utilizing any NY driver.
I'm going to show you one but if you attempt to do that the method was going to happen
it will definitely pull the element into focus is going to pull that element into focus and
the page will look something like this.
Yes.
The element is in focus but it's behind this Hetter up here.
So again if you attempt to click on this you're going to get another error telling you that
this Hetter up here is going to receive the click because that toggle button is not visible.
So what are you going to do.
It's really hard.
What I decided to do was rather than placing focus on the element on the toggle I placed
focus on this carousel of Polsce a ball of the toggle.
So now when I place focus on it the page is going to come to about this point.
And my toggle is clearly visible in the center of my screen and the ready to accept my clicks.
That's what I did.
Let me show you the code.
So here I road test five and as standard.
I created another complicated page went to the complicated page and asserted that the
complicated page opened successfully and nothing new here.
What's new here is I created a method called Open toggle and my open toggle is going to
do all of those actions that I mentioned.
I'm going to utilize the actions class to move to the element that I called the Post's
slider that pole slider that's the element right above the toggle which I identified
in this manner.
Now once we move to the element using the actions class then my toggle is open for clicking.
So I just use the object repository in the toggle element which I identified in this
manner and I clicked on it and that was fantastic.
After clicking on it I wrote an assertion stating that the complicated page that the
toggle is open and that is true.
So to check that the toggle is open.
I had to use a selenium web driver weight class which has an explicit weight instead
of an implicit weight.
I didn't want to use an implicit weight because if you noticed here the toggle opens a little
bit slow and an implicit wait has a time out of zero seconds.
So if for whatever reason the toggle does not open fast enough your implicit wait also
known as find element's method is going to fail.
So rather what you have to do is use an explicit weight.
I use an explicit weight and set a time out of five seconds.
I rafted in a try catch block and I'm checking to see if the elements I'm waiting until the
expected conditions.
That element is visible and the element that's visible is my toggle inner text locator.
Let me go to the definition and my toggle text locator is of type by and I located it
using X bath and making sure that it has text called Inside the toggle.
So we wait until this element is visible if the element is not visible what's going to
happen is selenium is going to throw a web drive or timeout exception which we're going
to catch and return false to our method.
If the element is visible however no exception is going to be thrown.
And we're going to return a true therefore giving us a fantastic test.
Now that I've explained to you what I've done.
Let me just run this test so that you can actually see it working.