Skip to main content

SELENIUM: Test Structure - Use of Different Attributes


As I mentioned in my previous post that in this post we will be discussing about test structure that we will are writing.

Here I will be explaining in brief different types of attributes that can be used while writing our tests. Till now we have just used one attribute i.e. [TestMethod] which contains the test to be executed.

There are other important attributes that can be used during tests to make tests well organized.

Few of the attributes are as given below:
  • [ClassInitialize]
  • [ClassCleanup]
  • [TestInitialize]
  • [TestCleanup]
  • [AssemblyInitialize]
  • [TestMethod]
  • [TestClass]

[ClassInitialize] attribute can be used with a method that you want to execute before intialization of a class. So, let's suppose there are five tests in a test class then the method having [ClassInitialize] attribute will run only once for this class before running all the tests.


[ClassCleanup] attribute can be used with a method that you want to execute after all the tests in the class are run.


If you want a code to execute before execution of each test than [TestInitialize] is the right attribute for you. Methods with this attribute will run before execution of a test.


[TestCleanup] attribute can be used with a method that you want to execute after execution of each test. So, if you have something that you want to run after each test than just put that code in a method with this attribute.


[AssemblyInitialize] attribute can be used with a method that you want to execute before all tests and classes i.e. it runs before running any test in a test assembly.


[TestMethod] is the attribute which we were using till now. Methods with this attribute is the unit test that we want to execute for a particular scenario. So, we put all the neccessary steps that we want to execute in this method.

All of above attributes have there existence if there is [TestClass] attribute used with the class. If this attribute is not used than nothing will be executed. It can be seen that many times people just write everything but forget to include this attribute with the class. While executing tests nothing gets executed and spends lots of time in finding what actually went wrong. So, don't forget to include this attribute.


Summary:
 In short, if there is on test class TESTCLASS_A with five test methods and TESTCLASS_B with five test methods. Also, there is a TESTCLASS_C with [AssemblyInitialize] method and this class is inherited by TESTCLASS_A and TESTCLASS_B.

Now, if you execute all tests, AssemblyInitialize method will be called before running any other method and will called just once. After that within each class methods will be called in below order:
  1. ClassInitialize
  2. TestInitialize
  3. TestMethod..1
  4. TestCleanup
  5. TestMethod..2.
  6. TestCleanup and so on till TestMethod..n
  7. ClassCleanup

In my later posts we will be using these attributes in our examples. In my next post, we will be discussing about logging and I will explain you how we can configure log4net so that we can log to console or files while our tests executes.

Thanks for reading this post and see you in the next post. cya!





Comments

Popular posts from this blog

Logging in Selenium

Any application is incomplete until it's activities are traceable. Here comes the use of logging. By logging important actions you can later on track anything you want especially when there are some error situations. It becomes easy to debug the issues if logs are generated properly. Similarly, it is very important that our automation framework generates logs so that whenever any test fails, we can track the issue by tracing it's logs. In this post, I will be explaining you how we can log using log4net in selenium. Before using methods of this library we need to configure this to our application. Let's do this first then. Configuring log4net in selenium: First of all you need to download this library so that this can be used. You can read about log4net and then download it from this link and save the dll file after extracting it to folder that is under your project folder. After downloading this library you need to add references of this in you project. To do ...

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: Using Common Methods in Different Tests

As I mentioned in my previous post that we will create few more tests which will make use of the nested navigation class and common menu selector class to see how we can make use of these. So, let create one more test to check some other navigation e.g. navigating to "Add New" page and to implement this we will make use of the same navigation class and menu selector class. About test: In this test we will write code to implement below steps: Navigate to login page Login to application Navigate to Posts->Add New post page Confirm that we are on right page Again, we are already familiar with step 1 and 2 so, we will be focusing step 3 and 4 only. Before we start writing code for this test let's go through some of the refactoring that is needed MenuSelector class's select methods created in previous post. MenuSelector class created in previous past: We will be refactoring below areas in this class: #1: Used WebDriverWait #2: Removed browser.W...