Skip to main content

Interesting Facts

Why do we need automation

This has been always a point of curiosity to everyone that why actually we need automation. Let's try to look at some of its aspects here.

In general automation is needed whenever we need work to done efficiently and quickly to increase the productivity which is not possible manually. Here I used not possible and you might be having thoughts in mind that why it is not possible as work can be done efficiently and quickly manually too.

Yes, it can be done manually but it will require more resources and hence you have to spend more money for it. Moreover, it is not always true that similar productivity will be there every time the same work is done due to the fact that not all resources have similar efficiency level.

So, in testing world too, whenever we need our tests to be done quickly, efficiently and effectively, we have to do test automation. It not only saves time but increases the accuracy of test executions too.

Moreover, tests run through automation scripts are unambiguous too. This is due to the fact that every person evaluates a particular sentence in different way or not always in the same way. Because, when a test engineer writes tests, he/she will try to write steps as per his understanding level and comfort. Test will be run as needed until he/she runs this test but when someone else run this test again he/she will run as per their understanding and in this way it might not be executed as needed originally.

But, in automation scripts if test script is tested correctly before done then it's going to give you the 100% absolute result.

Further, automated tests can help developers in finding issues before a feature is in testing too as developers can just run the scripts while developing the feature and check for failed tests if any and correct if required. Hence it helps in finding bugs in early stages and hence saves time and money too.

Locators in selenium and best way to use them 

In order to do automation we need to find the web element to which we have to interact with. Here comes the use of locators which help us to locate a web element we want to interact with. There are quite many locators in selenium which can be used. Below are the list of mainly used locators:
  • By "Id"
  • By "Name"
  • By "LinkText"
  • By "ClassName"
  • By "TagName"
  • By "CssSelector"
  • By "XPath"
Let's go through each of these with respect to below example html page.
By "Id" - This locators helps in finding a web element having attribute "Id". e.g. from our test page, in order to find the radio button with "Red" option we can use driver.FindElement(By.Id("color_red")) in code.

By "Name" - This locator helps us in finding a web element having attribute "name". e.g. if we need to find a radio button with "Green" option we can use driver.FindElement(By.Name("color_green")) in code.

By "LinkText" -  This locator is useful when we need to find a web element by using any link text. e.g. from above example if we need to find anchor web element having link text as "more colors" then we can use driver.FindElement(By.LinkText("more colors")) in our code.

By "ClassName" This locator can be used to find a web element by its class attribute. In above example, we can find a span by using driver.FindElement(By.ClassName("span_class")) in our code.

By "TagName" - In order to find any tag we can use this locator. e.g. we can use driver.FindElement(By.TagName("div")) in our code to find a web element with div tag.

By "CssSelector" - This locator can be used whenever you don't have "Id" or "Name" attributes used with elements. It can be used to find any web element on html page. Creating a css selector is similar to creating a xpath. I will be writing more about creating these selectors later separately. In this example, if we need to locate e.g. ok button then we can use driver.FindElement(By.CssSelector("span.span_class input#button1")) in our code.

By "XPath" - As mentioned above, like css selector, this locator can also be used to locate any element on html page. For above case, ok button can be found using xpath by using driver.FindElement(By.XPath("//*[@id="button1"]")) in our code.


Use of any locator varies based on usage or availability of attribute. In case "Id" or "Name" attribute is available, it is always good to use these locators to locate any web element as it is possible that the structure of page may change but probability of changing id or name are very less. The only problem is that it is not always available as in most of the cases page designers don't use these hence other locators comes into action. 

We can use "ClassName" locator to locate any web element but there may be many elements with same class name so it is not always possible with this locator to locate a particular attribute.

Using "CssSelector" or "XPath" we can locate any web element. Now, the question comes into mind that which one of these should be preferred. It is always recommended to use css selector over xpath as it is more simple, faster and reliable. It's been seen many times that evaluation of xpath fails especially with IE browser.  



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