12
Jun

Getter dependency for Cucumber scenarios with Picocontainer

Step 1: Create Maven project with following POM.xml
=================================================== UTF-8 UTF-8 1.8
1.8

4.2.6
3.141.59
6.5.1


io.cucumber
cucumber-picocontainer
4.3.1
test


org.testng
testng
${testng.version}
test


io.cucumber
cucumber-java
${cucumber.version}
test


io.cucumber
cucumber-testng
${cucumber.version}
test


io.cucumber
cucumber-picocontainer
${cucumber.version}
test


org.seleniumhq.selenium
selenium-java
${selenium.version}
test


log4j
log4j
1.2.17



org.slf4j
slf4j-log4j12
1.7.25
test


org.apache.maven.plugins
maven-failsafe-plugin
3.0.0-M3



integration-test



**/*Runner.java



Step 2: Configure eclipse project structure:
============================================

src/test/java
picocontainerUtilities
PicoDependencyInjector.java
runner Runner.java
stepDef StepImplementation.java
utilities CSVUtilities.java
SharedDrive.java
src/test/resources
features
first.feature

Step 3: PicoDependencyInjector code:
=====================================

package picocontainerUtilities;

import cucumber.runtime.java.picocontainer.PicoFactory;
import utilities.CSVUtilities;
import utilities.SharedDrive;

public class PicoDependencyInjector extends PicoFactory {

public PicoDependencyInjector() {
addClass(CSVUtilities.class);
addClass(SharedDrive.class);
}
}

Other code:
===========

package runner;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;

import cucumber.api.CucumberOptions;
import cucumber.api.testng.AbstractTestNGCucumberTests;

@CucumberOptions(glue = “stepdef”, plugin = { “json:target/json-cucumber-reports/cukejson.json”,
“testng:target/testng-cucumber-reports/cuketestng.xml” }, features = “src/test/resources/features/”)
public class Runner extends AbstractTestNGCucumberTests {

private static long duration;

@BeforeClass
public static void before() {
duration = System.currentTimeMillis();
System.out.println(“Tests started at :”+duration);
}

@AfterClass
public static void after() {
duration = System.currentTimeMillis() – duration;
System.out.println(“TOTAL DURATION OF RUN – ” + duration + “ms”);
}
}

======================

package stepdef;

import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import utilities.CSVUtilities;
import utilities.SharedDrive;

public class StepImplementation {

private CSVUtilities obj;
private SharedDrive sd;

public StepImplementation(CSVUtilities obj, SharedDrive sd){
this.obj = obj;
this.sd = sd;
}

@Given(“I want to write a step with precondition”)
public void i_want_to_write_a_step_with_precondition() {
System.out.println(“Given I want to write a step with precondition”);
System.out.println(“$$$$$$$$$$$$”);
obj.CSVFunction();
}

@Given(“some other precondition”)
public void some_other_precondition() {
System.out.println(“Given some other precondition”);
System.out.println(“%%%%%%%%%%%%%%%%%”);
sd.SharedDriveFunction();
}

@When(“I complete action”)
public void i_complete_action() {
System.out.println(“When I complete action”);
}

@When(“some other action”)
public void some_other_action() {
System.out.println(“When some other action”);
}

@When(“yet another action”)
public void yet_another_action() {
System.out.println(“When yet another action”);
}

@Then(“I validate the outcomes”)
public void i_validate_the_outcomes() {
System.out.println(“Then I validate the outcomes”);
}

@Then(“check more outcomes”)
public void check_more_outcomes() {
System.out.println(“Then check more outcomes”);
}

@Given(“I want to write a step with name{int}”)
public void i_want_to_write_a_step_with_name(Integer int1) {
System.out.println(“Given I want to write a step with name{int}”);
}

@When(“I check for the {int} in step”)
public void i_check_for_the_in_step(Integer int1) {
System.out.println(“When I check for the {int} in step”);
}

@Then(“I verify the success in step”)
public void i_verify_the_success_in_step() {
System.out.println(“Then I verify the success in step”);
}

@Then(“I verify the Fail in step”)
public void i_verify_the_Fail_in_step() {
System.out.println(“Then I verify the Fail in step”);
}

}

===================

package utilities;

public class CSVUtilities {

private CSVUtilities obj;

public CSVUtilities getObj() {
return obj;
}

public void setObj(CSVUtilities obj) {
this.obj = obj;
}

public void CSVFunction(){
System.out.println(“Function call from CSVUtilities”);
}

}

==================

package utilities;

public class SharedDrive {

private SharedDrive obj;

public SharedDrive getObj() {
return obj;
}

public void setObj(SharedDrive obj) {
this.obj = obj;
}

public void SharedDriveFunction(){
System.out.println(“Function call from SharedDrive”);
}

}

=======================

#Author: your.email@your.domain.com
#Keywords Summary :
#Feature: List of scenarios.
#Scenario: Business rule through list of steps with arguments.
#Given: Some precondition step
#When: Some key actions
#Then: To observe outcomes or validation
#And,But: To enumerate more Given,When,Then steps
#Scenario Outline: List of steps for data-driven as an Examples and #Examples: Container for s table
#Background: List of steps run before each of the scenarios
#””” (Doc Strings) s
#| (Data Tables)
#@ (Tags/Labels):To group Scenarios
#<> (placeholder)
#””
## (Comments)
#Sample Feature Definition Template
@tag
Feature: Title of your feature
I want to use this template for my feature file

@tag1
Scenario: Title of your scenario
Given I want to write a step with precondition
And some other precondition
When I complete action
And some other action
And yet another action
Then I validate the outcomes
And check more outcomes

@tag2
Scenario Outline: Title of your scenario outline
Given I want to write a step with
When I check for the in step
Then I verify the in step

Examples:
| name | value | status |
| name1 | 5 | success |
| name2 | 7 | Fail |

Sample4Picocontainerzip