Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headless Mode Testing #2698

Merged
merged 20 commits into from
Dec 17, 2024
Merged

Headless Mode Testing #2698

merged 20 commits into from
Dec 17, 2024

Conversation

hecrj
Copy link
Member

@hecrj hecrj commented Dec 17, 2024

This PR introduces a brand new crate for headless mode testing: iced_test.

This crate features a Simulator type that can be used to simulate user interactions and query any Element produced by iced.

Basic Usage

Let's assume we want to test the classical counter interface.

First, we will want to create a Simulator of our interface:

use iced_test::simulator;

let mut counter = Counter { value: 0 };
let mut ui = simulator(counter.view());

Now we can simulate a user interacting with our interface. Let's use Simulator::click to click the counter buttons:

use iced_test::selector::text;

let _ = ui.click(text("+"));
let _ = ui.click(text("+"));
let _ = ui.click(text("-"));

Simulator::click takes a Selector. A Selector describes a way to query the widgets of an interface. In this case, selector::text lets us select a widget by the text it contains.

We can now process any messages produced by these interactions and then assert that the final value of our counter is indeed 1!

for message in ui.into_messages() {
    counter.update(message);
}

assert_eq!(counter.value, 1);

We can even rebuild the interface to make sure the counter displays the proper value with Simulator::find:

let mut ui = simulator(counter.view());

assert!(ui.find(text("1")).is_ok(), "Counter should display 1!");

And that's it! That's the gist of testing iced applications!

Simulator contains additional operations you can use to simulate more interactions—like tap_key or typewrite—and even perform snapshot testing!

@hecrj hecrj added this to the 0.14 milestone Dec 17, 2024
@hecrj hecrj merged commit f2c9b6b into master Dec 17, 2024
30 checks passed
@hecrj hecrj deleted the feature/test-crate branch December 17, 2024 16:28
@GyulyVGC
Copy link
Contributor

This is a remarkable milestone for Iced.

GG Héctor 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants