0

我正在使用 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();       

有谁知道发生了什么?

4

1 回答 1

0

你试过了findById()吗?

例如

/**
 * @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();
}

例如

/**
 * 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));
    }
}
于 2015-02-24T16:03:25.750 回答