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).

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?