nightwatch-api icon indicating copy to clipboard operation
nightwatch-api copied to clipboard

[Question] how to capture `client` instance before any steps have been executed

Open pendenaor opened this issue 7 years ago • 13 comments

I'm currently migrating my bdd test (based on the cucumber example) and i want to create some page urls with url property set in global test settings, eg:

const pageUrls = {
  'application page': `${url}/`,
  'login page': `${url}/login`,
  'dashboard page': `${url}/main/dashboard`
}

pendenaor avatar Sep 06 '18 06:09 pendenaor

Why you don’t create a simple node module with these constants and import it where needed?

mucsi96 avatar Sep 06 '18 12:09 mucsi96

You can add the url also to this module. And import it in nightwatch configuration

mucsi96 avatar Sep 06 '18 12:09 mucsi96

I have some troubles to understand what to do. Below a sample of my code :

import { client } from 'nightwatch-api';
import { defineStep } from 'cucumber';

const { url } = client.globals; // <= BOOM!

const pageNames = {
  'application page': `${url}/`,
  'login page': `${url}/login`,
  'dashboard page': `${url}/main/dashboard`
}

defineStep(/^I (?:browse|open|visit).*? `(.*?)`$/, async pageName => {
  console.log(pageNames[pageName])
});

When i run this code, it raises an exception

Error: Nightwatch client is not ready.
              Looks like function "createSession" did not succeed or was not called yet.
    at Object.get (C:\Public\Repo\sinoc_ui-test\node_modules\nightwatch-api\lib\proxy.js:21:17)
    at Object.<anonymous> (C:/Public/Repo/sinoc_ui-test/step-definitions/steps.js:5:24)
    at Module._compile (module.js:652:30)
    at loader (C:\Public\Repo\sinoc_ui-test\node_modules\babel-register\lib\node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (C:\Public\Repo\sinoc_ui-test\node_modules\babel-register\lib\node.js:154:7)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at C:\Public\Repo\sinoc_ui-test\node_modules\cucumber\src\cli\index.js:63:42
    at Array.forEach (<anonymous>)
    at Cli.getSupportCodeLibrary (C:\Public\Repo\sinoc_ui-test\node_modules\cucumber\src\cli\index.js:63:22)
    at Cli.<anonymous> (C:\Public\Repo\sinoc_ui-test\node_modules\cucumber\src\cli\index.js:78:37)
    at Generator.next (<anonymous>)
    at Generator.tryCatcher (C:\Public\Repo\sinoc_ui-test\node_modules\bluebird\js\release\util.js:16:23)

pendenaor avatar Sep 06 '18 12:09 pendenaor

May be i've to tweak test/support/setup.js

import { setDefaultTimeout, AfterAll, BeforeAll} from 'cucumber'
import { createSession, closeSession } from 'nightwatch-api';
import { client } from 'nightwatch-api'; // <== add this

setDefaultTimeout(60000);

BeforeAll(async () => {
  await createSession('default');
  // inject here client globals and calculated url into World (=this) ?
});

AfterAll(async () => {
  await closeSession();
});

pendenaor avatar Sep 06 '18 12:09 pendenaor

I see. Maybe its a bug. Will try to investigate. Until this is resolved I suggest not using nightwatch globals. Just creating an node module for that.And using it in page objects, steps and nightwatch configuration as well. Is it fine for you?

mucsi96 avatar Sep 06 '18 12:09 mucsi96

ok, thanks for your time. And thanks for nightwatch-api 👍

pendenaor avatar Sep 06 '18 12:09 pendenaor

I am also facing the same issue while running cucumber tests using nightwatch-api. Can we please get a solution for this. Thanks

pratyush23 avatar Nov 16 '18 16:11 pratyush23

Hi @pendenaor and @pratyush23 , are you still facing this issue? If yes, could you point me to a repo with sample project with similar config?

spnraju avatar Jan 27 '19 03:01 spnraju

Closing due to lack of activity. Feel free to reopen if the issue still exist

mucsi96 avatar Feb 04 '19 19:02 mucsi96

@spnraju @mucsi96 i am facing the same issue Looks like function "createSession" did not succeed or was not called yet. ( i am trying to attach vscode debugger with nightwatch test case with jest )

hardy12994 avatar May 13 '19 09:05 hardy12994

@mucsi96 I am facing this issue, too. My tests need the client.globals to know where they have to start. As an example I am checking the currently used .env File in my nightwatch.conf. Afterwards I set the URL dynamically.

I have also to distinguish between some things and therefor I am using boolean values.

Example for my config:

globals: {
                url: isStagingEnvironment ? 'www.example.com' : 'http://localhost',
                apiUrlApplicant: process.env.URL_APPLICANT,
                apiTokenApplicant: process.env.API_TOKEN,
                useNetworkStubs,
                isNotMocked,
                isStagingEnvironment,
            },

Example for a using in my step files:

if (client.globals.isStagingEnvironment) {
        client.expect.element(_SELECTOR_).to.be.visible.before(DEFAULT_WAIT_IN_MS);
    }

Example for a using in my helper files:

const { client } = require('nightwatch-cucumber');

const { url } = client.globals;

module.exports = {
    'applications page': `${url}/.../...`,
....

KatiKop avatar Jun 25 '19 06:06 KatiKop

Hi @mucsi96

First, thanks a lot for this new package API. Looks interesting. I don't know if my problem is similar but after did the configurations I could not run steps files when I am using page object model.

I am trying to update my framework test from the old "Nightwatch-Cucumber (deprecated)" to this new "Nightwatch-api". After run my step test, I am facing the issue:

Error: Nightwatch client is not ready.
              Looks like function "createSession" did not succeed or was not called yet.

Basically, this was working fine before in old "Nightwatch-Cucumber (deprecated)" but in this new api is not running:

This is the code that I am using in my step file:

const { client } = require('nightwatch-api');
const { Given, Then, When } = require('cucumber');
var core = require('../../../../../pages/core');
core(client).loginRequired();

Is there something that I am not considering to point correctly to my function?

Thanks in advance for your help!

gornelex avatar Sep 21 '19 03:09 gornelex

Hi @gornelex , thanks for reaching out. I see that you are facing issues in migration. Please note that nightwatch-api is a lot different than that of nightwatch-cucumber. Please follow the examples provided in the repo. if you still face the issue, an example setup will help in understanding it better. Please let us know.

spnraju avatar Sep 21 '19 16:09 spnraju