Sunday, 17 June 2018

Extent Reports

JARS- Dependency

<dependency>
    <groupId>com.relevantcodes</groupId>
    <artifactId>extentreports</artifactId>
    <version>2.41.0</version>
</dependency>



extent-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<extentreports>
  <configuration>
    <!-- report theme -->
    <!-- standard, dark -->
    <theme>standard</theme>
 
    <!-- document encoding -->
    <!-- defaults to UTF-8 -->
    <encoding>UTF-8</encoding>
   
    <!-- protocol for script and stylesheets -->
    <!-- defaults to https -->
    <protocol>https</protocol>
   
    <!-- title of the document -->
    <documentTitle>ExtentReports 2.0</documentTitle>
   
    <!-- report name - displayed at top-nav -->
    <reportName></reportName>
   
    <!-- report headline - displayed at top-nav, after reportHeadline -->
    <reportHeadline>Automation Report</reportHeadline>
   
    <!-- global date format override -->
    <!-- defaults to yyyy-MM-dd -->
    <dateFormat>yyyy-MM-dd</dateFormat>
   
    <!-- global time format override -->
    <!-- defaults to HH:mm:ss -->
    <timeFormat>HH:mm:ss</timeFormat>
   
    <!-- custom javascript -->
    <scripts>
      <![CDATA[
        $(document).ready(function() {
         
        });
      ]]>
    </scripts>
   
    <!-- custom styles -->
    <styles>
      <![CDATA[
       
      ]]>
    </styles>
  </configuration>

</extentreports>


Extent Report Class


import java.io.File;

import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.SkipException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.LogStatus;

public class ExtentReportsClass {


ExtentReports extent;
static com.relevantcodes.extentreports.ExtentTest logger;


@BeforeTest
public void startReport(){
//ExtentReports(String filePath,Boolean replaceExisting)
//filepath - path of the file, in .htm or .html format - path where your report needs to generate.
//replaceExisting - Setting to overwrite (TRUE) the existing file or append to it
//True (default): the file will be replaced with brand new markup, and all existing data will be lost. Use this option to create a brand new report
//False: existing data will remain, new tests will be appended to the existing report. If the the supplied path does not exist, a new file will be created.
extent = new ExtentReports (System.getProperty("user.dir") +"/test-output/STMExtentReport.html", true);
//extent.addSystemInfo("Environment","Environment Name")
extent
                .addSystemInfo("Host Name", "SoftwareTestingMaterial")
                .addSystemInfo("Environment", "Automation Testing")
                .addSystemInfo("User Name", "Rajkumar SM");
                //loading the external xml file (i.e., extent-config.xml) which was placed under the base directory
                //You could find the xml file below. Create xml file in your project and copy past the code mentioned below
                extent.loadConfig(new File(System.getProperty("user.dir")+"\\extent-config.xml"));
}

@Test
public void passTest(){
//extent.startTest("TestCaseName", "Description")
//TestCaseName – Name of the test
//Description – Description of the test
//Starting test
logger = extent.startTest("passTest");
Assert.assertTrue(true);
//To generate the log when the test case is passed
logger.log(LogStatus.PASS, "Test Case Passed is passTest");

}

@Test
public void failTest(){
logger = extent.startTest("failTest");
Assert.assertTrue(false);
logger.log(LogStatus.FAIL, "Test Case  Status is passed");
}

// @Test
public void skipTest(){
logger = extent.startTest("skipTest");
throw new SkipException("Skipping - This is not ready for testing ");
}

// @AfterMethod
public void getResult(ITestResult result){
if(result.getStatus() == ITestResult.FAILURE){
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
}else if(result.getStatus() == ITestResult.SKIP){
logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
}
// ending test
//endTest(logger) : It ends the current test and prepares to create HTML report
extent.endTest(logger);
}
@AfterTest
public void endReport(){
// writing everything to document
//flush() - to write or update test information to your report.
                extent.flush();
                //Call close() at the very end of your session to clear all resources.
                //If any of your test ended abruptly causing any side-affects (not all logs sent to ExtentReports, information missing), this method will ensure that the test is still appended to the report with a warning message.
                //You should call close() only once, at the very end (in @AfterSuite for example) as it closes the underlying stream.
                //Once this method is called, calling any Extent method will throw an error.
                //close() - To close all the operation
               // extent.close();
    }
}



API Testing-Rest Assured

package framework.RestAPI;

import org.testng.Assert;
import org.testng.annotations.Test;

import com.jayway.restassured.http.ContentType;
import com.jayway.restassured.response.Response;
import com.relevantcodes.extentreports.LogStatus;

import framework.TestBase.TestBase;
import framework.configReader.ObjectRepo;

import  static com.jayway.restassured.RestAssured.*;

public class weatherGetRequest extends TestBase {

@Test(priority=1)

public void weatherGetStatusCode(){
Response resp=
       given().
       param("q","London").
       param("appid","b6907d289e10d714a6e88b30761fae22").
       when().
       get("http://samples.openweathermap.org/data/2.5/weather");
     

int statuscode=resp.getStatusCode();
Assert.assertEquals(statuscode, 200);
TestBase.logger.info("Get the Status code");
TestBase.extentlogger = TestBase.extent.startTest("Verify the status code -->passTest");
TestBase.extentlogger.log(LogStatus.PASS, "Test Case Passed is passTest");

//System.out.println(resp.getStatusCode());


}
@Test(priority=2)
public void getResponse(){

Response resp=
       given().
       parameter("q","London").
       parameter("appid","b6907d289e10d714a6e88b30761fae22").
       when().
       get("http://samples.openweathermap.org/data/2.5/weather");
String Expected="light intensity drizzle";
String Actual=resp.then().contentType(ContentType.JSON).extract().path("weather[0].description");
Assert.assertEquals(Actual,Expected);
TestBase.logger.info("check response data");
TestBase.extentlogger = TestBase.extent.startTest("Check the response data -->passTest");
TestBase.extentlogger.log(LogStatus.PASS, "Test Case Passed is passTest");

// System.out.println(resp.asString());
}

@Test(priority=3)
public void transferLonLat(){

Response resp=given().
       parameter("q","London").
       parameter("appid","b6907d289e10d714a6e88b30761fae22").
       when().
       get("http://samples.openweathermap.org/data/2.5/weather");

   String respbyid =   resp.then().
       contentType(ContentType.JSON).
       extract().
       path("weather[0].description");

System.out.println(respbyid);





String lon=String.valueOf(resp.then().
contentType(ContentType.JSON).
extract().
path("coord.lon"));
System.out.println(lon);

String lat=String.valueOf(resp.then().
contentType(ContentType.JSON).
extract().
path("coord.lat"));
System.out.println(lat);



String respbycoord=given().
parameter("lat",lat ).
parameter("lon", lon).
parameter("appid","b6907d289e10d714a6e88b30761fae22").
get("http://samples.openweathermap.org/data/2.5/weather").
then().
contentType(ContentType.JSON).extract().
path("weather[0].description");

System.out.println(respbycoord);

try{
Assert.assertEquals(respbyid, respbycoord);
TestBase.logger.info("Verify longitude and latitude");
TestBase.extentlogger = TestBase.extent.startTest("Check the longitude and latitude -->passTest");
TestBase.extentlogger.log(LogStatus.PASS, "Test Case Passed is passTest");
}
catch(Exception e){
TestBase.logger.info("Verify longitude and latitude");
TestBase.extentlogger = TestBase.extent.startTest("Check the longitude and latitude -->passTest");
TestBase.extentlogger.log(LogStatus.FAIL, "Test Case failed");
}

}

}



JARS needed


<dependency>
    <groupId>com.jayway.restassured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>2.9.0</version>
    <scope>test</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/com.jayway.jsonpath/json-path -->
<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.4.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.jayway.restassured/json-schema-validator -->
<dependency>
    <groupId>com.jayway.restassured</groupId>
    <artifactId>json-schema-validator</artifactId>
    <version>2.2.0</version>
</dependency>
    
  </dependencies>