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

使用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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试选择一个包含文本框的iFrame。 以下是页面的结构:

 &lt; html&gt; 
&lt; body&gt; 
&lt;许多文本字段&gt; 
&lt; iframe&gt; 
#document 
&lt;  html&gt; 
&lt; body&gt; 
&lt; p&gt; 
WRITE TEXT HERE; 
&lt; / p&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
&lt; / iframe&gt; 
&lt;更多标签做其他事情&gt  ; 
&lt; / body&gt; 
&lt; / html&gt; 
   
 
 

这是我在

标签中访问和撰写文字的功能 在标签内:

  //选择iFrame 
 / ** 
 * 
 * 
 * @Given / ^我切换到iframe“([^”]  *)“$ / 
 * / 
公共函数iSwitchToIframe($ field1)
 {
 $ field1 = $ this-&gt; fixStepArgument($ field1); 
 $ page = $ this-&gt; getSession() -  &gt; getPage(); 
 $ el = $ page-&gt; find('css',$ field1); 
 $ this-&gt; fillField($ el,'This is field value'); 
} \  n   
 
 

我不确定我在这里做错了什么。我一直得到'类NodeElement的对象无法转换为字符串'错误。 \ n

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题