【问题标题】:How to get scenario name from a scenario outline in cypress gherkin setup?如何从 cypress gherkin 设置中的场景大纲中获取场景名称?
【发布时间】:2020-02-27 08:42:50
【问题描述】:

假设我有一个测试用例 -

Scenario: Scenario to verify Title Matched 

  When Navigate to the App "Facebook"

  Then verify the "TitleName" Field

如何从“当导航到应用程序 Facebook”和“然后验证“TitleName”字段”对应的步骤定义方法中获取场景名称

步骤定义方法是 -

When('Navigate to the App {string} for demo',(AppURL:string)=>{

    if(AppURL=="FaceBook"){

    }

});

Then('verify the Title of the page for demo',()=>

 {    
        SampleAPPUI.verfiyTitledemo('');

});

注意:我正在使用带有 typescript 的 cypres-cucumber

【问题讨论】:

    标签: cucumber cypress gherkin


    【解决方案1】:

    我在 Java-Selenium-Gherkin 测试套件中执行此操作。它可能不是您需要的解决方案,但它会为您提供有关如何获取值的一些指导:

    @BeforeStep
    public void doSomethingBeforeStep(Scenario scenario) throws Exception {
        testScenario = scenario.getName().toString();
        scenarioObj = scenario;
        Field f = scenario.getClass().getDeclaredField("testCase");
        f.setAccessible(true);
        TestCase r = (TestCase) f.get(scenario);
    
        List<PickleStepTestStep> stepDefs = r.getTestSteps()
                .stream()
                .filter(x -> x instanceof PickleStepTestStep)
                .map(x -> (PickleStepTestStep) x)
                .collect(Collectors.toList());
    
        PickleStepTestStep currentStepDef = stepDefs.get(currentStepIndex);
        testCase = currentStepDef.getStepText();
    }
    

    另外,请参阅 herehere

    【讨论】:

    • 我没有办法使用打字稿在柏树黄瓜中检索场景对象。
    • @HardikRana 所以我假设 Java Cucumber 和 Typecscript Cucumber 之间没有相关性。
    【解决方案2】:

    这对我来说非常有效,虽然我没有使用 TS,但它背后的逻辑应该会给你一个很好的起点:

        function getScenarioName(){
        const _onRunnableRun = Cypress.runner.onRunnableRun
        Cypress.runner.onRunnableRun = function (runnableRun, runnable, args) {
          const r = runnable
          const isHook = r.type === 'hook'
          const isAfterAllHook = isHook && r.hookName.match(/after all/)
          const isBeforeHook = isHook && r.hookName.match(/before each/)
          const test = r.ctx.currentTest || r
    
          var testTitle = test.title //this is the test title
    
          const next = args[0]
          if (
            isHook &&
            r.ctx.currentTest &&
            r.ctx.currentTest.trueFn &&
            !isAfterAllHook
          ) {
            return next.call(this)
          }
          const onNext = function (err) {
            const fail = function () {
              return next.call(this, err)
            }
            const noFail = function () {
              test.err = null
              return next.call(this)
            }
            if (err) {
              if (test._retries === -1) {
                test._retries = getDefaultRetries()
              }
              if (isBeforeHook && test._currentRetry < test._retries) {
                test.trueFn = test.fn
                test.fn = function () {
                  throw err
                }
                return noFail()
              }
            }
            return fail()
          }
          args[0] = onNext
          return _onRunnableRun.apply(this, [runnableRun, runnable, args])
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 2020-07-11
      • 2014-03-23
      相关资源
      最近更新 更多