You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Why does the test does not wait for the Before hooks to complete? #3870
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,asyncfunction(){const{I}=inject();constresponse1=awaitI.sendGetRequest('https://codecept.io/helpers/REST/');console.log('Response 1 execution is complete')constresponse2=awaitI.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')constresponse3=awaitI.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)
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?
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
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.
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
@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().
Activity
kobenguyent commentedon Sep 16, 2023
Any reason why you don't use Before()?
myrepojuly commentedon Sep 16, 2023
@kobenguyent tried with
Before()as well. I get the same output :(myrepojuly commentedon Sep 16, 2023
@kobenguyent - I think I get the problem. Looks fine with using
Before(). Thank you! Closing thismyrepojuly commentedon Sep 16, 2023
@kobenguyent - this doesn't work if I use BDD format. Could you please let me know the mistake that I am making?
Output
Test something --
kobenguyent commentedon Sep 17, 2023
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?
kobenguyent commentedon Sep 17, 2023
so basically, move the steps from Before() block to Given and all thing works as expected
myrepojuly commentedon Sep 17, 2023
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.
kobenguyent commentedon Sep 17, 2023
I noticed one thing that After() block works fine. So I'm not sure why Before behaves differently on Gherkin, tho.
kobenguyent commentedon Sep 17, 2023
I'm not sure but I think with Gherkin, the Background is literally meaning Before().