How to create a class for textbox elements
One of the things that everyone notices soon after starting to work with Selenium library is that the same interface, WebElement, is being used for interacting with all types of elements.
The same method is being used for clicking a link, or a button, or a textbox or a radiobutton or a checkbox:
WebElement element = driver.findElement(elementId);
element.click();
The same method is used for checking if an element is displayed:
boolean isDisplayed = element.isDisplayed();
This applies to other methods such as isEnabled(), getText(), getAttribute().
There is a type of element, textbox, that has specific methods such as clear() and sendKeys() but these are also used with the WebElement interface:
WebElement textbox = driver.findElement(textboxId);
textbox.click();
textbox.clear();
textbox.sendKeys(keyword);
textbox.sendKeys(Keys.ENTER);
textbox.sendKeys(Keys.TAB);
Sometimes, code would look better if we could have classes created for specific types of elements such as textbox:
TextBox textbox = new TextBox();
textbox.select();
textbox.clear();
textbox.type(keyword);
textbox.pressEnter();
textbox.pressTab();
Or
TextBox textbox = new TextBox();
textbox.select();
textbox.clear().type(keyword).pressEnter().pressTab();
How can we do this?
Keep reading with a 7-day free trial
Subscribe to Selenium For Beginners to keep reading this post and get 7 days of free access to the full post archives.