使用自己的Firefox做測試

在執行Windows上的測試,我們都有固定一包firefox,直接丟到測試系統中,並設定firefox.exe於環境變數PATH中;Linux則是直接使用系統預設的。然而在更新Selenium2Library同時,發現新的selenium在Windows上會強制讀取ProgramFiles內的Firefox。讓我直接參考selenium的firefox_binary.py (v2.46.0):

    def _get_firefox_start_cmd(self):
        """Return the command to start firefox."""
        start_cmd = ""	
        if platform.system() == "Darwin":
            start_cmd = ("/Applications/Firefox.app/Contents/MacOS/firefox-bin")
        elif platform.system() == "Windows":
            start_cmd = (self._find_exe_in_registry() or
                self._default_windows_location())
        elif platform.system() == 'Java' and os._name == 'nt':
            start_cmd = self._default_windows_location()
        else:
            for ffname in ["firefox", "iceweasel"]:
                start_cmd = self.which(ffname)
                if start_cmd is not None:
                    break
            else:
                # couldn't find firefox on the system path
                raise RuntimeError("Could not find firefox in your system PATH." +
                    " Please specify the firefox binary location or install firefox")
        return start_cmd
 
    def _default_windows_location(self):
        program_files = [os.getenv("PROGRAMFILES", r"C:\Program Files"),
                         os.getenv("PROGRAMFILES(X86)", r"C:\Program Files (x86)")]
        for path in program_files:
            binary_path = os.path.join(path, r"Mozilla Firefox\firefox.exe")
            if os.access(binary_path, os.X_OK):
                return binary_path
        return ""
鄉民可能會問: 那不是直接在系統上裝一個就好了嗎? 我只能說: 我們的系統不是想裝就可以裝阿!

我選擇最直接的: 修改原始碼,多增加一個自用變數判斷。優先抓取我自訂的系統變數,這變數是宣告Firefox執行檔路徑:

    def _get_firefox_start_cmd(self):
        """Return the command to start firefox."""
        start_cmd = self._tony_firefox_location()
        if start_cmd != "":
            return start_cmd		
        if platform.system() == "Darwin":
            start_cmd = ("/Applications/Firefox.app/Contents/MacOS/firefox-bin")
        elif platform.system() == "Windows":
            start_cmd = (self._find_exe_in_registry() or
                self._default_windows_location())
        elif platform.system() == 'Java' and os._name == 'nt':
            start_cmd = self._default_windows_location()
        else:
            for ffname in ["firefox", "iceweasel"]:
                start_cmd = self.which(ffname)
                if start_cmd is not None:
                    break
            else:
                # couldn't find firefox on the system path
                raise RuntimeError("Could not find firefox in your system PATH." +
                    " Please specify the firefox binary location or install firefox")
        return start_cmd
 
    def _tony_firefox_location(self):
        binary_path = os.getenv("TONY_FIREFOX", r"C:\Program Files\Mozilla Firefox\firefox.exe")
        if os.access(binary_path, os.X_OK):
            return binary_path
        return ""		
在Windows上的batch,可以這樣寫:
@echo off
set BASE=%~dp0
set TONY_FIREFOX=%BASE%firefox_win\firefox.exe
在Linux上的shellscript,可以這樣寫:
#!/bin/bash
FULLPATH=`dirname "$0"`/`basename "$0"`
BASE=`readlink -f "$FULLPATH"`
BASEPATH=`dirname $BASE`
FF_HOME=$BASEPATH/firefox_linux
 
TONY_FIREFOX=$FF_HOME/firefox
export TONY_FIREFOX
透過Create WebDriver的Keyword,也是可以傳入Firefox的執行路徑。但考量到SeleniumLibrary升級問題,所以我使用最直接的方法。