Introduction
Katalon Studio’s ConditionType enum is a key feature that allows testers to define specific criteria for interacting with web elements. This flexibility is crucial for creating robust and reliable automated tests. The ConditionType enum provides a variety of condition types, each serving a unique purpose in the identification and interaction with web elements.
We’ll understand various condition types available in the ConditionType enum, illustrating their usage with practical examples. By the end of this post, you’ll have understanding of how to leverage these condition types to enhance your Katalon Studio projects.
Â
ConditionType Overview
CONTAINS
The CONTAINS condition type checks if a web element’s attribute or text content includes a specific value. This is particularly useful for verifying that a certain string or substring exists within the element’s content.
TestObject button = new TestObject(“submitButton”);
button.addProperty(“xpath”, ConditionType.CONTAINS, “//button[contains(text(), ‘Submit’)]”);
Â
Example: Suppose you want to check if a button’s text contains the word “Submitâ€. You can use the CONTAINS condition type as follows:
In this example, Katalon Studio will look for a button element whose text contains the word “Submitâ€.
Â
ENDS_WITH
The ENDS_WITH condition type is used to verify if a web element’s attribute or text content ends with a particular value. This is useful for checking specific suffixes in an element’s content.
Example: To check if a link’s URL ends with a .pdf extension, you can use the ENDS_WITH condition type:
TestObject link = new TestObject(“pdfLink”);
link.addProperty(“href”, ConditionType.ENDS_WITH, “.pdf”);
Â
Here, Katalon Studio will identify link elements whose href attribute ends with .pdf.
Â
EQUALS
The EQUALS condition type is employed to verify if a web element’s attribute or text content exactly matches a specific value. This is useful for precise match comparisons.
Example: To check if a text input field’s value is exactly “John Doeâ€, use the EQUALS condition type:
TestObject nameField = new TestObject(“nameInput”);
nameField.addProperty(“value”, ConditionType.EQUALS, “John Doe”);
Â
In this scenario, Katalon Studio will search for an input field whose value attribute is exactly “John Doeâ€.
EXPRESSION
The EXPRESSION condition type allows for the use of custom expressions to define the matching criteria for a web element. This is a powerful feature for creating more complex and specific conditions.
Example: To check if a table cell’s value is a valid email address, you can use the EXPRESSION condition type with a regular expression:
TestObject emailCell = new TestObject(“emailTableCell”);
emailCell.addProperty(“text”, ConditionType.EXPRESSION, “^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$”);
Â
In this example, Katalon Studio will look for table cells whose text matches the given email address pattern.
Â
Practical Examples
To solidify your understanding, let’s explore more comprehensive practical examples that demonstrate how to use these condition types in real-world scenarios.
Â
Scenario 1: Verify Login Button Presence
Suppose you’re testing a login page and need to verify the presence of a login button that contains the text “Loginâ€.
TestObject loginButton = new TestObject(“loginButton”);
loginButton.addProperty(“xpath”, ConditionType.CONTAINS, “//button[contains(text(), ‘Login’)]”);
WebUI.verifyElementPresent(loginButton, 10);
Â
Scenario 2: Check File Download Link
You need to verify that a download link for a PDF file is correctly displayed on a page.
TestObject pdfDownloadLink = new TestObject(“pdfDownloadLink”);
pdfDownloadLink.addProperty(“href”, ConditionType.ENDS_WITH, “.pdf”);
WebUI.verifyElementPresent(pdfDownloadLink, 10);
Â
Best Practices
To make the most of the ConditionType enum in Katalon Studio, consider the following best practices:
Use Descriptive Names: Name your TestObject instances clearly to reflect their purpose.
Combine Conditions: Use multiple properties and condition types to refine element identification.
Validate Regularly: Regularly validate your tests to ensure condition types are correctly implemented.
Optimize Performance: Avoid overly complex expressions that might impact test execution performance.
Document Your Tests: Provide comments and documentation for complex condition types to aid future maintenance.
Â
Conclusion
The ConditionType enum in Katalon Studio provides a versatile set of predefined condition types that significantly enhance your ability to define criteria for identifying and interacting with web elements. By understanding and effectively utilizing these condition types, you can create more robust and flexible automated tests.
Source: Read MoreÂ