Showing posts with label Automation. Show all posts
Showing posts with label Automation. Show all posts
Friday, February 13, 2015
If you are into automation testing using selenium, you may be knowing about the Page Object Pattern suggested by selenium forum available at url :
https://code.google.com/p/selenium/wiki/PageObjects
The documentation suggests the approach for implementing Page Object Pattern for automation and what are the advantages of it. In here I wont be writing about what the documentation suggests and talks about it. So lets directly jump onto the main subject and learn about what the documentation does not contain. Following are those points:
With new web application development technologies coming into play on a daily basis web applications has reduced to very few pages and even sometimes to one page url. This becomes a challenge while implementing Page Object Pattern. The question arises is, how to segregate the pages as there are only few pages in the application but there are lot of functionalities to cover in them. If we go ahead with the conventional segregation methodologies we will have very few page classes but with lot of functional methods defined in them. We will fall into the same problem of code management that we were trying to solve using Page Object Pattern.
The solution to this is to enhance the Page Object Pattern guidelines. This includes segregating your page classes based on features, sub-features, functionality etc. and not by page urls. To explain this we have a perfect example of Gmail. Gmail is a single page but there are lot of functionalities available in the said page like navigation, hangouts, inbox, Search, compose and mail folders.
If we go with the conventional method we will be forced to define all the functionalities of compose, hangouts contacts chat, inbox etc in a single class. With the enhanced model we will define separate classes for each of the feature ex. ComposePage, HangoutPage, InboxPage, NavigationPage etc. By using the enhanced model we can fully utilize the advantages of using the Page Object Pattern.
The guideline suggests users to define functionalities as method inside page classes and it ends there. It does not talk much about the segregation, approach or anything of such sort. Anyways lets cover it here. When planning to define methods for the functionality try to follow following added guidelines:
The above suggestion helps in making tests more readable, manageable and in providing more functionality to the user who is automating. The initial time taken to write page class and page methods will be more but there will be very less time spent on writing automation test cases for the said functionality as the methods list will contain methods that supports all kind of related functionality from low level to high level as well as both positive and negative scenarios.
This is a cross post from my company blog here, where I had originally written it.
https://code.google.com/p/selenium/wiki/PageObjects
The documentation suggests the approach for implementing Page Object Pattern for automation and what are the advantages of it. In here I wont be writing about what the documentation suggests and talks about it. So lets directly jump onto the main subject and learn about what the documentation does not contain. Following are those points:
- Implementing a Page Object Pattern for a single page application
- Approach towards defining methods for functionality
Implementing Page Object Pattern for a single page application
With new web application development technologies coming into play on a daily basis web applications has reduced to very few pages and even sometimes to one page url. This becomes a challenge while implementing Page Object Pattern. The question arises is, how to segregate the pages as there are only few pages in the application but there are lot of functionalities to cover in them. If we go ahead with the conventional segregation methodologies we will have very few page classes but with lot of functional methods defined in them. We will fall into the same problem of code management that we were trying to solve using Page Object Pattern.
The solution to this is to enhance the Page Object Pattern guidelines. This includes segregating your page classes based on features, sub-features, functionality etc. and not by page urls. To explain this we have a perfect example of Gmail. Gmail is a single page but there are lot of functionalities available in the said page like navigation, hangouts, inbox, Search, compose and mail folders.
If we go with the conventional method we will be forced to define all the functionalities of compose, hangouts contacts chat, inbox etc in a single class. With the enhanced model we will define separate classes for each of the feature ex. ComposePage, HangoutPage, InboxPage, NavigationPage etc. By using the enhanced model we can fully utilize the advantages of using the Page Object Pattern.
Defining methods for Page functionality
The guideline suggests users to define functionalities as method inside page classes and it ends there. It does not talk much about the segregation, approach or anything of such sort. Anyways lets cover it here. When planning to define methods for the functionality try to follow following added guidelines:
- Segregate your functionality into smaller functionally executed methods. Lets take an example of login use case where user enters username, password and click on submit button. While writing a functional method for login you should think of having methods like enter username, enter password and clicking on submit button. This way you will have more control on the code and you can provide more utilities to user who is writing automation tests.
- Think of functional as well as structural method declarations. Taking the above example. login method that accepts username and password arguments is a functional method whereas enter username, enter password and click on submit button methods are structural methods. As said in the first point structural methods makes code more readable and represents a flow of user actions. It also provide more power into the hands of end user who is using them for writing automation tests.
- Think of positive and negative scenarios of a functionality while defining methods. I will again take the example of the login functionality. A login can be successful or it can be failure. try to support both kind of scenarios in a single method. This can be achieved in two ways:
- Allow a Boolean value to be passed as argument to the method that will decide whether the scenario for which the method is being used in positive or negative.
- Another way is you written a Boolean or some other object as return type representing whether the method execution was success or a failure.
The above suggestion helps in making tests more readable, manageable and in providing more functionality to the user who is automating. The initial time taken to write page class and page methods will be more but there will be very less time spent on writing automation test cases for the said functionality as the methods list will contain methods that supports all kind of related functionality from low level to high level as well as both positive and negative scenarios.
This is a cross post from my company blog here, where I had originally written it.
Monday, February 9, 2015
While using selenium for web automation many a time we face numerous kind of exceptions. Many of these exceptions are mainly selenium exceptions. In this blog I will write about few of those selenium exceptions, why they occur and how to resolve/avoid them.
- StaleElementReferenceException -
As the name suggests this exception occurs when the element stale, which means the element reference on which you are trying to take a action upon is no longer available on the page or has changed.
To avoid this exception, try to find the element as and when you need to take an action upon it rather than getting the element at some point of code and then reusing it at different places. - InvalidCoordinatesException -
This exception occurs when invalid co-ordinates are provided for an interaction operation like move, click, drag & drop etc.
You can avoid this exception by identifying a Webelement on the page for interaction rather than providing a co-ordinates. - ElementNotVisibleException -
This exception occurs when an element may be available in the dom but not displayed on the page and hence not able to be interacted. This exception may also occurs if the element is unable to scroll onto the browser view.
You can avoid this exception by verifying that the element is visible and then taking action on the element. There are some methods present in "ExpectedConditions" class that can be used to wait for the element to be visible. These methods are "visibilityOf(Webelement)" and "visibilityOfElementLocated(By)". - InvalidSelectorException -
This exception is thrown when the selector used to find the element is invalid or wrong, this may be a syntax error or a compound class name to be provided to "By.className" method.
To avoid this exception verify that the selector that you are using are verified on the said page using jQuery or firebug or firepath. - NoSuchElementException -
This is a common exception faced by selenium users and occurs when there is no element found on the current page by selenium based on the user provided selector to method "findElement". There can be multiple reasons for this exception to occur like selector being wrong, element taking time load(in case of ajax), earlier step execution failed etc.
You can avoid this exception by implementation by waiting for element to be present using "FluentWait" with "ExpectedConditions" class methods "presenceOfElementLocated(By)" and "visibilityOfElementLocated(By)". - UnreachableBrowserException -
This exception mainly occurs due to two reasons first when the browser to which the selenium commands has died mid-test and second when the remote server address to which the RemoteWebDriver trying to contact is invalid.
There is no proper solution to this error you can only take precautionary measures in this cases and is more or less related to hardware.
Tuesday, January 27, 2015
I had extensively worked on Java and developed different types of frameworks depending upon the project requirements. I understand the pain points of developing the framework using Selenium and Java. I also understand it takes a lot of time to develop the framework to start supporting the automation for a particular application. So when I was given a responsibility of automating an application framework using Groovy I came across "GEB" which is a Web automation framework using groovy. It is a wrapper over selenium and provides a lot of in-built utilities that will allow the end user to jump into the job of automating their test-cases and validating their application within a short period.
Geb provides a enhanced Page Object modelling to its users with the advantage of re-usability and maintainability of tests and framework. Page Object modelling/pattern as you may be knowing divides the areas on the UI under test as classes in the code. All the functionality related to the said area is put onto a class, hence reducing the probability of code duplicity.
Geb provides a enhanced Page Object modelling to its users with the advantage of re-usability and maintainability of tests and framework. Page Object modelling/pattern as you may be knowing divides the areas on the UI under test as classes in the code. All the functionality related to the said area is put onto a class, hence reducing the probability of code duplicity.
Following are few of the advantages of using Geb:
- Uses webdriver as an internal web automation framework which is well tested and used worldwide
- Uses css as the locating strategy which makes it faster and easy to use.
- Jquery like methods and usage
- Extensive list of in-built utilities that reduces code and time.
- In-built driver manager
- Support for parallel execution.
Its a complete automation framework package that reduces the automation framework development time by almost 2-3 months.
More details on Geb can be found at http://www.gebish.org/
Wednesday, September 4, 2013
Today I will be writing about Page Objet pattern which became famous when details about it was mentioned in the selenium wiki.
Page Object pattern solves one of the biggest problem that any automation framework face i.e Managing utils, managing methods, reducing code duplicity etc. It solves this by suggesting certain methodology for managing and modelling the code.
Some of the main points that it suggests are:
1. Model your application UI pages as classes in your framework. (There is no hard-and-fast rule that the whole page on the UI should be represented by one class, you can also create classes for the sections of the page if they are used in other UI pages or sections.)
2. Each class will contain methods that perform the actual functionality provided on the page or section that the said class represents.
3. Each method in the Page class should return the object of the same page class or another page class if the said method/functionality navigate the UI to a new page.
4. Method names inside the page class should represent the actual functionality that it performs.
Let’s take this by an example:
We’ll take the same example as mentioned in the Selenium wiki, you have a login page where user enters username and password. If the login is successful user will get navigated to the home page else an error will be thrown on the UI.
Now converting this use case to a Page Object pattern we’ll have two classes LoginPage and HomePage defined. LoginPage will contains functions related to entering username, entering password, login using a given username and password, clicking on submit button etc. HomePage will contain functionality related to the HomePage, in this case we will have the functionality to verify the HomePage. Below is a class representation of the Pages and their respective functionality defined as methods. There is also a sample test-class with name "SampleTest" which contains two test methods that makes the use of the Page Object Pattern defined using "LoginPage" and "HomePage" class
As you can see from the above test-method in the above “SampleTest” class, there two different test-methods. Both the test-methods have the same flow defined in them , the only difference is the way they had been written. Also you can notice that the test are more readable when implemented by following the Page Object model. Without a Page Object Pattern the tests may have been more cluttered and non-readable.
Advantages of using Page Object pattern:
- Better code management
- Reduced code duplicity
- In case of change in functionality very minimal changes required to be done. Change the respective page method and it will automatically take effect in all test-methods.
- Testcases are more readable.
Page Object pattern solves one of the biggest problem that any automation framework face i.e Managing utils, managing methods, reducing code duplicity etc. It solves this by suggesting certain methodology for managing and modelling the code.
Some of the main points that it suggests are:
1. Model your application UI pages as classes in your framework. (There is no hard-and-fast rule that the whole page on the UI should be represented by one class, you can also create classes for the sections of the page if they are used in other UI pages or sections.)
2. Each class will contain methods that perform the actual functionality provided on the page or section that the said class represents.
3. Each method in the Page class should return the object of the same page class or another page class if the said method/functionality navigate the UI to a new page.
4. Method names inside the page class should represent the actual functionality that it performs.
Let’s take this by an example:
We’ll take the same example as mentioned in the Selenium wiki, you have a login page where user enters username and password. If the login is successful user will get navigated to the home page else an error will be thrown on the UI.
Now converting this use case to a Page Object pattern we’ll have two classes LoginPage and HomePage defined. LoginPage will contains functions related to entering username, entering password, login using a given username and password, clicking on submit button etc. HomePage will contain functionality related to the HomePage, in this case we will have the functionality to verify the HomePage. Below is a class representation of the Pages and their respective functionality defined as methods. There is also a sample test-class with name "SampleTest" which contains two test methods that makes the use of the Page Object Pattern defined using "LoginPage" and "HomePage" class
As you can see from the above test-method in the above “SampleTest” class, there two different test-methods. Both the test-methods have the same flow defined in them , the only difference is the way they had been written. Also you can notice that the test are more readable when implemented by following the Page Object model. Without a Page Object Pattern the tests may have been more cluttered and non-readable.
Advantages of using Page Object pattern:
- Better code management
- Reduced code duplicity
- In case of change in functionality very minimal changes required to be done. Change the respective page method and it will automatically take effect in all test-methods.
- Testcases are more readable.
Wednesday, August 15, 2012
CSS locating strategy is the fastest way to identify an element compared to any other locating strategy currently available. Many of the automation frameworks now uses CSS as a locating strategy for locating elements. In this blog I will explain about how to identify an element using CSS locators.
1. Locate an element using id.
Consider the above html code of a text box and a button, you can locate the above elements in html using there "id" attribute value.
In CSS an element with id is identified by appending "#" to the start of its id attribute value. For ex the above text element can be located as "#textid". Similarly the button element can be identified as "#buttonid"
2. Locate an element using class attribute.
Consider the above html code of a text box and a button, you can locate the above elements in html using there "class" attribute value.
In CSS an element with class is identified by appending "." to the start of its class attribute value. For ex the above text element can be located as ".textusingcls". Similarly the button element can be identified as ".btnusingcls"
Locating an element having more than one class, for ex. consider the following html code.
3. Locate an element using html tag.
Consider the above html code of a text box and a button. To locate the above elements using there html tags in css, you just have to use the tag name. For ex. The above two elements can be located as "input" and "button" respectively.
4. Locate an element using html tag, class, id all together.
Consider the above html code of a text box and a button. You can locate the above element using a combination of html tag, id and class all together. To do that just append all the three independent locator together. For the above element the unique locators will be "input#textidcls.textidcls" and "button#buttonidcls.buttonidcls" respectively.
5. Locate element based on Parent element:
Consider the above html code, if you try to find the text element using css locator ".textcls" you will get 3 elements in return. In case you need to find the text box under the div element with id "parent" you can locate it using the locator "#parent .textcls", but this also gives you 2 text box elements that are under the element with id "parent".
To find the direct child element of the "#parent" element you can use the ">" sign of css locator. So if you use the locator "#parent > .textcls" it will give you the direct child of "parent" element.
In css if you use space between the parent and child element, it will return all the child elements under the said parent irrespective of the hierarchy.
6. Identify an element using attribute value:
Consider the above html code, there are 4 text boxes each having a different value for attribute "test".Each element can be uniquely identified by using the following strategies:
I had created a dummy html page which can be download from the following link: CSS-Locator-demo
To use the demo for identifying a locator:
1. Unzip the downloaded zip and open the page "css_locator.html" in chrome or firefox.
2. Now go to console in the browser. For Chrome: Right Click -> Inspect Element -> Console. For Firefox: Right Click ->Inspect with Firebug -> Console.
3. Use the jquery way of identifying element in the cosole. To do that type $('<Ur CSS locator>') under console and press enter.
Jquery will find the matching element for the said css locator and display it on the console as a link. If you hover on the said link it will highlight the said element.
More info on CSS selector at W3C link
1. Locate an element using id.
<input id="textid" value="textid" type="text"/>
<button id="buttonid">Button wiht id</button>
Consider the above html code of a text box and a button, you can locate the above elements in html using there "id" attribute value.
In CSS an element with id is identified by appending "#" to the start of its id attribute value. For ex the above text element can be located as "#textid". Similarly the button element can be identified as "#buttonid"
2. Locate an element using class attribute.
<input class="textusingcls" value="Text with single class" type="text"/>
<button class="btnusingcls">Button with single Class</button>
Consider the above html code of a text box and a button, you can locate the above elements in html using there "class" attribute value.
In CSS an element with class is identified by appending "." to the start of its class attribute value. For ex the above text element can be located as ".textusingcls". Similarly the button element can be identified as ".btnusingcls"
Locating an element having more than one class, for ex. consider the following html code.
<input class="textcls1 textcls2" value="Text with multiple class" type="text"/>
<button class="buttoncls1 buttoncls2">Button with multiple Class</button>
The above element can be identified by replacing the spaces between the class values with "." . The above elements can be located as ".textcls1.textcls2" and ".buttoncls1.buttoncls2" respectively.3. Locate an element using html tag.
<input id="textid" value="textid" type="text"/>
<button id="buttonid">Button wiht id</button>
Consider the above html code of a text box and a button. To locate the above elements using there html tags in css, you just have to use the tag name. For ex. The above two elements can be located as "input" and "button" respectively.
4. Locate an element using html tag, class, id all together.
<input id="textidcls" class="textidcls" value="Text with id and class" type="text"/>
<button id="buttonidcls" class="buttonidcls">Button with id and Class</button>
Consider the above html code of a text box and a button. You can locate the above element using a combination of html tag, id and class all together. To do that just append all the three independent locator together. For the above element the unique locators will be "input#textidcls.textidcls" and "button#buttonidcls.buttonidcls" respectively.
5. Locate element based on Parent element:
<input class="textcls" value="Text" type="text"/>
<button class="buttoncls">Button</button>
<div id="parent">
<div id="project-label">4. Locate Child element:</div>
<input class="textcls" value="Text" type="text"/>
<button class="buttoncls">Button</button>
<div>
<input class="textcls" value="Text" type="text"/>
<button class="buttoncls">Button</button>
</div>
</div>
Consider the above html code, if you try to find the text element using css locator ".textcls" you will get 3 elements in return. In case you need to find the text box under the div element with id "parent" you can locate it using the locator "#parent .textcls", but this also gives you 2 text box elements that are under the element with id "parent".
To find the direct child element of the "#parent" element you can use the ">" sign of css locator. So if you use the locator "#parent > .textcls" it will give you the direct child of "parent" element.
In css if you use space between the parent and child element, it will return all the child elements under the said parent irrespective of the hierarchy.
6. Identify an element using attribute value:
<input test="excttxt" value="Text for exact attr. value" type="text"/> </br>
<input test="strttxt1234" value="Text for starts with option" type="text"/> </br>
<input test="1234endtxt" value="Text for ends with option" type="text"/> </br>
<input test="12cntntxt34" value="Text for contains option" type="text"/> </br>
Consider the above html code, there are 4 text boxes each having a different value for attribute "test".Each element can be uniquely identified by using the following strategies:
- By exact attribute value: 'input[test="excttxt"] '
- By using starts with: 'input[test^="strttxt"]'
- By using ends with: 'input[test$="endtxt"]'
- By using contains: 'input[test*="cntntxt"]'
I had created a dummy html page which can be download from the following link: CSS-Locator-demo
To use the demo for identifying a locator:
1. Unzip the downloaded zip and open the page "css_locator.html" in chrome or firefox.
2. Now go to console in the browser. For Chrome: Right Click -> Inspect Element -> Console. For Firefox: Right Click ->Inspect with Firebug -> Console.
3. Use the jquery way of identifying element in the cosole. To do that type $('<Ur CSS locator>') under console and press enter.
Jquery will find the matching element for the said css locator and display it on the console as a link. If you hover on the said link it will highlight the said element.
More info on CSS selector at W3C link
Wednesday, February 8, 2012
Its been long time that I had written a blog here. I was busy with writing an android automation tool for native android application testing, named as "bot-bot". Please take a look at it and let us know your comments. This is work-in-progress and there are lot-of features that still have to implement.
Following are some of the links giving information on bot-bot:
Webpage:
http://imaginea.github.com/bot-bot/
Github page:
https://github.com/Imaginea/bot-bot
Most of the things are covered in the webpage or in Github.
In case of issues you can contact me or send a mail.
Following are some of the links giving information on bot-bot:
Webpage:
http://imaginea.github.com/bot-bot/
Github page:
https://github.com/Imaginea/bot-bot
Most of the things are covered in the webpage or in Github.
In case of issues you can contact me or send a mail.
Thursday, October 20, 2011
Most of the guys who are into
automation may be knowing the term Data-Driven testing. But the word
will still be new for some fresh faces in the field of automation. In this blog I will explain what is Data-Driven testing and will give an example of Data-driven testing using Junit and TestNG frameworks.
Data-Driven testing as the name
suggests is a test driven by the Data. For ex. You have a user detail
form where you need to enter details of multiple users and save them.
Here if you have 5 different user data available and you have to
write automation cases for these, you may end up writing 5 different
automation test cases(one for each user data).
If you apply a Data-Driven approach you
will end up with only one test-case, that is filling the form with
user data and then submitting it. The test case will get executed
based on the data provided to it. In this case it will be 5 and hence
the test case will get executed 5 times with different data-set.
The advantage of using a
Data-driven approach is that you reduce your effort in writing/maintaing test-cases for your different type of data. In case of
additions or deletion of new/old entries , you just have to change the
data and not your actual test-case.
Following I will mention a Data-Driven
approach for searching on google with different data using Junit and TestNg frameworks:
Using Junit:
Using TestNg:
Using Junit:
Using TestNg:
The main difference in the above two functions is that you provide a Paramaterized option to the class in Junit and supply data to the constructor of the said class. Where as in TestNG you do the same at the test-method level.
Its simple to do data-driven testing in TestNG framework as you can provide a different data providing function for each test-method, but the same is not possible in Junit.
Subscribe to:
Posts
(
Atom
)