0

I'm trying to do nightwatch and cucumber integration for BDD. I'm unable to access client.elements function in a step function.

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


    Then(/^click "([^"]*)" service from list$/, async(service)  => {
        let result = await client.url('http://localhost:8080/').elements('css selector', 'div.flex.mt-4.v-card.v-sheet.theme--light > div > div > table > tbody > tr');
        console.log(" col length is " + "<->" + result.value.length);
        return homePage; 

      });

Any help is appreciated. thanks!

Tajinder Singh
  • 205
  • 1
  • 5
  • 12

1 Answers1

0

I tried to replicate this and below is working for me.

await client.url('https://www.phptravels.net/home').moveToElement('css selector', '.product-grid-item', 0, 0).elements('css selector', '.product-grid-item', (res) => {
    console.log("%%%%%%% col length is " + "<-> " + res.value.length);
  });

I am not sure if we can store the response returned from elements api in a variable. I will update this answer if I find a way to do that.

Raju
  • 2,299
  • 2
  • 16
  • 19
  • is the client a nightwatch or a nightwatch-api instance in your example? – Tajinder Singh Feb 28 '20 at 22:32
  • It is nightwatch-api – Raju Feb 29 '20 at 02:40
  • @TajinderSingh Did this work? You can directly put it in one of your step definitions and check. The issue with your code could be that your element is not in the viewport and needs to be scrolled before reading the elements. – Raju Feb 29 '20 at 19:41
  • This works but I don"t want a result from a callback. The reason is the code I showed is just a simple version of what I'm trying to do. Basically from Then function I've to call my own custom method what is located in one of the modules I've created and that method has this.api.elements statement... its failing there... I dont want to do a call back there, thats why I need result in a variable which I can return... – Tajinder Singh Feb 29 '20 at 19:46
  • Just add a let to beginning and upgrade it inside the callback. After await the variable will be set – mucsi96 Feb 29 '20 at 20:11
  • I basically have a getTableData function and that function has 3 calls, first to find rows using this.api.elements , second to find number of columns using this.api.elements and then i am running a nested for loop and populating a Map using this.api.getText to store the table data and returning the map.... everything here is using await.... Everything works fine when I use it with a nightwatch browser object but when I try to use it using nightwatch-api client, its not working..... – Tajinder Singh Feb 29 '20 at 20:32
  • is nightwatch-api client and nightwatch browser objects are 100% identical?? any idea? anyway I can check that? – Tajinder Singh Feb 29 '20 at 20:33