-3
\$\begingroup\$

I'm trying to create my first Selenium 'framework' using C# (more of a way of practising and developing my skills).

I have created a class in which I am creating a new browser instance:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;


namespace SeleniumDemo.Utilities
{
    class WebDriverFactory
    {
        public static IWebDriver driver { get; set; }
        public static IWebDriver CreateBrowser(BrowserType name)
        {
            switch (name)
            {
                case BrowserType.Chrome:
                    driver = new ChromeDriver();
                    driver.Manage().Window.Maximize();
                    break;
            }
            return driver;

        }

        public static void Destroy()
        {
            driver.Quit();
        }
    }
}

So that I can access it in my test thus:

[TestInitialize]
        public void TestSetup()
        {
            WebDriverFactory.CreateBrowser(BrowserType.Chrome);
        }

        [TestCleanup]
        public void TestEnd()
        {
            WebDriverFactory.Destroy();
        }

But then, when I want to access my driver instance in a test, I am inheriting from the factory:

class LocateElement : WebDriverFactory
    {
        public static void ClickElement(ElementType name, string element)
        {
            switch (name)
            {
                case ElementType.Id:
                    driver.FindElement(By.Id(element)).Click();
                    break;

            }
        }
    }

Is this the best way to do it? I have tried storing the initially created value in a variable and passing it around. But it doesn't seem to work.

Here is a my full testcase file:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using SeleniumDemo.Utilities;
using OpenQA.Selenium;
using SeleniumDemo.ElementActions;

namespace SeleniumDemo
{
    [TestClass]
    public class UnitTests
    {
        [TestInitialize]
        public void TestSetup()
        {
            WebDriverFactory.CreateBrowser(BrowserType.Chrome);
            WebDriverHelpers.NavigateToPage(PageName.Elements);
        }

        [TestCleanup]
        public void TestEnd()
        {
            WebDriverFactory.Destroy();
        }


        [TestMethod]
        public void ClickTest()
        {
            // Find link by Id
            // LocateElement.ClickElement(ElementType.Id, "simpleElementsLink");

            // Find link by className
            //LocateElement.ClickElement(ElementType.ClassName, "et_pb_blurb_description");

            // Find link by css selector
            //LocateElement.ClickElement(ElementType.CssSelector, "#simpleElementsLink");

            // Find link by link text
            //LocateElement.ClickElement(ElementType.LinkText, "Click this link");

            // Find link by partial link text
            //LocateElement.ClickElement(ElementType.PartialLinkText, "Click this l");

            // Find link by XPath
            LocateElement.ClickElement(ElementType.XPath, @"//*[@id=""simpleElementsLink""]");



        }
    }
}

And here is the custom method I am using to call the LocateElement.ClientElement() method.

using System;
using OpenQA.Selenium;
using SeleniumDemo.Utilities;

namespace SeleniumDemo.ElementActions

{
    class LocateElement : WebDriverFactory
    {
    public static void ClickElement(ElementType name, string element)
    {
        switch (name)
        {
            case ElementType.Id:
                driver.FindElement(By.Id(element)).Click();
                break;
            case ElementType.ClassName:
                driver.FindElement(By.ClassName(element)).Click();
                break;
            case ElementType.CssSelector:
                driver.FindElement(By.CssSelector(element)).Click();
                break;
            case ElementType.LinkText:
                driver.FindElement(By.LinkText(element)).Click();
                break;
            case ElementType.PartialLinkText:
                driver.FindElement(By.PartialLinkText(element)).Click();
                break;
            case ElementType.XPath:
                driver.FindElement(By.XPath(element)).Click();
                break;
        }
    }
}

}

I feel this is a bit 'hacky', so if there is a better way. Please let me know!

\$\endgroup\$

closed as off-topic by dfhwze, t3chb0t, IEatBagels, 200_success, VisualMelon Aug 15 at 11:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." – dfhwze, t3chb0t, IEatBagels, VisualMelon
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    \$\begingroup\$ Could you include the unit tests so we can review how you use this driver? \$\endgroup\$ – dfhwze Aug 14 at 11:25
  • 3
    \$\begingroup\$ Welcome to Code Review! The current question title, which states your concerns about the code, is too general to be useful here. Please edit to the site standard, which is for the title to simply state the task accomplished by the code. Please see How to get the best value out of Code Review: Asking Questions for guidance on writing good question titles. \$\endgroup\$ – Toby Speight Aug 14 at 16:53
  • \$\begingroup\$ The question could do with a written explanation of the purpose of the code. Test cases are great, but a description is important for making sure we understand why you designed the code the way you did. \$\endgroup\$ – VisualMelon Aug 15 at 11:25
0
\$\begingroup\$
  1. Remove static modifier from IWebDriver instances, my expectation is that at some point you will need to run your tests in multi-threaded manner to speed up execution or perform cross-browser testing and if the IWebDriver will be static - it will be shared between different threads causing race conditions
  2. Default section is missing from your switch statement
  3. Consider refactoring your code to use Page Object Model Design Pattern, it will allow you to separate test logic from UI elements representation making tests faster to develop and easier to maintain. Take a look at Page Object Model with C# for more details and example implementation.
\$\endgroup\$

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