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

SELENIUM: Some Useful Facts

Before we start designing a framework, there are few facts that we should always remember. It is not important what you have designed, it is more important that how good your product is. Also, this kind of analysis should always be done in the beginning so that you don't have to devote extra time and efforts for it later. It is always good to fix something in beginning than worrying about it later when it has become a big issue and become difficult to locate the issue. It's not like that we won't be able to fix it, we will definitely fix it but with more cost and efforts. Let's go through few of these facts. Layers in Framework You should always keep in mind the layers that should be there in the framework you are designing. Layers to be created generally varies application to application that you are creating but most of the frameworks contains below layers often. In above picture, there is workflow layer at the top. Workflows can be think of as somet...

Logging in Selenium - To File

In my previous post I explained you how you can configure log4net in your project to do logging to console with example. Now, in this post I will be explaining how we can configure log4net to log to file instead of console. As we already have working logs to console from our TestFramework so logging to file doesn't take much efforts. You just need to follow below steps and you are done. Configuring log4net to log to file Step 1 : Add an " app.config " file in your UITests project with content as shown below: Step 2:   Change configurator in Configure method of Log class That's it. Now, when you will run the test it will log to console as well as file named " Test.log ". This file can be found at \TestFramework\UITests\bin\Debug location in you project folder. As you saw, we just added app.config file and then changed the configurator in order to log to file. Earlier we used "BasicConfigurator.Configure()" which writes logs to cons...

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