Design and Extension of a Selenium 2.0 and JUnit 4 Based UI Automation Testing Framework
This article explains how to design and extend a UI automation testing framework using Selenium 2.0 and JUnit 4, covering control identification and encapsulation, test case organization and execution, as well as custom logging and reporting to improve maintainability and efficiency.
Introduction
Although many think that Web UI automation is outdated in the era of mobile apps, the design principles of UI automation frameworks remain the same; the main differences lie in the underlying tools (Selenium for Web, Appium for mobile). The core components are control identification and operation, test case organization and execution, and log/report generation.
Control Identification and Operation
The key Selenium classes involved are WebElement (represents UI controls), Mouse and Keyboard (encapsulate input actions), and WebDriver (provides element search methods). These classes are not object‑oriented enough for efficient script writing, so a custom wrapper hierarchy is recommended.
In the author's project, a set of HTML‑native controls (including Vue and React components) were wrapped into an object‑oriented tree, allowing each control type to expose its specific operations.
Control Encapsulation
Using JavascriptExecutor , a unified JavaScript script can be created to locate elements, separating the search logic from the concrete control implementations. A Driver class is defined to hold the domain and proxy methods for element lookup, and specific lookup methods (by id, text, index, type) are added as needed.
Test Case Organization and Execution
JUnit annotations such as @BeforeClass, @Before, @Test, @After, and @AfterClass control the test lifecycle, but they require methods to be defined in each test class or its parent, which can be cumbersome for common actions like browser cleanup or screenshot capture. Extending the JUnit runner (e.g., BlockJUnit4ClassRunner ) and adding listeners allows dynamic insertion of such operations.
Test Log and Report Generation
Standard test reports lack detailed per‑case information (title, description, priority, duration, logs, checkpoints, exceptions). To address this, a custom @CaseInformation annotation and corresponding listener ( ReportAndLogSetListener ) are introduced, enabling rich metadata collection and custom HTML report generation using the open‑source JavaScript tree library dtree.
The final report consists of an overall structure and detailed case sections, providing a comprehensive view of test execution results.
转转QA
In the era of knowledge sharing, discover 转转QA from a new perspective.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.