mucsi96 / nightwatch-api

[DEPRECATED] Cross-runner API for Nightwatch.js
https://nightwatch-api.netlify.com/
MIT License
84 stars 64 forks source link

nightwatch-api client.elements is not same as nightwatch browser.elements #841

Closed tajinders closed 3 years ago

tajinders commented 4 years ago

.elements is not working in the Given/Then step methods of cucumber when call with “nightwatch-api” context/client

module.exports = {
    async 'demo test'(browser) {
        const homePage = browser.page.homepage();
        homePage.navigate();
        let result = await homePage.api.elements('css selector', 'div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr');
            console.log(" row length is " + "<->" + result.value.length);
    }
};

Output : row length is <->10 However the same code is not working when I try to use it within the Given/Then methods of cucumber with "nightwatch-api" context.

const { client } = require('nightwatch-api'); const { Given, Then, When } = require('cucumber');

Given(/^click "([^"]*)" service from list$/, async function (service)  {
    const homepage = client.page.homepage();
        homepage.navigate();
        let result = await homepage.api.elements('css selector', 'div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr');
    console.log(" row length is " + "<->" + result.value.length);
  });

Error : TypeError: Cannot read property 'value' of undefined Any idea how can I make it work with nightwatch-api and cucumber, Thanks!

Link to Stack overflow : https://stackoverflow.com/questions/60468313/elements-is-not-working-in-the-given-then-step-methods-of-cucumber-when-call-wi

mucsi96 commented 4 years ago

On quick look you miss await statements. Every command like navigate is async so you have to await it. Please let me know if the issue persists even after adding missing awaits

mucsi96 commented 4 years ago

Also you trying to get the result in wrong place. You get result in callback. Not as result of await

mucsi96 commented 4 years ago

I know its strange. But the reason for that that this is the api of original nightwatch

tajinders commented 4 years ago

So this below is not working and result is undefined.

Given(/^I open dashboard page$/, async function () {

const { client } = require('nightwatch-api'); const homepage = client.page.homepage(); await homepage.navigate(); let result = await homepage.api.elements('css selector', 'div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr'); console.log(" row length is " + "<->" + result.value.length); }); -- output : result is undefined

I agree that If I get result with in callback it will work, but then this behavior is not consistent with nightwatch. Also, its not a good practice to use await with callbacks, kind of defeats the overall purpose of async/await.

Below is an example code from nightwatch and it works perfectly fine!! No callbacks needed and my "nightwatch": version is "1.3.4"

module.exports = { async 'demo test'(browser) { const homePage = browser.page.homepage(); homePage.navigate(); let result = await homePage.api.elements('css selector', 'div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr'); console.log(" row length is " + "<->" + result.value.length); } };

If possible please fix this issue with nightwatch-api, its impacting the way how I want to use async/await in my code, thanks!

spnraju commented 3 years ago

Hi @tajinders was there any update on this?