The Element Object Mannequin (COM) is a design sample that permits you to construction assessments in check automation initiatives. Impressed by the favored Web page Object Mannequin (POM), COM goes past dealing with whole pages and focuses on particular UI elements, resembling buttons, textual content fields, dropdown menus, or different reusable components.
On this tutorial, we are going to clarify how one can implement COM to check an online utility with Selenium WebDriver and Cucumber and the way this strategy could make your assessments extra versatile, modular, and simpler to take care of.
What Is the Element Object Mannequin?
The Element Object Mannequin (COM) is an evolution of the POM model. As a substitute of modeling a whole web page as an object with strategies interacting with all of the web page components, COM breaks the consumer interface into particular person elements, resembling:
- Buttons
- Textual content fields
- Dropdown menus
- Search bars
- Tables
Every element is encapsulated in a Java class, and its interactions are managed by particular strategies, permitting every ingredient to be maintained and examined independently. This improves code reusability, upkeep, and adaptability in assessments.
Why Use COM As a substitute of POM?
1. Elevated Modularity
With COM, every element (like a button or textual content discipline) is an unbiased entity. If a element adjustments (for instance, a button’s textual content adjustments), you solely want to switch the element class with out affecting different elements or pages. This enables for top modularity and prevents the necessity to modify whole pages for minor changes.
2. Reusability and Flexibility
Parts could be reused throughout a number of pages of the applying, lowering code duplication. For instance, a ButtonComponent
can be utilized on any web page the place the button seems, lowering redundancy and rising check flexibility.
3. Simplified Upkeep
Upkeep is less complicated with COM. If a element adjustments (for instance, a button or textual content discipline), you solely want to switch the category representing that element. All assessments utilizing that element will probably be routinely up to date with out having to revisit every check situation.
4. Tailored to Dynamic Interfaces
Trendy functions are sometimes dynamic, with interfaces that change ceaselessly. COM is right for such functions as a result of it permits the testing of elements unbiased of the interface. You’ll be able to modify or add elements with out affecting assessments for different elements of the applying.
5. Sooner Check Automation
COM quickens check automation. By centralizing interactions with elements into reusable lessons, testers don’t have to redefine actions for each web page or element. For example, a single-step definition for the motion “click on a button” can be utilized throughout all assessments within the challenge, considerably lowering the time wanted to automate assessments.
6. Avoiding Repetition of Work Between Testers
With COM, testers now not have to repeat the identical work for each check. Centralized step definitions for widespread actions, resembling “click on a button” or “enter textual content,” can be utilized by all testers within the challenge. This ensures consistency in assessments whereas avoiding pointless repetition, bettering effectivity and collaboration amongst testers.
COM Undertaking Structure
The structure of a COM-based challenge is structured round three primary components: elements, step definitions, and the runner.
1. Parts
Every element represents a UI ingredient, resembling a button, textual content discipline, or dropdown menu. These lessons encapsulate all doable interactions with the ingredient.
Right here’s an instance of the ButtonComponent
class:
public class ButtonComponent {
personal WebDriver driver;
public ButtonComponent(WebDriver driver) {
this.driver = driver;
}
public void clickButton(String buttonText) {
WebElement button = driver.findElement(By.xpath("//button[text()='" + buttonText + "']"));
button.click on();
}
}
2. Step Definitions
Step definitions hyperlink the steps outlined within the Gherkin information to the strategies within the elements. They’re answerable for interacting with the elements and implementing the actions specified within the check situation.
Right here’s an instance of ButtonStepDefinition
:
public class ButtonStepDefinition {
personal ButtonComponent buttonComponent;
public ButtonStepDefinition(WebDriver driver) {
this.buttonComponent = new ButtonComponent(driver);
}
@When("I click on on the button {string}")
public void iClickOnTheButton(String buttonText) {
buttonComponent.clickButton(buttonText);
}
}
3. Runner
The runner class is answerable for operating the assessments utilizing JUnit or TestNG. It configures Cucumber to load the check situations outlined within the .characteristic
information and run them utilizing the step definitions.
Right here’s an instance of TestRunner
:
@RunWith(Cucumber.class)
@CucumberOptions(
options = "src/check/sources/options",
glue = "com.componentObjectModel.stepDefinitions",
plugin = {"fairly", "html:goal/cucumber-reports.html"}
)
public class TestRunner {
}
Writing and Explaining a Gherkin State of affairs
One of many important components of automation with Cucumber is utilizing the Gherkin language to put in writing check situations. Gherkin permits you to describe assessments in a readable and comprehensible means, even for non-technical folks.
Let’s think about a situation the place we need to check the interplay with a button utilizing the ButtonComponent
we outlined earlier. Right here’s the way it is perhaps written in Gherkin:
State of affairs: Person clicks on the "Submit" button Given I'm on the login web page When I click on on the button "Submit" Then I must be redirected to the homepage
Clarification of the State of affairs
State of affairs
This situation describes the motion the place a consumer clicks the ‘Submit’ button on the login web page and ensures they’re redirected to the homepage after clicking.
- Given I’m on the login web page: The preliminary state of the check is that the consumer is on the login web page.
- Once I click on on the button “Submit“: The motion carried out within the check is clicking the ‘Submit’ button.
- Then, I must be redirected to the homepage: The anticipated verification is that the consumer is redirected to the homepage after clicking the button.
Hyperlink With COM
Every step on this situation is mapped to a step definition in ButtonStepDefinition
, the place the clicking motion is dealt with by the ButtonComponent
, making the check modular and straightforward to take care of.
Further Clarification
Discover that the steps take the displayed textual content on the buttons (or placeholders in enter fields, and many others.) as parameters. This makes the situations extra readable and generic. For instance, within the situation above, the textual content of the button “Submit” is used immediately within the step “Once I click on on the button ‘Submit.’” On this means, the identical step definition could possibly be reused to check one other button, resembling “Login,” by merely altering the textual content within the Gherkin situation. This improves the reusability of the check code whereas making the situations extra intuitive and versatile.
Reusability of Steps With COM
One of many key benefits of COM is the reusability of step definitions for various buttons. For instance, the identical step Once I click on on the button {string}
can be utilized for all buttons, whatever the textual content displayed on the button. The COM strategy permits you to dynamically parameterize the clicking motion based mostly on the button textual content.
Let’s think about one other situation with a unique button:
State of affairs
Person clicks on the "Login" button
Given I'm on the login web page
When I click on on the button "Login"
Then, I must be redirected to the dashboard
In each circumstances, the identical clickButton
methodology within the ButtonComponent
will probably be used, however the button textual content will change relying on the situation. This demonstrates how COM permits reusing the identical element and step definition, making assessments versatile and modular.
How COM Improves Check Automation
COM improves check automation in a number of methods:
- Discount of code duplication: By utilizing reusable elements, you scale back code duplication in assessments. For instance, a
ButtonComponent
used on a number of pages of the applying eliminates the necessity to write repetitive assessments. - Extra readable and modifiable assessments: Checks are clearer and simpler to know as they’re decoupled from page-specific implementation particulars. You’ll be able to concentrate on interacting with elements with out worrying concerning the underlying web page construction.
- Ease of upkeep: Any modification to a element (e.g., a button textual content change) solely impacts the element class, not the assessments. This makes upkeep a lot less complicated and sooner.
- Extra versatile assessments: Checks can simply be tailored to altering UIs, as elements are unbiased of one another. You’ll be able to check new elements or change current ones with out affecting different assessments.
When Is the COM Design Sample Advisable?
This design sample is advisable if the net utility being examined makes use of a unified design system for all elements. For instance, if all buttons are declared in the identical means, with a constant construction for all UI components. In such circumstances, utilizing COM permits you to centralize interactions with every sort of element (resembling buttons, textual content fields, and many others.), making assessments extra modular, reusable, and simpler to take care of.
Reusing Step Definitions Throughout A number of Merchandise
In case you are creating and testing a number of merchandise that use the identical design system, you possibly can create a library that encompasses all of the step definitions and actions (or virtually all) for every element. This enables testers to focus solely on writing Gherkin situations, and the assessments will probably be automated. Since all internet components are written in the identical means (HTML), elements like buttons, textual content fields, and different UI components will behave constantly throughout all initiatives.
Because of this, testers now not have to repeat the identical duties — defining step definitions and actions for an identical elements throughout a number of initiatives. This strategy saves time and improves maintainability. If an online ingredient is modified (for instance, if the XPath adjustments), you solely have to replace that ingredient within the shared library, and the modification will probably be routinely utilized to all automation initiatives. This reduces the chance of errors and makes updates extra environment friendly throughout totally different merchandise.
Conclusion
The Element Object Mannequin (COM) is a strong design sample for organizing check automation. By specializing in reusable elements, COM permits you to create extra modular, maintainable, and scalable assessments. This sample is especially helpful for contemporary functions, the place the consumer interface adjustments rapidly, and the place interacting with unbiased elements is important.
With reusable assessments, simplified upkeep, and a versatile structure, COM is the best resolution for check automation in Selenium and Cucumber initiatives.