54. Refactoring the Selenium Automation code to make it readable




Till now we've Refactored the code by:

1. Increasing the @Test Annotation methods to hold the single Tests (i.e. Now allowing @Test Annotation method to have more than one Test)

2. Using @BeforeClass and @AfterClass JUnit instead of @Before and @After Annotation to reduce the execution time or to reduce the number of the times the same code under the @Before and @After JUnit annotations is executed.

Now, we are going to Refactor the Selenium Automation Code by making it readable:

1. Lets view the current code containing few things that are not readable  as shown below:


2. To start with, we've created a Selenium  Class object and named it as 'object1' above. But when you are going through the remaining steps like object1.start( ); etc, This statement is not self explanatory i.e. As a reader you will understand that you are staring an object but you are not able to understand that you are starting selenium using this statement. In order to make it more readable for the reader, we've to change the 'object1' to 'selenium'  as shown below:

3. Now lets change the 'setUp( )' method to 'startSeleniumAndOpenChapter1( )' to make it more readable as shown below:


4. Now lets change the 'testMethod1( )' to 'assertThatSeleniumBeginnersGuidePageHeadingIsPresent( )' to make it more readable as shown below:


5. In the similar manner lets change the method names of the remaining test methods as shown below:


6. Finally, lets change the 'tearDown( )' method to readable method name 'stopSeleniumAndCloseeBrowser( )' as shown below:

7. Now have a look at the complete code and observe that the code is readable now as shown below:


8. Start the Selenium Standalone Server
9. Save and Run the 'Class16.java' file by selecting the 'JUnit Test' option
10. Observe that the Test got passed as shown below:


Watch the Below Video:

Click Here to watch the Video

Download this Project:


Click here to download this project and import into Eclipse IDE  on your machine.

Thats it, we're done Refactoring the Selenium Automation code. 

Please comment below to feedback or ask questions.

Upgrading Selenium Standalone Server will be explained in the next post.



No comments:

Post a Comment

Followers

Labels