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.
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 "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.
- By "Id"
- By "Name"
- By "LinkText"
- By "ClassName"
- By "TagName"
- By "CssSelector"
- By "XPath"
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
Post a Comment