【问题标题】:Selenium RC: How to click buttons that use onclick window.location?Selenium RC:如何单击使用 onclick window.location 的按钮?
【发布时间】:2011-08-16 04:38:14
【问题描述】:

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

这是我的 HTML:

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

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

【问题讨论】:

  • 点击它会发生什么,标准方式?
  • 手动点击它可以正常工作。但是在尝试使用 Selenium RC 单击它时它不会重定向。
  • 当您在另一个浏览器中运行测试时会发生什么?我已经在 Firefox 中成功测试过,这只是 Safari 的问题吗?

标签: button safari selenium phpunit window.location


【解决方案1】:

如果按钮有onclick="window.location='/something';",我们决定扩展默认的clickAndWait() 功能以调用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);
}

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-07
      • 2018-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-27
      • 2019-06-10
      相关资源
      最近更新 更多