2013-05-19 58 views
3

我正在運行Win2k8 EC2實例在*馬錢盒從面料部署運行後幾瀏覽器自動化任務的目的。允許硒的webdriver找到Cygwin和標準的Firefox路徑安裝

我的腳本,在Mac和Linux的作品呈現在cygwin下下面的錯誤使用Cygwin的Python:

File "/home/Myuser/.virtualenvs/myproject/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 141, in _get_firefox_start_cmd 
    " Please specify the firefox binary location or install firefox") 
RuntimeError: Could not find firefox in your system PATH. Please specify the firefox binary location or install firefox 

中有在Cygwin的(selenium)支持webdriver的一個已知的bug /缺乏興趣。

一位同行,使用戶操作更加有益的,這裏有一個解決方案:https://stackoverflow.com/a/11104952/1668057

這種方法好像它會打破我在Mac/* nix的代碼。

我該如何實現並保持代碼的可移植性?

(我的硒從PIP安裝,所以寧願要覆蓋的方法比編輯任何模塊文件)

編輯:

眼見更Python的方式建議傑夫的答案,我想出了用下面的(注意我的劇本已經子類/推翻了FirefoxProfile類禁用圖像):

from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile 
from subprocess import Popen, PIPE 

class CygwinFirefoxProfile(FirefoxProfile): 

    @property 
    def path(self): 

     path = self.profile_dir 

     try: 
      proc = Popen(['cygpath','-d',path], stdout=PIPE, stderr=PIPE) 
      stdout, stderr = proc.communicate() 
      path = stdout.split('\n', 1)[0] 
      print("cygwin path found") 

     except OSError: 
      print("No cygwin path found") 

     return path 

class CarServiceOnlineBookingsTest(unittest.TestCase):  

    def setUp(self): 

     firefoxProfile = CygwinFirefoxProfile() 

     ## Disable CSS 
     firefoxProfile.set_preference('permissions.default.stylesheet', 2) 
     ## Disable images 
     firefoxProfile.set_preference('permissions.default.image', 2) 
     ## Disable Flash 
     firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false') 

     self.driver = webdriver.Firefox(firefoxProfile) 

我的Mac上,現在這個捕捉異常和正常繼續,但日ËWin2k8盒,其中檢測cygwin的路徑,但它仍然失敗,出現以下錯誤:

Traceback (most recent call last): 
    File "myscript.py", line 45, in setUp 
    self.driver = webdriver.Firefox(firefoxProfile) 
    File "/home/Myuser/.virtualenvs/myenv/lib/python2.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 50, in __init__ 
    self.binary = FirefoxBinary() 
    File "/home/Myuser/.virtualenvs/myenv/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 33, in __init__ 
    self._start_cmd = self._get_firefox_start_cmd() 
    File "/home/Myuser/.virtualenvs/myenv/lib/python2.7/site-packages/selenium/webdriver/firefox/firefox_binary.py", line 141, in _get_firefox_start_cmd 
    " Please specify the firefox binary location or install firefox") 
RuntimeError: Could not find firefox in your system PATH. Please specify the firefox binary location or install firefox 

我一點兒也不熟悉POPEN或我期待什麼積極的結果返回。即,我應該期待像C:\Program Files (x86)\Firefox\Firefox.exe這樣的東西嗎?

下一個調試步驟在哪裏?

編輯#2:

從Cygwin的bash shell中執行這個命令執行的Firefox:

/cygdrive/c/Program\ Files\ \(x86\)/Mozilla\ Firefox/firefox.exe

我想我的下一個步驟是硬編碼到這個腳本,看看是否它將使硒通過cygwin的bash或通過SSH遠程啓動Firefox在本地...

回答

4

OK,那種公然明顯,但在手動設置日e在Win2k8 cygwin上的PATH變量,來自Jeff的答案的代碼工作正常,現在我正在Win2k8機器上通過遠程Linux機器快樂地運行Firefox。

我沒有手動設置PATH,認爲是作弊,但即使我想全自動化,可以做的面料腳本的一部分...

這裏是一個正在工作的代碼在Mac和Windows上都很好:

from selenium.common.exceptions import NoSuchElementException 
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile 
from subprocess import Popen, PIPE 

class CygwinFirefoxProfile(FirefoxProfile): 

    @property 
    def path(self): 

     path = self.profile_dir 

     # cygwin requires to manually specify Firefox path a below: 
     # PATH=/cygdrive/c/Program\ Files\ \(x86\)/Mozilla\ Firefox/:$PATH 
     try: 
      proc = Popen(['cygpath','-d',path], stdout=PIPE, stderr=PIPE) 
      stdout, stderr = proc.communicate() 
      path = stdout.split('\n', 1)[0] 

     except OSError: 
      print("No cygwin path found") 

     return path 

class CarServiceOnlineBookingsTest(unittest.TestCase):  

    def setUp(self): 

     firefoxProfile = CygwinFirefoxProfile() 

     ## Disable CSS 
     firefoxProfile.set_preference('permissions.default.stylesheet', 2) 
     ## Disable images 
     firefoxProfile.set_preference('permissions.default.image', 2) 
     ## Disable Flash 
     firefoxProfile.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', 'false') 

     self.driver = webdriver.Firefox(firefoxProfile) 

希望這段旅程能幫助別人做類似的事情。