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

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

Open
pendenaor opened this issue Sep 6, 2018 · 13 comments
Labels
bug Something isn't working

Comments

@pendenaor
Copy link

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`
}
@mucsi96
Copy link
Owner

mucsi96 commented Sep 6, 2018

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

@mucsi96
Copy link
Owner

mucsi96 commented Sep 6, 2018

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

@pendenaor
Copy link
Author

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
Copy link
Author

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();
});

@mucsi96
Copy link
Owner

mucsi96 commented Sep 6, 2018

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?

@pendenaor
Copy link
Author

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

@mucsi96 mucsi96 added the bug Something isn't working label Sep 9, 2018
@pratyush23
Copy link

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

@spnraju
Copy link
Collaborator

spnraju commented Jan 27, 2019

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?

@mucsi96
Copy link
Owner

mucsi96 commented Feb 4, 2019

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

@mucsi96 mucsi96 closed this as completed Feb 4, 2019
@hardeep-bit
Copy link

@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 )

@mucsi96 mucsi96 reopened this May 13, 2019
@KatiKop
Copy link

KatiKop commented Jun 25, 2019

@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}/.../...`,
....

@gornelex
Copy link

gornelex commented Sep 21, 2019

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!

@spnraju
Copy link
Collaborator

spnraju commented Sep 21, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants