Created
August 18, 2016 07:35
-
-
Save MaksymKrut/2e751dde1659a972fecc2d4329ac32e0 to your computer and use it in GitHub Desktop.
@mink @Behat Name all existing fields on page with JS. Workaround against "id|name|label|value missing" error during getPage()->fillField($field, $value).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Differ from Mink as does not have fixStepArgument check | |
* Fills in form field with specified id|name|label|value | |
* Example: When I fill in "username" with: "bwayne" | |
* Example: And I fill in "bwayne" for "username" | |
* @When /^(?:|I )fill in field "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)"$/ | |
* @param $field | |
* @param $value | |
* @throws \Behat\Mink\Exception\ElementNotFoundException | |
*/ | |
public function fillFieldInWebDriver($field, $value) | |
{ | |
$idExists = $this->getSession()->getPage()->findField($field); | |
if ($idExists !== null) { | |
echo "\n\nField with id|name|label|value - $field - have found!\n\n"; | |
$this->getSession()->getPage()->fillField($field, $value); | |
} else { | |
$locator = $this->takeValueFromIniFile('locators', $field); | |
echo "\n\nField with locator - $locator - to be located\n\n"; | |
echo "\n\nField has no name. Let's name it!\n\n"; | |
$javascript = "(function(){ | |
var fields = document.getElementsByTagName('input'); | |
var textareas = document.getElementsByTagName('textarea'); | |
for (var i = 0; i < fields.length; i++) { | |
fields[i].name = 'input_number_' + (i + 1); | |
} | |
for (var i = 0; i < textareas.length; i++) { | |
textareas[i].name = 'textarea_number_' + (i + 1); | |
} | |
})() | |
"; | |
$this->getSession()->executeScript($javascript); | |
$fieldToHaveId = $this->getSession()->getPage()->find("css", $locator); | |
$fieldName = $fieldToHaveId->getAttribute("name"); | |
echo "\n\nField has new name: " . $fieldName . "\n\n"; | |
$this->getSession()->getPage()->fillField($fieldName, $value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment