Google+

284. Using JUnit annotation in our Selenium WebDriver Tests







In order to Run a Selenium WebDriver Test using JUnit, we have to add few JUnit Annotations to the Selenium WebDriver Test code:

To start with lets use the following JUnit Annotations in our Selenium WebDriver Tests:
  1. @Before
  2. @After
  3. @Test

As we've already run a Selenium WebDriver Test code in Eclipse IDE i.e Run As -> JUnit Test in our previous post#8 Run the Created Selenium WebDriver Test. Lets look into the Test code and find out whether it has JUnit annotation specified or not.

1. View the Selenium WebDriver Test code which we have already Run as shown below and find out whether the code has JUnit annotations specified:



2. After viewing the Test code its very clear that the Selenium WebDriver Test is using the JUnit Annotations @Before, @Test and @ After.

@Test

All the methods in the class which are specified as @Test as shown in the below screenshot will be executed  by the JUnit  (i.e. when we select Run As -> JUnit Test ) as shown below:



@Before

All the methods in the class which are specified as @Before as shown in the below screenshot will be executed  by the JUnit  before executing the methods which are specified as @Test in the same class. @Before is mainly used to setup some objects needed by tests. @Before specified methods are a kind of setup methods. (as shown below):



@After

All the methods in the class which are specified as @After as shown in the below screenshot will be executed  by the JUnit after executing the methods which are specified as @Test in the same class (as shown below):



Running the Selenium WebDriver Test code without Annotations:

1. Comment the JUnit annotations from the Selenium WebDriver Test code as shown below:



2. Run the Selenium WebDriver Test code with JUnit Test after commenting the annotations as shown below:


After looking the above screenshot, its very clear that  we cannot run the Selenium WebDriver Test with JUnit if the code don't contain any JUnit annotations.

3. Now remove the comments for the annotations as shown below:



4. Try to Run the Selenium WebDriver test using JUnit as shown below:



5. Hence, its very clear that we can run the Selenium WebDriver Test using JUnit annotations if and only if the test code contains JUnit annotations else JUnit Test option wont be available to Run the test.




Please comment below to feedback or ask questions.

Eclipse Automatically adds the Package Name into the newly created Java Class will be explained in the next post.



2 comments:

Unknown said...

Awesome videos to watch at home & awesome blogspot to study in office.

Arun Motoori said...

Thank you Anurag :)