Jump to content

Webdriver implementation of FFPrefGet and FFPrefSet?


Recommended Posts

In the old FF.au3 library, there were two functions for getting and setting values of about:config preferences for Firefox. I was wondering if there are any equivalents for WebDriver au3, @Danp2 

Func _FFPrefGet($sName)
    Local Const $sFuncName = "_FFPrefGet"

    Local $sCommand
    Local $iType = _FFCmd('FFau3.obj=Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); FFau3.obj.getPrefType("' & $sName & '");')
    If @error Then
        SetError(__FFError($sFuncName, $_FF_ERROR_InvalidValue, $sName))
        Return 0
    EndIf

    Switch $iType
        Case 0
            SetError(__FFError($sFuncName, $_FF_ERROR_NoMatch, "$sName: " & $sName))
            Return 0
        Case 32 ; PREF_STRING
            $sCommand = "getCharPref"
        Case 64 ; PREF_INT
            $sCommand = "getIntPref"
        Case 128 ; PREF_BOOL
            $sCommand = "getBoolPref"
    EndSwitch

    $sCommand = StringFormat('FFau3.obj.%s("%s");', $sCommand, $sName)

    Return _FFCmd($sCommand)
EndFunc   ;==>_FFPrefGet


 

Func _FFPrefSet($sName, $vValue)
    Local Const $sFuncName = "_FFPrefSet"

    Local $sCommand
    Local $iType = _FFCmd('FFau3.obj=Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);FFau3.obj.getPrefType("' & $sName & '");')
    If @error Then
        SetError(__FFError($sFuncName, $_FF_ERROR_InvalidValue, $sName))
        Return 0
    EndIf
    Local $vOldValue = _FFPrefGet($sName)

    Switch $iType
        Case 0
            SetError(__FFError($sFuncName, $_FF_ERROR_NoMatch, "$sName: " & $sName))
            Return 0
        Case 32 ; PREF_STRING
            If Not IsString($vValue) Then
                SetError(__FFError($sFuncName, $_FF_ERROR_InvalidDataType, "(string) $vValue: " & $vValue))
                Return 0
            EndIf
            $sCommand = StringFormat('FFau3.obj.%s("%s","%s");', "setCharPref", $sName, $vValue)
        Case 64 ; PREF_INT
            If Not IsInt($vValue) Then
                SetError(__FFError($sFuncName, $_FF_ERROR_InvalidDataType, "(int) $vValue: " & $vValue))
                Return 0
            EndIf
            $sCommand = StringFormat('FFau3.obj.%s("%s",%s);', "setIntPref", $sName, $vValue)
        Case 128 ; PREF_BOOL
            If Not IsBool($vValue) Then
                SetError(__FFError($sFuncName, $_FF_ERROR_InvalidDataType, "(bool) $vValue: " & $vValue))
                Return 0
            EndIf
            $sCommand = StringFormat('FFau3.obj.%s("%s",%s);', "setBoolPref", $sName, __FFB2S($vValue))
    EndSwitch

    _FFCmd($sCommand)
    If Not @error Then
        Local $vRetVal = _FFPrefGet($sName)
        If Not @error And $vRetVal = $vValue Then
            SetExtended($vOldValue)
            Return 1
        Else
            SetError(__FFError($sFuncName, $_FF_ERROR_RetValue, "$vValue <> " & $vRetVal))
            Return 0
        EndIf
    EndIf

    Return 0
EndFunc   ;==>_FFPrefSet

 

Link to comment
Share on other sites

Posted (edited)

Found a possible solution here but not sure how I can implement it in AutoIt:
https://stackoverflow.com/a/75925250

Actually, this exactly what the set_context() method is for.

https://github.com/SeleniumHQ/selenium/blob/selenium-4.8.0/py/selenium/webdriver/firefox/webdriver.py#L224-L247

For example:

driver.set_context("chrome")
driver.execute_script("Services.prefs.setStringPref('intl.accept_languages', 'es-ES');")
driver.set_context("context")

Make sure you switch back to "context" or else you won't be able to continue.

In the linked Github:

def set_context(self, context) -> None:
        self.execute("SET_CONTEXT", {"context": context})

    @contextmanager
    def context(self, context):
        """Sets the context that Selenium commands are running in using a
        `with` statement. The state of the context on the server is saved
        before entering the block, and restored upon exiting it.

        :param context: Context, may be one of the class properties
            `CONTEXT_CHROME` or `CONTEXT_CONTENT`.

        Usage example::

            with selenium.context(selenium.CONTEXT_CHROME):
                # chrome scope
                ... do stuff ...
        """
        initial_context = self.execute("GET_CONTEXT").pop("value")
        self.set_context(context)
        try:
            yield
        finally:
            self.set_context(initial_context)

 

To specify, I want to change a preference when I'm already running geckodriver, it isn't something I want to set in `_WD_CapabilitiesAdd`.

Edited by noellarkin
clarity
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...