nightwatchjs / nightwatch

Integrated end-to-end testing framework written in Node.js and using W3C Webdriver API. Developed at @browserstack
https://nightwatchjs.org
MIT License
11.8k stars 1.32k forks source link

Variables Not Cleared Between Tests #3988

Open kreuka opened 9 months ago

kreuka commented 9 months ago

Description of the bug/issue

I can reuse the usual const/let variable from test number 1, in test number 2the

Steps to reproduce

No response

Sample test

    @a
    Scenario: Set 'test' variable
        Given I set the "test" value to the test variable
        When I log the test variable

    @a
    Scenario: Log 'test' variable
        When I log the test variable

const {  Given } = require("@cucumber/cucumber")

let test

Given(/^I set the "(.*?)" value to the test variable$/, (value) => {
    test = value
})

Given(/^I log the test variable$/, () => {
    console.log(test)
})

Command to run

npx nightwatch ./test

Verbose Output

info: Running the Scenario: Set 'test' variable
..STEP 1: Given I set the "test" value to the t
est variable
.STEP 2: When I log the test variable
test

info: Running the Scenario: Log 'test' variable
..STEP 1: When I log the test variable
test

Nightwatch Configuration

No response

Nightwatch.js Version

3.3.2

Node Version

18.21.0

Browser

No response

Operating System

No response

Additional Information

No response

garg3133 commented 9 months ago

Well, that's just how JavaScript (or any programming language for that matter) works... since test is declared in global scope (in the context of the file), it will be accessible in all the functions declared or called in that file. And if one such function changes its value, it will be changed for all the functions that access it afterwards.

What you can do is reset the variable test manually at the end or start of each test, but the function to reset should be mentioned in the same file so that it can access the test variable.

kreuka commented 9 months ago

@garg3133 But it worked in another way in the 2.2.3 version. It also seems to me that it violated the principle where each test should be completely independent

garg3133 commented 9 months ago

@kreuka we'll look into it in that case.