0

I have set up a testproject using NUnit and Selenium Webdriver of which you can find a shortened version below.

class ByHolder
{
    public readonly string name, path;
    public readonly Func<string, By> call;

    public ByHolder(string name, string path, Func<string, By> call)
    {
        this.name = name;
        this.path = path;
        this.call = call;
    }
}

class Page
{
    private readonly List<ByHolder> LocatorList = new List<ByHolder>();

    public Page()
    {
        SetUpList();
    }

    private void SetUpList()
    {
        AddLocator("Button0", "//button0", By.XPath);
        AddLocator("Button1", "button1", By.Id);
        ...

    }


    public By Get(string locatorName)
    {
        var holder = LocatorList.FirstOrDefault(p => p.name.Equals(locatorName));

        return holder?.call(holder.path);
    }

    public void AddLocator(string name, string path, Func<string, By> call)
    {
        LocatorList.Add(new ByHolder(name, path,call ));
    }


}

class PersonelDriver : IWebDriver
{
    IWebDriver driver = new FirefoxDriver();
    Page page = new Page();


    public void Click(string locatorName)
    {
        driver.FindElement(page.Get(locatorName)).Click();
    }

    ...
}

[TestFixture]
class PageTest
{
    private readonly PersonelDriver d = new PersonelDriver();

    [Test]
    public void ClickTest0()
    {
        d.Click("Button0");
    }
    [Test]
    public void ClickTest1()
    {
        d.Click("Button1");
    }

    ...
}

As you can hopefully see I tried implementing a shortened method with a minimum of variables to make longer testcases easier to read mainly for outsiders but also for me, for example.

d.Click("that");
d.EnterText("thisLocator","text");
d.WaitFor("somethingElse");
d.Click("this");

(After using Selenium for some time I find that things can become chaotic quite fast when repeatedly using the driver.FindElement... in the tests themselves.)

Even tough I'm happy with the shortened versions and readability, there is of course no autocomplete or check since i'm handling strings and not IWebElement objects or By references that have been named or put in a specific getter. What I used to do was the following, but it just felt wrong:

class Locators
{
    public By GetButton()
    {
        return By.Id("button");
    }

    ...
}

I was wondering if there is a way to implement an autocomplete or some other check for the string values when adding for example d.Click("stringvalue");

Thank you in advance.

ImP
  • 175
  • 1
  • 3
  • 12
  • Autocomplete what with what, when and how? Some [other check](http://c2.com/cgi/wiki?ArrangeActAssert)? I have no idea what you're asking. – lloyd Dec 01 '15 at 23:27
  • Sorry for being so unclear. what i mean is, autocomplete like it is the case in wpf when you enter a binding reference. Or a check, which gives a warning when compiling for example, if the given string doesn't match up with one of the name from the list – ImP Dec 02 '15 at 00:16
  • You want to extend the base class WebElement with a Click(string method). [Go for it](http://stackoverflow.com/questions/11642348/extend-selenium-webdriver-webelement). – lloyd Dec 02 '15 at 00:25
  • I thought I already did that;. I want the compiler or my code editor (in my case visual studio) to notify me when a given string value for d.Click("stringValue") is not available in the LocatorList with ByHolder objects from my Page class, specifically the name of all those objects present in the list. – ImP Dec 02 '15 at 00:36
  • Prefer readability and understandability over less typing. Typing "d" vs "driver" doesn't really save you that many keystrokes and everyone is going to know what "driver" is... who knows what "d" stands for. If you look up best practices for most languages, they will say to make your variable names readable and understandable. Modern IDEs will make typing longer names almost instant anyway. – JeffC Dec 02 '15 at 03:16
  • This is a fair point, however, I would have no beef changing d to driver. My goal of readability would still be met. I would just like VS to check if the entered stringvalue corresponds with an existing name value from my list. or let the compiler give me a warning. By now, i'm kind of presuming it's impossible. – ImP Dec 02 '15 at 07:32

0 Answers0