#3042·karate

2.1.2: optional('#missing').click() throws for an absent element instead of doing nothing

Author: sentrybottaleCreated Sep 15, 2026Updated Sep 17, 2026
Labelsbugfixed

Description

With the Chrome/CDP driver in Karate 2.1.2, optional('#missing').click() fails when the element is absent. The identical feature passes on Karate 1.5.2.

The current UI documentation explicitly demonstrates this syntax and says: “optional() safely clicks if element exists, does nothing if not”.

This reproduces in a single standalone scenario on about:blank, without called features, authentication, an application server, or a karate-config.js.

Steps to Reproduce

  1. In an empty directory, place the Karate 2.1.2 standalone JAR. Have Java 21+ and Google Chrome installed.
  2. Create repro.feature with exactly:
gherkin
Feature: Missing optional element click

Scenario: Clicking a missing optional element should do nothing
  * configure driver = { type: 'chrome', headless: true }
  * driver 'about:blank'
  * match exists('#missing') == false
  * optional('#missing').click()
  * match driver.url == 'about:blank'
  1. Run:
bash
java -jar karate-2.1.2.jar run -o report-2.1.2 repro.feature
  1. For comparison, place the Karate 1.5.2 standalone JAR in the same directory and run the unchanged feature:
bash
java -jar karate-1.5.2.jar -o report-1.5.2 repro.feature

Expected Behavior

The missing optional element's click() should do nothing. Execution should reach the final assertion and the scenario should pass, consistent with the documented contract and Karate 1.5.2.

Actual Behavior

The existence assertion passes, confirming the element is absent. Karate 2.1.2 then fails at line 7:

js failed:
==========
  Line: 7, Col: 0
  Code: optional('#missing').click()
  Error: element not found: #missing
==========

Verified with the same Java and Chrome installations:

Karate version Result CLI exit code
2.1.2 1 scenario failed at the optional click; final assertion not executed 1
1.5.2 1 scenario passed, including the final assertion 0

Karate Version

2.1.2 standalone JAR; comparison: 1.5.2.

Java Version

Oracle Java 24.0.2 (build 24.0.2+12-54).

Operating System

macOS