Nightwatch: Checking a Checkbox

Created on 27 Aug 2014  Â·  11Comments  Â·  Source: nightwatchjs/nightwatch

Is click() the right way to check a checkbox. It doesn't seem to be working for me. Please advise.

Most helpful comment

I had to do .execute("$('" + selector + "').click();"); to get this to work.
Ideally .click() or .setValue() would work.

All 11 comments

Would you mind looking at the api-docs ..

I had to do .execute("$('" + selector + "').click();"); to get this to work.
Ideally .click() or .setValue() would work.

If this is a legit bug I wouldn't close it?

As far as I can tell it is a legit bug, but it's unclear what is expected to work.

It's not a bug. it's something that concerns Selenium not something which
can be fixed in Nightwatch.

On Thu, Aug 28, 2014 at 12:29 AM, richard-flosi [email protected]
wrote:

As far as I can tell it is a legit bug, but it's unclear what is expected
to work.

—
Reply to this email directly or view it on GitHub
https://github.com/beatfactor/nightwatch/issues/249#issuecomment-53650137
.

@richard-flosi what about this ?

module.exports = {
  tags: ['checkbox'],

  'Demo test select a checkbox' : function (client) {
    client
      .url('your-domain.tld')
      .verify.visible('input[id="your_checkbox"]', 'Checkbox is visible')
      .click('input[id="your_checkbox"]')
      .pause(1000)
      .element('id', 'your_checkbox', function(response) {
        client.assert.ok(response.value.ELEMENT, 'Checkbox response is OK');
        client.elementIdSelected(response.value.ELEMENT, function(result){
          client.verify.ok(result.value, 'Checkbox is selected');
        });
      })
      .end();
  }
};

The above test runs fine for me.. (with replaced url of course ;)

Select-checkbox Test Suite
==========================

Running:  Demo test select a checkbox 

✔  Testing if element <input[id="your_checkbox"]> is visible.
✔  Assertion passed: Checkbox response is OK
✔  Assertion passed: Checkbox is selected

OK. 3 total assertions passed. (5571 ms)

regards
~david

I was having an issue with a selector like input[name="foo"][value="1"] which selenium doesn't seem to like. I think it is unlikely that there will be an id on every checkbox. When I get a chance, I'll play with the selector again. I think something like input[name="foo"]:nth-child(1) might work, but that is far less elegant.

For comparison, webdriverio addresses the issue by injecting a click event: https://github.com/webdriverio/webdriverio/blob/f3de02f3c29cd0aa933979b26ca80b70609031c9/lib/helpers/click.js

indeed, this is some kind of 'issue' .

but just to make you know.
iirc the selenium docs say,
working with "id"s is actually THE way to go or at least the/their
preferred method of finding elements..

~david

.click() seems to be working properly for me

Had similar problems to this with PhantomJS and changing the selector fixes it. No idea why.

In general I've run into problems if my selector matches more than one item. Make sure you selector only matches one item and things tend to work. When they don't I use execute().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

chaseconey picture chaseconey  Â·  4Comments

antogyn picture antogyn  Â·  4Comments

davidlinse picture davidlinse  Â·  4Comments

bushev picture bushev  Â·  4Comments

lgaticaq picture lgaticaq  Â·  3Comments