admin管理员组

文章数量:1353615

I'm having some trouble getting my nightwatch tests to fetch data from a webpage and print it to my console. If anyone could point me in the right direction I would be very thankful.

Every time i try to get the data to my console log it only returns [object Object]. I am fairly new to test automation and haven't been able to make sense out of this from googling. The code is as follows (test site is google frontpage:

module.exports = {
  tags: ['google'],
  'Demo test Google' : function (client) {
client
  .url('')
  .pause(500);
  var msg = "---> : ";

client.expect.element('body').to.be.present;

client.getText("#gbw > div > div > div.gb_7d.gb_R.gb_le.gb_ee > div:nth-child(1) > a", function(result) {
client.expect.element("#gbw > div > div > div.gb_7d.gb_R.gb_le.gb_ee > div:nth-child(1) > a").text.to.equal("Gmail");
console.log(msg.toString()+result);
});

client.getValue("#tsf > div.tsf-p > div.jsb > center > input[type='submit']:nth-child(1)", function(result) {
client.expect.element("#tsf > div.tsf-p > div.jsb > center > input[type='submit']:nth-child(1)").to.have.value.that.equals("Sök på Google");
console.log(msg+result);
});

client.end();
}
};

I'm having some trouble getting my nightwatch tests to fetch data from a webpage and print it to my console. If anyone could point me in the right direction I would be very thankful.

Every time i try to get the data to my console log it only returns [object Object]. I am fairly new to test automation and haven't been able to make sense out of this from googling. The code is as follows (test site is google frontpage:

module.exports = {
  tags: ['google'],
  'Demo test Google' : function (client) {
client
  .url('http://google.')
  .pause(500);
  var msg = "---> : ";

client.expect.element('body').to.be.present;

client.getText("#gbw > div > div > div.gb_7d.gb_R.gb_le.gb_ee > div:nth-child(1) > a", function(result) {
client.expect.element("#gbw > div > div > div.gb_7d.gb_R.gb_le.gb_ee > div:nth-child(1) > a").text.to.equal("Gmail");
console.log(msg.toString()+result);
});

client.getValue("#tsf > div.tsf-p > div.jsb > center > input[type='submit']:nth-child(1)", function(result) {
client.expect.element("#tsf > div.tsf-p > div.jsb > center > input[type='submit']:nth-child(1)").to.have.value.that.equals("Sök på Google");
console.log(msg+result);
});

client.end();
}
};
Share Improve this question asked Nov 12, 2015 at 10:32 Erik StenhammarErik Stenhammar 211 silver badge2 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 5

I have encountered this issue just recently and the way how I solved this was by using 'textContent' to capture the string between the elements.

On this example:

<div data-v-407a767e="" class="alert alert-success alert-styled-left alert-bordered text-center">
"Success! Check your email for the password reset link. "
<br data...>
<a data-v-407a767e="" class="alert-link">Go back to login.</a>
</div>

I retrieve the string by using:

client.getAttribute('selector', 'textContent', function(result){
        console.log("Value: ", result.value);
    });

You can check out this link on using the textContent attribute.

I spent hours today scratching my head over the same thing. It appears that the documentation for the getText mand is misleading, if not incorrect. I say this because it says the return is a string, but I'm seeing objects.

nightwatchjs example code

The only way I've been able to get the string value of the element is by using the (supposedly optional) callback parameter.

Without the callback parameter, the returned value appears to be the browser object (i.e. this, for making chained Command calls). If you output this value, (and it's a lot of information), you will see references to the browser Commands documented.

This contradiction in return value and the documentation appears to repeat itself for many of the mands, such as (in my case today) getText.

If, later, you learn more that explains the apparently varying return types (depending on whether or not a callback parameter is used), please do share back here. I'm very curious, myself.

The documentation is pretty unclear about this. The return value is an object. You can view it with

self.getText('selectorvalue', function (result) {
   console.log('result: ' + JSON.stringify(result));
});

The result is going to have many properties, most importantly a status and value property.

If the result.status is -1 the element was not found. Otherwise result.value should have the text.

self.getText('selectorvalue', function (result) {
    if (result.status !== -1) {
        console.log('text: ' + result.text)
    }
});

If you wanted to only get the text of elements that exist, and avoid failing tests where the element did not exist, then you could do something like this:

this.element('css selector', 'selectorvalue', function (present) {
     if (present.status !== -1) {
         self.getText('selectorvalue', function (result) {
             if (result.status !== -1) {
                 console.log('text: ' + result.text);
             }
         });
     }else{
         //don't try to get the text
     }
});

'this.element' returns a similar object to ''getText' with a status of -1 for nonexistant selector elements.

Element does not fail the test when the element is not found.

By the way, in mands you may want to set var self = this and use self in place of this. In the test you may be using 'client' or 'browser' instead of 'this'.

Generally you are expecting the element to be there so the test should fail. If not, this is a workaround.

A simple solution will be to retrieve from the object the necessary value. The returned object has properties and you simply have to reach to the correct property.

In your example you could use something like this:

client.getText("your_element_locator", function(result) {
  expect(result.value).to.equal("Gmail");
  console.log(msg.toString()+result.value);
});

本文标签: