php - 功能 (@javascript) 测试效果不佳
问题描述
当我在浏览器的输入字段中键入关键字以查看结果时,我正在设置一个简单的检查。并且当在 javascript 部分开始检查时,检查需要很长时间才能完成并出现错误。
我正在使用:Linux RedHat 7 Behat 3.5.0 Selenium Standalone Server 3.141.59 Mink 1.6
# behat.yml
default:
extensions:
Behat\MinkExtension:
browser_name: chrome
goutte: ~
javascript_session: selenium2
selenium2:
wd_host: http://99.80.48.204:4444/wd/hub
capabilities: { "browser": "chrome", "version": "*", 'chrome': {'switches':['--start-maximized']}}
base_url: https://www.bing.com
suites:
ui:
contexts: [FeatureContext, WebContext]
#Webcontext.php
<?php
use Behat\MinkExtension\Context\MinkContext;
class WebContext extends MinkContext {
/**
*@When I wait for :arg1 seconds
*/
public function iWaitForSeconds($args)
{
$this->getSession()->wait($args * 1000);
}
/**
* @When I fill in :arg1 with: :arg2
*/
public function iFillInWith($value, $field)
{
$javascript = "window.onload = function () {var e = document.getElementById('$field').value='$value';}";
$this->getSession()->executeScript($javascript);
}
}
# bing.feature
@insulated
Feature: Bing
Scenario: Homepage
Given I am on the homepage
Then I should see "Bing"
And I should see "Images"
And I should see "Office Online"
@javascript
Scenario: Search
Given I am on the homepage
When I fill in "sb_form_q" with "grafikart"
And I wait for 1 seconds
Then I should see "Grafikart.fr"
我希望快速检查,我的所有线路都是绿色的,但目前它不起作用。
解决方案
似乎页面没有加载是的并且元素不在页面中。
尝试:
1. 使用wait方法等待页面加载
->wait(5000, "document.readyState === 'complete'");
- 如果需要,请使用 php 中的循环等待元素出现,或者查看是否可以
wait
将document.getElementById('$field') != null
根据情况,如果您从另一个页面导航,您可能只需要其中一个或两者都需要,您可以将第二个包含在填充方法中。
等待完成后,您可以填写该字段。
+改变你的$javascript
方法
document.getElementById('$field').value='$value';
推荐阅读
- spring-boot - [请求处理失败;嵌套异常是 java.lang.IllegalStateException: 没有可用的实例
- javascript - 编码新手,我不知道为什么我的 javascript 按钮无法正常工作?
- c++ - 编译时显示未定义的引用
- xamarin - AdMob 横幅不会显示,测试广告没有错误消息并且无法加载:3 与真实广告
- for-loop - Google appcipt - 使用 for 循环导出多个 PDF,使用工作表作为模板,意外结果
- xml - XSLT document() 无法与 javascript xsltProcessor 一起使用
- android - 在真机上颤振构建apk文件的问题
- javascript - 如何在画布元素内的另一个图像上方居中图像
- html - 为什么“标签”属性不适用于“选项”元素?
- opencv - cmake c++ 库与 opencv addexternal