Wednesday, 1 March 2017

Implement Logging in Page Classes

In the previous post, we have created the extent reports & used in sign in test. Now, suppose we want to implement the logging in our page classes. For logging in page classes, we need to pass the ExtentReports & ExtentTest object into our page classes. We can follow the same steps as we did while passing the WebDriver object in page classes.
To implement these, follow the below steps (Here we need to restructure the codes in each Page Classes, Test Cases & Also in BaseTest & BasePage classes):
  •  Download the .zip file from here.
  • Copy & Replace BasePage.java, LunchPage.java, MyAccountPage.java & SignInPage.java files inside com.demo.pom.pages package.
  • Copy & Replace BaseTest.java & SignInTest.java files inside com.demo.pom.testcases package.

Code Overview:
The main important code changes are, While Creating the Object of any class we will create the object in normal method & will pass that object inside PageFactory.initElements() method.
BasePage.java
Here, we are declaring the global ExtentTest object which can be used in its child classes.
public ExtentTest test;
Also, we have added the ExtentTest object in the constructer & inside the constructer we are assigning the global test object with the local test object using this.
LunchPage.java
Here also in the constructer we are passing the ExtentTest object. And, if you will consider the below code:
                                SignInPage signInPage = new SignInPage(driver,test);
                                PageFactory.initElements(driver, signInPage);
return signInPage;
In the 1st line we have created the object of SignInPage class & passed the WebDriver & ExtentTest objects inside it.
In the 2nd line we are calling the initElements method where we are passing the driver object & signInPage Object.
In the last line we are returning the page object.
MyAccountPage.java
Here, I have added the line to check the test object. Which can be used in future
SignInPage.java
As explained earlier in LunchPage.java, here also we are passing the ExtentTest object inside constructer. Using the test object we are adding the logs inside doLogIn() method.
BaseTest.java
Declaring the test object as public. So, that each child classes can access the same test object.
SignInTest.java
As of now we are using only SignInTest case. So here inside the @Test annotation 1st we are initializing the test object by using the below line:
test = extent.startTest("Sign In Test");
After that we have added the log statements & we have added a new annotation @AfterMethod where we can end the test object & flush the extent objects. Please check the code below:
              @AfterMethod
              public void afterMethod(){
                     if(extent!=null){
                            extent.endTest(test);
                            extent.flush();
                            }
       }


·         @AfterMethod can also be used in any other operations latter. Once the test case execution completed, refresh the project directory & verify the below results:
1.      It should create a folder inside src & the name should be report.
2.      Inside report folder, there should be .html file with current time stamp.
3.      Open the file in any web browser & verify the logs & reports in below format:


In the above attachment, you can see the logs generated properly.

Note: If you are facing any problem please comment below. I will try to explain the solutions.



<<<Prev       Next>>>

No comments:

Post a Comment