2013-07-24 122 views
12

我有一個單元測試,當我通過python manage.py test運行時傳遞,但在PyCharm中運行時失敗。PyCharm無法連接到GhostDriver

def test_alpha(self): 
    from selenium.webdriver.common.utils import free_port 
    from selenium import webdriver 

    driver = webdriver.PhantomJS(executable_path=PHANTOMJS_PATH, port=free_port()) 
    driver.quit() 

從PyCharm運行時,我得到的例外是

WebDriverException: Message: 'Can not connect to GhostDriver' 

我花了很多時間挖掘到這個問題了相當數量,而且我發現,當我手動指定端口測試在PyCharm中傳遞。

# suppose 50000 happens to be a free port on your computer 
driver = webdriver.PhantomJS(executable_path=PHANTOMJS_PATH, port=50000) 

快速回顧一下:如果端口手動指定

  • 測試中PyCharm失敗如果port=free_port()
  • 什麼是

    • 測試與python manage.py test
    • 測試通過在PyCharm通行證PyCharm這樣做是否讓測試無法連接到Ghostdriver?


      # For convenience, the `free_port()` code snippet is here 
      # selenium.webdriver.common.utils.freeport 
      
      def free_port(): 
          free_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
          free_socket.bind(('127.0.0.1', 0)) 
          free_socket.listen(5) 
          port = free_socket.getsockname()[1] 
          free_socket.close() 
          return port 
      
    +0

    PyCharm中的'free_port()'返回什麼?你使用什麼操作系統? – CrazyCoder

    +0

    我使用OS X,而free_port()通常返回50000-60000之間的端口。 –

    +4

    如果從終端啓動PyCharm,它有幫助嗎:'open -a/Applications/PyCharm.app /'? – CrazyCoder

    回答