10 Advanced WebDriver Tips and Tricks Part 3

10 Advanced WebDriver Tips and Tricks Part 3

As you probably know I am developing a series posts called- Pragmatic Automation with WebDriver. They consist of tons of practical information how to start writing automation tests with WebDriver. Also, contain a lot of more advanced topics such as automation strategies, benchmarks and researches. In the next couple of publications, I am going to share with you some Advanced WebDriver usages in tests. Without further ado, here are the today’s advanced WebDriver Automation tips and trips.

1. Start FirefoxDriver with Plugins

Just use the AddExtension of the FirefoxProfile class to load the desired extension.

FirefoxProfile profile = new FirefoxProfile();
profile.AddExtension(@"C:extensionsLocationextension.xpi");
IWebDriver driver = new FirefoxDriver(profile);

2. Set HTTP Proxy ChromeDriver

The configuration of a proxy for ChromeDriver is a little bit different from the one for  FirefoxDriver. You need to use the special Proxy class in combination with ChromeOptions.

ChromeOptions options = new ChromeOptions();
var proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.HttpProxy =
proxy.SslProxy = "127.0.0.1:3239";
options.Proxy = proxy;
options.AddArgument("ignore-certificate-errors");
IWebDriver driver = new ChromeDriver(options);

Digital security finger print scan in blue and black

3. Set HTTP Proxy with Authentication ChromeDriver

The only difference from the previous example is the configuration of the —proxy-server argument.

ChromeOptions options = new ChromeOptions();
var proxy = new Proxy();
proxy.Kind = ProxyKind.Manual;
proxy.IsAutoDetect = false;
proxy.HttpProxy =
proxy.SslProxy = "127.0.0.1:3239";
options.Proxy = proxy;
options.AddArguments("--proxy-server=http://user:password@127.0.0.1:3239");
options.AddArgument("ignore-certificate-errors");
IWebDriver driver = new ChromeDriver(options);

While ago when we were working on the first version of the BELLATRIX test automation framework, I did this research and afterward we used many of the tricks in lots of the features of our solution.

4. Start ChromeDriver with an Unpacked Extension

Chrome extensions can be either packed or unpacked. Packed extensions are a single file with a .crx extension. Unpacked Extensions are a directory containing the extension, including a manifest.json file. To load an unpacked extension you need to set the load-extension argument.

ChromeOptions options = new ChromeOptions();
options.AddArguments("load-extension=/pathTo/extension");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(ChromeOptions.Capability, options);
DesiredCapabilities dc = DesiredCapabilities.Chrome();
dc.SetCapability(ChromeOptions.Capability, options);
IWebDriver driver = new RemoteWebDriver(dc);

Create Jenkins Job for Creating NuGet Packages

5. Start ChromeDriver with an Packed Extension

Instead of setting the load-extension argument, you need to use the AddExtension method of the ChromeOptions class to set the path to your packed extension.

ChromeOptions options = new ChromeOptions();
options.AddExtension(Path.GetFullPath("local/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(ChromeOptions.Capability, options);
DesiredCapabilities dc = DesiredCapabilities.Chrome();
dc.SetCapability(ChromeOptions.Capability, options);
IWebDriver driver = new RemoteWebDriver(dc);

6. Assert a Button Enabled or Disabled

You can check if an element is disabled through the Enabled property of the IWebElement interface.


public void AssertButtonEnabledDisabled()
{
    this.driver.Navigate().GoToUrl(@"http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_button_disabled");
    this.driver.SwitchTo().Frame("iframeResult");
    IWebElement button = driver.FindElement(By.XPath("/html/body/button"));
    Assert.IsFalse(button.Enabled);
}

7. Set and Assert the Value of a Hidden Field

You can get the value of a hidden field through the GetAttribute method, part of the IWebElement interface. Get the value attribute of the element. You can set the same attribute with a little bit of a JavaScript code.


public void SetHiddenField()
{
    ////<input type="hidden" name="country" value="Bulgaria"/>
    IWebElement theHiddenElem = driver.FindElement(By.Name("country"));
    string hiddenFieldValue = theHiddenElem.GetAttribute("value");
    Assert.AreEqual("Bulgaria", hiddenFieldValue);
    ((IJavaScriptExecutor)driver).ExecuteScript(
    "arguments[0].value='Germany';",
    theHiddenElem);
    hiddenFieldValue = theHiddenElem.GetAttribute("value");
    Assert.AreEqual("Germany", hiddenFieldValue);
}

8. Wait AJAX Call to Complete Using JQuery

jQuery.active is a variable JQuery uses internally to track the number of simultaneous AJAX requests. Wait until the value of jQuery.active is zero. Then continue with the next operation.

Download File

9. Verify File Downloaded ChromeDriver

To change the default download directory of the current Chrome instance set the download.default_directory argument. When we initiate the file download, we use the WebDriverWait to wait until the file exists on the file system. Finally, we assert the file size. The whole code is surrounded with a try-finally block. In the finally we delete the downloaded file so that our test to be consistent every time.


public void VerifyFileDownloadChrome()
{
    string expectedFilePath = @"c:tempTesting_Framework_2015_3_1314_2_Free.exe";
    try
    {
        String downloadFolderPath = @"c:temp";
        var options = new ChromeOptions();
        options.AddUserProfilePreference("download.default_directory", downloadFolderPath);
        driver = new ChromeDriver(options);
        driver.Navigate().GoToUrl("https://www.telerik.com/download-trial-file/v2/telerik-testing-framework");
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
        wait.Until((x) =>
        {
            return File.Exists(expectedFilePath);
        });
        FileInfo fileInfo = new FileInfo(expectedFilePath);
        long fileSize = fileInfo.Length;
        Assert.AreEqual(4326192, fileSize);
    }
    finally
    {
        if (File.Exists(expectedFilePath))
        {
            File.Delete(expectedFilePath);
        }
    }
}

10. Verify File Downloaded FirefoxDriver

The code for FirefoxDriver is similar to the one for ChromeDriver. We need to set a few arguments so that the browser not to ask us every time where to save the specified file.


public void VerifyFileDownloadFirefox()
{
    string expectedFilePath = @"c:tempTesting_Framework_2015_3_1314_2_Free.exe";
    try
    {
        String downloadFolderPath = @"c:temp";
        FirefoxProfile profile = new FirefoxProfile();
        profile.SetPreference("browser.download.folderList", 2);
        profile.SetPreference("browser.download.dir", downloadFolderPath);
        profile.SetPreference("browser.download.manager.alertOnEXEOpen", false);
        profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", "application/msword, application/binary, application/ris, text/csv, image/png, application/pdf, text/html, text/plain, application/zip, application/x-zip, application/x-zip-compressed, application/download, application/octet-stream");
        this.driver = new FirefoxDriver(profile);
        driver.Navigate().GoToUrl("https://www.telerik.com/download-trial-file/v2/telerik-testing-framework");
        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
        wait.Until((x) =>
        {
            return File.Exists(expectedFilePath);
        });
        FileInfo fileInfo = new FileInfo(expectedFilePath);
        long fileSize = fileInfo.Length;
        Assert.AreEqual(4326192, fileSize);
    }
    finally
    {
        if (File.Exists(expectedFilePath))
        {
            File.Delete(expectedFilePath);
        }
    }
}
public void WaitForAjaxComplete(int maxSeconds)
{
    bool isAjaxCallComplete = false;
    for (int i = 1; i <= maxSeconds; i++)
    {
        isAjaxCallComplete = (bool)((IJavaScriptExecutor)driver).
        ExecuteScript("return window.jQuery != undefined && jQuery.active == 0");
        if (isAjaxCallComplete)
        {
            return;
        }
        Thread.Sleep(1000);
    }
    throw new Exception(string.Format("Timed out after {0} seconds", maxSeconds));
}
public void WaitForAjaxComplete(int maxSeconds)
{
    bool isAjaxCallComplete = false;
    for (int i = 1; i <= maxSeconds; i++)
    {
        isAjaxCallComplete = (bool)((IJavaScriptExecutor)driver).
        ExecuteScript("return window.jQuery != undefined && jQuery.active == 0");
        if (isAjaxCallComplete)
        {
            return;
        }
        Thread.Sleep(1000);
    }
    throw new Exception(string.Format("Timed out after {0} seconds", maxSeconds));
}

Related Articles

Web Automation

Design Grid Control Automated Tests Part 3

In my previous articles Design Grid Control Automated Tests Part 1 and Design Grid Control Automated Tests Part 2 I started a mini-series about writing decent g

Design Grid Control Automated Tests Part 3

Web Automation

10 Advanced WebDriver Tips and Tricks Part 1

As you probably know I am developing a series posts called- Pragmatic Automation with WebDriver. They consist of tons of practical information how to start writ

10 Advanced WebDriver Tips and Tricks Part 1

Web Automation

Selenium C# NUnit Test Automating Angular, React, VueJS and 20 More

In the new article from the Web Automation Series with C#, we will talk about creating a data-driven NUnit test automating all major web technologies such as Re

Selenium C# NUnit Test Automating Angular, React, VueJS and 20 More

Resources, Web Automation

Most Exhaustive WebDriver Locators Cheat Sheet

As you know, I am keen on every kind of automation especially related to web technologies. So, I enjoy using Selenium WebDriver. You can find lots of materials

Most Exhaustive WebDriver Locators Cheat Sheet

Resources, Web Automation

Most Complete Selenium WebDriver C# Cheat Sheet

As you know, I am a big fan of Selenium WebDriver. You can find tonnes of useful code in my WebDriver Series. I lead automated testing courses and train people

Most Complete Selenium WebDriver C# Cheat Sheet

Web Automation

Selenium C# MSTest Test Automating Angular, React, VueJS and 20 More

In the new article from the Web Automation Series with C#, we will talk about creating a data-driven MSTest test automating all major web technologies such as R

Selenium C# MSTest Test Automating Angular, React, VueJS and 20 More
Anton Angelov

About the author

Anton Angelov is Managing Director, Co-Founder, and Chief Test Automation Architect at Automate The Planet — a boutique consulting firm specializing in AI-augmented test automation strategy, implementation, and enablement. He is the creator of BELLATRIX, a cross-platform framework for web, mobile, desktop, and API testing, and the author of 8 bestselling books on test automation. A speaker at 60+ international conferences and researcher in AI-driven testing and LLM-based automation, he has been recognized as QA of the Decade and Webit Changemaker 2025.