lib-innerbrowser icon indicating copy to clipboard operation
lib-innerbrowser copied to clipboard

Do not randomly click the first matching element

Open jeanmonod opened this issue 3 years ago • 4 comments

Here is my case. A simple web page with a menu and a form:

<nav>
     <a href="/dolphins">Save the dolphins</a>
</nav>

...

<form action="/profile/update">
    <input type="text" name="email">
    <input type="submit" value="Save">
</form>

And here is my test case

    $I->amOnPage('/profile');
    $I->fillField('email', '[email protected]');
    $I->click('Save');
    $I->seeAlertMessage('success', 'Email updated');

In this case, I have a test fail. And it's difficult to understand what was the issue.

So in this PR, I suggest stopping clicking the first matching element on the page, but providing to the developer a appropriate error message about the click that was not precise enough.

Wdyt?

jeanmonod avatar Feb 16 '22 06:02 jeanmonod

My example above is quite naive. But when you have two actions on a page, both of which redirect to the same destination, and you don't understand why the action you are testing didn't work. Believe me, it's enough to drive you crazy...

jeanmonod avatar Feb 16 '22 06:02 jeanmonod

This is certainly a breaking change and we would have to raise a major version.

Codeception has always worked this way and there are 27 other places in 17 other methods of InnerBrowser that take the first matching element.

Naktibalda avatar Feb 16 '22 08:02 Naktibalda

@jeanmonod please specify context as the second paramater for this case:

$I->click('Save', 'form');

I agree this behavior could be improved. Not to throw exception but to check for exact matches before clicking "Save dolphins: And yeah this might be BC break

DavertMik avatar Feb 16 '22 11:02 DavertMik

there are 27 other places in 17 other methods

Ok, I was not aware of this 😅

Maybe throwing an exception is a bit too much then. My goal was not to break existing tests suites, but to avoid long debugging session while writing new test cases. I don't know conception enough, but possibly you have a mechanism to transmit warning to the developers? Is it the case?

jeanmonod avatar Feb 17 '22 05:02 jeanmonod