Selenium RC:如何单击使用 onclick window.location 的按钮?

发布于 2024-09-03 19:08:15 字数 553 浏览 2 评论 0原文

我有一个按钮(表单外部),使用 onclick 属性重定向到另一个页面,该属性调用 window.location 将用户重定向到另一个页面。这次我无法更改 HTML。我正在使用 Safari 4 进行测试。如何单击使用 onclick 属性和 window.location 的按钮以使用 Safari 4 和 Selenium RC PHPUnit 扩展进行重定向?

这是我的 HTML:

<input type="button" onclick="window.location='/registrations/new'" value="Start a new registration" id="create">

更新: 或者,我正在考虑做一些事情,断言在 window.location='' 中指定了一个位置,存储该位置,并使用该位置调用 open() 命令。不确定这是否是可接受的测试方法。

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?

Here's my HTML:

<input type="button" onclick="window.location='/registrations/new'" value="Start a new registration" id="create">

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念﹏祤嫣 2024-09-10 19:08:15

我们决定扩展默认的 clickAndWait() 功能,以便在按钮具有 onclick="window.location='/something';" 时调用 openAndWait()

/**
 * 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);
}

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);
}
忆梦 2024-09-10 19:08:15

应该可以工作,但是您也可以尝试使用fireEvent命令触发点击事件。

selenium.fireEvent("id=create", "click");

This should work, however you could also try firing the click event using the fireEvent command.

selenium.fireEvent("id=create", "click");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文