0

I'm a beginner in using Nightwatch and I'm stuck with the following:

I've got a table that has a <tbody> and multiple <tr>s in it. I need to check if a specific word is in one of the cells in the first column.

First, I tried getting the whole table using document.GetElements... but it appears this is not supported by Nightwatch. Currently, I'm looking for a solution without using Nightwatch custom command.

P.S. I can't create tags yet, so it'd be awesome if someone could create one, like table-to-array or something like that.

tz0
  • 300
  • 2
  • 10
  • 1
    May this work for you https://stackoverflow.com/q/25540997/5803974 – Satish Nov 13 '17 at 10:32
  • Thanks, but no. I can't use _document._ in code. – tz0 Nov 13 '17 at 11:01
  • I am not knowing about Nightwatch but I found this http://czyzykowski.com/posts/nightwatch-table-data.html – Satish Nov 13 '17 at 11:09
  • That's the custom command I've mentioned. I was hoping to avoid it since I'm not proficient in nightwatch, but it seems I can't. Thanks! – tz0 Nov 13 '17 at 11:12

1 Answers1

1

You can execute javascript code into the browser using execute command (Documentation here). So you can do something like this:

client.execute(function(){
    /*your js to get the DOM element and check if the element has the text you want*/
    return true //or false if didnt find it
},[],function(response){
    console.log(response.value)//this will contain your returned value
    client.assert.equal(response.value, true, "Text not found on table")
})
Frankusky
  • 1,040
  • 9
  • 18
  • Thanks! Here's what I wrote for js code: var tbs=document.getElementByTagName('tbody')[0].getElementByClassName('carrier-name'); for(var i=0; i – tz0 Nov 13 '17 at 14:55
  • Does in your assert are you checking the `response.value` value? The callback function receives as parameter other data (I dont remember what other data). At the `value` key, it is the data that you have returned – Frankusky Nov 13 '17 at 15:00
  • Here's my entire test.js: module.exports = { 'Test' : function (client) { client.execute(function(){ var tbs=document.getElementByTagName('tbody')[0].getElementByClassName('carrier-name'); for(let i=0; i – tz0 Nov 13 '17 at 15:11
  • Not sure what exactly you're asking me (as I've said, I'm a beginner). I don't know what type response.value is.. the way I understood it, it's what is returned in the js function (true or false), isn't it? – tz0 Nov 13 '17 at 15:12
  • I've added _console.log(tds[i].innerText)_ in the loop, but in console, nothing gets written... only the same error: _Failed [equal]: (Error) - expected "true" but got: "[object Object]"_ – tz0 Nov 13 '17 at 15:21
  • the first parameter is a function its executed on browser, so in the browser console you will see tds[i].innerText values, the second parameter is an array of arguments that the browser function will receive, in this case you can send an empty array. The third parameter is the function that will be executed on nodejs, and it receives as parameter an object with a bunch of data, in that data there should be an object key named `value` where its the true/false value that you returned on the first function – Frankusky Nov 13 '17 at 15:25
  • I've read a bunch of texts about this function, but it is only now that I actually understand it, so thank you very much! :) Unfortunately, for some reason, _console.log(tds[i].innerText_ that is written as the part of the first parametar isn't executed at all (there's no result of it in the terminal), but the error still occurs when it gets to the 3rd parametar. I guess I'll just try some more researching about that response.value.. – tz0 Nov 13 '17 at 15:36
  • I've just noticed, console shows the following message with the error: { message: 'unknown error: document.getElementByClassName is not a function\n (Session info: chrome=61.0.3163.100)\n (Driver info: chromedriver=2.33.506092 (733a02544d189eeb751fe0d7ddca79a0ee28cce4),platform=Linux 4.4.0-98-generic x86_64)' } – tz0 Nov 13 '17 at 15:43
  • I just created a chat, lets talk there: https://chat.stackoverflow.com/rooms/158872/nightwatch-i-need-to-search-for-a-word-in-a-table-without-using-document – Frankusky Nov 13 '17 at 15:45