硒RC:如何点击使用的onclick window.location的按钮? [英] Selenium RC: How to click buttons that use onclick window.location?
问题描述
我有一个按钮(表格外) $ C>将用户重定向到另一个页面。这一次,我不能改变的HTML。我使用的Safari 4进行测试。我怎么可以单击使用一个按钮的onclick
属性和 window.location的
使用Safari 4和硒RC PHPUnit的重定向扩展?
I have a button (outside of a form) that redirects to another page using the onclick
attribute that calls window.location
to redirect the user to another page. This time I can't change the HTML. I am using Safari 4 for testing. How can I click a button that uses the onclick
attribute and window.location
to redirect using Safari 4 and Selenium RC PHPUnit Extension?
下面是我的HTML:
<input type="button" onclick="window.location='/registrations/new'" value="Start a new registration" id="create">
更新:另外,我在考虑做一些事情,我断言,一个位置中指定的了window.location =''
使用该位置存储位置,并调用的open()
命令。不知道这是为了测试这种可以接受的方法。
Update: Alternatively, I'm thinking about doing something where I assert that a location is specified in the window.location=''
, store that location, and call the open()
command using that location. Not sure if this is an acceptable approach to testing this.
推荐答案
我们决定延长默认 clickAndWait()
功能调用 openAndWait( )
如果按钮有一个的onclick =window.location的='/某物,
We decided to extend the default clickAndWait()
functionality to call openAndWait()
if the button has an onclick="window.location='/something';"
/**
* Extends original clickAndWait() functionality to provide the ability to click buttons that use window.location to redirect users
* @param $locator
*/
public function clickAndWait($locator)
{
$this->assertElementPresent($locator);
$hasOnclickAttribute = $this->getEval('this.browserbot.findElement("' . $locator . '").hasAttribute("onclick")');
if ($hasOnclickAttribute === 'true') {
$onclickValue = $this->getAttribute("$locator@onclick");
if (strpos($onclickValue, "window.location=") !== false) {
// Clean up the location
$temp = explode("=" , $onclickValue);
$location = trim($temp[1], "';");
$location = trim($location, '"');
return $this->openAndWait($location);
}
}
return parent::clickAndWait($locator);
}
这篇关于硒RC:如何点击使用的onclick window.location的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!