Kevin McDowell
Kevin McDowell

Reputation: 578

Driver.ExecuteJavaScript<object> returns null

If I execute this command from a developer console in Internet Explorer 11,

angular.element('viewer-uigrid').scope().$parent.$parent.reportReq.ReportDefinition.DataSets[0].ColumnDefinitions

I get back an array of objects.

In test automation, if I attempt to execute that same code using:

var colDefs = Driver.ExecuteJavaScript<object>("angular.element('viewer-uigrid').scope().$parent.$parent.reportReq.ReportDefinition.DataSets[0].ColumnDefinitions[0]; ");

The colDefs variable comes back as null. Driver is an object of type OpenQA.Selenium.IE.InternetExplorerDriver and ExecuteJavaScript is an extension available from OpenQA.Selenium.Support.Extensions.

Any idea why the return value in C# is null, but it returns an array from the console?

Upvotes: 1

Views: 211

Answers (1)

Kevin McDowell
Kevin McDowell

Reputation: 578

A co-worker shed light on this. The correct way to get a value back is to use "return" in your java script string so that the value comes back and isn't discarded, like so:

var colDefs = Driver.ExecuteJavaScript<object>("return angular.element('viewer-uigrid').scope().$parent.$parent.reportReq.ReportDefinition.DataSets[0].ColumnDefinitions[0]; ");

Upvotes: 2

Related Questions