maxgalbu / nightwatch-custom-commands-assertions

Nightwatch.js custom commands and assertions
MIT License
136 stars 32 forks source link

jqueryClick not clicking element #2

Closed joshwebdev closed 9 years ago

joshwebdev commented 9 years ago

In my test, jqueryClick() isn't actually performing a click. It throws no errors, but it's not navigating to the page linked to by the tag that's supposed to be clicked.

In my chain of browser...() calls, I have these three calls:

    .assert.jqueryElementPresent("a:contains('Add to Cart')")
    .jqueryClick("a:contains('Add to Cart'):last")
    .waitForElementVisible('body', 1000)

The assert passes, and no errors/failures are reported by Nightwatch, but no actual "click" is taking place. I watch the browser open and perform the calls, but the link is never actually getting clicked (so, in this example, the browser stays on the last page without ever going to the page "Add to Cart" links to).

Am I doing something wrong, or is this a bug?

joshwebdev commented 9 years ago

I was able to get it to work by editing the jqueryClick.js file.

Line 18, changed from:

    $(selector).click();

to

    $(selector)[0].click();
maxgalbu commented 9 years ago

I've written a test and it's working ATM:

browser
    .url(baseurl+"/jqueryClick")
    .jqueryClick("div:eq(2) button")
    .assert.visible("#div")
    .end();

and this is the HTML:

<html>
<head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#button").on("click", function() {
                $("#div").show();
            });
        });
    </script>
</head>
<body>
    <div class="myclass"></div>
    <div class="myclass"></div>
    <div class="myclass">
        <button type="button" id="button">click me</button>
        <div id="div" style="display:none">invisible</div>
    </div>
</body>
</html>

I didn't test an actual anchor with href, I'll add a test to check

maxgalbu commented 9 years ago

This is a limitation in $.fn.click(), jquery simply triggers a click event, but doesn't simulate a click event. I'll edit the command to use your code