Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing NodeElement to/from JS scripts #352

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

mvorisek
Copy link
Contributor

@mvorisek mvorisek commented Apr 15, 2022

merge after #353

no BC break (\Behat\Mink\Driver\DriverInterface interface cannot be updated in behat/mink v1.x, see related discussion minkphp/Mink#826, but this can be easily overcome by using @var phpdoc in the code, see example below)

serializing elements is natively supported by W3C WebDriver [1] and already fully supported by instaclick/php-webdriver [2] since v1.4.13

[1] https://w3c.github.io/webdriver/#executing-script
[2] instaclick/php-webdriver#110

example usage:

// expecting:
// a) $minkSession with working \WebDriver\Session driver
// b) loaded webpage with at least one <div> element with some ID attribute

$dumpElementFx = function ($v) {
    return $v instanceof \Behat\Mink\Element\NodeElement
        ? 'NodeElement: ' . $v->getXpath()
        : $v;
};

/** @var \Behat\Mink\Driver\Selenium2Driver */
$driver = $minkSession->getDriver();

$element = $driver->find('//div')[0]; // returns instance of \Behat\Mink\Element\NodeElement
var_dump($dumpElementFx($element));

$jsResult = $driver->evaluateScript(
    'return (function (elem) { return [elem, elem.id]; })(arguments[0])',
    [$element],
);

// will dump array with:
// [0] (new) instance of \Behat\Mink\Element\NodeElement to demonstrate conversion from php to webdriver and back
// [1] ID of given element extracted by JS
var_dump(array_map(fn ($v) => $dumpElementFx($v), $jsResult));

$jsResult = $driver->evaluateScript(
    'return (function (elem) { return [elem, elem.id]; })(arguments[0])',
    [$jsResult[0]],
);

// dump must be the same, but this time, element passed to a Selenium2Driver::evaluateScript had no xpath
var_dump(array_map(fn ($v) => $dumpElementFx($v), $jsResult));

outputs:

string(23) "NodeElement: (//div)[1]"
array(2) {
  [0]=>
  string(252) "NodeElement: webdriver-element--JZPOYwW6bl1bVmyK--aHR0cDovLzEyNy4wLjAuMTo0NDQ0L3dkL2h1Yi9zZXNzaW9uL2U0YjFlYWYyYTdmNTkzNjhlMWVkNTcxN2Y3YzlkNGZkL2VsZW1lbnQvYWU3YWI4ZTUtZGFlNS00NjA1LWFlZmUtYTgwZDg2YjY2ZmEy--YWU3YWI4ZTUtZGFlNS00NjA1LWFlZmUtYTgwZDg2YjY2ZmEy"
  [1]=>
  string(10) "atk_layout"
}
array(2) {
  [0]=>
  string(252) "NodeElement: webdriver-element--JZPOYwW6bl1bVmyK--aHR0cDovLzEyNy4wLjAuMTo0NDQ0L3dkL2h1Yi9zZXNzaW9uL2U0YjFlYWYyYTdmNTkzNjhlMWVkNTcxN2Y3YzlkNGZkL2VsZW1lbnQvYWU3YWI4ZTUtZGFlNS00NjA1LWFlZmUtYTgwZDg2YjY2ZmEy--YWU3YWI4ZTUtZGFlNS00NjA1LWFlZmUtYTgwZDg2YjY2ZmEy"
  [1]=>
  string(10) "atk_layout"
}

@mvorisek mvorisek changed the title Convert element to NodeElement from execute result Allow passing NodeElement to/from scripts Apr 15, 2022
src/Selenium2Driver.php Outdated Show resolved Hide resolved
src/Selenium2Driver.php Outdated Show resolved Hide resolved
@mvorisek mvorisek marked this pull request as draft April 15, 2022 14:19
@codecov
Copy link

codecov bot commented Apr 17, 2022

Codecov Report

Merging #352 (6c3cd06) into master (5d15043) will decrease coverage by 2.77%.
The diff coverage is 65.30%.

@@             Coverage Diff              @@
##             master     #352      +/-   ##
============================================
- Coverage     90.02%   87.25%   -2.78%     
- Complexity      150      161      +11     
============================================
  Files             1        1              
  Lines           421      455      +34     
============================================
+ Hits            379      397      +18     
- Misses           42       58      +16     
Impacted Files Coverage Δ
src/Selenium2Driver.php 87.25% <65.30%> (-2.78%) ⬇️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@mvorisek
Copy link
Contributor Author

@stof I need to add a test, but can you please review this PR briefly?

@mvorisek mvorisek changed the title Allow passing NodeElement to/from scripts Allow passing NodeElement to/from JS scripts Apr 20, 2022
@mvorisek mvorisek force-pushed the wdelem_from_selenium branch 2 times, most recently from 61ff9ec to 4d0d8f8 Compare April 20, 2022 12:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants