title: How to create a common steps class for all feature file
body: <p><strong>My Goal:</strong><br>
  Due to there are many feature files will use some common steps, I want to create a reusable common step definition class for all feature file.</p>
  
  <p><strong>My Code:</strong></p>
  
  <p>Common Step Class</p>
  
  <pre><code>public class CommonStep {
  
  static WebDriver driver;
  
  public CommonStep(WebDriver driver) {
      CommonStep.driver = driver;
  }
  
  @Given("^I login to XXXXXX window title \"([^\"]*)\"$")
  public boolean cadLogin(String winTitle){
      return driver.getTitle().contains(winTitle);
  }
  
  @When("^I enter \"([^\"]*)\" into search box$")
  public void search_Screen(String cntrl){
      driver.findElement(By.id("idNEXT_SCRN")).clear();
      driver.findElement(By.id("idNEXT_SCRN")).sendKeys(cntrl);
  }
  
  @And("^I click Go button for search MERCH screen$")
  public void btnGo(){
      driver.findElement(By.id("idGO_IND_1")).click();
  }
  
  @Then("^I navigate to a new window with window title \"([^\"]*)\"$")
  public boolean navToScreen(String winTitle) {
      return driver.getTitle().contains(winTitle);
  }
  
  public static void setDriver(WebDriver driver) {
      CommonStep.driver = driver;
  }
  </code></pre>
  
  <p>Specific Step Class</p>
  
  <pre><code>public class MerchCreationStep {
  
  static WebDriver driver;
  private CommonStep commonStep;
  private MerchPage merchPage;
  private Merc2Page merc2Page;
  private Merc3Page merc3Page;
  private Merc4Page merc4Page;
  private LoginPage loginPage;
  
  
  @And("^I enter \"(\\d)\" letters of DBA Name$")
  public void inpDBA_NAME(int num) {
      merchPage.inpDBA_NAMEStr(num);
  }
  
  ......
  
  @When("^I click New Button$")
  public void clickNew(){
      merchPage.clickNew();
  }
  
  @Before
  public void startUp() {
      System.setProperty("webdriver.chrome.bin", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
      System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
      driver = new ChromeDriver();
      commonStep = new CommonStep(driver);
      loginPage = new LoginPage(driver);
      merchPage = new MerchPage(driver);
      merc2Page = new Merc2Page(driver);
      merc3Page = new Merc3Page(driver);
      merc4Page = new Merc4Page(driver);
      driver.get("http://172.16.1.68:8080/XXXXXX/servlet/app");
      try {
          Thread.sleep(1000);
      } catch(InterruptedException e){
          // TO DO Auto-generated catch block
          e.printStackTrace();
      }
      Utility.switchToWindow("XXXXXXX- User Logon [LOGON]", driver);
      try{
          Thread.sleep(2000);
      } catch(InterruptedException e){
          // TO DO Auto-generated catch block
          e.printStackTrace();
      }
      loginPage.inpBankStr("01");
      loginPage.inpEmpStr("200006");
      loginPage.inpPwdStr("Ajay123456");
      loginPage.clickResetBtn();
      loginPage.inpPwdStr("Ajay123456");
      loginPage.clickLoginBtn();
  }
  
  @After
  public void shutDown() {
      driver.quit();
  }
  </code></pre>
  
  <p>Feature File:
      @Merch_Creation</p>
  
  <pre><code>@Merch_Creation
  Feature: Merchant Creation
  
    Scenario: Merchant Creation
      **Given I login to XXXXXXX window title "XXXXXX- Main System Menu [MENU]"
      When I enter "MERCH" into search box
      And I click Go button for search MERCH screen
      Then I navigate to a new window with window title "XXXXXX- Merchant Details Screen 1 [MERCH]"**
      And I enter "5" letters of DBA Name
      And I enter "8" letters of DBA Address Line 1
      And I enter "8" letters of DBA Address Line 2
      And I enter "BRISBANE" in DBA City
      And I select the option "NSW " as State Code
      And I enter "1010" in Post Code
      And I enter "AU" in Country Code
      And I enter "9" digits Phone Number
      And I select the option "1   " option as Statement
      And I select the option "I   " option as H.O./Individual
      And I enter "V222233339" in Customer Number
      And I enter 4 digits number "5499" in MCC
      And I select the option "0300" as Branch
      And I enter "MCMDVCVD" in Card Plans/Styles
      And I tick the check box of Data Entry Mode
      When I click New Button
      **Then Navigated to window "XXXXXXX- Merchant Details Screen 2 [MERC2]"**
      And I select the option "D   " as Service Fee Freq
      And I enter "1.9900" in Service Fee Rate
      And I select the option "1   " as Payment Method
      And I select the option "T   " as Payment Aggregation
      And I enter "15" digit as Settlement Acct
      And I select the option "A   " as Settlement Acct Link Code
      And I enter "5" letters in Settlement Acct Name
      And I select the option "A   " as Min. Service Fee Code
      And I tick the check box of Data Entry Mode on MERC2
      And I click Update button
      **Then I navigate to a new window with window title "XXXXXX- Merchant Details Screen 3 [MERC3]"**
  </code></pre>
  
  <p>Features with ** before are common features.<br>
  How can I use these Common step definitions in specific definitions class? Can someone give me an idea about this? When I give common stepdefs a constructor. It give me an error about Bean creation failed. If I don't give common stepdefs a constructor. It comes up with an error about <code>NullPointerException</code>.<br>
  Thank you very much.</p>