Inserting javascript variables and functions into the DOM with selenium

0

In order to improve the selenium waits on my website I need to check whether a particular table has finished drawing.

The site I am looking at uses datatables.net, it provides both pre and post draw event handlers.

My javascript code looks like the following...

var isDrawn;
var table = $("#CustomField-443c6726-05c3-48d0-8dfe-c1a26d749cdf").DataTable();

table.on('preDraw', function () {
    console.log('Redraw started at: ' + new Date().getTime());
    isDrawn = false;
});

table.on('draw', function () {
    console.log('Redraw finished at: ' + new Date().getTime());
    isDrawn = true;
});

function returnDrawStatus() {
    console.log(isDrawn);
    return isDrawn;
};

If I paste this into the console window in chrome dev tools, it seems the event handlers are attached, and I can access the 'isDrawn' variable and the 'returnDrawStatus()' function.

When I try to do the same thing using the selenium javascript executor, the event handlers are attached (evidenced by the console log messages if I pause my selenium script and reorder the table), but I can't return the value of 'IsDrawn' as I get the following error..

System.InvalidOperationException occurred
  HResult=0x80131509
  Message=unknown error: returnDrawStatus is not defined

This is the method that executes the javascript, with the value of script string variable being returnDrawStatus();

internal static bool ExecuteJavascriptReturn(IWebDriver driver, string script)
{
    var result = ((IJavaScriptExecutor) driver).ExecuteScript(script);
    return (bool)result;
}

TLDR

  • Why if I defined variables and functions in a script are they not accessible with the selenium javascript executor at a later point in time? (no page refresh)
  • How can I wait for this particular event handler to be trigger via selenium

Thanks

javascript
c#
selenium
asked on Stack Overflow Oct 31, 2017 by Konzy262 • edited Oct 31, 2017 by Amy

1 Answer

0

C# + Selenium WebDriver

((IJavaScriptExecutor)driver).ExecuteScript("window.foo = false;");
answered on Stack Overflow Oct 31, 2017 by Bzzoiro

User contributions licensed under CC BY-SA 3.0