Selenium WebDriver is the most powerful and famous tool of selenium toolkit. Using Selenium WebDriver, one can write automated web application UI tests in any programming language against any website. It is an open source and free tool that extends support to almost all the major browsers. Selenium WebDriver supports multiple programming languages like Java, C#, Ruby, Python, Javascript and PHP. To make the automated testing using Selenium WebDriver more efficient, test automation frameworks are used.
A test automation framework is basically a well integrated coding pattern that shapes up the thought and clarity of automation testing for better results and understanding. The main function of a framework is to direct the behavior of the test performed. There are various types of frameworks that suit the different test operations. Some of the most popular ones are :
~ Module Based Framework: In this framework, the entire Application under Test ( AUT) is divided into smaller, independent modules and tests scripts that are created for each modules. One can combine the smaller test scripts in order to generate a larger master test script so that they can accomplish the desired test scenarios. This master script is used to call the individual test scripts to run end to end test scenarios. In this framework, testers write function libraries to use it whenever required.
~ Data-driven Framework: In this framework, the Test Script and Test Data are separated from each other. This allows us to create test automation scripts by just passing different sets of test data. The Test data does not needs to be hard-coded. The test data set is kept in the external files or resources such as MS Excel Sheets, SQL Database Tables, XML files, etc. The test scripts accesses the test data from the external resources. By using this framework we could make the test scripts work properly for different sets of test data. This framework significantly reduces the effort required for creating multiple scripts for different data.
~ Keyword Driven Framework: In a Keyword-driven testing, they use the table format to describe the keywords or action words for each of the functions or methods that needs to be performed. It executes automation test scripts based on only those keywords that have been stated in the excel sheet. With the help of this Framework, it is easy for testers to work with keywords for developing a test automation script. The logic to read keywords and call for the needed action cited in the external excel sheet is located in the main class. Hence, Keyword-driven testing is very much similar to data-driven testing.
~ Hybrid Framework: A Hybrid framework is a modeled testing automation framework that involves various features of the above mentioned three frameworks as described. It involves taking the required attributes of other frameworks for creating the most suitable framework for complex testing scenarios. It also has the ability of keyword driven, data driven and module based operations too. Thus, it is one of the most efficient testing frameworks to be used for complex tests.
One of the most commonly used testing framework is the TestNG framework. The NG in TestNG stands for Next Generation. The TestNG framework covers a wider range of test categories: unit, functional, end-to-end, integration, system, regression, etc. This enables the developer to write more flexible and dynamic tests with the help of annotations, grouping, prioritizing & parametrizing. TestNG can be used for creating a hybrid framework involving Data driven and Module Based frameworks.
It has been inspired by JUnit and NUnit but introduction of some new functionalities make it much better and easier to use than before, The functionalities added are:
Annotations: Various Annotations are available in TestNG
– @BeforeSuite: It will run before all tests in this suite.
– @AfterSuite: It will run after all tests in this suite have been executed.
– @BeforeTest: This method runs before the test method that belongs to the classes inside the tag is performed.
– @AfterTest: This method runs after each and every test methods that belong to the classes inside the tag are executed.
– @BeforeGroups: This method runs shortly before the first test method that belongs to any of these groups has been performed.
– @AfterGroups: This method will run shortly after the last test method that belongs to any of these groups have been executed.
– @BeforeClass: This method runs before the first test method in the current class is applied.
– @AfterClass: This method runs only after all the test methods in the current class have been performed.
– @BeforeMethod: This method will run before all the test method.
– @AfterMethod: This method runs after all the test method.
– @Test: This method is a part of a test case.
~ Grouping: TestNG allows to perform groupings of test methods. We can also execute only set of groups while excluding another groups. This gives us the maximum flexibility in dividing tests according to the requirement. Groups are specified in the testng.xml file and are found under the <test> or <suite> tag. Groups that are specified in the <suite> tag will apply to all the <test> tags underneath it. The groups can be included or excluded using the IDE tools like Eclipse, but the using the testng.xml file is more better.
~ Prioritizing: In TestNG, Priority is used to set the sequence in which the test cases have to be executed. Without prioritizing, the test cases may execute in the disorderly sequence, instead of sequence they are ought to be executed. When there are multiple test cases representing an end-to-end functionality, they have to be executed in a sequence for proper testing. For example, we need to execute a Login test case before executing other test of other functionality in the application. To set priority, we need to set the priority beside the @Test annotation like @Test(priority=0). The test case with priority=0 will be executed first, then the test cases with priority=1, and so on. If you do not give priority to all test cases, then all the tests will be executed in the alphabetical order of their class names.
~ DataProvider: A Data Provider is a method annotated with @DataProvider. A Data Provider returns an array of objects. It means marking a method to supplying data for a test method. The annotated method has to return an Object[][] where each Object[] can be assigned the parameter list of the test method. The @Test method ifit wants to collect data from this DataProvider then it needs to use a dataProvider name equivalent to the name of this annotation.
~ Parallel Tests: Your site will look, feel and perform differently in different browsers. That is because different browsers understand some code slightly differently. Each project needs you to perform multi-browser testing to ensure that the functionality is working as expected in all the browsers to give same user experience to all the audience. TestNG gives us a functionality to execute the same test on different browsers in a simple and easy way. Using the feature provided by TestNG for Parallel Executions, we can execute same tests on various browsers at the same time, helping to perform cross browser testing in very less time.
~ Reports and Asserts: TestNG also has the logging facility for tests. During the execution of a test case, user may want some information to be written on the console, after failure of certain tests or some other event. With the help of TestNG Logs, it is possible to use logging during the Selenium test case execution. TestNG also has the ability to take decisions in the middle of the test run with the help of Asserts methods. Asserts are mainly used to check the presence of an element during a test. There are three main assert statements :
– Assert.assertTrue(): Assert.assertTrue() statement fails the test and stops the execution of the test, if the required element is not found.
– Assert.assertFalse(): Assert.assertFalse() statement fails the test, if some element which not supposed to be present in the page, is present.
– Assert.assertEquals(): Assert.assertEquals() statement fails the test, if the expected value of an element is not equal to the expected value.
~ Reports and Listeners: TestNG provides a very helpful reporting feature. After the test execution, a test-output folder is generated at the root of project. This folder contains two type of Reports:
– Index.html: It is an html report containing information like results, errors, test time, reporter logs, testng XML files, etc.
– emailable-report.html: This is a summarized report of the test execution which contains passed test case messages in green and failed test cases messages in red.
By implementing two interfaces, ITestListener and IReporter, the reports can be customized as per requirement. The ITestListener enables to get a report between execution of the tests, while the IReporter is able to generate a final report after the test is executed. Screenshots can be taken simply by typecasting WebDriver to the screenshot interface. Log files can be generated by implementing the Log4j logging utility.
TestNG has many more features which make it one of the favourite and commonly used framework for Test Automation. Thus, a framework is a crucial necessity for performing automated test operations using Selenium. There are multiple test frameworks available, but choosing the right type of framework based on the type of test to be performed on the application matters the most. Choosing the right framework will not only help with the desirable results but also make the automated testing operation more efficient and effective.