I am migrating test scripts from Selenium RC to WebDriver. The biggest issue so far is that our extension classes are having to be rewritten to use the WebDriver API, which I don’t mind, but the By class is giving me issues, mostly because you have to explicitly state what type of element you are using when you call the FindElements method. So, instead of a single click method that implicitly determines the element type, I made a method for each element type (css, XPath, name, id, etc.), which look like this:
public void ClickByXPath(string xpath) { _driver.FindElement(By.XPath(xpath)).Click(); }
This works, but I am having to create separate methods for each element type a lot (for GetText, IsElementPresent, etc.). When writing the scripts it gets old typing the names for each method, since they are long and I have to check to see what type of element is being used as a parameter. I would like to have something like this…
public void Click(XPath locator) { ... }
public void Click(Name locator) { ... }
However, I am not sure the best way to go about this with WebDriver. Any suggestions would be greatly appreciated. Thanks in advance.
You can use an
EnumSelectorType{Xpath,Css, etc..} and use one click method which has (Stringargument,SelectorTypetype) and usingswitch-caseto perform different actions depending on the type.