Skip to content
omar edited this page Oct 12, 2022 · 19 revisions

What is ImGuiTestRef?

Most functions in the ImGuiTestContext API are taking a ImGuiTestRef argument in order to refer to an item/widget.

void ItemClick(ImGuiTestRef ref, ImGuiMouseButton button = 0);

Typical usage:

ctx->ItemClick("Button");

A ImGuiTestRef can hold either the path to an item, either a precomputed item identifier (ImGuiID which is hash of an absolute path).
Using named paths is greatly convenient and open many possibilities. The structure itself is pretty lightweight and straightforward (see its definition).

Basic Usage

Consider this GUI code:

ImGui::Begin("Window");
ImGui::Button("Button");
if (ImGui::TreeNode("Node"))
{
    ImGui::Checkbox("Checkbox", &b);
    ImGui::TreePop();
}
ImGui::End();

Different ways to access the button and checkbox with named paths:

ctx->ItemClick("//Window/Button");        // Absolute path to the button
ctx->ItemCheck("//Window/Node/Checkbox"); // Absolute path to the checkbox

In order to simplify testing code, it is common to use SetRef() to set Base Reference:

ctx->SetRef("//Window");
ctx->ItemClick("Button");                 // Relative path (from base provided to SetRef() call) to the button
ctx->ItemCheck("Node/Checkbox");          // Relative path (from base provided to SetRef() call) to the checkbox

Single forward slashes / are used as a natural delimitation for items in a path.

Fun Fact! Because the underlying identifier used by Dear ImGui are concatenated hash, the single forward slashes are technically unnecessary (but wholly recommended for readability):

ctx->ItemCheck("NodeCheckbox");           // Also works! (but not recommended: weird and harder to read)

In case you need to refer to item containing a slash in their name, you'll need to escape it using a backslash \. Note that in the majority of programming languages, backslashes in literal strings themselves need to be escaped..

ctx->ItemClick("A\\/B");                  // Click item called "A/B"

Unless prefixed with //, named references passed to most functions are appended to the base reference set using ctx->SetRef().

Named Paths

  • todo: explain ctx->SetRef()
  • todo: explain ImGuiTestRef
  • todo: explain using paths features such as "/", "//", "../", "//$FOCUSED/"

Clone this wiki locally