重用黄瓜中两个要素文件之间的通用步骤定义(java)

时间:2018-06-21 11:14:42

标签: selenium automated-tests cucumber bdd cucumber-jvm

我想知道(如果可能的话)-如何重用与场景的几个或所有步骤相关的步骤定义,从一个功能文件到另一个功能文件? 例如- 在我的功能文件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..

create属性的步骤定义是-

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

    }

在这里,我必须重用Feature-1中的TBD块中的内容

1 个答案:

答案 0 :(得分: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();
     }
   }