Skip to content

Why does the test does not wait for the Before hooks to complete? #3870

Closed

Description

@myrepojuly

What are you trying to achieve?

I am trying for perform few API requests with the Before event. But, before even it completes, the Tests starts to run. I need a response data from the Before event that I can use in the Tests. Could you please let me know the mistake that I am making?

What do you get instead?

Before even the Before event completes, the Tests starts to run. I need a response data from the Before event that I can use in the Tests

Provide console output if related. Use --verbose mode for more details.
### Please see the below output, where the order is not maintained

Test something --
Response 1 execution is complete
Response 3 execution is started
Response 2 execution is complete
Response 3 execution is complete
  √ some scenario in 3270ms
Test Complete

  OK  | 1 passed   // 4s

Provide test source code if related

 event.dispatcher.on(event.test.before, async function () {
    const {I} = inject();
     
    const response1 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
    console.log('Response 1 execution is complete')
    
    const response2 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
    console.log('Response 2 execution is complete')
    
  });
****************Tests********************
Feature('Test something');

Scenario('some scenario', async ({ I }) => {
  console.log('Response 3 execution is started')
    const response3 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
    console.log('Response 3 execution is complete')
});

Details

  • CodeceptJS version: latest
  • NodeJS Version:
  • Operating System:
  • puppeteer || webdriverio || testcafe version (if related)
  • Configuration file:

@kobenguyent @DavertMik Could you please help me with this?

Activity

  1. kobenguyent commented on Sep 16, 2023

    @kobenguyent
    Collaborator

    Any reason why you don't use Before()?

  2. myrepojuly commented on Sep 16, 2023

    @myrepojuly
    Author

    @kobenguyent tried with Before() as well. I get the same output :(

  3. myrepojuly commented on Sep 16, 2023

    @myrepojuly
    Author

    @kobenguyent - I think I get the problem. Looks fine with using Before(). Thank you! Closing this

  4. myrepojuly commented on Sep 16, 2023

    @myrepojuly
    Author

    @kobenguyent - this doesn't work if I use BDD format. Could you please let me know the mistake that I am making?

    const {I} = inject();
    
    Before(async () => {
       
      const response1 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
      console.log('Response 1 execution is complete')
      
      const response2 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
      console.log('Response 2 execution is complete')
      
    });
    
    When('I send a http get request', async() => {
      console.log('Response 3 execution is started')
      const response3 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
      console.log('Response 3 execution is complete')
    });
    

    Output
    Test something --

    Response 3 execution is started
    Response 1 execution is complete
    Response 3 execution is complete
    Response 2 execution is complete
    
  5. kobenguyent commented on Sep 17, 2023

    @kobenguyent
    Collaborator

    I'm not really familiar with gherkin but is it the case you could try https://codecept.io/bdd/#advanced-gherkin Background which is literally like before for each scenario?

  6. kobenguyent commented on Sep 17, 2023

    @kobenguyent
    Collaborator
    const { I } = inject();
    
    Given('I prepare data', async () => {
    
        const response1 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
        console.log('Response 1 execution is complete')
    
        const response2 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
        console.log('Response 2 execution is complete')
    
    });
    
    When('I send a http get request', async() => {
        console.log('Response 3 execution is started')
        const response3 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
        console.log('Response 3 execution is complete')
    });
    
    Feature: Business rules
        In order to achieve my goals
        As a persona
        I want to be able to interact with a system
    
    Background:
        Given I prepare data
    
    Scenario: do something
        When I send a http get request
    

    so basically, move the steps from Before() block to Given and all thing works as expected

    CodeceptJS v3.5.4 #StandWithUkraine
    Using test root "/Users/t/Desktop/codeceptJSExample"
    
    Business rules --
        In order to achieve my goals
        As a persona
        I want to be able to interact with a system
    Response 1 execution is complete
    Response 2 execution is complete
    Response 3 execution is started
    Response 3 execution is complete
    
  7. myrepojuly commented on Sep 17, 2023

    @myrepojuly
    Author

    Thanks @kobenguyent … was able to address this using the Before() block itself… Believe it is because of the asynchronous nature of JavaScript. Another option was to add the https request to the recorder as given here https://codecept.io/hooks/#recorder … This worked if I don’t use ‘I’ object(example await I.sendGetRequest()) … It just hangs and does nothing if use the ‘I’ object.

    Anyway thank you so much for your time for looking into this.

  8. kobenguyent commented on Sep 17, 2023

    @kobenguyent
    Collaborator

    I noticed one thing that After() block works fine. So I'm not sure why Before behaves differently on Gherkin, tho.

    After(async () => {
        console.log('Response 4 execution is started')
        const response4 = await I.sendGetRequest('https://codecept.io/helpers/REST/');
        console.log('Response 4 execution is complete')
    });
    
    CodeceptJS v3.5.4 #StandWithUkraine
    Using test root "/Users/t/Desktop/codeceptJSExample"
    
    Business rules --
        In order to achieve my goals
        As a persona
        I want to be able to interact with a system
    Response 1 execution is complete
    Response 2 execution is complete
    Response 3 execution is started
    Response 3 execution is complete
    Response 4 execution is started
      ✔ do something in 1596ms
    Response 4 execution is complete
    
  9. kobenguyent commented on Sep 17, 2023

    @kobenguyent
    Collaborator
    @Puppeteer @WebDriverIO @bdd
    Feature: Site test
      In order to achieve my goals
      As a persona
      I want to be able to interact with a system
    
      Background:
        Given I opened website
    
      Scenario: check text
        Then I should see "Welcome to test app"
    

    I'm not sure but I think with Gherkin, the Background is literally meaning Before().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions