Introduction
ZK Webdriver
ZK Webdriver is a powerful testing utility library designed specifically for ZK applications. Built on top of the industry-standard Selenium WebDriver, it provides a high-level API that simplifies testing ZK applications by abstracting away the complexities of AJAX and ZK component internals.
Why ZK Webdriver?
While Selenium is a great tool for general web testing, it often requires manual handling of AJAX requests and complex CSS selectors to interact with ZK’s rich UI components. ZK Webdriver addresses these challenges by providing:
- AJAX Awareness: ZK applications are highly interactive and often use AJAX to update parts of the page without a full refresh. Standard Selenium tests can struggle with these asynchronous updates, leading to “element not found” errors if they try to interact with elements before they are rendered or updated. ZK Webdriver’s built-in mechanisms, such as
waitResponse(), automatically synchronize with ZK’s server-side processing, ensuring your tests interact with a stable DOM.
Example (Without ZK Webdriver):
// Raw Selenium: might fail if the update takes time
driver.findElement(By.id("myButton")).click();
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("updatedContent")));
// ... continue test
Example (With ZK Webdriver):
// ZK Webdriver: automatically waits for ZK's AJAX response
click(to</code>(By.id("myButton")));
waitResponse(); // waits for the ZK AJAX response to complete
// Now 'updatedContent' is guaranteed to be ready
Assert.assertTrue(isElementPresent(to</code>(By.id("updatedContent"))));
- ZK Component Integration: ZK Webdriver provides easy access to ZK’s client-side widget objects and their properties. This allows you to interact with ZK components using their logical ZK IDs and methods, rather than relying on the generated, often complex, HTML IDs or class names. This makes tests more readable, stable, and less dependent on the underlying DOM structure.
Example (Without ZK Webdriver):
// Raw Selenium: interacting with a ZK Textbox might require complex selectors
WebElement textbox = driver.findElement(By.cssSelector("input[type='text'][id$='_real']"));
textbox.sendKeys("Hello ZK");
Example (With ZK Webdriver):
// ZK Webdriver: direct interaction with the ZK component
Widget textbox = Widget.get("myTextbox"); // assuming 'myTextbox' is the ZK component ID
textbox.type("Hello ZK");
Assert.assertEquals("Hello ZK", textbox.getValue());
- Simplified Locators: ZK Webdriver extends Selenium’s locator strategies with powerful selectors (
jq,zk) that leverage ZK-specific attributes and component types. This simplifies element identification, especially when dealing with ZK’s dynamically generated HTML.
Example (Without ZK Webdriver):
// Raw Selenium: finding a ZK Button might involve specific CSS classes
WebElement button = driver.findElement(By.cssSelector(".z-button:contains('Submit')"));
button.click();
Example (With ZK Webdriver):
// ZK Webdriver: using zk locator for ZK components
click(jq("~button:contains('Submit')")); // Find a ZK button containing "Submit"
// Or directly by ZK component ID and type
click(zk("$mySubmitButton")); // Find ZK component with ID "mySubmitButton"
-
Standard Selenium Power: Since it’s built on Selenium, you still have full access to all Selenium WebDriver features. This means you can seamlessly combine ZK Webdriver’s specialized capabilities with standard Selenium commands when needed, offering the best of both worlds.
-
Server-Side Environment (e.g., Jetty): ZK applications are inherently client-server; ZK components require a running Java servlet container (like Jetty) to serve pages, process AJAX requests, and execute server-side logic.
zk-webdrivertests often embed a lightweight server (such as Jetty) to provide this essential server-side context, ensuring that ZK components can be tested in an environment that accurately mimics their production deployment. This setup is crucial for components to initialize, render, and react to user interactions correctly.
Relationship with Selenium
ZK Webdriver is not a replacement for Selenium; rather, it is a wrapper and extension of Selenium WebDriver.
+-----------------------------------+
| Your Test Code |
+-----------------------------------+
|
v
+-----------------------------------+
| ZK Webdriver | <-- AJAX synchronization, ZK Widget API
+-----------------------------------+
|
v
+-----------------------------------+
| Selenium WebDriver | <-- Browser control, standard interactions
+-----------------------------------+
|
v
+-----------------------------------+
| Web Browser |
+-----------------------------------+
Suitable Scenarios
ZK Webdriver is ideal for:
- End-to-End (E2E) Testing: Verifying critical user flows across multiple pages and interactions.
- Browser Compatibility: Testing browser-specific behaviors and rendering.
- Integration Testing: Validating the full stack, including database interactions and server-side logic in a real browser environment.
Limitations and Trade-offs
Compared to ZATS (ZK Automated Testing Suite), ZK Webdriver has some trade-offs:
- Performance: It is slower than ZATS because it requires starting a real web server (e.g., Jetty) and a real browser (headless or UI).
- Complexity: Requires more setup (driver management, server configuration) compared to ZATS’s mock environment.
Recommendation: Use ZATS for fast, isolated unit/component testing. Use ZK Webdriver for final verification of critical paths and browser-specific features.