Test Automation Framework – Selenium Part 2.
Part 2 is the follow on from Part 1 of our beginners step by step guide in setting up a basic Test Automation Framework for Web Application Testing.
The Framework was created by the Exactest offshore team and is being successfully implemented by a number of our clients for automated regression testing. When deciding to create this Framework, our primary objective was to give Test Teams a user friendly framework which would easily allow the extension of additional test cases without too much coding knowledge. The framework is structured to give a logical and easily maintainable extendable automation solution.
Follow Part 2 below to download the base Framework and start to execute your first tests. Please make sure that you have already completed Part 1 and verified that your tooling is installed and configured correctly.
Test Automation Framework – Eclipse Setup
- 01. Configuration: A Configuration file is used to hold property values in the config.properties file. These relate to the properties used in the Test Framework file
- 02. Page Objects: Page objects are designed to store static information of specific web pages.
For example the class PageAbout.java will include About page url,title,contents and all required web elements and their access methods. This class can be reused in all the scripts using that element. In future, if there is a change in the web element you just need to make the change in just 1 class file and not 10 different scripts. - 03. Common Services: Contains reusable functions that can be used all over the framework to achieve some tasks.
For example the ReadExcel.java can be used all over the framework to read excel data files.
Also the ImageValidator.java class can be reused to image validation tests all over the framework. - 04. Test Implementation : Contains the implementation steps for the Gherkin statements defined in the cucumber feature files.
- 05. Test Runner: This is the main class to execute the automation tests.
Test cases and test suites can be controlled from here by using cucumber options. - The above highlighted field is a cucumber tag, these tags are used to specify which test scenario’s are to be executed
- To run the tests you should press Run from the eclipse menu as per the screenshot underneath.
- 06. Config: Configurations relating to the framework are stored in here.
config.properties file is used to specify the for given path of the selenium chrome driver (Set your local chrome driver path here, Use two backslashes when given path “\\” )
extent-config.xml file is used to configure the output of test results - 07. Cucumber Features : This is the initial stage of any new tests.
In the feature files, tests are written using Gherkin statements such as “Given”, “When”, “Then”. These test scenarios should be written so that they can be understood by all stakeholders such as Product Owners, Business Analysts, Testers and Developers. - 08. Test Data : This is the location where the test data can be stored
For example the TestPageAbout.java class includes all the implementation steps for the statements relating to the about page cucumber feature file.
Test Class Structure
Each test class can have Before, After and Test methods.
SetUp methods used to prepare all preconditions(Open Browser, Navigate to URL etc) to start tests methods.
TearDown method used to clear test environment(Close browser) for each tests
<
Cucumber Introduction:
framework which is used to write acceptance tests for the web based applications. It allows automation of functional validation in easily readable and understandable format (like plain English) to Product Owners, Business Analysts, Developers, Testers, etc.
Feature Files
- Ability to categorize and prioritize test cases by using tags(User can specify what tests to run using tags )
- Ability to get idea about ongoing test execution (It shows test execution test step wise)
- Test report-Cucumber support Extent report plugin(We can see test results very clearly and graphically)
- Test Result
- Test result are updated after every test run. We have the ability to see test results by opening the highlighted html page below.
- You can check test result by expanding the target folder and opening the html file.
- All test results are categorized according to the scenarios that have been executed.
- Step 1-Create feature file with steps:
- In the Project Explorer expand “src/test/resources” folder (If you expand features folder you can see feature files created for 5 test pages), right-click a features folder On the context menu of the target directory, choose New | File,
- In the New File dialog box, type the name of the file
and give it a .feature name extension - Write the cucumber script. In BDD terms the scenario would look similar to the following.
- Please read Feature file section above for more information on writing Feature files
- You will notice colored part of the tests (Feature, Scenario, Given and Then). These are keywords defined by Gherkin. Gherkin have more keywords but the above keywords are sufficient for us to cover the exactest test scope.
- As you see above some lines are highlighted. This indicated that those lines haven’t matching steps definitions
- Step2-Generate step definition:
- Step Definition is a small piece of code with a pattern attached to it that links it to a specific Gherkin statement in one of the Feature files. Use must write a java method in a class with the specific annotation above it.
To generate step definition code you should run the feature file you created. To run the feature file you should update test runner class with the feature tag you created above.
- To run the tests you should press Run from eclipse menu as follow
- Step 3-Add step definition:
- Copy the above missing step definition and paste it into test java method in test definition Section
- You should have a test class to paste above step definition into. We have specified how to do this in Step 4 below
- Step 4-Implement Test Class
- In the Project Explorer window expand “src/test/java” folder (If you expand “ie.exactest.tests” you can see java test classes created for 5 test pages), right-click on “ie.exactest.tests” package. On the context menu of the target directory, choose New | Class,
- In the New File dialog box, type the class name
and press “Finish” - Write you java test methods and paste step definition above it……
- See code comments of java selenium projet’s test pages for more details about implementation of test class.
- Step 5-Implement Page object class for new test class
- In java-selenium framework there are 5 separate page objects classes for 5 web pages. So if you need to add new page to the current test scope you will need to implement a new java class for that class’s page objects(include new page url,title,contents and all required web elements and their access methods)
- Add new page objects class>
- To add a new page object class expand the “src/main/java” folder in the Project Explorer (If you expand “ie.exactest.pages” you can see java page object classes created for 5 pages). Right-click on “ie.exactest.pages” package. On the context menu of the target directory, choose New | Class,
- In the New File dialog box, type a new class name
and press “Finish” - Initiate you new page url,title,contents and all required web elements and their access methods. Please check existing page object class comments for more details.
Adding new test cases
After running the feature file it will generate a skeleton methods under the console tab. These can be used to implement the missing step definition
Exactest is an Independent Software Testing Services company based in Dublin Ireland. We have helped a wide and varied range of clients in successful IT Delivery in areas such as Strategy, Process, Automation, Performance and Acceptance. If you need assistance with your IT Testing capability please Contact Us for a friendly discussion.