你嘗試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));
}
}
你好!你的解決方案會很好,但正如你所看到的,我的元素是在一張桌子裏面,沒有ID ... – nitche 2015-02-24 16:09:33
@nitche - 請參閱'edit'部分。 – BentCoder 2015-02-24 16:32:13