0

I inherited a test automation suite that I have built a lot on top of, however I am unclear on how to migrate the current setup to use drivers past the chromedriver. I'd like to also use IE, FF, and Edge if possible. I've Googled a significant amount and haven't seemed to find a scenario that works perfectly for me - I'd really appreciate a senior set of eyes on the below code. I'm pasting all of the sections I believe relevant:

This is in a class called BrowserManage.cs

using System;
using System.Diagnostics;
using System.Linq;
using OpenQA.Selenium;

namespace AutomationPackage.Infrastructure
{
public class BrowserManage
{
    public void TearDownBrowser(IWebDriver currentBrowser)
    {
        CloseBrowser(currentBrowser);
    }

    protected void CloseBrowser(IWebDriver currentBrowser)
    {
        currentBrowser.Quit();
        KillChromeDriver();
    }

    private static void KillChromeDriver()
    {
        var processes = Process.GetProcessesByName("chromedriver");
        foreach (var process in processes.Where(process => !process.HasExited))
            try
            {
                process.Kill();
            }
            catch (Exception)
            {
                process.Close();
            }
    }
}
}

This line is in app.config

<appSettings>
<add key="BrowserType" value="Chrome" />

This is in a BrowserUtilitiesPageObject.cs file which I have heavily cleaned up EXCEPT the browserdriver portion:

using System;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using AutomationPackage.Infrastructure;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;

namespace AutomationPackage.Page_Objects
{
public class BrowserUtilitiesPageObject
{
    public static IWebDriver CurrentBrowser
    {
        get; private set;
    }

    public static void SetUpBrowser(string browserName = "")
    {
        CurrentBrowser = new ChromeDriver();
        CurrentBrowser.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(15);
    }

    public static IWebDriver CurrentBrowserInstance()
    {
        return CurrentBrowser;
    }

As always clarifications/more information can be made available and I greatly appreciate the help. The above are snippets that are fairly heavily redacted, I was trying to limit down to pertinent points only. Thanks!

1

As per the code shared,I think in your SetUpBrowser function, you may implement a switch block where you will initialize the specific browser instance of browser type passed from config file.

In the same block, you will also add any browser specific steps/settings after object intialization.

After that, rest of the framework does not need to know which specific browser it is working on.All it should interact with driver instance(CurrentBrowser)

1

You can extend your code to run any browser you have webdriver files for. That's all you need. Webdrivers can send commands to their respective browsers. You may need to find and download additional webdrivers.

Where is the part you read the value of BrowserType from app.config? If there isn't, you should implement it.

Modify your BrowserUtilitiesPageObject class so that it doesn't have ChromeDriver hardcoded (and anywhere else where you have ChromeDriver as dependency). I believe you are able to do this from here, since your code doesn't seem very bad.

BrowserManage class doesn't seem perfect, but I guess that it works is what matters, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.