根据肥皂请求的某些条件循环到一定数量

Looping to a certain number based on some condition for a soap request

场景:发送 post 请求直到条件满足但最多 5 次否则脚本失败

第一步:从数据库中获取一条数据

step2:根据第一步的结果从db中获取第二个数据(如果step 2给出null则从step 1重新开始,最多出现5次)

第 3 步:准备一个 soap 请求,并将第 1 和第 2 个数据作为输入放入我们在第 1 步和第 2 步中获得的请求正文中。

第 4 步:发送 soap 请求并打印响应

第 5 步:验证 response.id >5000(如果此条件不满足,请重复从第 1 步到第 5 步的过程,但再次重复仅直到 5 次,如果直到第 5 次仍不满足条件然后测试用例失败)

注意:-我也试过重试,但看起来它只发送第 5 步,所以它不会更改我们需要更改的请求正文数据从步骤 1 到步骤 5 每 运行。

另外,我做了很多研究,遇到了很多类似的问题,但都没有达到我想要的结果。

Scenario: GetCountriesWithId
    * def RandomNumber =
      """
      function() {
      var count = 1
       while (count<=5) {
        var RandomNumber = db.readRows("select trunc(dbms_random.value(10000000, 10010000)) min, trunc(dbms_random.value(10200000, 10250000)) max from dual")
        karate.log('RandomNumber->',RandomNumber)
        var HolderFromDB = db.readRows("SELECT productId, userId from table1 WHERE id between "+RandomNumber[0].MIN+" and "+RandomNumber[0].MAX+" ")
          if (HolderFromDB.length >= 1) {
            karate.log('condition satisfied, exiting');
            return HolderFromDB;
          }
          count++
        }
      }
      """
    * def HolderFromDB = call RandomNumber
    * def productId = HolderFromDB.productId
    * def userId = HolderFromDB.userId
    * def javaclass = Java.type('epos.positionmanagement.service.test.GetSpendingLimitRequestTest')
    * def map = {deviceTypeId: 2, entityId: '#(defaultEntity)', ppr_id: '#(productId)', userId: '#(userId)' }
    * def createGetSpendingLimitRequest = javaclass.getPayload(map)
    Then print 'Request body--->', createGetSpendingLimitRequest
    Given request createGetSpendingLimitRequest
    When soap action 'Request'
    Then status 500
    Then print '\n', response
    * def revisionId = get response //GetSpendingLimitResponse/spendingLimit/totalAmount*1
    * def result = revisionId<=50000 ? karate.call('test1.feature') : {}

我认为这个测试用例不应该自动化。因为不清楚,有很多依赖的情况。您应该遵循使每个测试用例独立且独立的原则。此外,测试用例应该设计得很好,以便您可以轻松地为它们开发代码。否则,即使您已经实现了它们的自动化,它们也很可能是不稳定的测试,您将花费更多时间来修复它们而不是手动测试它们。三思而后行,再次考虑场景并决定下一步行动。另外请再看一下你的场景中提到的这个业务逻辑,因为它看起来不太合乎逻辑。

我已经解决了以下问题:-

将 post 请求之前的步骤放在另一个功能文件中,如下所示:-

@ignore
Feature: Reusable
 
  Background: Maintenance Soap Request
    * def DbUtils = Java.type('util.DbUtils')
    * def db = new DbUtils(eposEnvironment.database[defaultEntity])
    * url eposEnvironment.baseUrl
 
  @Limit-GTC-Straddle @parallel=false
  Scenario: Limit-GTC-Straddle
    * def derivativeOrderTemp =
    """
      function() {
          var count = 1
          while (count <= 10) {
              var selectRandomLetter = db.readRows("select chr(trunc(dbms_random.value(65,74))) firstLetter, chr(trunc(dbms_random.value(80,90))) lastLetter, trunc(dbms_random.value(1, 4)) positionToBuy, to_char(current_date + 364, 'YYYY-MM-DD') expDate from dual")
              var symbol1FromDB = db.readRows("select * from   (select be_symbool, RTRIM(be_muntsoort) as be_muntsoort,   be_code, be_optietype, be_expiratiedatum as be_expiratiedatum2,   to_char(to_date(be_expiratiedatum, 'YYYYMMDD'), 'YYYY-MM-DD') as be_expiratiedatum, be_exerciseprijs,   brs_naam, be_bv_beurs,   round(max (quot_laat) keep (dense_rank last order by quot_datum)+ 1, 0) as limit,   (round(max (quot_laat) keep (dense_rank last order by quot_datum)+ 1, 0)*100*-1*" + selectRandomLetter[0].POSITIONTOBUY + ") effAmount,   be_volgnummer   from  beleggingsinstrument     join fn_quotes_table   on quot_symbool = be_symbool   and quot_optietype = be_optietype   and quot_expiratiedatum = be_expiratiedatum   and quot_exerciseprijs = be_exerciseprijs     join beurzen   on brs_nummer = be_bv_beurs     where be_Symbool in     (select be_symbool     from beleggingsinstrument     where be_bi_nummer = 100     and length(trim(be_symbool)) < 4     and substr(trim(be_symbool),1,1) between '" + selectRandomLetter[0].FIRSTLETTER + "' and '" + selectRandomLetter[0].LASTLETTER + "'     and be_geblokkeerd = 0     and be_handelen_toegestaan = 1     and be_referentiesymbool = ' ')   and be_optietype = 'CALL'   and be_handelen_toegestaan = 1   and be_expiratiedatum > to_char(sysdate, 'yyyymmdd')   group by be_symbool, be_muntsoort, be_code, be_optietype, be_expiratiedatum, be_exerciseprijs, be_bv_beurs, quot_laat, brs_naam, quot_laat, quot_datum, be_volgnummer   ) where rownum = 1")
              karate.log('Symbol1FromDB-->', symbol1FromDB)
              if (symbol1FromDB.length >= 1) {
                  karate.log('condition satisfied, exiting from first conditionalGoto1 loop counter');
                  var indirectCosts1FromDB = db.readRows("select icf_cfcu_id, cfcu_description, decode(icf_amount, null, 0, icf_amount) icf_amount, decode(icf_currency, null, 'NULL', icf_currency) icf_currency, decode(icf_percentage, null, 0, icf_percentage) icf_percentage from EPP_OWNER.INDIRECT_COSTS_PER_FUND join epp_owner.calculationrules on cfcu_id = icf_cfcu_id where icf_be_id = " + symbol1FromDB[0].BE_VOLGNUMMER + " and (icf_import_date_till > to_char(sysdate, 'yy-mm-dd') or icf_search_date_till is null) and icf_ex_ante_ex_post_ind = 'A' and icf_cfcu_id in (67)")
                  karate.log('indirectCosts1FromDB-->', indirectCosts1FromDB)
                  var symbol2FromDB = db.readRows("select * from   (select be_symbool, RTRIM(be_muntsoort) as be_muntsoort,   be_code, be_optietype,   to_char(to_date(be_expiratiedatum, 'YYYYMMDD'), 'YYYY-MM-DD') as be_expiratiedatum, be_exerciseprijs,   brs_naam, be_bv_beurs,   round(max (quot_laat) keep (dense_rank last order by quot_datum)+ 1, 0) as limit,   (round(max (quot_laat) keep (dense_rank last order by quot_datum)+ 1, 0)*100*-1*" + selectRandomLetter[0].POSITIONTOBUY + ") effAmount,   be_volgnummer   from  beleggingsinstrument     join fn_quotes_table   on quot_symbool = be_symbool   and quot_optietype = be_optietype   and quot_expiratiedatum = be_expiratiedatum   and quot_exerciseprijs = be_exerciseprijs     join beurzen   on brs_nummer = be_bv_beurs     where be_Symbool = '" + symbol1FromDB[0].BE_SYMBOOL + "'   and be_optietype = 'PUT'   and be_handelen_toegestaan = 1   and be_expiratiedatum = '" + symbol1FromDB[0].BE_EXPIRATIEDATUM2 + "'   group by be_symbool, be_muntsoort, be_code, be_optietype, be_expiratiedatum, be_exerciseprijs, be_bv_beurs, quot_laat, brs_naam, quot_laat, quot_datum, be_volgnummer   ) where rownum = 1")
                  karate.log('Symbol2FromDB-->', symbol2FromDB)
                  var indirectCosts2FromDB = db.readRows("select icf_cfcu_id, cfcu_description, decode(icf_amount, null, 0, icf_amount) icf_amount, decode(icf_currency, null, 'NULL', icf_currency) icf_currency, decode(icf_percentage, null, 0, icf_percentage) icf_percentage from EPP_OWNER.INDIRECT_COSTS_PER_FUND join epp_owner.calculationrules on cfcu_id = icf_cfcu_id where icf_be_id = " + symbol2FromDB[0].BE_VOLGNUMMER + " and (icf_import_date_till > to_char(sysdate, 'yy-mm-dd') or icf_search_date_till is null) and icf_ex_ante_ex_post_ind = 'A' and icf_cfcu_id in (67)")
                  karate.log('indirectCosts2FromDB-->', indirectCosts2FromDB)
                  var selectRandomNumber = db.readRows("select trunc(dbms_random.value(10020000, 10050000)) min, trunc(dbms_random.value(11000000, 11500000)) max from dual")
                  karate.log('selectRandomNumber-->', selectRandomNumber)
                  var holderFromDB = db.readRows("SELECT * from   (select ap_relatienr, ap_rekeningnr,   hpp_holdernummer, ppr_id   from aktuele_posities ap     join epp_owner.rekeningen_per_product   on rpp_relatienummer = ap.ap_relatienr   and rpp_rekening_nummer = ap.ap_rekeningnr   and rpp_rekeningsoort = ap.ap_rekening_soort     join epp_owner.holders_per_product   on hpp_relatienummer = rpp_relatienummer   and hpp_product = rpp_productnummer   and hpp_product_volgnr = rpp_volgnr_per_product   and hpp_type_holder = 1       join rekeningen   on re_rekening = rpp_rekening_nummer   and re_nummer = rpp_relatienummer   and re_rekening_status = 2       join epp_owner.wwwusers   on wus_holder = hpp_holdernummer   and wus_userblocked = 0   join producten_per_relatie   on ppr_relatienummer = rpp_relatienummer   and ppr_productnummer = rpp_productnummer   and ppr_volgnr_per_product = rpp_volgnr_per_product    JOIN on_line_dossier onld   ON onld.onld_relatienummer = hpp_relatienummer   and onld_productnummer = rpp_productnummer    join kennis_per_client a   on rpp_relatienummer = ke_clientnr     where rpp_productnummer = 100   and rpp_volgnr_per_product = 1   and ap.ap_rekening_soort = 1000   and ap_saldo_positie > 7000   AND ap.ap_relatienr between " + selectRandomNumber[0].MIN + " and " + selectRandomNumber[0].MAX + "   and onld.onld_klasse = 16   and onld.onld_ontvangstdatum <> '00000000'   and onld.onld_vervaldatum = '00000000'   and ((ke_kennis_niv = 'V' or ke_ervaring = 'V')   and ke_mifcat_id = 25)   and not exists    (select 1      from kennis_per_client b       where (ke_kennis_niv = 'O' or ke_ervaring = 'O')       and a.ke_clientnr = b.ke_clientnr)   and rpp_relatienummer in     (select cl_nummer     from clienten     where cl_nummer between " + selectRandomNumber[0].MIN + " and " + selectRandomNumber[0].MAX + "     and cl_geb_datum < to_char(add_months(sysdate, -216), 'YYYYMMDD'))   and not exists     (select 1     from profiel_toevoeging_verwijderen     where ptv_relatie = ap.ap_relatienr)   and not exists     (select 1     from rekeninghouders_details     where UPPER(eor_compliancecode) like 'INSIDER%'     and eor_partij_id = hpp_holdernummer)   ) where rownum = 1")
                  karate.log('holderFromDB-->', holderFromDB)
                  while (count <= 10) {
                      if (holderFromDB.length >= 1) {
                          karate.log('condition satisfied, exiting from conditionalGoto2');
                          productId = holderFromDB[0].get('PPR_ID')
                          userId = holderFromDB[0].get('HPP_HOLDERNUMMER')
                          return {
                              productId,
                              userId
                          };
                      }
                      count++;
                      if (count >= 9) {
                          karate.fail('Count exceeded more than 10 times and conditionalgoto2 loop counter failed')
                      }
                  }
                  count++;
                  if (count >= 9) {
                      karate.fail('Count exceeded more than 10 times and conditionalgoto1 loop counter failed')
                  }
              }
          }
      }
    """
    * def derivativeTemp = call derivativeOrderTemp
    * def productId = derivativeTemp.productId
    * print 'Product Id is-->',productId
    * def userId = derivativeTemp.userId
    * print 'User Id is-->',userId
                        ################GetSpendingLimit###########################
    * def javaclass = Java.type('epos.positionmanagement.service.test.GetSpendingLimitRequestTest')
    * def map = {deviceTypeId: 2, entityId: '#(defaultEntity)', ppr_id: '#(productId)', userId: '#(userId)' }
    * def createGetSpendingLimitRequest = javaclass.getPayload(map)
    Then print 'Request body--->', createGetSpendingLimitRequest
    Given request createGetSpendingLimitRequest
    When soap action 'Request'
    Then print '\n', response

然后从其他测试用例特征文件中调用这个特征文件,如下所示:-

@Ordering
Feature: CreateCombinationDerivativeOrderTemp
 
  Background: Ordering Soap Request
    * url eposEnvironment.baseUrl
 
  @parallel=true
  Scenario: Limit-GTC-Straddle
    * call read('reusable.feature@Limit-GTC-Straddle')
    * def revisionId = get response //GetSpendingLimitResponse/spendingLimit/totalAmount*1
#    * eval for(var i=0; i<=10; i++) (revisionId<3000 && i<10 ? karate.call('request1.feature') : karate.fail('Condition did not fulfill in 10 occurrence hence failing the test case'))
    * def condionalgoto3 =
    """
    function() {
        for (var i = 0; i <= 10; i++) {
            if (revisionId < 3000) {
                karate.call('request1.feature')
                if (i >= 9) {
                    karate.fail('Count exceeded more than 10 times and conditionalgoto3 loop counter failed')
                }
            }
        }
    }
    """
    * call condionalgoto3

注意:- 这些 java 类 我用来创建有效负载 xml 这是项目的要求,这些 java 类 是只不过是我在我的项目中作为 pom 依赖项添加的实际服务,它直接给我有效负载 xml 请求,我只是创建一个映射并在请求正文中填充值,所以这些 java 类 对 use.And 是强制性的,这些数据库查询也被用于从数据库中获取实际的客户详细信息以准备请求正文,因此在这个测试用例中没有什么我可以忽略或改进的,但在某些情况下是的查询他们使用一些随机值来获取一些客户数据,我绝对可以使用一些 Java/Js/Karate.range() 但除此之外我真的不认为我可以在这里改进任何东西。