Forum Discussion

gjaiwal's avatar
gjaiwal
Occasional Contributor
2 years ago

Execute Scenario from AfterFeature function

I was trying to call a specific scenario from a specific Feature file.
And it looks like the listed Scenario is not getting executed.
The scenario is related to deleting everything that is created inside the test cases.

It needs to open up the browser and execute the code, same as the normal Scenario.

Have a look at the code here:

 

AfterFeature(function (feature){
 // Perform some action after executing a feature file, for example:
  for (i = 0; i < Features.Count; i++) {
    var name = Features.Item(i).Name;
    // Check the feature name
    if (name.indexOf("TeardownFeature") >= 0) {
      // Features.Item(i).Run(); NOT WORKING
      // Iterate through scenarios
      for (j = 0; j < Features.Item(i).Scenarios.Count; j++) {
        var scenarioName = Features.Item(i).Scenarios.Item(j).Name;
        // Check the scenario name
        if (scenarioName.indexOf(feature.Name) >= 0) // I gave same scenario name as exiting feature
          Features.Item(i).Scenarios.Item(j).Run(); // NOT WORKING
      }
    }
  }
})

 

 

 

 

  • This is another issue from the original post?

     

    Check your project settings,

    and define what you want your project to do, if an error occurs.

     

    Or capture the error and deal with it appropriately, instead of allowing TC to deal with it.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    This is another issue from the original post?

     

    Check your project settings,

    and define what you want your project to do, if an error occurs.

     

    Or capture the error and deal with it appropriately, instead of allowing TC to deal with it.

    • gjaiwal's avatar
      gjaiwal
      Occasional Contributor

      Thank you so much rraghvani 
      This is the same thing that I did.
      And it started working as expected.

       

      Thank you.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Having the same name for Feature and Scenario, makes it confusing to diagnose the issue. 

     

    Note, "The purpose of the Feature keyword is to provide a high-level description of a software feature, and to group related scenarios". 

     

    In the code that I have provided, I am using the parameter feature that is passed to AfterFeature function.

    • gjaiwal's avatar
      gjaiwal
      Occasional Contributor

      rraghvani I got what was wrong on my side.

      My requirement is to execute the particular Teardown in both cases:

      1. When the whole feature file gets executed and returns success.
      2. When any scenario from that feature file fails.

      In the first case, everything works as expected as you mentioned.

      But when I get into 2nd case teardown doesn't work properly.

      You can try failing any scenario from GroupFeature.

      In this case,

      Features.Item(i).Scenarios.Item(j).Run();

      This won't get executed.

      Could you please help me to fix this case?

      Is there any configuration we need to change to make it work?

      Thank you.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    I can see you are using ReportsEvenHistory which is different from TeardownFeature - which looks exactly the same as your question! You don't seem to be using the code that I had provided?

     

    I suggest you create a new project, with the examples I have provided so that you can see how it works.

    • gjaiwal's avatar
      gjaiwal
      Occasional Contributor

      When I created the new project I was able to run the scenario properly.
      Not sure what is the issue with the current project.

      Will check in detail and get back to you.

      Thank you rraghvani for your quick response.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    What version of TestComplete are you using? And what is not working?

    • gjaiwal's avatar
      gjaiwal
      Occasional Contributor

      I am using Version: 15.57.2.7 x64
      I am on a Free trial.

      Expected to Log this warning, which is not happening.

      The scenario "tearDown" is not getting executed.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Running all scenarios in Groups.feature,

    Running all scenarios in Teardown.feature,

    The code that will run scenarios in Teardown after Groups.

     

     

     

    • gjaiwal's avatar
      gjaiwal
      Occasional Contributor

      Thank you rraghvani,
      I really appreciate your help.
      Is it possible to share the project with me either on Git Hub or by sharing the zip of code?

      I am also doing the same thing but not working on my end.
      If you share the code I can try to run it on my end and compare the code properly.

      I shared the details in inbox.

    • gjaiwal's avatar
      gjaiwal
      Occasional Contributor

      This will be a good alternative for executing teardown after each feature in the Execution Plan.
      But it will be great if I can run the same from AfterFeature.

       

      If I run the Feature file separately in that case Teardown will not run after the feature file as we need to Run the Execution Plan.

       

      Thank you.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Ensure you understand the differences between Feature and Scenario. Your scenario name should not be the same as your feature name.

     

    I just created a quick project to show you an example. See Using Hooks

     

    • gjaiwal's avatar
      gjaiwal
      Occasional Contributor

      Yes, I am aware of the Feature and Scenario.

      Here is the full Screenshot of 2 different files.
      Is it possible to share the project that you created on Git Hub or in any other way?

      When the Groups feature file stops execution then I want to run the GroupFeature Scenario from Treardown.feature File.

      This is from Teardown.feature file.

       

      This is from Groups.feature file

      It would be a great help if you could share the example project that you just created.

      Thank you.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Here's an example,

    The result,

     

    Here, you are checking if the feature name equals the scenario name, which will always be false.

    if (scenarioName.indexOf(feature.Name) >= 0) 

     

    • gjaiwal's avatar
      gjaiwal
      Occasional Contributor

      Thank you for the example rraghvani 
      The scenario Name is the same as the feature name that I provided.

      Here are the names of the Feature and Scenario that I am trying to execute.
      Did you write the AfterFeature code in the same file where you wrote the steps definition?
      I am using different script files for AfterFeature and for the step definition of my scenario.


  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Debug your code, and step through each of the statements to ensure that it's doing what it's supposed to do.

    • gjaiwal's avatar
      gjaiwal
      Occasional Contributor

      Yes, I tried,
      My code is running till, 

      Features.Item(i).Scenarios.Item(j).Run();

      But nothing happened after that.
      Expected to execute the Scenario by opening a browser and navigating to the URL.