Drupal 的 Behat 自动完成测试问题
Issues with Drupal's autocomplete test with Behat
我正在尝试测试由 Drupal 生成的表单,其中有一些自动完成功能可以帮助人们做出正确的选择。
在此表单中,有 3 个字段让用户 select 3 篇文章 ('Actualités'),一旦您开始输入内容,auto-completes 会生成一个文章列表有相似的标题。
这是一个例子:
问题是我在做一些测试时意识到使用 $this->getSession()->getPage()->fillField()
会使 Behat 松散地关注该字段,就像它在完成填充字段后按 Tab .
所以我尝试了很多技巧来获得这个领域的关注,如下所示:
$field = $this->getSession()->getPage()->findField('field_mb_actualites[0][target_id]');
if($field) {
// Solution #1
// the dropdown menu is inside a `ul#ui-id-1` element
$this->getSession()->executeScript("jQuery('#ui-id-1').show();");
// Solution #2
$field->focus();
// Solution #3
// Consists of pressing "shift + tab" for a "reverse tab" and trying to get back to the field
$this->getSession()->getDriver()->keyDown($xpath, 16);
$this->getSession()->getDriver()->keyDown($xpath, 9);
$this->getSession()->getDriver()->keyUp($xpath, 9);
$this->getSession()->getDriver()->keyUp($xpath, 16);
// And I take a screenshot at the end to see if anything worked
file_put_contents('screenshot-focus.png', $this->getSession()->getDriver()->getScreenshot());
}
但我总是得到相同的结果,如下所示:
获得焦点后我需要做的就是按 "right" 方向箭头使下拉菜单再次可见,然后按 "down" 方向箭头到 select 一个建议, 然后 "Enter" 确认选择。
然而,我开始 运行 没有想法,尽管我在 Behat github 上发现了几个关于这个问题的问题,但我无法设法让答案有效.如何触发 auto-complete?
提前致谢
您可以使用 JavaScript 函数来触发事件,如下所示:
public function triggerEventOn($css_selector){
$function = <<<JS
(function(){
var element = document.querySelector("$css_selector");
var event = document.createEvent('Event');
event.initEvent('change', true, true);
element.dispatchEvent(event);
})()
JS;
try{
$this->getSession()->getDriver()->executeScript($function);
}catch (Exception $e){
}
}
如果你愿意,你也可以调整它来填充字段并在之后触发事件。
要检查您需要触发的事件,您需要检查该元素并在检查器中检查 ev
。
我正在尝试测试由 Drupal 生成的表单,其中有一些自动完成功能可以帮助人们做出正确的选择。
在此表单中,有 3 个字段让用户 select 3 篇文章 ('Actualités'),一旦您开始输入内容,auto-completes 会生成一个文章列表有相似的标题。
这是一个例子:
问题是我在做一些测试时意识到使用 $this->getSession()->getPage()->fillField()
会使 Behat 松散地关注该字段,就像它在完成填充字段后按 Tab .
所以我尝试了很多技巧来获得这个领域的关注,如下所示:
$field = $this->getSession()->getPage()->findField('field_mb_actualites[0][target_id]');
if($field) {
// Solution #1
// the dropdown menu is inside a `ul#ui-id-1` element
$this->getSession()->executeScript("jQuery('#ui-id-1').show();");
// Solution #2
$field->focus();
// Solution #3
// Consists of pressing "shift + tab" for a "reverse tab" and trying to get back to the field
$this->getSession()->getDriver()->keyDown($xpath, 16);
$this->getSession()->getDriver()->keyDown($xpath, 9);
$this->getSession()->getDriver()->keyUp($xpath, 9);
$this->getSession()->getDriver()->keyUp($xpath, 16);
// And I take a screenshot at the end to see if anything worked
file_put_contents('screenshot-focus.png', $this->getSession()->getDriver()->getScreenshot());
}
但我总是得到相同的结果,如下所示:
获得焦点后我需要做的就是按 "right" 方向箭头使下拉菜单再次可见,然后按 "down" 方向箭头到 select 一个建议, 然后 "Enter" 确认选择。
然而,我开始 运行 没有想法,尽管我在 Behat github 上发现了几个关于这个问题的问题,但我无法设法让答案有效.如何触发 auto-complete?
提前致谢
您可以使用 JavaScript 函数来触发事件,如下所示:
public function triggerEventOn($css_selector){
$function = <<<JS
(function(){
var element = document.querySelector("$css_selector");
var event = document.createEvent('Event');
event.initEvent('change', true, true);
element.dispatchEvent(event);
})()
JS;
try{
$this->getSession()->getDriver()->executeScript($function);
}catch (Exception $e){
}
}
如果你愿意,你也可以调整它来填充字段并在之后触发事件。
要检查您需要触发的事件,您需要检查该元素并在检查器中检查 ev
。