Going ahead on the theme of testing publicly available websites using selenium, in this blog I want to work out an example which touches on following points:
• Run Selenium WebDriver tests under TestNG
• Use the following best practices:
◦ Page Object Model
◦ UI Mapping
◦ Data Driven Testing
◦ And most importantly take screenshot on an event of test failure
Now to do all of the above I need a publicly available website that sounds interesting enough and devise testing around it. So here we go...
Test the DirectGov Council Tax Band website, given postcode, first line of address and an expected band.
The site that we are using is http://www.voa.gov.uk/cti/InitS.asp
In the test case above we have used various testng annotations. The important one is @Listeners which basically defines one or more of users classes, which implement some interfaces that TestNG provides, so that when TestNG raises certain events it will look for all “classes” which are basically looking for such events and call the respective event handlers in them. In this case we are using it to define handler for onTestFailure so that we can capture the screenshot and include it in testng report.
We are also using the @DataProvider annotation to make our test case data driven which basically means that we have more than 1 data set points to run through same test case.
• Run Selenium WebDriver tests under TestNG
• Use the following best practices:
◦ Page Object Model
◦ UI Mapping
◦ Data Driven Testing
◦ And most importantly take screenshot on an event of test failure
Now to do all of the above I need a publicly available website that sounds interesting enough and devise testing around it. So here we go...
Test the DirectGov Council Tax Band website, given postcode, first line of address and an expected band.
The site that we are using is http://www.voa.gov.uk/cti/InitS.asp
Setup Java project with Maven
Now import the maven project in your favorite IDE. I am using Eclipse.
Since we are going to use selenium with testng instead of junit my pom.xml looks like following.
Test and Helper Class
Now that we have got a Java project set up, lets get cracking. Since I have already done my code before writing this blog, my final directory tree looks like following.
Let me add a bit of context around what is the role of each file in the directory tree above is. Later I will add more details about each of the relevant files.
- CTaxBandTest.java - This is the testng file that contains all the test cases just by the virtue of keyword 'test' in the file name.
- CTaxBandSearch.java - Page Object Model Class. The Page Object Design Pattern provides the followingadvantages.
- There is clean separation between test code and page specific code suchas locators (or their use if you’re using a UI map) and layout
- There is single repository for the services or operations offered by thepage rather than having these services scattered through out the tests.
- WebDriverManager.java - This class will manage the singleton WebDriver instance that will be used in our tests. The importance of this manager class will become relevant when we go through the details of how to get screenshot on test failure.
- ScreenShotOnFailure.java - Contains code to capture and store screenshot on event of test failure at correct location so that these screenshots is available via test reports.
- ui_mapping.properties - UI Mapping file. Contains all the UI locators in one file for ease of modification.
CTaxBandTest.java
In the test case above we have used various testng annotations. The important one is @Listeners which basically defines one or more of users classes, which implement some interfaces that TestNG provides, so that when TestNG raises certain events it will look for all “classes” which are basically looking for such events and call the respective event handlers in them. In this case we are using it to define handler for onTestFailure so that we can capture the screenshot and include it in testng report.
We are also using the @DataProvider annotation to make our test case data driven which basically means that we have more than 1 data set points to run through same test case.
WebDriverManager.java
This is a static helper class which manages the only Selenium WebDriver instance we are using for all test cases. This makes access to this webdriver instance very easy later on when we want to take a screenshot on event of test failure.
CTaxBandSearch.java
This is the Page Object Model class which knows how to navigate the website page which we want to test. As you can see I have also made use of UI Mapping best practice here and get all locators from a properties file.
ScreenShotOnFailure.java
And lastly the listener class which will override the onTestFailure handler of testng and help us get the crucial screenshot when a test case fails.
We can get hold of the webdriver instance very easily since its now managed by a WebDriverManager class. Above class also demonstrates how to include the screenshot link in the test reports.
Run the test
Now that we have all the bits and pieces in place lets run our test case. You might have not noticed but I have deliberately included a council tax band incorrect in our @DataProvider which means that 1 test case will pass and 1 fails with a screenshot!
The reports are generated in target/surefire-reports/ directory and looking at the reports... hurray screenshots at last!!!
This has been kind of a long post, but I did want to touch on a few points in one go and glad that its out there now.
Greetings,
ReplyDeleteI have added your ScreenShotOnFailure.java to my project and everything is OK except here:
WebDriver driver = WebDriverManager.getDriverInstance();
The system does not recognize the "WebDriverManager". Do you have nay idea why?
Many thanks in advance,
- Miguel
I forgot to say that his is a great explanation on how to do it
ReplyDeleteHello Nishant
ReplyDeleteplease send me your testNG.xml and ui_mapping.properties of this project
to vpham2000js@gmail.com .
Great article
Thanks
Very nice Article
ReplyDeleteHi! Can you send me the project to my mail? Pimagra@gmail.com
ReplyDeleteThanks!
A nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
ReplyDeleteSelenium Training in Chennai
This is a nice article here with some useful tips for those who are not used-to comment that frequently. Thanks for this helpful information I agree with all points you have given to us. I will follow all of them.
ReplyDeleterpa training in bangalore
best rpa training in bangalore
RPA training in bangalore
rpa course in bangalore
rpa training in chennai
rpa online training
Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
ReplyDeleteAWS Course Interview Questions and Answers 2019 | AWS Interviews Questions and Answers for Devops
AWS Tutorial 2019
AWS Interview questions and answers |AWS Interview Question and Answers 2019
AWS Tutorial for Beginners 2019 | AWS Tutorial with AWS Training Videos
Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
ReplyDeletepython training in rajajinagar
Python training in bangalore
Python training in usa
Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
ReplyDeleteAWS Training in Bangalore | Best AWS Amazon Web Services…
Amazon Web Services Training in Pune India
AWS Online Training in India | AWS Training | AWS online training
AWS Training in Bangalore | Aws training in Bangalore with placements
The knowledge of technology you have been sharing thorough this post is very much helpful to develop new idea. here by i also want to share this.
ReplyDeleteAWS Training in pune
AWS Online Training
AWS Training in Bangalore
Nice blog..! I really loved reading through this article. Thanks for sharing such an amazing post with us and keep blogging...Well written article Thank You for Sharing with Us
ReplyDeleteSalesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune
A charming conversation is worth remark. I believe that you should distribute more on this topic, it probably won't be an untouchable issue yet for the most part individuals don't examine these issues. To the following! Kind respects!! best interiors
ReplyDelete