Behat / Mink - 无法通过xout和goutteDriver找到元素

时间:2015-02-24 12:55:21

标签: selenium-webdriver behat mink zombie.js goutte

我正在使用Behat 3.0和Mink 1.6。

这些代码适用于Selenium2和Zombie,但不适用于Goutte:

    $this->assertSession()->elementTextContains('xpath', "//div[@id='pagecontent-shop']/form/table/tbody/tr[12]/td[2]", $arg1);

    $page = $this->getSession()->getPage();
    $element = $page->find('xpath', "//div[@id='pagecontent-shop']/form/table/tbody/tr[12]/td[2]",)->getText();       

有谁知道发生了什么事?

1 个答案:

答案 0 :(得分:0)

你试过findById()吗?

e.g。

/**
 * @When /^I click an element with ID "([^"]*)"$/
 *
 * @param $id
 * @throws \Exception
 */
public function iClickAnElementWithId($id)
{
    $element = $this->getSession()->getPage()->findById($id);
    if (null === $element) {
        throw new \Exception(sprintf('Could not evaluate element with ID: "%s"', $id));
    }
    $element->click();
}

e.g。

/**
 * Click on the element with the provided css id
 *
 * @Then /^I click on the element with id "([^"]*)"$/
 *
 * @param $elementId ID attribute of an element
 * @throws \InvalidArgumentException
 */
public function clickElementWithGivenId($elementId)
{
    $session = $this->getSession();
    $page = $session->getPage();

    $element = $page->find('css', '#' . $elementId);

    if (null === $element) {
        throw new \InvalidArgumentException(sprintf('Could not evaluate CSS: "%s"', $elementId));
    }

    $element->click();
}

编辑: 下面的示例遍历表中的input个元素,并查找具有特定name的元素。你需要稍微修改一下。

/**
 * @Given /^the table contains "([^"]*)"$/
 */
public function theTableContains($arg1)
{
    $session = $this->getSession();
    $element = $session->getPage()->findAll('css', 'input');

    if (null === $element) {
        throw new \Exception(sprintf('Could not evaluate find select table'));
    }

    $options = explode(',', $arg1);
    $match = "element_name";

    $found = 0;
    foreach ($element as $inputs) {
        if ($inputs->hasAttribute('name')) {
            if (preg_match('/^'.$match.'(.*)/', $inputs->getAttribute('name')) !== false) {
                if (in_array($inputs->getValue(), $options)) {
                    $found++;
                }
            }
        }
    }

    if (intval($found) != intval(count($options))) {
        throw new \Exception(sprintf('I only found %i element in the table', $found));
    }
}