问题
I'm writing my tests with behat and I'm facing a problem when I try to call fillField on a input inside a bootstrap modal. When I send fillField in this input selenium throws an exception saying:
Element is not currently visible and so may not be interacted with
I've created a selenium (via selenium IDE) test manually and called type on the same field and it worked fine.
$page = $this->getSession()->getPage()
$page->fillField("id_item", $value);
The $value
is a parameter from my test. I've tried to call a function to wait some seconds, but it didn't worked as well.
UPDATE:
Scenario:
Scenario: Realizar Pedido de Compra
Given I am on "/"
When I fill the form with "{\"id\": 1, \"items\":[{\"id_item\": 1}]}"
Then I should see "Ok"
My FeatureContext:
/**
* @When I fill the form with :arg1
*/
public function iFillForm($json) {
$formHelper = FormHelper::getInstance($this->getSession());
$formHelper->fill($json);
}
In my class FormHelper:
public function fill($json) {
$handler = new SelectorsHandler();
$handler->registerSelector("css", new CssSelector());
$fileJson = json_decode($json, true);
$page = $this->session->getPage();
foreach ($json as $key => $value) {
if (is_array($value)) {
$addSubGrid = $page->find("css", "#btn-plus-" . $key);
if ($addSubGrid != null) {
$subGrid = $page->find("css", "#" . $key);
$formId = $subGrid->getAttribute("data-form");
$ok = $page->find("css", "#$formId .btn-ok");
foreach ($value as $formItem) {
$itemFilled = array();
$addSubGrid->click();
$this->session->wait(
1000, "$('#modal-$formId').is(':visible')"
);
$this->fillForm($page, array("form-item" => $formItem), $itemFilled, false);
$ok->press();
}
}
} else {
$page->fillField($key, $value);
}
}
The $addSubGrid
var is the elment to show the modal. When I execute the test it opens the modal but when it goes into $page->fillField($key, $value)
it does not work.
UPDATE I've found that I was trying to fill a disabled field. I've enabled it and the problem now is that it does not fill the fields inside the modal, just the ones outside.
回答1:
I've managed to solve this using the setValue from the driver manually. It seems there's a bug with fillField from mink. I've used:
$this->session->getDriver()->setValue('//*[@id="'.$key.'"]', $value);
instead of
$page->fillField($key, $value);
回答2:
I have the same issue with "hidden" check/radio boxes designed using "custom icons" instead of "native components" like shown here http://codepen.io/jamesbarnett/pen/yILjk
Solution for my problem was to set native components to "visible" before cliking:
private function checkMyBox($locator)
{
$this->getSession()->executeScript("
var element = document.getElementById('".$locator."');
element.style.opacity = 1;
element.style.filter = 'alpha(opacity=100)'; //iexplorer!
element.style.visibility = 'visible'
");
$this->getSession()->wait(2000);
$this->getSession()->getPage()->findField($locator)->click();
}
In my case it was "opacity" and "visibility" but you should check following css-options (inspect the field in your browser):
visibility != hidden
display != none
opacity != 0
height and width are both > 0
type != hidden (on input fields)
I'm using Selenium2, Behat and Mink and i hope this code snippet will help someone fixing similar problems.
来源:https://stackoverflow.com/questions/26239561/behat-mink-selenium2-element-is-not-visible