Google+

287. Implementing JUnit Annotations






In our previous post we've used only @Test JUnit annotation. But in order to make the code more organized we've to divide the code under three JUnit annotations i.e. @Before, @Test and @After annotations.

Before going forward please refer to our previous post Post#9 JUnit Annotations.

Please follow the below steps to implement the JUnit Annotations:

1.  View the previous post code where we have used only @Test JUnit Annotation as shown below:



2. Create a method setUp( ) as shown below:




3. Specify the newly created setUp( ) method with @Before JUnit Annotation as shown below:



4. Ensure that an error is displayed, Hover your mouse cursor over the @Before Annotation and select "Import Before (org.junit) " option as shown below:



5. Ensure that the error got resolved and the selected Import statement got added as shown below:



6. Now we have to move all the selenium commands which fall into the setup category inside the openSelenium143( ) method  into the 'setUp( )' method as this method is now specified with @Before JUnit annotation as shown below (i.e.The Set up related statement for creating Selenium WebDriver Object and launching the Firefox Browser need to be moved under a method specified with @Before JUnit Annotation ):



7. As you have observed that after moving the setup related selenium statement into the setUp( ) method which is specified with @Before JUnit Annotation, all the non-moved commands in openSelenium143( ) method are showing errors as shown below:



8. All these errors are displayed as _driver instance object is not accessible to the statements in openSelenium143( ) method. As we have moved the _driver instance object declaration statement into the setUp( ) method, the scope of the _driver object is limited to setUp( ) method. All the methods outside the setUp( ) method cannot access this _driver object. In order to resolve this problem, we've to first break the following statement in the setUp( ) method into two parts as shown below:

Before Breaking:



After Breaking:



9. Did the Error got resolved ? The Answer will be No. Still you have something to do. i.e. After breaking the statement into two, you have to move the _driver instance object declaration statement outside the 'setUp( )' method such that it gets accessible by all the remaining methods inside the class as shown below (i.e. changing the scope of the _driver object, such that it gets accessible by all the methods in the Class) :



10. Did the Error messages got resolved ? The Answer is Yes now.
11. Create another method say tearDown( ) as shown below:



12. Specify the newly created tearDown( ) method with @After Annotation as shown below:



13. Ensure that an error is displayed as shown below, Hover your mouse cursor over the @After Annotation and select "Import After (org.junit) " option as shown below:




14. Ensure that the error got resolved and the selected Import statement got added as shown below:




15. Now we have to move all the selenium commands inside the openSelenium143( ) method which fall into the ending checklist category into the 'tearDown( )' method as this method is now specified with @After JUnit annotation as shown below (i.e. All the selenium commands that need to be performed after executing any Test ( For example:- Closing the Browser statement)  need to be moved under a method specified with @After JUnit Annotation ):




This is how we have to organize the Selenium Commands/Statements under the methods specified with different JUnit Annotations i.e. @Before, @Test and @After.

16. Lets Run the Selenium WebDriver Test using JUnit  (i.e. After moving the selenium commands/statements  to respective methods having different JUnit annotations) . Watch the below video to know how the test has run:

Click here to watch the video

Download this Project:

Click here to download the project used in this post and import into  Eclipse IDE on your machine.




Please comment below to feedback or ask questions.

'WebDriver' Class and its Predefined Methods will be explained in the next post.



No comments:

Blog Index