2010-06-01 68 views
1

我有一個按鈕(表單外部),使用onclick屬性重定向到另一個頁面,該屬性調用window.location將用戶重定向到另一個頁面。這一次我無法更改HTML。我正在使用Safari 4進行測試。如何單擊使用onclick屬性和window.location的按鈕以使用Safari 4和Selenium RC PHPUnit Extension重定向?Selenium RC:如何點擊使用onclick window.location的按鈕?

這裏是我的HTML:

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

更新:另外,我想這樣做,我斷言,位置在window.location=''指定的東西,存儲位置,並使用調用open()命令那個位置。不知道這是否是一種可接受的方法來測試這一點。

+0

當你點擊它,會發生什麼標準呢? – Nicole 2010-06-01 23:01:33

+0

當您手動點擊它時它工作正常。但是在使用Selenium RC單擊它時不會重定向。 – Andrew 2010-06-01 23:11:19

+0

當您在另一個瀏覽器中運行測試時會發生什麼?我已經在Firefox中成功測試過了,這只是Safari的一個問題嗎? – 2010-06-02 09:19:19

回答

0

這個應該工作,但是你也可以嘗試使用fireEvent命令來觸發點擊事件。

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

我已經在Safari 4中試過了,它不起作用。 – Andrew 2010-06-02 14:44:42

+0

我同意。嘗試在Firefox 3.6中,它不起作用。 – 2012-03-14 14:57:28

2

我們決定延長默認clickAndWait()功能調用openAndWait()如果按鈕的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("[email protected]"); 
     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); 
}