Jump to content

Seeking Assistance with Implementing Various Browsers in WebDriver Project


Go to solution Solved by Danp2,

Recommended Posts

Hello dear forum,

I am working on a project where I aim to make all browsers operable with WebDriver. For this, I have created a GUI that will later be implemented in the settings, allowing both the IT department and users to conduct tests in case of issues with the browser being used.

So far, my setup works with Edge, Google Chrome, and Chromelegacy, but I am encountering difficulties with Opera, Firefox, and the Edge IE Mode. Although I have managed to open the Firefox browser with the Geckodriver, I have not yet been successful in running it in headless mode.

I plan to continue working on correctly implementing the other browsers once I get Firefox running as desired.

Here is my code. Below are several attempts to get Firefox running:

#include <StaticConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
#include <EditConstants.au3>
#include <ComboConstants.au3>
#include <nWebdriver\wd_capabilities.au3>
#include <nWebdriver\wd_helper.au3>
Func _guisetupweb()
    Local $setupWEB[]
    $gui = GUICreate("webdrivertestgui",582,524,-1,-1,-1,-1)
    $setupWEB.gui = $gui
    GUICtrlCreateGroup("web_driver",4,0,571,153,$BS_RIGHT,-1)
    GUICtrlCreateGroup("debug Level",353,16,81,61,-1,-1)
    GUICtrlCreateGroup("browser",15,16,316,61,$BS_CENTER,-1)
    $browser = GUICtrlCreateCombo("",30,43,290,21,-1,-1)
    $setupWEB.browser = $browser
    GUICtrlSetData($setupWEB.browser,"edge|chrome|chrome_legecy|opera|firefox|msedgeie", "edge")    
    $idebug = GUICtrlCreateCombo("",359,43,66,21,-1,-1)
    $setupWEB.idebug = $idebug
    GUICtrlSetData($setupWEB.idebug ,"0|1|2", "0")  
    GUICtrlCreateGroup("browser window",439,90,130,55,-1,-1)
    $WindowON = GUICtrlCreateRadio("ON",454,114,46,20,-1,-1)
    $setupWEB.WindowON = $WindowON
    $WindowOFF = GUICtrlCreateRadio("OFF",514,114,41,20,-1,-1)
    $setupWEB.WindowOFF = $WindowOFF
    GUICtrlCreateGroup("logpath",15,80,316,65,$BS_CENTER,-1)
    $saveLOG = GUICtrlCreateInput("",30,104,290,20,-1,$WS_EX_CLIENTEDGE)
    $setupWEB.saveLOG = $saveLOG
    $save = GUICtrlCreateButton("save",357,104,58,30,-1,-1)
    $setupWEB.save = $save
    GUICtrlCreateGroup("test browser",4,163,571,348,-1,-1)
    $edit = GUICtrlCreateEdit("",10,220,559,281,-1,-1)
    $setupWEB.edit = $edit
    $btntest = GUICtrlCreateButton("TEST",434,180,119,30,-1,-1)
    $setupWEB.btntest = $btntest
    $url = GUICtrlCreateInput("http://",107,187,308,20,-1,$WS_EX_CLIENTEDGE)
    $setupWEB.url = $url
    GUICtrlCreateLabel("URL",30,192,50,15,-1,-1)
    GUICtrlSetBkColor(-1,"-2")
    return $setupWEB
EndFunc

Global $msetupWeb,  $capa,  $iWebDriver_PID,  $sSession

$msetupWeb = _guisetupweb()
GUISetState()

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
                If $iWebDriver_PID <>  '' Then 
                    _WD_DeleteSession($sSession)
                    $sSession =  ''
                    _WD_Shutdown($iWebDriver_PID,Default)
                    $capa = ''
                EndIf 
            Exit            
        Case $msetupWeb.btntest
        first_test()
    EndSwitch
WEnd





func SetupWebdriver($sBrowser = 'edge',  $ilvl = 0,  $window = 0,  $sLogPath = '')
Local $headless = True 
local $log = true 

    IF $ilvl = 0 Then 
        $_WD_DEBUG = $_WD_DEBUG_None
    ElseIf $ilvl = 1 Then   
        $_WD_DEBUG = $_WD_DEBUG_Full            
    ElseIf $ilvl = 2 Then   
        $_WD_DEBUG = $_WD_DEBUG_Info
    EndIf

    IF $window = 1 Then 
        $headless = False 
    EndIf   
        
    IF $sLogPath =  '' Then 
        $log = False        
    EndIf
        
            IF $sBrowser = 'edge' Then              
                    $capa = SetupEdge($headless,$log,$sLogPath)             
            Elseif  $sBrowser =  'chrome' Then 
                    $capa = SetupChrome($headless,$log,$sLogPath)
            Elseif  $sBrowser = 'chrome_legecy' Then
                    $capa = SetupChromelegacy($headless,$log,$sLogPath) 
            ElseIf  $sBrowser =  'opera' Then 
                    $capa = SetupOpera($headless,$log,$sLogPath)    
            ElseIf  $sBrowser =  'firefox' Then 
                    $capa = SetupFireFox($headless,$log,$sLogPath)
            ElseIf  $sBrowser = 'msedgeie' Then 
                    $capa = SetupEdgeIE($headless,$log,$sLogPath)
            EndIf
EndFunc 

func first_test()
Local $window = 0

     $sbrowser = GUICtrlRead($msetupWeb.browser,1)
     $sidebug = GUICtrlRead($msetupWeb.idebug,1)
     $ssaveLOG = GUICtrlRead($msetupWeb.saveLOG,1)
     $sWindowON = GUICtrlRead($msetupWeb.WindowON)
        If $sWindowON = 1 Then 
            $window = 1
        EndIf
        
    If $iWebDriver_PID <>  '' Then 
    _WD_DeleteSession($sSession)
    $sSession =  ''
    _WD_Shutdown($iWebDriver_PID,Default)
    $capa = ''
    EndIf 

    SetupWebdriver($sbrowser,  $sidebug,  $window,  $ssaveLOG)
    $sSession = _WD_CreateSession($capa)
    IF @error Then MsgBox(0,  '_WD_CreateSession_fehler',  @Error)
    _WD_Navigate($sSession, 'https://google.de')
    IF @error Then MsgBox(0,  '_WD_Navigatefehler',  @Error)
    ConsoleWrite('test:'& _WD_GetSession($sSession) & @CRLF)

EndFunc 

;edge is tested, logpath not working.
    Func SetupEdge($headless, $log, $sLogPath)  
        _WD_UpdateDriver('msedge', @Tempdir & '\msedge\')
        _WD_Option('Driver', @Tempdir & '\msedge\msedgedriver.exe')
        _WD_Option('Port', 9515)
    IF $log Then
        _WD_Option('DriverParams', '--verbose --log-path=' & $sLogPath)     
    EndIf
        $iWebDriver_PID = _WD_Startup()
        _WD_CapabilitiesStartup()
        _WD_CapabilitiesAdd('alwaysMatch', 'msedge')
        _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')

    If $headless Then
        _WD_CapabilitiesAdd('args', 'headless') ; Headless-Modus
        _WD_CapabilitiesAdd('args', 'disable-gpu') ; Manchmal notwendig für Headless
    EndIf
        
        _WD_CapabilitiesAdd('args', 'guest')
        _WD_CapabilitiesDump(@ScriptLineNumber)
        Local $sCapabilities = _WD_CapabilitiesGet()
        Return $sCapabilities
    EndFunc 

;chrome is not tested, logpath not working.
    Func SetupChrome($headless,$log, $sLogPath) 
        Consolewrite('func start : chromedriver.exe' & @CRLF)
        _WD_UpdateDriver('chrome', @Tempdir & '\chrome\')
        _WD_Option('Driver', @Tempdir & '\chrome\chromedriver.exe')
        _WD_Option('Port', 9515)
        $iWebDriver_PID = _WD_Startup()
        
        _WD_CapabilitiesStartup()   
        _WD_CapabilitiesAdd('alwaysMatch', 'chrome')
        _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
        IF $log Then
        _WD_Option('DriverParams', '--verbose --log-path=' & $sLogPath)     
    EndIf   
    If $headless Then
        _WD_CapabilitiesAdd('args', 'headless') ; Headless-Modus
        _WD_CapabilitiesAdd('args', 'disable-gpu') ; Manchmal notwendig für Headless
    EndIf
        
        _WD_CapabilitiesAdd('args', 'guest')
        _WD_CapabilitiesDump(@ScriptLineNumber)
        Local $sCapabilities = _WD_CapabilitiesGet()
        Return $sCapabilities
    EndFunc   ;==>SetupChrome
    
    ;chrome legacy is tested, logpath not working.
    Func SetupChromelegacy($headless,$log,$sLogPath)    
        Consolewrite('func start : chromedriver.exe' & @CRLF)
        _WD_UpdateDriver('chrome_legacy', @Tempdir & '\chromely\')
        _WD_Option('Driver', @Tempdir & '\chromely\chromedriver.exe')
        _WD_Option('Port', 9515)
        $iWebDriver_PID = _WD_Startup()     
        _WD_CapabilitiesStartup()   
        _WD_CapabilitiesAdd('alwaysMatch', 'chrome')
        _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
        IF $log Then
        _WD_Option('DriverParams', '--verbose --log-path=' & $sLogPath)     
    EndIf   
    If $headless Then
        _WD_CapabilitiesAdd('args', 'headless') ; Headless-Modus
        _WD_CapabilitiesAdd('args', 'disable-gpu') ; Manchmal notwendig für Headless
    EndIf
        
        _WD_CapabilitiesAdd('args', 'guest')
        _WD_CapabilitiesDump(@ScriptLineNumber)
        Local $sCapabilities = _WD_CapabilitiesGet()
        Return $sCapabilities
    EndFunc   ;==>SetupChromelegacy

;opera is not tested.
    Func SetupOpera($headless, $log,$sLogPath)  
        _WD_UpdateDriver('opera', @Tempdir & '\opera\') 
        _WD_Option('Driver', @Tempdir & '\opera\operadriver.exe')
        ConsoleWrite( @Tempdir & '\opera\operadriver.exe')
        _WD_Option('Port', 9515)
        $iWebDriver_PID = _WD_Startup()
        ;Startparameter sammeln 
        _WD_CapabilitiesStartup()
        _WD_CapabilitiesAdd('alwaysMatch', 'chrome')
        _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
        IF $log Then
        _WD_Option('DriverParams', '--verbose --log-path=' & $sLogPath)     
    EndIf
    If $headless Then
        _WD_CapabilitiesAdd('args', 'headless') ; Headless-Modus
        _WD_CapabilitiesAdd('args', 'disable-gpu') ; Manchmal notwendig für Headless
    EndIf
        
        _WD_CapabilitiesAdd('args', 'guest')
        _WD_CapabilitiesDump(@ScriptLineNumber)
        Local $sCapabilities = _WD_CapabilitiesGet()
        Return $sCapabilities
    EndFunc   ;==>SetupOpera
    
;IEmode - Edge is not tested.
    Func SetupEdgeIE($headless,$log, $sLogPath) 
        _WD_UpdateDriver('msedgeie', @Tempdir & '\msedgeie\')   
        _WD_Option('Driver', @Tempdir & '\msedgeie\IEDriverServer.exe')
        ConsoleWrite( @Tempdir & '\msedgeie\IEDriverServer.exe')
        _WD_Option('Port', 9515)
        $iWebDriver_PID = _WD_Startup()
        ;Startparameter sammeln 
        _WD_CapabilitiesStartup()
        _WD_CapabilitiesAdd('alwaysMatch', 'opera')
        _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
            IF $log Then
        _WD_Option('DriverParams', '--verbose --log-path=' & $sLogPath)     
    EndIf
    If $headless Then
        _WD_CapabilitiesAdd('args', 'headless') ; Headless-Modus
;~         _WD_CapabilitiesAdd('args', 'disable-gpu') ; Manchmal notwendig für Headless
    EndIf
        
        _WD_CapabilitiesAdd('args', 'guest')
        _WD_CapabilitiesDump(@ScriptLineNumber)
        Local $sCapabilities = _WD_CapabilitiesGet()
        Return $sCapabilities
    EndFunc   ;==>SetupEdgeIE




;not headless :(
Func SetupFireFox($headless, $log, $sLogPath)
    local $sDriver = 'geckodriver.exe'
    _WD_UpdateDriver('firefox')
    IF @error Then MsgBox(0,  '_WD_UpdateDriverfehler',  @Error)
    _WD_Option('Driver', $sDriver)
    IF @error Then MsgBox(0,  '_WD_Optionfehler driver',  @Error)
    _WD_Option('Port', 4444)
    IF @error Then MsgBox(0,  '_WD_Optionfehler port',  @Error)
    If $headless Then
        $sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"moz:firefoxOptions": {"args": ["-headless"]}}}}' ;_WD_CapabilitiesAdd ==> Browser or feature not supported [21] : Not supported KEY parameter ( must be defined in $_WD_KEYS__*** ).      $key = moz:firefoxOptions     $value1 = {"capabilities":{"alwaysMatch": {"moz:firefoxOptions": {"args": ["-headless"]}}}}     $value2 = 
    Else
        $sDesiredCapabilities = '{"capabilities":{"alwaysMatch": {"moz:firefoxOptions": {"args": []}}}}'
    EndIf

    _WD_CapabilitiesStartup()
    IF @error Then MsgBox(0,  '_WD_CapabilitiesStartup ',  @Error)
    _WD_CapabilitiesAdd('alwaysMatch', 'firefox')
    IF @error Then MsgBox(0,  '_WD_CapabilitiesAdd ',  @Error)
    _WD_CapabilitiesAdd('browserName', 'firefox')
    IF @error Then MsgBox(0,  '_WD_CapabilitiesAdd ',  @Error)
    _WD_CapabilitiesAdd('acceptInsecureCerts', True)
    IF @error Then MsgBox(0,  '_WD_CapabilitiesAdd ',  @Error)
    _WD_CapabilitiesAdd('binary', "C:\Program Files\Mozilla Firefox\firefox.exe")
    IF @error Then MsgBox(0,  '_WD_CapabilitiesAdd ',  @Error)
    _WD_CapabilitiesAdd('moz:firefoxOptions', $sDesiredCapabilities)
    IF @error Then MsgBox(0,  '_WD_CapabilitiesAdd ',  @Error)

    _WD_CapabilitiesDump()
    IF @error Then MsgBox(0,  '_WD_CapabilitiesDump ',  @Error)
   $iWebDriver_PID =  _WD_Startup()
    IF @error Then MsgBox(0,  '_WD_CapabilitiesDump ',  @Error)
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupFireFox

Func SetupFireFox1($headless, $log, $sLogPath)
    local $sDriver = 'geckodriver.exe'
    _WD_UpdateDriver('firefox')
    _WD_Option('Driver', $sDriver)
    _WD_Option('Port', 4444)

    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'firefox')
    _WD_CapabilitiesAdd('browserName', 'firefox')
    _WD_CapabilitiesAdd('acceptInsecureCerts', True)
    _WD_CapabilitiesAdd('binary', "C:\Program Files\Mozilla Firefox\firefox.exe")
    If $headless Then
        Local $aArgs = ['headless']
        _WD_CapabilitiesAdd('moz:firefoxOptions', 'args', $aArgs)
    EndIf

    _WD_CapabilitiesDump()
   $iWebDriver_PID = _WD_Startup()

    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc 
    
func SetupFireFox2($bHeadless, $log, $sLogPath)
    local $sDriver = 'geckodriver.exe'
    _WD_UpdateDriver('firefox')
    _WD_Option('Driver', $sDriver)
    _WD_Option('Port', 4444)

    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'firefox')
    _WD_CapabilitiesAdd('browserName', 'firefox')
    _WD_CapabilitiesAdd('acceptInsecureCerts', True)
    _WD_CapabilitiesAdd('binary', "C:\Program Files\Mozilla Firefox\firefox.exe")
    _WD_CapabilitiesDump()
    _WD_CapabilitiesAdd("window.resizeTo(1920,1080)")
    _WD_Startup()
        Local $sCapabilities = _WD_CapabilitiesGet()
        Return $sCapabilities
EndFunc  

Func SetupFireFox4($bHeadless, $log, $sLogPath)
    _WD_UpdateDriver('firefox', @Tempdir & '\firefox\', True, True)
    _WD_Option('Driver', @Tempdir & '\firefox\geckodriver.exe')
    _WD_Option('Port', 9515)
    $iWebDriver_PID = _WD_Startup()
    _WD_CapabilitiesStartup()
    _WD_CapabilitiesAdd('alwaysMatch', 'firefox')
    _WD_CapabilitiesAdd('excludeSwitches', 'enable-automation')
    If $log Then
        $firefoxOptions = '{"log": {"level": "trace"}}'
        _WD_CapabilitiesAdd('moz:firefoxOptions', $firefoxOptions)
        If $sLogPath <> '' Then
            _WD_CapabilitiesAdd('moz:firefoxOptions', '{"logPath": "' & $sLogPath & '"}')
        EndIf
    EndIf
    If $bHeadless Then
        _WD_CapabilitiesAdd('moz:firefoxOptions', '{"args":["-headless"]}')
    EndIf
    _WD_CapabilitiesAdd('args', 'guest')
    Local $sCapabilities = _WD_CapabilitiesGet()
    Return $sCapabilities
EndFunc   ;==>SetupFireFox

I hope you can help me further.

Best regards,
mojoe

Link to comment
Share on other sites

I even once copied over and adapted the demo start script because my own driver could not be operated. And to see if it runs for me.

I have now added this, deleting my condition:
 

If $headless Then
    _WD_CapabilitiesAdd('args', '--headless')
EndIf

It always shows me this as an error:

_WD_CapabilitiesAdd ==> Browser or feature not supported [21] : Capability name are case sensitive ( check proper case in $_WD_KEYS__*** for "args").      $key = args     $value1 = --headless     $value2 = 

 

Link to comment
Share on other sites

  • Solution

Something is wrong with your setup, but it's difficult to identify the issue without more details. I would suggest making sure that you are running the latest release of the Webdriver UDF library (see link in my sig), and also check to make sure that there isn't an older version on disk that is accidentally being accessed.

Link to comment
Share on other sites

Posted (edited)

I apologize, I was testing in the wrong script window. :/^^ sry
Now it works :) I have now placed

If $headless Then
_WD_CapabilitiesAdd('args', '--headless')
EndIf

directly above

$sCapabilities = _WD_CapabilitiesGet()
Return $sCapabilities

tested it, and now headless works too. Thanks for the prompt! Danp2


I am using all the latest updates specifically for this project, everything is updated.

 


 

Edited by MojoeB
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...