# WebDriverExtensions

This class provides extension methods from the `IWebDriver` interface for easier JavaScript execution on the WebDriver.

## ExecuteScript

{% tabs %}
{% tab title="C#" %}
Executes JavaScript in the context of the currently selected frame or window. If the generic type is omitted than this method will not return any result.

```csharp
driver.ExecuteScript("myJavaScriptMethod();");
```

{% endtab %}

{% tab title="C#" %}
Executes JavaScript in the context of the currently selected frame or window. If the generic type is given than this method will return the result from the JavaScript code. Please note that the result is cast following the same rules that Selenium has for JavaScript results:

* For an HTML element, this method returns a `IWebElement`,
* For a number, a `long` is returned,
* For a boolean, a `bool` is returned,
* For all other cases a `string` is returned,
* For an array, the first element is checked, and the attempt is made to return a `List<T>` of that type, following the rules above. Nested lists are not supported.

```csharp
var result = driver.ExecuteScript<string>("return myJavaScriptMethod();");
```

{% endtab %}
{% endtabs %}

## LoadJQuery

When `JQuerySelector` is used the library will detect if the tested page contains the loaded jQuery library and if jQuery is not present, the library will load it before running the selector. jQuery will be loaded from a CDN from this url: <https://code.jquery.com/jquery-latest.min.js>

In some cases there may be a need to load an alternative version of the library, or to load the library from a different source and for this purpose the `LoadJQuery` method is available.

{% tabs %}
{% tab title="C#" %}
Load a different version of the jQuery library from CDN.

```csharp
driver.LoadJQuery("1.11.1");
```

{% endtab %}

{% tab title="C#" %}
Load the jQuery library from an altenative URL.

```csharp
driver.LoadJQuery(new Uri("https://example.com/jquery.min.js"));
```

{% endtab %}

{% tab title="C#" %}
Specify a timeout for jQuery library load. The timeout can be specified with both version and URL overrides. The default value for the timeout is 10 seconds.

```csharp
driver.LoadJQuery(timeout: TimeSpan.FromSeconds(1));
```

{% endtab %}
{% endtabs %}

## LoadSizzle

When `SizzleSelector` is used the library will detect if the tested page contains the loaded Sizzle library and if Sizzle is not present, the library will load it before running the selector. Sizzle will be loaded from a CDN from this url: <https://cdnjs.cloudflare.com/ajax/libs/sizzle/2.0.0/sizzle.min.js>

In some cases there may be a need to load an alternative version of the library, or to load the library from a different source and for this purpose the `LoadSizzle` method is available.

{% tabs %}
{% tab title="C#" %}
Load a different version of the Sizzle library from CDN.

```csharp
driver.LoadSizzle("2.3.3");
```

{% endtab %}

{% tab title="C#" %}
Load the Sizzle library from an altenative URL.

```csharp
driver.LoadSizzle(new Uri("https://example.com/sizzle.min.js"));
```

{% endtab %}

{% tab title="C#" %}
Specify a timeout for Sizzle library load. The timeout can be specified with both version and URL overrides. The default value for the timeout is 10 seconds.

```csharp
driver.LoadSizzle(timeout: TimeSpan.FromSeconds(1));
```

{% endtab %}
{% endtabs %}
