0

i am using Behat 3.0 and Mink 1.6.

Those codes work with Selenium2 and Zombie, but not with 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();       

Does anyone knows what is happening?

nitche
  • 121
  • 3
  • 13

1 Answers1

0

Did you try 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();
}

EDIT: Example below goes thru input elements in a table and finds the ones with specific name. You need to modify it a bit though.

/**
 * @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));
    }
}
BentCoder
  • 12,257
  • 22
  • 93
  • 165
  • hello! your solution would be great, but as you can see, my element is inside a table and has no id... – nitche Feb 24 '15 at 16:09