Reputation: 229
I have a form with submit button
<input id="myid" type="submit" value="My button"></input>
I tried a lot different ways to click on it, but it doesn't work. Why? I use Selenium Driver with PhantomJS browser. What I tried:
$page = $this->getSession()->getPage();
$page->find('xpath', '//*[@id="myid"]')->click();
$page->find('xpath', '//*[@id="myid"]')->doubleClick();
$page->find('xpath', '//*[@id="myid"]')->press();
$page->find('css', '#myid')->click();
$page->find('css', '#myid')->doubleClick();
$page->find('css', '#myid')->press();
$this->getMinkContext()->pressButton('myid');
Upvotes: 0
Views: 3714
Reputation: 3067
We use a function like this:
/**
* @Then /^(?:|I )click (?:on |)(?:|the )"([^"]*)"(?:|.*)$/
*/
public
function iClickOn($arg1)
{
$findName = $this->getSession()->getPage()->find("css", $arg1);
if (!$findName) {
throw new Exception($arg1 . " could not be found");
} else {
$findName->click();
}
}
Usage 1:
Scenario: Test Click
Given I go to "<url>"
And I click the "#myId" button - to reveal a hidden element
Then I should see an "<url2>" element
Usage 2:
Scenario: Test Click
Given I go to "<url>"
And I click the "#myId" link
Then I should be on "<url2>"
If this doesn't work for you, then it may be because there is an element in the way? Is the button that you want to click on genuinely there?
If it isn't, then perhaps this will help you too:
/**
* @When I scroll :elementId into view
*/
public function scrollIntoView($elementId) {
$function = <<<JS
(function(){
var elem = document.getElementById("$elementId");
elem.scrollIntoView(false);
})()
JS;
try {
$this->getSession()->executeScript($function);
}
catch(Exception $e) {
throw new \Exception("ScrollIntoView failed");
}
}
Personally, I haven't used the final function, however the same function helped a person in one of the posts I was viewing before this.
Upvotes: 2