JUnit framework in Selenium
It is a java unit testing framework where we can implement the scripts in java programming language and it can be executed. We can develop the test scripts in junit framework in selenium in the same way as we implement the test cases in manual testing. Junit is used to test the small codes, so that defects can be identified by the developers at the early stage of development process.
How to use Webdriver with Junit in eclipse?
- Take a Junit class by right clicking on the project >> click on New >> select Junit test case.
- Give the Package Name as Junit Practice and give the class name as practice_01 >> check the checkboxes of setup and tear down >> click on Finish
- Junit contains multiple annotations like @before, @test, @after etc.
@before: It contains setup method where it acts as a Pre – Condition.
- When we run the script, setup method will be executed initially.
@test: It represents the test scenario, where it contains the test steps.
- It is responsible for execution, it act as a main method.
@after: It contains tear down method, which is responsible to stop the execution.
JUnit Execution Flow:
- When we run the script initially, it invokes @before method then @test then it executes @after
- The same process will be continue for all the @test
- Once the execution is completed, it will provide the results in the Junit section of eclipse.
Limitations of JUnit:
There are some limitations or disadvantages of Junit framework in selenium
- For each and every, the @before and @after will be applicable, so we need to maintain all the test very independently.
- Once the execution is completed, it is providing the result in Junit console section of eclipse but not in any external file.
- To perform batch execution, it is having the only option that runs all the tests in selected project but it is not having the option to run the selected classes & selected methods.
- It is having very limited annotations: @before class, @before, @test, @after, @after class.
To perform batch or test suite execution, Right click on the script >> click on Run as >> select Run Configuration >> click on Radio button >> run all the test in the selected project >> click on Apply >> click on Run.