问题
I'm trying to perform tests on my Magento installation using Mink with Goutte driver, and it fails when I'm trying to use click()
or press()
functions on a button.
Code:
$page = $this->getSession()->getPage();
$checkout = $page->find('css', '.btn-proceed-checkout');
$checkout->click();
How to get this work? Why does Mink actually need a form to click a button element? Or is that the problem connected with Goutte not being able to fetch JS?
回答1:
TL;DR: use different driver or make sure form elements are actually inside the form.
Goutte driver is pretty awesome, but not as awesome as the real browser. It gets the response content and uses Goutte scrapper to analyse it. To add some spice your testing it allows you to enter and submit form data by intercepting value setting calls and storing form data in the object. When you click on a button (I'd assume only on type="submit"
) that form data is added into request data and sent to the server.
In order to create that form object it searches for the closest parent form to get the basic form details. So, when you try to set a value on the input that is not part of the form, or do a submit without a form – it can't find the form and throws an exception.
Ensuring that form elements sit inside the form tag should resolve such issues. Alternatively you can use Selenium2 driver – it uses a completely different mechanism.
回答2:
The problem is in the DOM. I managed to debug by placing the <button>
closer to the <form>
in the DOM tree.
In my case, the problem was some <script>
blocks (angular templates) inside the <form>
.
来源:https://stackoverflow.com/questions/25401236/the-selected-node-does-not-have-a-form-ancestor-mink-error