In previous post we have now refactored our code in a way that now we are not using static objects any more. Also, we have now tests with all required methods in it.
Let's add next test to our framework which will be in proper test structure.
We have already created code for step 1 and 2. So, we need to add code for steps 3 and 4 only. See the below code snippet first for this.
We have added two test steps here i.e. "homePage.Logout();" and "Assert.IsTrue(loginPage.IsCurrentPage(), "Logout unsuccessful");". In first step we are calling logout function of homePage that opens once we login to application successfully. And finally using Assert method we can verify whether we are logged out successfully or not. For this we can just check whether login page is shown after logout or not. If login page shows then logout was successful otherwise it is not successful.
Now, let go through the "Logout()" method of homePage.

To perform logout functionality I have used above code. Let's go through it.
In order to do this we have used "Actions" class from "OpenQA.Selenium.Interactions;" which is basically used to build advance interactions with browser.
In order to do logout our test application we have to perform two steps i.e.
"Actions actions = new Actions(browser.Driver);"
After this we just find the logout menu and then move mouse over to it using below code:
"var logOutDropdown = browser.Driver.FindElement(By.Id("wp-admin-bar-my-account"));"
"actions.MoveToElement(logOutDropdown).Build().Perform();"
After this I have added code to make system wait for 1 second before it goes to next step. This is needed as otherwise system throws exception as logout button not found or so due to the fact that it does the mouse over and tries to find logout button so quickly that dropdown was even opened yet.
"browser.Wait(1);"
To achieve this wait I have created a "Wait()" function in browser class which in turn just calls the "Thread.Sleep()" method.
After wait, we just need to click on logout button which can be done using below code:
"var logOutButton = browser.Driver.FindElement(By.Id("wp-admin-bar-logout"));"
"actions.MoveToElement(logOutButton).Click().Perform();"
After logout we need to confirm whether it was successful or not for which I have added one Assert method i.e.
"Assert.IsTrue(loginPage.IsCurrentPage(), "Logout unsuccessful");"
Let's go through it.
You can see here that I have created one more method to check whether we are on login page or not i.e. "loginPage.IsCurrentPage()"
This can be done by just confirming whether login button is showing on login page after logout or not. If login button is showing then it means that we are on login page and hence logout is successful otherwise it is not.
Output of this test:
That's it. In this post we added logout test to our framework successfully and learnt few new things too during this. In next post I will be using nested classed pattern to do some tasks e.g. Navigations can be achieved using this. In almost all applications there are navigations defined e.g. left navigation menues, top navigation menus etc. So, let's see in the next post that how usage of nested classes benefits us.
Thanks for reading this post. See you in next post. Cya.
Let's add next test to our framework which will be in proper test structure.
About test
Let's add next test to check that our logout works successfully. In order to do this we need to perform below steps:
- Navigate to login page
- Login to application
- Logout from application
- Check if logout was successful
We have already created code for step 1 and 2. So, we need to add code for steps 3 and 4 only. See the below code snippet first for this.
We have added two test steps here i.e. "homePage.Logout();" and "Assert.IsTrue(loginPage.IsCurrentPage(), "Logout unsuccessful");". In first step we are calling logout function of homePage that opens once we login to application successfully. And finally using Assert method we can verify whether we are logged out successfully or not. For this we can just check whether login page is shown after logout or not. If login page shows then logout was successful otherwise it is not successful.
Now, let go through the "Logout()" method of homePage.
To perform logout functionality I have used above code. Let's go through it.
In order to do this we have used "Actions" class from "OpenQA.Selenium.Interactions;" which is basically used to build advance interactions with browser.
In order to do logout our test application we have to perform two steps i.e.
- Move mouse over to logout menu -> it shows the logout dropdown menu
- Click on logout button
"Actions actions = new Actions(browser.Driver);"
After this we just find the logout menu and then move mouse over to it using below code:
"var logOutDropdown = browser.Driver.FindElement(By.Id("wp-admin-bar-my-account"));"
"actions.MoveToElement(logOutDropdown).Build().Perform();"
After this I have added code to make system wait for 1 second before it goes to next step. This is needed as otherwise system throws exception as logout button not found or so due to the fact that it does the mouse over and tries to find logout button so quickly that dropdown was even opened yet.
"browser.Wait(1);"
To achieve this wait I have created a "Wait()" function in browser class which in turn just calls the "Thread.Sleep()" method.
After wait, we just need to click on logout button which can be done using below code:
"var logOutButton = browser.Driver.FindElement(By.Id("wp-admin-bar-logout"));"
"actions.MoveToElement(logOutButton).Click().Perform();"
After logout we need to confirm whether it was successful or not for which I have added one Assert method i.e.
"Assert.IsTrue(loginPage.IsCurrentPage(), "Logout unsuccessful");"
Let's go through it.
You can see here that I have created one more method to check whether we are on login page or not i.e. "loginPage.IsCurrentPage()"
This can be done by just confirming whether login button is showing on login page after logout or not. If login button is showing then it means that we are on login page and hence logout is successful otherwise it is not.
Output of this test:
That's it. In this post we added logout test to our framework successfully and learnt few new things too during this. In next post I will be using nested classed pattern to do some tasks e.g. Navigations can be achieved using this. In almost all applications there are navigations defined e.g. left navigation menues, top navigation menus etc. So, let's see in the next post that how usage of nested classes benefits us.
Thanks for reading this post. See you in next post. Cya.
Comments
Post a Comment