Skip to main content

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 e.g. "All Posts", "Add New", "Categories" and "Tags" and I choose "AllPosts" from it.


This way it becomes very easy to use and further doing this makes sure that we are using same type of conventions to access all the menues in our application and this was our main motive always to make our framework more easy to use.


Let's see how we can achieve this.


Here you can see that I have created a LeftNavigation class and inside this we need to create all classes that we want to show as options when we press a dot. Here I have created Posts class and inside Posts class we need to created all classes that we want to show as options when we hit a dot. I have created "AllPosts" class here. Inside "AllPosts" class I have created a "Select()" method which will basically have code to navigate to this menu. In this way we can just add other required classes under the class where we want them to show as options. We will add more classes during our later posts.


About MenuSelector class

In "Select()" method you can see that I have used a "MenuSelector" class and its "Select()" method to navigate to a particular menu.

We could have used used code here directly to go to a particular menu but I have opted for this MenuSelector class option as with this it becomes more easy to use this API. Further it also reduces the redundancy as we will have just one common method to access a all menues and we don't have to write similar code on many places e.g.


 From above code snippet you can see that we have just a common "Select()" method which takes two string arguments i.e. "topLevelMenu" and "subMenu".

So, whenever we want to access any menu we just call this method and pass these two details. e.g. in our case I have done this by using below code:


"menuSelector.Select("menu-posts", "Add New");"


We have to pass browser instance as parameter to this MenuSelector class as we want this browser to be used here.


Let's go through "Select()" method of MenuSelector class.


In this method I have first written code to find the topLevelMenu i.e.


"var topLevelM = browser.Driver.FindElement(By.Id(topLevelMenu));"


once it is found I have clicked this menu using below code:


"topLevelM.Click();"


Before we click the sub menu we need to wait for few seconds so that sub menu items are shown otherwise application may throw exception as element not found. For this I have added below code:


"browser.Wait(1);"


 After clicking this topLevelMenu, it will show sub menues so, I have written code to find the sub menu and click it as:


"var subMenuL = browser.Driver.FindElement(By.LinkText(subMenu));"

"subMenuL.Click();"

That's it :).


In this post we have learnt how to create nested classes and created a nested class which can be used in our later posts. Further, we have also created a class with common method to navigate to a particular menu in application.


In my next post I will be writing a test which will use this LeftNavigation class.


 




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: 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...