views:

293

answers:

1

I have button on the page that being used as a link to another page. So when the user clicks the button they are redirected to the right page.

I am using the Selenium extension for PHPUnit and I would like to test that the button is correctly redirecting the user. Here is the HTML that I am trying to test:

<button onclick="window.location='/sponsor';" value="Continue" type="button" id="sponsorContinue" name="sponsorContinue">Continue</button>

I have tried a lot of different approaches to click the button, but I can't seem to get any of them to work:

$this->click("//button[@id='sponsorContinue']");

This command executes and does not throw any errors, but the page isn't redirected. It works fine when I manually click the button. What should I do?

A: 

Here's the problem...the HTML is wrong, and it's forcing users to use javascript to continue to the next page. Use this HTML instead. It's cleaner and doesn't rely on javascript.

<form method="get" action="/sponsor">
    <button type="submit" id="sponsorContinue">Continue</button>
</form>

Also, this will now work:

$this->click('sponsorContinue');

However, if you're trying to get window.location to work, see this question.

Andrew
+1 for the Javascript-less solution. But still why isn't Selenium triggering the onclick event?
Alexandre Jasmin
I agree with changing the button HTML in this case. However if you need to fire events in the future Selenium lets you do this with fireEvent!
Zugwalt
@Zugwalt I am not able to get fireEvent to work in Safari 4
Andrew
@Andrew, I have not tried Safari 4 but am sorry fireEvent isn't working. While not "pure", you could click and use javaScript injection to manually call the onClick js function.
Zugwalt