mardi 1 décembre 2015

AutoComplete for string parameter

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.

Aucun commentaire:

Enregistrer un commentaire