Thursday, 2 March 2017

Running all test scenarios in same browser & implement failure/skip in SignInTest

For running all test scenarios in same browser, we need to do the below steps:
  1. Open browser – 1time.
  2.  Do the operation(s) – multiple time.
  3.  After operation come to the initial state.
  4. After completion of all operation close the browser.



To achieve the steps mentioned above we can use below annotations provided by TestNG.
@BeforeTest – This annotation will be executed only once before executing the test case. So, we write the code for initializing the browser.
@Test – We are already using this annotation & it will be called till it reaches to the no.of rows provided in the excel sheet.
@AfterMethod – We are also using this annotation to flush the extent report. Apart from that we can also add some code which will reset the browser to its initial state.
@AfterTest – This annotation will be called only once after completing all the Test operations. So, here can add the code to close the browser.
By using the above annotations, we can resolve our problem. Also, I will explain more in the code overview part. Now let’s consider how to Skip the test case(s)?
Ans: In excel sheet we have a column known as Runmode where we are passing Y or N. Y – to execute the test case & N – to skip.
We have set the Runmode for Test Case as well as Test Data. If the Runmode is N in Test Case, then all the test data inside that test case will be skipped but if test case Runmode is Y & a test data Runmode is N then all the test data will be executed except the data which have Runmode as N.
Eg. If you consider the DataSheet.xlsx file, then we have the SignInTest Runmode as Y & inside that test case the row 7 in Data sheet the Runmode is N. So, after implementing the code changes you can observe the result.
  • Download the .zip from here.
  • Extract the folder.
  • Inside the Project directory create a folder & name it as “screenshots”.
  • Copy & replace BasePage.java, Menu.java & SignInPage.java inside com.demo.pom.pages package.
  •  Copy & Replace BaseTest.java & SignInTest.java inside com.demo.pom.testcases package.
  • Copy & replace DemoConstants.java inside com.demo.pom.util package.
Code Overview:
BasePage.java
Here, I have changed the initialization of menu object.
SignInPage.java
  •  Declared the xpath for errors.
  •  Updated the doLogin() method. Here I have added the code to validate the login success or not.

Menu.java
  • Here I have declared the xpath of log out link & also added the WebDriver code for logout.

BaseTest.java
  • checkTestCaseRunmode(String testCaseName): This method will accept the test case name as parameter & will check for the Runmode. If Runmode is N, then it will throw the SkipException & the test case will be skipped.
  •  checkTestRunMode(String testCaseRunMode): This method will take the runmode of test data as parameter. . If Runmode is N, then it will throw the SkipException & the test data will be skipped.
  •  checkErrors(List<WebElement> elements): This method will take the errors xpath & will check whether any errors present or not.
  •  getErrorsContent(List<WebElement> elements): This method will take the errors xpath & will add the error messages inside the logs file.
  • takeScreenShot() : This method will capture the screenshot & will add the screenshot in the logs.

SignInTest.java
  •  Updated the code for validating the test result.
  • Updated the code to Initialize the browser once & checking the Runmode of Test Case in @BeforeTest.
  • Updated the code inside @Test for validating the test result & also checking the Runmode of test data.
  • Added @AfterTest to close the browser after completion of the tests.


Once you have done the changes, right click on SignInTest.java > Run As > TestNG Suite & observe the test result as below:

  • Verify the logs – you can see in logs it will add the screenshot.
  • It will also skip the test cases which have the Runmode as N.
  • In TestNG result check the below results:
[Extent Report]
    [TestNG Result]


<<<Prev       Next>>>




No comments:

Post a Comment