dpqvwgr74759 2015-07-16 18:00
浏览 54
已采纳

使用Mink扩展和selenium2自动化测试用例

I am trying to select an iFrame which has a text box within it. Here is the structure of the page:

<html>
<body>
<many text fields>
<iframe>
#document
<html>
<body>
<p>
WRITE TEXT HERE;
</p>
</body>
</html>
</iframe>
<more tags to do other things>
</body>
</html>

And here is my function to access and write text in the

tags within the tags:

// Select iFrame
/**
 * 
 *
 * @Given /^I switch to iframe "([^"]*)"$/
 */
public function iSwitchToIframe($field1)
{
    $field1 = $this->fixStepArgument($field1);
    $page = $this->getSession()->getPage();
    $el = $page->find('css', $field1);
    $this->fillField($el, 'This is field value');
}

I am not sure what am I doing wrong here. I keep getting 'Object of class NodeElement could not be converted to a string' error.

  • 写回答

1条回答 默认 最新

  • dongzongzi0379 2015-07-17 16:22
    关注

    fillField() accepts two arguments:

    • locator - input id, name or label (string)
    • value (string)

    The find() method returns a NodeElement, so you cannot pass it to fillField().

    You could call setValue() on the field:

    $field1 = $this->fixStepArgument($field1);
    $page = $this->getSession()->getPage();
    $formElement = $page->find('css', $field1);
    if (null === $formElement) {
        throw new \LogicException(sprintf('Form field not found: "%s"', $field1));
    }
    $formElement->setValue('a value');
    

    However, it looks like you're passing a css selector from your scenario. It would be better if you passed a meaningful name, and converted it to a css selector or an id in the context file:

    // you'll need to covert a name that came from the scenario to a selector
    // you could also use a label
    $selector = $this->convertFieldNameToIdOrNameOrLabel($field1);
    $this->fillField($selector, 'This is field value');
    

    By putting css selectors into your scenarios you're polluting them with technical details. If you prefer to follow this practice, you'd be better off using mink directly with no Behat (since you're not really doing acceptance testing, but simply functional testing).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog