首页 > 解决方案 > 功能 (@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"

我希望快速检查,我的所有线路都是绿色的,但目前它不起作用。

标签: phpseleniumrhelbehat

解决方案


似乎页面没有加载是的并且元素不在页面中。

尝试:
1. 使用wait方法等待页面加载
->wait(5000, "document.readyState === 'complete'");

  1. 如果需要,请使用 php 中的循环等待元素出现,或者查看是否可以waitdocument.getElementById('$field') != null

根据情况,如果您从另一个页面导航,您可能只需要其中一个或两者都需要,您可以将第二个包含在填充方法中。
等待完成后,您可以填写该字段。

+改变你的$javascript方法
document.getElementById('$field').value='$value';


推荐阅读