ExecuteScript failing on Selenium Grid

66 views
Skip to first unread message

Matt Smith

unread,
Sep 23, 2024, 2:13:09 PM9/23/24
to Selenium Users
Hi everyone,

I have a test step that calls my ExecuteScript helper method which is just a wrapper around the JavaScriptExecutor ExecuteJavaScript() method:

        /// <summary>
        /// Wrapper method for executing client-side JavaScript in the browser
        /// instance. This is useful for triggering events or executing specific
        /// methods that aren't triggered by Click or EnterData events.
        /// </summary>
        /// <param name="script"></param>
        public void ExecuteScript(string script)
        {
            IJavaScriptExecutor js = (IJavaScriptExecutor)_driver;
            js.ExecuteScript(script);
        }

This step works fine when I am running my test cases locally. However, it fails when sending the tests to our Selenium Grid. Unfortunately there aren't a lot of details in the failure messages and logs:

  Message:
OpenQA.Selenium.WebDriverException : An unknown exception was encountered sending an HTTP request to the remote WebDriver server for URL http://ourseleniumgrid.com:4444/session/e63d4e0ace694a095a7b9040383a651f/execute/sync. The exception message was: An error occurred while sending the request.
  ----> System.Net.Http.HttpRequestException : An error occurred while sending the request.
  ----> System.Net.WebException : The underlying connection was closed: A connection that was expected to be kept alive was closed by the server.
  ----> System.IO.IOException : Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
  ----> System.Net.Sockets.SocketException : A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

  Stack Trace:
<ExecuteAsync>d__34.MoveNext()

--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ConfiguredTaskAwaiter.GetResult()
<ExecuteAsync>d__63.MoveNext()

--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
TaskAwaiter`1.GetResult()
WebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
WebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
WebDriver.ExecuteScript(String script, Object[] args)
UIHelper.ExecuteScript(String script) line 230
<ExecuteTest>d__4.MoveNext() line 80

--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
TaskAwaiter.GetResult()
<TestUI>d__5.MoveNext() line 83

--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
GenericAdapter`1.GetResult()
AsyncToSyncAdapter.Await[TResult](Func`1 invoke)
TestMethodCommand.RunTestMethod(TestExecutionContext context)
TestMethodCommand.Execute(TestExecutionContext context)
<>c__DisplayClass1_0.<Execute>b__0()
DelegatingTestCommand.RunTestMethodInThreadAbortSafeZone(TestExecutionContext context, Action action)
--HttpRequestException
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ConfiguredTaskAwaiter.GetResult()
<MakeHttpRequest>d__37.MoveNext()

--- End of stack trace from previous location where exception was thrown ---
TaskAwaiter.ThrowForNonSuccess(Task task)
TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
ConfiguredTaskAwaiter.GetResult()
<ExecuteAsync>d__34.MoveNext()
--WebException
HttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
HttpClientHandler.GetResponseCallback(IAsyncResult ar)
--IOException
NetworkStream.EndRead(IAsyncResult asyncResult)
PooledStream.EndRead(IAsyncResult asyncResult)
Connection.ReadCallback(IAsyncResult asyncResult)
--SocketException
Socket.EndReceive(IAsyncResult asyncResult)
NetworkStream.EndRead(IAsyncResult asyncResult)

I spent a lot of time trying to find answers but no luck. Spent some time with ChatGPT as well but no luck there either. I just don't know why it seems like the grid server node does not seem to want to receive that request. We have hundreds of other test cases that run fine locally and through Selenium Grid. But this one test case happens to have this ExecuteScript command to try to change a value via JavaScript. I've look at some different Chrome option arguments but haven't found anything that let's it work.

Thanks in advance for any suggestions,
Matt

Matt Smith

unread,
Sep 24, 2024, 3:14:13 PM9/24/24
to Selenium Users
I tried calling the url flagged in the error message (http://ourseleniumgrid.com:4444/session/{sessionid}/execute/sync) during a new session and get the following exception:

{ "value": { "error": "unknown command", "message": "unknown command: unknown command: session/{sessionid}/execute/sync", "stacktrace": "\tGetHandleVerifier [0x00007FF680B0B125+29573]\n\t(No symbol) [0x00007FF680A7FF50]\n\t(No symbol) [0x00007FF68093B6EA]\n\t(No symbol) [0x00007FF6809B03BD]\n\t(No symbol) [0x00007FF6809AFEAD]\n\t(No symbol) [0x00007FF680903D75]\n\tGetHandleVerifier [0x00007FF680E3C76D+3377613]\n\tGetHandleVerifier [0x00007FF680E87B67+3685831]\n\tGetHandleVerifier [0x00007FF680E7CF8B+3641835]\n\tGetHandleVerifier [0x00007FF680BCB2A6+816390]\n\t(No symbol) [0x00007FF680A8B25F]\n\t(No symbol) [0x00007FF680902837]\n\tGetHandleVerifier [0x00007FF680EEC348+4097448]\n\tBaseThreadInitThunk [0x00007FFADFBC7AC4+20]\n\tRtlUserThreadStart [0x00007FFAE230A8C1+33]\n" } }

I've tried this with both /execute/sync and /execute/async and both yield the same error. It appears as if there is nothing at the endpoint to catch the request and process it successfully. Has anyone seen anything like this before?

Matt Smith

unread,
Sep 24, 2024, 6:42:26 PM9/24/24
to Selenium Users
Never mind. Two days in a row on two different issues.

I changed this line:
                _ui.ExecuteScript("isTrustedDomain = function isTrustedDomain() { return true;}");

to:
                _ui.ExecuteScript("isTrustedDomain = function(url) { return true; };");

And things appear to be working for now. Of course it took a lot to get there. The fact that it works locally really threw me for a loop. It made me believe that ExecuteScript wasn't working when calling via RemoteWebDriver. I had to do a lot to overcome that belief with some other simpler examples. 

                string title = _ui.ExecuteScript("return document.title").ToString();
                TestContext.WriteLine($"Browser title is '{title}'");

Once I saw that it did actually work then I decided to see if I could override the click action instead of one of the methods in the click action:

                _ui.ExecuteScript("ellipsisActions.Common.open.click = (e, data) => { e.preventDefault(); globalBlockUI(); location.href = location.origin + data.url + '&WindowId=' + getUniqueWindowId(); };");

Then once I realized that I could do that then I went back to look at my original code and compared it to some other examples and then decided to tweak it.

It's only taken me two days to work through it...

Thanks for listening!
Reply all
Reply to author
Forward
0 new messages