@@ -692,27 +692,36 @@ public void startServerV2(TestCaseExecution execution) throws CerberusException
692692 appiumDriver = new IOSDriver (url , finalCapabilities );
693693 }
694694
695- //if preloadScript set, add webSocketUrl capability to enable bidi
696- if (!StringUtil .isEmptyOrNull (execution .getRobotObj ().getPreloadScript ())) {
697- finalCapabilities .setCapability ("webSocketUrl" , true );
698- }
699-
700695 driver = appiumDriver == null ? new RemoteWebDriver (executor , finalCapabilities ) : appiumDriver ;
701696 execution .setRobotProviderSessionID (getSession (driver , execution .getRobotProvider ()));
702697 execution .setRobotSessionID (getSession (driver ));
703698
704- //if webSocketUrl = true, and capability to enable bidi
699+ // Init BiDi only if preload script is defined
705700 BiDi biDiSession = null ;
706- Object ws = finalCapabilities .getCapability ("webSocketUrl" );
707- if (ws != null && ParameterParserUtil .parseBooleanParam (ws .toString (), false )) {
708- biDiSession = BiDiUtils .enableBiDi (driver );
709- }
710701
711- //if preloadScript set and bidi not null, add preload script
712- if (biDiSession != null && !StringUtil .isEmptyOrNull (execution .getRobotObj ().getPreloadScript ())) {
713- String preloadJs = "function() {" + execution .getRobotObj ().getPreloadScript () + "}" ;
714- BiDiUtils .addPreloadScript (biDiSession , preloadJs );
715- execution .addExecutionLog (ExecutionLog .STATUS_INFO , "Set browser preload Script : " + execution .getRobotObj ().getPreloadScript ());
702+ if (!StringUtil .isEmptyOrNull (execution .getRobotObj ().getPreloadScript ())) {
703+
704+ // Get returned capabilities
705+ Object ws = ((RemoteWebDriver ) driver ).getCapabilities ().getCapability ("webSocketUrl" );
706+
707+ if (ws != null ) {
708+ try {
709+ biDiSession = BiDiUtils .enableBiDi (driver );
710+
711+ String preloadJs = "() => {" + execution .getRobotObj ().getPreloadScript () + "}" ;
712+
713+ BiDiUtils .addPreloadScript (biDiSession , preloadJs );
714+
715+ execution .addExecutionLog (ExecutionLog .STATUS_INFO ,"Set browser preload Script : " + execution .getRobotObj ().getPreloadScript ());
716+
717+ } catch (Exception e ) {
718+ execution .addExecutionLog (ExecutionLog .STATUS_WARN ,"Failed to enable BiDi or set preload script : " + e .getMessage ()
719+ );
720+ }
721+ } else {
722+ execution .addExecutionLog (ExecutionLog .STATUS_WARN ,"BiDi not available (no webSocketUrl capability)"
723+ );
724+ }
716725 }
717726
718727 break ;
0 commit comments