12. 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 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 inside the openSelenium143( ) method which fall into the setup category into the 'setUp( )' method as this method is now specified with @Before JUnit annotation as shown below (i.e. All the Set up related commands like Creating Selenium Object,  Specifying the Test Browser, Specifying the Base URL and Starting the Selenium Server Driver need to be moved under a method specified with @Before JUnit Annotation ):


7. As you have observed that after moving the setup commands into the setUp( ) method, all the non-moved commands in openSelenium143( ) method are showing errors as shown below:


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

Before Breaking:


After Breaking:


9. Did the Error got resolved ? The Answer will be No. Still you have to something to do. i.e. After breaking the statement into two, you have to move the 'object1' 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. the scope of the object is now changed to the complete class) :


10. Did the Error 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, 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 like 'Stopping the Selenium Server Driver' etc need to be moved under a method specified with @After JUnit Annotation ):



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

16. Lets Run the Selenium RC Test (i.e. After moving the selenium commands to respective methods having different JUnit annotations) using JUnit. 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.

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





1 comment:

  1. Next post link on this page is not working........Please take care of this.....

    kudos to you.....

    Thnx!!!

    ReplyDelete

Followers

Labels