Checkpoints Guide

In automated testing, accuracy and efficiency are essential. Checkpoints help improve both.

A checkpoint is a validation point placed at critical spots in a test script to verify that the system under test meets expectations. For example, when testing a calculator application, you may expect that after entering "2+2", the calculator should display "4". In this case, you can use a checkpoint to validate this expectation.

CukeTest provides multiple ways to set checkpoints, such as the direct and flexible assert library, and the checkProperty method, which is specifically designed for verifying graphical user interface (GUI) element properties. Next, we will explore how to leverage these methods in CukeTest for effective automated testing.

Using the assert Library for Checkpoints

In Node.js and Python, the assert library is a common assertion tool that provides a simple yet powerful way to write test assertions. This is particularly useful in scenarios where specific conditions need to be verified as true.

Let's take a simple example to understand how to use the assert library in Node.js. Suppose we need to verify whether the value of a variable result is 4, we can write:

JavaScript
const assert = require('assert');

let result = 2 + 2;
assert.strictEqual(result, 4, 'Expected value to be 4');

In Python, the assert statement works in a similar way:

Python
result = 2 + 2
assert result == 4, 'Expected value to be 4'

In both cases, assertions ensure that the expected result matches the actual output. However, these examples focus on non-GUI environments. When dealing with GUIs, assertions often involve checking UI element properties.

For example, when testing a calculator application, we may want to verify whether the displayed result is correct. In such cases, we could verify whether the displayed text matches the expectation using the assert library. However, this involves querying the UI element’s properties and comparing them with expected values, which can be a complex process.

Using the checkProperty Method for Checkpoints

To simplify property validation in GUI testing, CukeTest provides the checkProperty method. This method is available for Windows, Qt, Java, and ATK controls. Compared to manually using the assert library for property validation, the checkProperty method significantly reduces code complexity.

Basic Usage Example

Suppose we are testing a button and want to confirm whether it is enabled. We can use the checkProperty method in CukeTest as follows:

JavaScript
await model.getButton("Save").checkProperty('enabled', true, 'Button must be enabled');

Python
model.getButton("Save").checkProperty('enabled', True, 'Button must be enabled')

In this example, we instruct CukeTest to check whether the property enabled of the control Button with the object name Save is true. If the property value is false, an error will be thrown with the message "Button must be enabled".

Checking Properties Using Regular Expressions

The checkProperty method also supports regular expressions to verify whether a property value matches a specific pattern. This is particularly useful when validating partial text matches or enforcing format rules.

For example, to verify whether the text in a textbox starts with "Hello", we can write:

JavaScript
await model.getEdit("textEdit").checkProperty('text', /^Hello/, 'Text should start with "Hello"');

Python
# Note: r'^Hello' is a raw string to avoid escaping.
model.getEdit("textEdit").checkProperty('text', re.compile(r'^Hello'), 'Text should start with "Hello"')

This code checks whether the value of the property text of the control Edit with the object name textEdit starts with "Hello". If the text does not match the regular expression, an error is thrown with the message "Text should start with 'Hello'".

Checkpoint Dialog

Additionally, CukeTest provides a visual checkpoint menu. During recording, you can press the Alt key and right-click on a UI control to open a checkpoint configuration panel. This panel allows you to quickly add validation scripts for UI elements, significantly improving testing efficiency. For more details, see Adding Property Checkpoints During Recording.

Comparison of assert and checkProperty

assert and checkProperty are both commonly used verification tools in CukeTest, each with its own advantages and applicable scenarios.

The assert library provides powerful assertions such as deep and strict equality, and can throw errors. When complex validation is needed, assert is a good choice, though it often requires extra code to fetch GUI element properties, increasing complexity.

The checkProperty method, on the other hand, is simpler and more efficient. It directly checks the properties of GUI elements without the need to manually retrieve them, significantly reducing code complexity. However, checkProperty is not as powerful as the assert library. If more complex validation is needed, assert might be a better option.

In summary, both the assert library and the checkProperty method have their own strengths. The choice of tool should be based on specific testing requirements.

Image Comparison

In addition to verifying the properties of common UI elements, CukeTest supports image comparison. This is useful in scenarios where verifying the correctness of displayed images is crucial. You can use Image.imageEqual and Image.imageCompare to compare images.

The following is a simple example using Image.imageEqual for image comparison:

JavaScript
const { Screen } = require('leanpro.common');
const { Image } = require('leanpro.visual');
const assert = require('assert');

async function testCompareImages() {
    // Read the expected image from file
    const expectedImage = await Image.fromFile("expected.png");
    // Capture the actual image by taking a screenshot
    const actualImage = Screen.capture();
    // Compare the two images
    let isEqual = await Image.imageEqual(expectedImage, actualImage);
    assert(isEqual, "The images do not match!");
}

If the images are identical, Image.imageEqual returns true; otherwise, it returns false. More details can be found in Walkthrough: Image Comparison.

CukeTest also provides the checkImage() method and supports adding image checkpoints during recording. See Adding Image Checkpoints During Recording for more information.

Best Practices

Successful test automation is not just about using tools but also how you use them. Here are some best practices for using checkpoints effectively:

  1. Ensure the importance of checkpoints: Every checkpoint should be meaningful. If a checkpoint fails, it should indicate a critical test failure. Avoid unnecessary checkpoints to reduce false positives.

  2. Use appropriate validation methods: Choose between assert and checkProperty based on the testing scenario. Use checkProperty for property validation and assert for complex logic.

  3. Keep checkpoints readable and maintainable: Test scripts should be easy to understand and maintain. Avoid overly complex validation logic.

  4. Use image comparison wisely: While useful in verifying the correctness of GUI display, image comparison can be sensitive to display settings and environment changes. Use it cautiously.

Checkpoints play a crucial role in automated testing and are essential for verifying whether a test passes. Using them effectively can significantly improve the quality of automation tests. Hopefully, this guide helps you use CukeTest more effectively in your testing workflows.