Wednesday, February 5, 2020

Cucumber Framework Files with Screenshots



1. CucumberFW

2. src/test


3.1 java
di
 ScnCntxt.java

package di;
import org.openqa.selenium.WebDriver;
import cucumber.api.Scenario;
import po.PO_Cmn;
public class ScnCntxt {
public WebDriver DRIVER;
public PO_Cmn PO_CMN;
public Scenario SCN;
}


po
 PO_Cmn.java


package po;
import java.util.List;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
public class PO_Cmn {
WebDriver driver;
public PO_Cmn(WebDriver driver) {
this.driver = driver;
}
//Locators
@FindBy(how=How.NAME,using="hint_q")
private WebElement txtbx_search;
@FindBy(how=How.XPATH,using="//button[@title='Search']")
private WebElement btn_search;
@FindBy(how=How.XPATH,using="//img[starts-with(@id,'det_img_')]")
private List<WebElement> collection;
//Setters Methods
private void SetSearchTextBox(String arg) {
txtbx_search.sendKeys(arg);
}
private void ClickSearchButton() {
btn_search.click();
}
//Business Method
public void SearchProduct(String arg) {
SetSearchTextBox(arg);
ClickSearchButton();
}
//Validation Method
public void ValidateSearchIsWorking() {
//Validation 1
String expected="Search results";
String actual=driver.getTitle();
Assert.assertEquals("Page Title Validation", expected, actual);
//scn.write("Page Title is Validated. Expected: " + expected + " actual: "+ actual);
//Validation 2
List<WebElement> collection = driver.findElements(By.xpath("//img[starts-with(@id,'det_img_')]"));
if (collection.size()>0) {
Assert.assertTrue(true);
//scn.write("Search Results are displayed. Search Products Count: " + collection.size());
}else {
//scn.write("Search Results are displayed. No products are returned");
Assert.assertTrue(false);
}
}
}
runner
  RunTest.java


package runner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions
( features="classpath:features",
glue="",
tags="",
plugin = {"pretty",
"html:target/html/",
"json:target/json/file.json",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"},
dryRun=false
)
public class RunTest {
}
/*
* plugin = {"pretty",
"html:target/cucumber1",
"json:target/cucumber1.json",
"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"},
*/

stepdefs
   StepDefs_Search.java


package stepdefs;
import static org.junit.Assert.assertEquals;
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 org.openqa.selenium.support.PageFactory;
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;
import di.ScnCntxt;
import po.PO_Cmn;
import utils.Cmn;
//Task: Add Assert J lib/assertion
public class StepDefs_Search {
ScnCntxt context;
public StepDefs_Search(ScnCntxt cntxt) {
this.context = cntxt;
}
@When("I Search the Product {string}")
public void search_product(String arg) {
context.PO_CMN.SearchProduct(arg);
context.SCN.write("Product name entered as " + arg + " and search button clicked");
}
@Then("Page is navigated to Search Page")
public void page_is_navigated_to_Search_Page() {
context.PO_CMN.ValidateSearchIsWorking();
}
}
   StepDefs_SetUp.java


package stepdefs;
import static org.junit.Assert.assertEquals;
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 org.openqa.selenium.support.PageFactory;
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;
import di.ScnCntxt;
import po.PO_Cmn;
import utils.Cmn;
//Task: Add Assert J lib/assertion
public class StepDefs_SetUp {
ScnCntxt context;
public StepDefs_SetUp(ScnCntxt cntxt) {
this.context = cntxt;
}
@Before
public void beforeMethodSetUp(Scenario s) {
context.SCN = s;
}
@Given("I open a {string} browser")
public void i_open_a_browser(String browser) {
context.DRIVER = Cmn.getBrowser(browser);
context.PO_CMN = PageFactory.initElements(context.DRIVER, PO_Cmn.class);
}
@Given("I navigate to URL {string}")
public void i_navigate_to_URL(String url) {
Cmn.NavigateToUrl(context.DRIVER, url);
context.SCN.write("User Navigated to url: " + url);
}
}

utils
   Cmn.java


package utils;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Cmn {
public static WebDriver getBrowser(String browser) {
WebDriver driver=null;
if (browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
//scn.write("Chrome Browser is opened");
}else if (browser.equalsIgnoreCase("firefox")) {
//driver = new FireFox();
//scn.write("FireFox Browser is opened");
}
driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);
return driver;
}
public static void NavigateToUrl(WebDriver driver,String url) {
driver.get(url);
}
}

3.2 resources


features
       HealthCheck.feature
                  
@tag
Feature: Healtch Check Script for CSCart App
I will navigate the App till Search
@tag1
Scenario: Navigating the CSCart App till Search
Given I open a "chrome" browser
And I navigate to URL "http://demo.cs-cart.com/stores/e1c181e71211f4d1/"
When I Search the Product "computer"
Then Page is navigated to Search Page
#When I enter text "computer" in Search Box
#And I click on Submit Button

bdd-config.xml
extent.properties


html-config.xml

No comments:

Post a Comment