Saturday, February 8, 2020

Cucumber Framework Designing Process-3

@After Hook

@After method will run each and every Scenarios.
Just like TestNG we can use before & after method


Now we can try to Generate Extent reports feature.

For it we add some plugin files or Copy from Existing project this is how we injecting extending report functionality in cucumber framework. As we seen report before it are not much attractive or they are simple files. We want good interface to monitor out results so we have added external library "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:" So it has to have a dependencies. So we need to put dependency in a pom file also.


Step: 19

plugin= {"pretty",
"html:target/cucumber-reports",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"
},

Step: 20

Add dependency in pom file:

<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports-cucumber4-adapter</artifactId>
<version>1.0.7</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports</artifactId>
<version>4.0.9</version>
</dependency>


<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.10.2</version>
</dependency>

Step: 21

Also added two files in src/test/resources:
extent properties file
html-config.xml

This are configuration files, We know about it from website of extent report.

Step: 22

Now run it

There is some error will see it in next step

Step 23:



@RunWith(Cucumber.class) // RunWith is a Junit implementation by which we Run Cucumber Test.
@CucumberOptions // map feature file with its corresponding StepDefs file

( features="classpath:features",
glue="",
tags="",
plugin= {"pretty",
"html:target/cucumber-reports",
"json:target/json/file.json",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:" },
dryRun=false
)

// Plugin Section for Reporting purpose of Different format.

ExtentCucumberAdapter:  - It is easy for reporting by using ExtentCucumberAdaptor
Previously we use Extent Report It has lot of extent report objects need to be created & methods has to be called.

It Interviewer trying to ask questions about Extent Report your answer should be we are not using extent report. we have to do lot of object creation, flush and all these. we are using ExtentCucumberAdaptor. It is a new functionality of Extent report we need to add it in to plugin . It just read the steps and generate there reports from the cucumber automatically.

 For TestNG & cucumber both have new adapter callled ExtentCucumberAdapter:

dryRun: true    ->  If it is true it don't actually Run it. It Actually just go through a steps and tells us.
dryRun:false    -> For this check StepDefs is found or StepDefs is not found if not found it generates bug.

Now Come back to Extent Report:
We need to add three files in src/test/resource:
bdd.config.xml - this file not much to do
extent.properties - but here few properties we need to mention like what kinds of report we need to add. We need to set it. We just need to add it no need to do any changes every-time.
html-config.xml - this file not much to do

& last we need to add 4 dependencies
freemarker use to create html report

Last time it was not executed because of cucumber version was 4.2.6 & one file was missing now we will do it <cucumber.version>4.2.0</cucumber.version>

 So check what version of cucumber supports to added dependencies as well from website.

If facing such problem check what actually need to change. Dont just try to change versions randomly.Some times we are working on old versions as well & no need to do changes.

Now Run it with Junit & check Extent Report.

Extent report looks like this:


 On Web:



We can add what we want to show here.
Here it picking up all steps automatically.

It we see logger folder we have marked it as true in extent.properties it generates log as well
We can see it in logger folder index.html
What happened during execution it tells all the things.

Two types of logs:
1. low level Log
2. High Level Log
Most of the peoples Manager, BA , Product owner are interested in high level log. because they want to see whats happening and what happened during execution, What is passed what is failed, What steps used, what type of data exceptions.

Low level logs comes in to picture when we are debugging something. these are two important components.



Its better to use low level log for us every-time

Logs are everywhere.

Step: 24:

Now Enhance our StepDefs TCs


AssertJ libraries are for cucumber

Assert Library which we have in junit is not very reach. thats the reason we have to write below custom condition in TC. If we have Assert J or Hampress library then we don't need these if conditions. There are enough methods available in these libraries which helps to achieve it without having to put such type of Condition.

    //validation 2
   List<WebElement> collection = driver.findElements(By.xpath("//img(starts-with(@id,'del_img_'))"));
 
    if(collection.size()>0) {
   
    Assert.assertTrue(true);
 
    }else {
    Assert.assertTrue(false);


Once all TCs implemented then StepDefs look like this & also check report image as below:

package stepdefs;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.Scenario;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;

public class StepDefs {

WebDriver driver = null;
Scenario scn;

@Before
public void beforeMethodSeUp(Scenario s) {
this.scn =s;
}
@Given("I open a {string} browser")
public void i_open_a_chrome_browser(String browser) {

if(browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
scn.write("chrome Browser id opened");
}else if (browser.equalsIgnoreCase("firefox")) {
scn.write("Firefox Browser id opened");
//driver = new FireFox();
}

driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);
}

@Given("I navigate to URL {string}")
public void i_navigate_to_URL(String url) {
 
driver.get(url);
scn.write("User Navigated to url" + url);
}

@When("I enter text {string} in Search box")
public void i_enter_text_in_Search_box(String string) {
    driver.findElement(By.name("q")).sendKeys(string);
    scn.write("Product name entered:"+ string);
 
}

@When("I click on submit Button")
public void i_click_on_submit_Button() {
driver.findElement(By.xpath("//button[@title='Search']")).click();
    scn.write("Submit Button Clicked");
}


@Then("page is navigate to search page")
public void page_is_navigate_to_search_page() {
//validation 1
    String expected="Search Results";
    String actual= driver.getTitle();
    Assert.assertEquals(actual,expected);
    scn.write("page Title is validated. Expected:" +expected+ "actual:" + actual);
 
    //validation 2
   List<WebElement> collection = driver.findElements(By.xpath("//img(starts-with(@id,'del_img_'))"));
 
    if(collection.size()>0) {
   
    Assert.assertTrue(true);
    scn.write("Search Result are displayed. Search Product Count:" +collection.size());
 
    }else {
    scn.write("Search Result are displayed.No products are returned");
    Assert.assertTrue(false);
    }
    }

}


Report image:

On Web :



No comments:

Post a Comment