Skip to content

Commit

Permalink
implemented click by name in webdriver
Browse files Browse the repository at this point in the history
  • Loading branch information
DavertMik committed Apr 4, 2014
1 parent 06044bf commit 42ef901
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
16 changes: 13 additions & 3 deletions src/Codeception/Module/WebDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,19 @@ protected function findClickable($page, $link)
if (count($els)) return reset($els);

// wide

$xpath = Locator::combine(
".//a[./@href][((contains(normalize-space(string(.)), $locator)) or .//img[contains(./@alt, $locator)])]",
".//input[./@type = 'submit' or ./@type = 'image' or ./@type = 'button'][contains(./@value, $locator)]",
".//input[./@type = 'image'][contains(./@alt, $locator)]",
".//button[contains(normalize-space(string(.)), $locator)]"
".//button[contains(normalize-space(string(.)), $locator)]",
".//input[./@type = 'submit' or ./@type = 'image' or ./@type = 'button'][./@name = $locator]",
".//button[./@name = $locator]"
);

$els = $page->findElements(\WebDriverBy::xpath($xpath));
if (count($els)) return reset($els);

$els = $page->findElements(\WebDriverBy::xpath($xpath));
if (count($els)) return reset($els);
return null;
Expand All @@ -326,12 +332,16 @@ protected function findField($selector)
if ($selector instanceof \WebDriverElement) return $selector;
$locator = Crawler::xpathLiteral(trim($selector));

// by text or label
$xpath = Locator::combine(
".//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')][(((./@name = $locator) or ./@id = //label[contains(normalize-space(string(.)), $locator)]/@for) or ./@placeholder = $locator)]",
".//label[contains(normalize-space(string(.)), $locator)]//.//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]",
".//*[self::input | self::textarea | self::select][@name = $locator]"
".//label[contains(normalize-space(string(.)), $locator)]//.//*[self::input | self::textarea | self::select][not(./@type = 'submit' or ./@type = 'image' or ./@type = 'hidden')]"
);
$els = $this->webDriver->findElements(\WebDriverBy::xpath($xpath));
if (count($els)) return reset($els);

// by name
$xpath = ".//*[self::input | self::textarea | self::select][@name = $locator]";
$els = $this->webDriver->findElements(\WebDriverBy::xpath($xpath));
if (count($els)) return reset($els);

Expand Down
1 change: 1 addition & 0 deletions src/Codeception/Util/Framework.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ protected function getFieldByLabelOrCss($field)
if ($label->attr('for')) $input = $this->crawler->filter('#' . $label->attr('for'));
}

if (!isset($input)) $input = $this->match(sprintf('.//*[self::input | self::textarea | self::select][@name = "%s"]', $field));
if (!isset($input)) $input = $this->match($field);
if (!count($input)) throw new ElementNotFound($field, 'Form field by Label or CSS');
return $input->first();
Expand Down
2 changes: 1 addition & 1 deletion tests/data/app/view/form/button.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<body>
<form action="/form/button" method="POST">
<input type="hidden" name="text" value="val" />
<button type="submit">Submit</button>
<button type="submit" name="btn0">Submit</button>
</form>
</body>
</html>
20 changes: 20 additions & 0 deletions tests/unit/Codeception/Module/FrameworksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ public function testClickOnContext()
$this->module->seeInCurrentUrl('/info');
}

public function testClickByName()
{
$this->module->amOnPage('/form/button');
$this->module->click("btn0");
$form = data::get('form');
$this->assertEquals('val', $form['text']);

}

public function testRadioButton()
{
$this->module->amOnPage('/form/radio');
Expand Down Expand Up @@ -260,6 +269,17 @@ public function testTextFieldByLabel() {
$this->assertEquals('Nothing special', $form['name']);
}

public function testTextFieldByName()
{
$this->module->amOnPage('/form/example1');
$this->module->fillField('LoginForm[username]', 'davert');
$this->module->fillField('LoginForm[password]', '123456');
$this->module->click('Login');
$login = data::get('form');
$this->assertEquals('davert', $login['LoginForm']['username']);
$this->assertEquals('123456', $login['LoginForm']['password']);
}

public function testFileFieldByCss() {
$this->module->amOnPage('/form/file');
$this->module->attachFile('#avatar', 'app/avatar.jpg');
Expand Down
15 changes: 14 additions & 1 deletion tests/unit/Codeception/Module/TestsForMink.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,20 @@ public function testClick()

$this->module->amOnPage('/');
$this->module->click("descendant-or-self::a[@id = 'link']");
$this->module->seeInCurrentUrl('/info');
$this->module->seeInCurrentUrl('/info');

$this->module->amOnPage('/');
$this->module->click("descendant-or-self::a[@id = 'link']");
$this->module->seeInCurrentUrl('/info');
}

public function testClickByName()
{
$this->module->amOnPage('/form/button');
$this->module->click("btn0");
$form = data::get('form');
$this->assertEquals('val', $form['text']);

}

public function testClickOnContext()
Expand Down

0 comments on commit 42ef901

Please sign in to comment.