Codeception can't click on element it has seen just before

Viewed 2266

Hello dear people of Stackoverflow,

currently I am writing acceptance tests with Codeception using Selenium as WebDriver module. In my test to check if our sub-navigation exists, is complete and works I struggle with the following piece of code:

$I->see('Partners');
$I->click('Partners');

all the see calls are working perfectly, but the click call fails with an ElementNotVisibleException stating that the element to be clicked is not visible, which I don't understand since the see call worked.

To make sure we're not seeing any old "Partners" string on the page but it really is the link I want to click, I changed the call by adding the a selector.

$I->see('Partners', 'a');
$I->click('Partners');

Still, I'm getting the ElementNotVisibleException from before. In the next step, I added a context to the click call to this:

$I->click('Partners', 'a');

Which made the exception disappear. Still, the click never happens, the test simply fails with the message:

Link or Button or CSS or XPath element with 'Partner' was not found.

I even used XPath in the click call like this: //a[text()='Partners'] and get the same message.

What bothers me the most with this is that the see call seems to see the link I am trying to click and moreover, even the page source and screenshot provided by Codeception after a fail contain this very link, visible and in valid HTML.

I tried to click on some other elements within the sub-navigation with the same result, while - which is even more strange - clicks on links in the main navigation seem to work just fine.

I have no idea why this doesn't work but any help is greatly appreciated.

1 Answers

I don't understand exactly why it happens in some cases and not others, but I've found a solution that works for me: Use CSS selectors only.

Basically in some cases (I think it has to do with clicking on div/span rather than a/button) ->see() will work with the fuzzy text string as first argument, but ->click() requires the explicit CSS selector.

So this will have the "I can see but not click" problems:

// BROKEN
$I->see("All topics", ".header-taxonomy .taxonomy-list-title");
$I->click("All topics", ".header-taxonomy .taxonomy-list-title");

But this will work:

// WORKS
$I->see("All topics", ".header-taxonomy .taxonomy-list-title");
$I->click(".header-taxonomy .taxonomy-list-title");

Obviously it's not ideal at all, partly because it requires a unique selector, but for me mostly just because it makes the tests and testing output less consistent and harder to read.

Either way, I just wanted to get this testing script finished, and now it's working, and because I'm still using ->see() with the fuzzy text locator, I'm still confident that the text is showing before I click.

Notably, this issue doesn't happen when using the PHPBrowser module, which is what tripped me up. I "upgraded" to WebDriver and found my scripts were broken in all these places due to the different behavior of ->click() (in addition to the expected need to open menus before using them, which wasn't needed in PHPBrowser)

Related