47. Only one test should be written inside a single test method
1. Before going forward, lets once again view the code we have written in our previous post below: (i.e. The code with more than one tests inside a single test method)
2. As its not a good idea to write more than one tests inside a single test method, lets Refactor the Selenium Automation code by writing all the 3 tests into a separate test methods as shown below:
3. Save and Run the 'Class15.java' file by selecting the 'JUnit Test' option
4. In this case, Observe that the Firefox Browser has launched and closed for 3 times instead of once, for performing the above three tests as shown in the below video.
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.
Why did the firefox launched and closed separately for each test method in the code ?
As we've three tests and we've written test method for each of our tests, hence we got 3 test methods in the Selenium Automation code. But in this case 'chapter1' page is common for 3 of the tests and hence its not required to close the application and launch it for performing the remaining tests. This has happened because of @Before and @After JUnit annotations.
The code inside the @Before and @After annotations will be executed once for each test method in our Selenium Automation code. Since we've three test methods in our code, the methods under the @Before and @After got executed thrice (i.e. once for each test method).
How to resolve this problem ?
Suppose if we want to run the methods specified with the @Before and @After annotations only once, we've to replace these annotations with @BeforeClass and @AfterClass. (i.e. The method specified with @BeforeClass will be executed before executing all the test methods inside the class and The method specified with @AfterClass will be executed after executing all the test methods inside the class.
So to conclude the executing process will be as shown in the below screenshots:
@Before and @After:
@BeforeClass and @AfterClass:
So lets implement the @BeforeClass and @AfterClass Annotations in the next post.
Please comment below to feedback or ask questions.
Using @BeforeClass and @AfterClass Annotations will be explained in the next post.
Followers
Labels
- aaaa. 1. Introduction to Selenium WebDriver
 - aaab. 2. Downloading the Selenium WebDriver
 - aaac. 3. Configuring Projects in Eclipse IDE to work with Selenium WebDriver
 - aaad. 4. Is Selenium Server required by Selenium WebDriver ?
 - aaae. 5. Selenium RC versus Selenium Web Driver
 - aaaf. 6. Create a JUnit Selenium WebDriver Test using Selenium IDE
 - aaag. 7. Ensure compatible Firefox Browser is installed
 - aaah. 8. Run the Created Selenium WebDriver Test
 - aaai. 9. Introduction to JUnit
 - aaai. 9. Using JUnit annotation in our Selenium WebDriver Tests
 - aaaj. 10. Eclipse Automatically adds the Package Name into the newly created Java Class
 - aaak. 11. Writing Selenium WebDriver Test on our own
 - aaal. 12. Implementing JUnit Annotations
 - aaam. 13. WebDriver Class and its Predefined Methods
 - aaan. 14. 'get( )' - Selenium WebDriver method to open the specified URL page
 - aaao. 15. manage( ).window( ).maximize( ) - to maximize the Browser window
 - aaap. 16. manage( ).window( ).setSize( ) - to resize the Browser window
 - aaaq. 17. Using Thread.sleep(Millie Seconds) to pause the execution
 - aaar. 18. quit( ) - Selenium WebDriver method for closing the Test Browser window
 - aaas. 19. What are Locators ?
 - aaat. 20. Different Types of Locators to identify UI elements
 - aaau. 21. Tools in Selenium to find out the locators
 - aaav. 22. Locate UI elements by ID
 - aaaw. 23. Locate UI elements by Name
 - aaax. 24. Locate UI elements by Link
 - aaay. 25. Locate UI elements by XPath
 - aaaz. 26. Locate UI elements by CSS
 - aaba. 27. Finding locators for UI elements using the Selenium IDEs recording feature
 - aabb. 28. click( ) - predefined method for clicking a Link
 - aabc. 29. click( ) - predefined method for clicking a Button
 - aabd. 30. Using sendKeys( ) predefined method for entering text
 - aabe. 31. Using click( ) - predefined method to select a radio button
 - aabf. 32. Using click( ) predefined method for selecting a check box option
 - aabg. 33. Using sendKeys( ) predefined method to select a list item from the drop down list
 - aabh. 34. Using getText( ) to retrieve any elements Text
 - aabi. 35. Using getPageSource( ) method
 - aabj. 36. Using getTitle( ) method for retrieving the Title of the page
 - aabk. 37. Using getAttribute("value") method for retrieving text from the text fields
 - aabl. 38. Using isDisplayed( ) to verify whether an element is available
 - aabm. 39. Using FirefoxDriver for running Selenium WebDriver Automation Tests on Firefox Browser
 - aabn. 40. Using ChromeDriver for Running our Tests on Chrome Browser
 - aabo. 41. Using InternetExplorerDriver for Running our Tests on Internet Explorer Browser
 - aabp. 42. View the source code generated by Selenium IDE in Selenium WebDriver format
 - aabp. 43. Renaming the Projects and its items using Eclipse IDE 'Refactor' option
 - aabr. 44. Refactoring the Selenium Automation Code
 - aabs. 45. Identifying few sample real time tests for implementing the Refactoring concept
 - aabt. 46. Non-Refactored Selenium Automation code for the tests identified in previous post
 - aabu. 47. Only one test should be written inside a single test method
 - aabv. 48. Using @BeforeClass and @AfterClass JUnit Annotations
 - aabw. 49. Refactoring the Selenium Automation code to make it readable
 - aabx. 50. Upgrading Selenium WebDriver
 - aaby. 51. HTML BASICS
 - aabz. 52. Introduction to XPath
 - aaca. 53. Understanding the hierarchy of HTML Source
 - aacb. 54. Parse the HTML Source using its hierarchy and find XPath path
 - aacc. 55. Different types of Nodes in HTML
 - aacd. 56. Locate the Element Nodes using Relative XPath
 - aace. 57. XPath Functions
 - aacf. 58. last( ) - XPath Function
 - aacg. 59. position( ) - XPath function
 - aach. 60. contains( ) - XPath function
 - aaci. 61. starts-with( ) XPath Function
 - aacj. 62. Other XPath Functions
 - aack. 63. XPath Operators
 - aacl. 64. Wild Cards usage in XPath
 - aacm. 65. Using node( ) wild card in XPath Statements
 - aacn. 66. Using * wild card in XPath Statements
 - aaco. 67. Using | to combine XPath Statements
 - aacp. 68. XPath Optimization
 - aacq. 69. XPath Optimization Strategy One - Use the id attribute if available for the element but not used in XPath Statement
 - aacr. 70. XPath Optimization Strategy Two - Use the combination of attributes to make the XPath more specific
 - aacs. 71. XPath Optimization Strategy Three - If there is really nothing to distinguish find the first unique element from its siblings and add the index value to it
 - aact. 72. XPath Optimization Strategy Four - Use Relative XPath Statements instead of Absolute XPath Statements
 - aacu. 73. Practice XPath
 - aacv. 74. Easy and Regular way of finding XPath Statements for Elements
 - aacw. 75. Typing the Text into Text box by Locating the Text Box field with XPath Statement
 - aacx. 76. Using isSelected( ) predefined method to get the status of the radio buttons and check box options
 - aacy. 77. Selecting values from the Drop down list by locating the Drop down field using XPath Statement
 - aacz. 78. Clicking the Link by locating it using XPath Statement
 - aada. 79. Clicking the Button by locating it using XPath statement
 - aadb. 80. Introduction to CSS selectors
 - aadc. 81. html CSS Selector for locating the whole web page
 - aadd. 82. body CSS Selector for locating the body of the web page
 - aade. 83. Absolute CSS Selector path
 - aadf. 84. Relative CSS Selector path
 - aadg. 85. Using # to select an element with a specified id
 - aadh. 86. Using [attribute='value'] to select an element containing the specified attribute
 - aadi. 87. Using dot in CSS path to select an element with a specified class
 - aadj. 88. Using [attribute] to select all the element containing the specified attribute
 - aadk. 89. Locating first child last child and nth child with CSS path
 - aadl. 90. Locating different elements using a single CSS path
 - aadm. 91. Using * wild card in CSS selector
 - aadn. 92. Using ^= $= and *= in CSS selector
 - aado. 93. Using Boolean Operators in CSS selector
 - aadp. 94. CSS selector Optimization
 - aadq. 95. CSS selector Optimization Strategy One - Use the id attribute if available for the element but not used
 - aadr. 96. CSS selector Optimization Strategy Two - Use the combination of attributes to make the CSS selector statement more specific
 - aads. 97. CSS selector Optimization Strategy Three - Use Relative CSS Statements instead of Absolute CSS Statements
 - aadt. 98. Practice CSS Path
 - aadu. 99. Easy and Regular way of finding CSS Path for Elements
 - aadv. 100. Typing the Text into Text box by Locating the Text Box field using CSS Path Statement
 - aadw. 101. Using isSelected( ) predefined method to get the status of the radio buttons and check box options
 - aadx. 102. Selecting values from the Drop down list by locating the Drop down field using CSS Path Statement
 - aady. 103. Clicking a link by locating it using CSS Path statement
 - aadz. 104. Clicking the Button by locating it using CSS Path statement
 - aaea. 105. Using sendKeys( ) command for entering text into a text box field
 - aaeb. 106. Using sendKeys( ) command for entering text into a text box field
 - aaec. 107. Using sendKeys( ) command for entering single line text into a Text Area field
 - aaed. 108. Using sendKeys( ) command for entering multiple lines of text into a Text Area field
 - aaee. 109. Using clear( ) command to clear a text box field
 - aaef. 110. Using clear( ) command to clear a Text Area field
 - aaeg. 111. Difference between close( ) and quit( ) Selenium WebDriver commands
 - aaeh. 112. Launching more than one Browser windows
 - aaei. 113. Understanding how close( ) and quit( ) commands work when more than one window is opened
 - aaej. 114. Understanding how close( ) and quit( ) commands work when only one window is opened
 - aaek. 115. Try this if close( ) WebDriver command is not closing the Browser window
 - aael. 116. Using click( ) WebDriver command for selecting a check box option
 - aaem. 117. Using click( ) command for deselecting a check box option
 - aaen. 118. Using isSelected( ) method to get the status of the check box option
 - aaeo. 119. Using click( ) command to select a radio button
 - aaep. 120. Using isSelected( ) method to get the status of the radio button
 - aaeq. 121. Using isEnabled( ) to find out whether the button is enabled
 - aaer. 122. Using isEnabled( ) to find out whether the text box field is in enabled state or disabled state
 - aaes. 123. Using sendKeys( ) command to select a list item from the drop down field
 - aaet. 124. Using deselectAll( ) command to deselect any already selected list options in the multiple-selection Box
 - aaeu. 125. Using selectByVisibleText( ) command to select more than one list options from the Multi Select Box field
 - aaev. 126. Using deselectByVisibleText( ) command to deselect any list option from the Multi Select Box field
 - aaew. 127. Using selectByIndex( ) command to select any list option using its Index value from the Multi Select Box field
 - aaex. 128. Using deselectByIndex( ) command to deselect any list option using its Index value from the Multi Select Box field
 - aaey. 129. Using selectByValue( ) command to select the list option from the Multi Select Box field using the specified value
 - aaez. 130. Using deselectByValue( ) command to deselect the list option from the Multi Select Box field using its value
 - aafa. 131. Using selectByVisibleText( ) command with Drop Down Field
 - aafb. 132. Using selectByIndex( ) command with Drop Down field
 - aafc. 133. Using selectByValue( ) command with Drop Down field
 - aafd. 134. Using navigate( ).to( ) command to open the specified URL
 - aafe. 135. Using navigate( ).back( ) command to move backward in our browser
 - aaff. 136. Using navigate( ).forward( ) command to move forward in our browser
 - aafg. 137. Using sendKeys( ) command to browser a file
 - aafh. 138. Using click( ) for clicking a button
 - aafi. 139. Using click( ) for clicking a link
 - aafj. 140. Using getAttribute("value") with a text box field
 - aafk. 141. Using getAttribute("value") method with Password field
 - aafl. 142. Using getAttribute("value") method with Text Area field
 - aafm. 143. Using getAttribute("value") method with drop down field
 - aafn. 144. Using getFirstSelectedOption( ) with drop down field to retrieve the label text of the list option that is currently selected
 - aafo. 145. Using getFirstSelectedOption( ) with Multi-Selection Box field to retrieve the label text of the option that is selected first in the field
 - aafp. 146. Using isMultiple( ) with a drop down field
 - aafq. 147. Using isMultiple( ) with Multi-Selection Box field
 - aafr. 148. Using getAllSelectedOptions( ) to get all the selected options in Multi-Selection Box field
 - aafs. 149. Using getOptions( ) to get all the options available in Drop Down field
 - aaft. 150. Using getOptions( ) to get all the options available in Multi-Selection Box field
 - aafu. 151. Entering an integer variables value into a text box field using sendKeys( ) command
 - aafv. 152. Using size( ) to retrieve the count of list options available in the drop down field
 - aafw. 153. Using size( ) to retrieve the count of list options available in the Multi-Selection Box field
 - aafx. 154. Locating elements using By.tagName( )
 - aafy. 155. Using findElements( )
 - aafz. 156. Using getTitle( ) to retrieve the title of the page
 - aaga. 157. Using getText( ) to retrieve the elements text
 - aagb. 158. Get XPath Count in Selenium WebDriver
 - aagc. 159. Introduction to TestNG
 - aagd. 160. Installing TestNG in Eclipse IDE
 - aage. 161. Using TestNG Annotations in Selenium WebDriver code
 - aagf. 162. Non-Refactored Selenium Automation Code using TestNG framework
 - aagg. 163. Only one test should be written inside a single test method
 - aagh. 164. Using @BeforeClass and @AfterClass TestNG Annotations
 





