Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cucumber 如何在cypress gherkin设置中从场景大纲中获取场景名称?_Cucumber_Cypress_Gherkin - Fatal编程技术网

Cucumber 如何在cypress gherkin设置中从场景大纲中获取场景名称?

Cucumber 如何在cypress gherkin设置中从场景大纲中获取场景名称?,cucumber,cypress,gherkin,Cucumber,Cypress,Gherkin,假设我有一个像这样的测试用例- Scenario: Scenario to verify Title Matched When Navigate to the App "Facebook" Then verify the "TitleName" Field 如何从对应于“当导航到应用程序Facebook”和“然后验证”标题名“字段”的步骤定义方法中获取场景名称 步骤定义方法包括- When('Navigate to the App {string} for demo',(AppU

假设我有一个像这样的测试用例-

Scenario: Scenario to verify Title Matched 

  When Navigate to the App "Facebook"

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

步骤定义方法包括-

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

    if(AppURL=="FaceBook"){

    }

});

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

 {    
        SampleAPPUI.verfiyTitledemo('');

});

注意:我在Java Selenium小黄瓜测试套件中使用Cypress cucumber和typescript。这可能不是您需要的解决方案,但它将为您提供有关如何获取值的一些指导:

@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();
}
@BeforeStep
public void doSomethingBeforeStep(场景)引发异常{
testScenario=scenario.getName().toString();
Scenariobj=情景;
字段f=scenario.getClass().getDeclaredField(“testCase”);
f、 setAccessible(true);
TestCase r=(TestCase)f.get(场景);
List stepDefs=r.getTestSteps()
.stream()
.filter(PickleStepTestStep的x->x实例)
.map(x->(PickleStepTestStep)x)
.collect(Collectors.toList());
PickleStepTestStep currentStepDef=stepDefs.get(currentStepIndex);
testCase=currentStepDef.getStepText();
}

另外,请参见和

这对我来说非常有效,尽管我没有使用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])
    }
}
函数getScenarioName(){ const_onRunnableRun=Cypress.runner.onRunnableRun Cypress.runner.onRunnableRun=函数(runnableRun,runnable,args){ 常数r=可运行 const isHook=r.type=='hook' const isAfterAllHook=isHook&&r.hookName.match(/afterall/) const isBeforeHook=isHook&&r.hookName.match(/beforeach/) 常数测试=r.ctx.currentTest | r var testTitle=test.title//这是测试标题 const next=args[0] 如果( 伊舒克&& r、 电流测试&& r、 ctx.currentTest.trueFn&& !我在后面胡克 ) { 返回下一个。呼叫(此) } const onNext=函数(err){ 常量失败=函数(){ 返回下一个。调用(此,错误) } const noFail=函数(){ test.err=null 返回下一个。呼叫(此) } 如果(错误){ 如果(测试重试次数==-1){ 测试。_retries=getDefaultRetries() } 如果(isBeforeHook&test.\u currentRetry我没有办法在cypress cucumber中使用typescript检索场景对象。@HardikRana所以我假设Java cucumber和Typecscript cucumber之间没有关联。