Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No browser object is available for BeforeAll hooks on using cucumber #3038

Closed
SDETQATestAutomation opened this issue Feb 8, 2022 · 4 comments
Labels
cucumber Issues related to Cucumber Test runner needs more info

Comments

@SDETQATestAutomation
Copy link

Describe the bug

No browser object is available for BeforeAll hooks on using cucumber. On using browser in before all hooks no test is executing.

Sample test

sampleTest.js

// Please add the sample test here

const {When, Before, AfterAll, BeforeAll,AfterStep, BeforeStep} = require('@cucumber/cucumber');

BeforeAll(async function () {
    console.log("Before All")
    await browser.url("https://www.google.com")
});

Run with command

$ node nightwatch.js -c config/nightwatch.webdriver.cucumber.conf.js --env chrome

Verbose output

debug.log

<!-- Include the verbose output, if possible (run nightwatch with `--verbose` argument) -->

{
_: [],
c: 'config/nightwatch.webdriver.cucumber.conf.js',
config: 'config/nightwatch.webdriver.cucumber.conf.js',
browser: 'chrome',
env: 'chrome',
e: 'chrome',
reporter: 'junit',
r: 'junit',
'$0': 'C:\MyInstalledProgram\nodejs\node.exe C:\GartnerWorkspace\cpp_framework\cppdocs_v2.0\nightwatch.js',
_source: [],
format: [ 'json:reports/cucumber.json', '@cucumber/pretty-formatter' ]
}
Before All

Configuration

nightwatch.json

module.exports = {
  live_output: true,
  disable_colors: false,
  output_folder: "reports/",
  custom_commands_path: "",
  page_objects_path: "",
  globals_path: "",
  src_folders: ["tests/stepdefs"],

  test_workers: false,
  webdriver: {
    start_process: true,
    server_path: require('selenium-webdriver').path,
    log_path: "./",
    host: "127.0.0.1",
    port: 4444,
    cli_args: [
      "--verbose"
    ]
  },
  test_runner: {
    type: 'cucumber',
    options: {
      feature_path: 'tests/features/*.feature',
      additional_config: '',
      parallel: 2
    }

  },
  test_settings: {
    default: {
      launch_url: "http://localhost",
      "selenium.host": "127.0.0.1",
      "selenium.port": 4444,
      silent: true,
      disable_colors: false,
      disable_error_log: true,
      report_network_errors: false,
      skip_testcases_on_fail: false,
      end_session_on_fail: false,
      screenshots: {
        enabled: true,
        on_failure: true,
        on_error: true,
        path: "./screenshots"
      },
      log_screenshot_data: false,
      desiredCapabilities: {
        browserName: "chrome",
        javascriptEnabled: true,
        acceptSslCerts: true
      }
    },
    chrome: {
      desiredCapabilities: {
        browserName: "chrome",
        javascriptEnabled: true,
        acceptSslCerts: true,
        elementScrollBehavior: 1,
        "goog:chromeOptions": {
          "w3c": true,
          "args": [
            "--allow-cross-origin-auth-prompt",
            "--allow-control-allow-origin",
            "-–allow-file-access-from-files",
            "--test-type",
            "disable-infobars",
            "--disable-extensions",
            "--start-maximized",
            "--lang=en",
            "--no-sandbox",
          ],
          excludeSwitches: ["enable-automation", "disable-popup-blocking", "enable-logging"],
          useAutomationExtension: false,
          prefs: {
            credentials_enable_service: false,
            profile: { password_manager_enabled: false },
            extentions: {},
            download: {
              'prompt_for_download': false,
              'directory_upgrade': true,
              'default_directory': '/downloads',
            }
          }
        },
        "loggingPrefs": { "driver": "INFO", "server": "OFF", "browser": "INFO" }
      }
    },
  }
};

Your Environment

Executable Version
nightwatch --version 2.0.5
npm --version 6.14.13
node --version v14.17.3
Browser driver Version
chromedriver 96
OS Version
Windows 10 20H2
@gravityvi
Copy link
Member

browser object is inserted in before hook of cucumber due to certain reasons and how cucumber has been constructed. Can you share your use case?

@gravityvi gravityvi added needs more info cucumber Issues related to Cucumber Test runner labels Feb 8, 2022
@beatfactor
Copy link
Member

As far as I know, the BeforeAll() is executed before the Nightwatch session is started, so there's no browser object available. It is the same behaviour with the global before() hook in Nightwatch.

@AutomatedTester
Copy link
Member

No further info was provided so closing. Please reopen a new issue with the relevant info

@SDETQATestAutomation
Copy link
Author

SDETQATestAutomation commented Jul 3, 2024

@AutomatedTester @beatfactor @gravityvi


Issue: No browser object is available for BeforeAll hooks when using Cucumber

Description:

I am encountering an issue where the browser object is not accessible within the BeforeAll or Before hooks when using Nightwatch.js with Cucumber.

Scenario:

In my test setup, I need to fetch the user agent from the browser and update it before starting the test execution. To achieve this, I intend to use the execute command to retrieve the user agent. However, since the browser object is not available in the BeforeAll or Before hooks, I am unable to perform this operation.

Steps to Reproduce:

  1. Set up a Nightwatch.js project with Cucumber integration.
  2. Try to access the browser object within a BeforeAll or Before hook.
  3. Attempt to use the execute command to fetch the user agent.

Expected Behavior:

The browser object should be accessible within the BeforeAll or Before hooks, allowing the use of the execute command to retrieve and update the user agent before the test execution begins.

Actual Behavior:

The browser object is not available in the BeforeAll or Before hooks, preventing the execution of commands that require the browser object.

Example Code:

const { BeforeAll, Before } = require('cucumber');

BeforeAll(async function() {
  // Attempt to access the browser object and execute a command
  const userAgent = await browser.execute(function() {
    return navigator.userAgent;
  });

  // Update the user agent or perform other necessary actions
  console.log('User Agent:', userAgent);
});

Request:

It would be highly beneficial if the browser object could be made available in the BeforeAll or Before hooks when using Nightwatch.js with Cucumber. This would enable pre-test setup tasks such as fetching and updating the user agent or other necessary configurations.

Thank you for your attention to this issue. I look forward to any suggestions or potential fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cucumber Issues related to Cucumber Test runner needs more info
Projects
None yet
Development

No branches or pull requests

4 participants