在 Cucumber 中的两个功能文件之间重用通用步骤定义(java)

问题描述 投票:0回答:1

我想知道(如果可能)-如何将与场景的几个或所有步骤相关的步骤定义从一个功能文件重用到另一个功能文件? 例如- 在我的功能文件-1 中:我有以下场景/步骤:

场景大纲:通过输入必填字段创建新属性

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..

创建属性的步骤定义是-

@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
}

在我的功能文件 2 中:我必须根据条件重复使用上面的第二步(即,如果不存在记录,则创建新记录)

场景大纲:在房产上创建一个新空间

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 

下面是我对此的步骤定义-

@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 *****
        }

    }

这里我必须重用功能 1 中 TBD 块中的内容

selenium automated-tests cucumber bdd cucumber-jvm
1个回答
0
投票

是的,您可以重复使用该代码。有两种解决方案。

首先,创建一个通用库方法,例如。 createNewProperty 并在两个步骤定义中调用它,如下所示。

@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 
        }

    }

第二个,可以直接调用第二个步骤的定义方法,如下所示,但不建议这样做。

@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();
     }
   }
© www.soinside.com 2019 - 2024. All rights reserved.