saTya
saTya

Reputation: 325

Reusing common step definitions between two feature files in cucumber (java)

I want to know(if possible) -how to reuse step definitions related to few or all steps of a scenario from one feature file to another another? For Example- in my Feature file-1 :I have below scenario/steps:

Scenario Outline: Create a new property by entering mandatory fields

Given As an user I Log in to application and go to property Tab
And I create a new Property from main Menu by entering details in mandatory fields- FloorName as FName, Type..etc so mant columns..

Step definition for create property is-

@And ("^I Create a new Property from main Menu by entering details in mandatory fields FloorName as ([^\"]*), Type..so on."$)
public void createNewProperty(String floorName,String propType...){
//code for all data entry part
}

And in my Feature file 2: I have to reuse 2nd step from above based on a condition (i.e. if there is no record exist then create new one)

Scenario Outline: Create a new space on property

Given As an user I Log in to application and go to Space Tab
Then I navigate to Property Details tab 
And Make sure there is at least one active listing available if not-add new 

below is my step definition for this-

@And("^Make sure there is at least one active listing available if not- add new$")
    public void CheckOrCreateAnActiveListing() throws Throwable {
        //Check if active record is present else create one
        logger.info(" Verify if search has returned some matching results");
        int iRecords= leasePage.chkRowsCountOnActiveListApplet();
        if (iRecords>0) {
           logger.info(" Applet has enough active record(s) to proceed ");
        } else {
            logger.info(" Applet has no active record(s) to proceed, hence creating one ");

            //TBD *****
        }

    }

Here I have to reuse stuff in TBD block from Feature-1

Upvotes: 1

Views: 15409

Answers (1)

Murthi
Murthi

Reputation: 5347

Yes you can reuse the code. There are two solutions.

First, crate a common library method eg. createNewProperty and call it in both the step definitions as give below.

@And("^I have create a new Property from main Menu$")
public void createANewPropertyFromMainMenu(){
   //call library method createNewProperty 

}

@And("^Make sure there is at least one active listing available if not- add new$")
    public void CheckOrCreateAnActiveListing() throws Throwable {
        //Check if active record is present else create one
        logger.info(" Verify if search has returned some matching results");
        int iRecords= leasePage.chkRowsCountOnActiveListApplet();
        if (iRecords>0) {
           logger.info(" Applet has enough active record(s) to proceed ");
        } else {
            logger.info(" Applet has no active record(s) to proceed, hence creating one ");

            //call library method createNewProperty 
        }

    }

Second, you can directly call the first step definition method in second as given below, but it is not advisable.

@And("^Make sure there is at least one active listing available if not- add new$")
public void CheckOrCreateAnActiveListing() throws Throwable {
     //Check if active record is present else create one
     logger.info(" Verify if search has returned some matching results");
     int iRecords= leasePage.chkRowsCountOnActiveListApplet();
     if (iRecords>0) {
           logger.info(" Applet has enough active record(s) to proceed ");
     } else {
           logger.info(" Applet has no active record(s) to proceed, hence creating one ");

          //create object of step definition class and class the method
          obj.createANewPropertyFromMainMenu();
     }
   }

Upvotes: 0

Related Questions