Codeception: Get and click Element via executeJS and jQuery

Created on 15 Jul 2014  路  7Comments  路  Source: Codeception/Codeception

Why is the following working:

$I->executeJS('return $(".icon-rowExpand:visible").get(0).click()');

But not:

$I->click($I->executeJS('return $(".icon-rowExpand:visible").get(0)'));
$I->click($element);

--debug -vvv log is

There was 1 error:

---------
1) Failed to perform actions and see result in SnippetsCept ({path}tests\acceptance\SnippetsCept.php)
Couldn't click {"ELEMENT":"1"}:
ErrorException: Array to string conversion

Scenario Steps:
4. I click {"ELEMENT":"1"}
3. I execute js "return $(".icon-rowExpand:visible").get(0)"
2. I see "back"
1. I am on page "/my_snippets.php"


  [ErrorException] Array to string conversion

#1  {path}tests\acceptance\AcceptanceTester.php:421
#2  {path}tests\acceptance\SnippetsCept.php:17
#3  {path}codecept.phar:7

--

Most helpful comment

@DavertMik Do you know where we could ask to clarify how to convert "HTMLElement to CSS/Xpath locator"?

I followed your advice and implemented a function in my AccentanceHelper class )which extends the WebDriver Module)

public function clickJQuerySelectedElement($element) {
    $this->executeJS('return $("' . $element . '").get(0).click()');
}

and call it in my cept like

$I->clickJQuerySelectedElement('.icon-rowExpand:visible');

That is working great, thanks again!

All 7 comments

Because I'm not sure how should HTMLElement from JavaScript should be converted into CSS/XPath locator for WebDriver.

What you actually can do is to write your own (pretty basic) module where you would replace some methods of WebDriver with simulated methods from JQuery. I didn't write such module by myself as it was not obvious when to use it, as well, as I might could confuse users with 2 similar modules. But for your case this can be an option, you can execute javascripts for clicking, filling fields, etc

@DavertMik Do you know where we could ask to clarify how to convert "HTMLElement to CSS/Xpath locator"?

I followed your advice and implemented a function in my AccentanceHelper class )which extends the WebDriver Module)

public function clickJQuerySelectedElement($element) {
    $this->executeJS('return $("' . $element . '").get(0).click()');
}

and call it in my cept like

$I->clickJQuerySelectedElement('.icon-rowExpand:visible');

That is working great, thanks again!

Here you can find jQuery plugin to get selector for an arbitrary element, then use it like

$selector = $I->executeJS('return $(".icon-rowExpand:visible:first").getSelector()');
$I->click($selector);

@splinter89 Nice solution, thanks for that.

Is there any recommended way to load/inject the jQuery plugin within "codeception run" rather then include it in the app?

I guess it's just $I->executeJs(file_get_contents($plugin_location));. Give it a try.

$(...).getSelector is not a function

@alxvallejo have you loaded that plugin?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sasha-x picture sasha-x  路  3Comments

nikolaykolev picture nikolaykolev  路  3Comments

jterry picture jterry  路  3Comments

Breadleyg picture Breadleyg  路  3Comments

sebastianneubert picture sebastianneubert  路  3Comments