Skip to main content

SELENIUM: Adding Next Test With Proper Test Structure-2

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.

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
To achieve this, what we need to do is first create an instance of "Actions" class and pass driver instance as parameter.
"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

Popular posts from this blog

"Hello Selenium"

Now, as we have already setup the required environment, let's just start with a hello world test with selenium. In this let's just write a basic test to make sure that our environment is working properly. For this hello selenium test we will be automating below test: Navigate to " https:// www.google.com  " Write "Hello selenium" in search box Press search button Below are the code snippets: Test class UnitTest1 containing required test. Here in this class, at the top I have used two statements i.e.     using Microsoft.VisualStudio.TestTools.UnitTesting;     using TestFramework; First using statement is required in for writing unit tests and second using statement is required so that we can access methods from class under TestFramework namespace. There is [TestClass] attribute used with this class. This is required so that tests written in this class can be recognized. If you miss this attribute then your tests are not going ...

SELENIUM: Using Nested Classes

In this post I will be using nested class pattern to make few things easier. e.g. If you see in our test application there is a navigation bar on left side containing various menus i.e. Posts, Pages etc. Further each main menu contains sub-menu e.g. Posts menu contains sub-menues as "All Posts", "Add New", "Categories" and "Tags". In this type of situations, it is good to use nested classes to make things more readable and easy to use. E.g. Suppose I want to access "All Posts" navigation then I would want this to be done like this. " LeftNavigation.Posts.AllPosts.Select(); " i.e. I would want that there would be a class called "LeftNavigation" and when I press '.' (dot) it shows all the main menues in this navigation e.g. "All Posts", "Pages" etc. then I choose " LeftNavigation.Posts " and again when press '.' it shows all the sub-menues inside this Posts menu...

SELENIUM: Adding Next Test With Proper Test Structure-1

As I mentioned in my previous post that now its time to refactor our tests a little bit so that each test is written with proper test structure as required. After refactoring tests we will add one more test to our framework with proper test structure. So, lets do it now. Refactoring tests Till now we have added two tests to our framework i.e.         And Let's refactor these tests first before adding more tests to this framework.  In each method let's log some message so that we come to know how the execution was done. # AssemblyInitialize method:     In this method let's put code to initialize the log so that it can be used throughout the assembly. Before moving to next method let's talk about other refactoring made in this test project. #1: All static classes are made non-static so that we can created instances of page objects instead of directly using them. So, now on we cannot use class directly. Instead we will...