Monday, 27 February 2017

Add BaseTest class file to keep all the common functionality of Test Cases & remove hard coding

As you have seen in SignTest there are some code which is used to Open Browser & navigate to the URL. Similarly, there might be multiple test cases like RegistrationTest, CartTest etc. So instead of writing the same code to Open Browser & navigate to URL, we will add a BaseTest.java class file in our test cases & every test cases will extend to that BaseTest class.

The benefits of adding the BaseTest is it will have all the common features inside it so that we can get the reusability of the code.
To add the BaseTest class file, Right click on com.demo.pom.testcases package > Click on New > Click on Class > Insert the name as BaseTest > Click on Finish
Once the BaseTest class is created, you can move the common codes into it & in every test you should extend the BaseTest.
Now let’s discuss about hardcoding in our project. If you will see our code, we have almost hard coded all values like the username, password, etc Xpaths, site URL, browser type. The problem of hard coding is supposed after some time the Xpaths got changed then we should search that xpath & replace that xpath with new value which is not the correct way. So, instead of hardcoding we can create a DemoConstants.java file & will keep store hard coded values in a constant.
To add DemoConstants class file, Right click on com.demo.pom.util package > Click on New > Click on Class > Insert the name as DemoConstants > Click on Finish.
Download the .zip file from here & follow the below steps:
  • Extract the downloaded file.
  • Copy & replace the BaseTest.java & SignInTest.java files inside com.demo.pom.testcases package.
  • Copy IEDriverServer.exe & chromedriver.exe inside src/test/resources.
  • Copy & replace the SignInPage.java files inside com.demo.pom.pages package.
  • Copy & replace the DemoConstants.java files inside com.demo.pom.util package.
  • Also for rest of the test classes extend with BaseTest.
After completing the above steps, right click on SignInTest.java > Run As > Test NG suite. It should successfully open the browser & will logged in to the application successfully. Also, the Test NG result should show as PASSED.


NOTE: If you want to run the application in different browsers, then In DemoConstants.java file at line 14 change the BROWSER_NAME = “IE” for running in IE or Chrome to run in Chrome browser.



<<<Prev       Next>>>

No comments:

Post a Comment