While using Selenium 2, I have following statements scattered in test methods –
driver.findElement(By.name(usernameBox)).sendKeys(userEmailAddress);
I think of abstracting them in static methods of a WebUtil class –
public class WebUtils {
public static void type(WebDeriver driver, String locator, String testData) {
driver.findElement(By.name(locator)).sendKeys(testData);
}
}
And the method call would be –
WebUtils.type(driver, usernameBox, userEmailAddress);
Could I improve it more, for example if I could avoid passing driver object every time, or some thing more?
I would suggest you extend the base class that you generally use and add your extension methods there.
here is an example to extend Selenium class.
}
You initialize it as follows in a common class that is used by all your tests:
Now you invoke type as follows:
P.S: Srry the above code is in c# but you can port it over to java..