{"record":{"id":"ed7b11b0edc762e4","repo":"karatelabs/karate","slug":"waitfortext-timeout-locator-expected-expected","errorCode":null,"errorMessage":"waitForText timeout: ${locator} expected: ${expected}","messagePattern":"waitForText timeout: (.+?) expected: (.+?)","errorType":"exception","errorClass":"DriverException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/driver/w3c/W3cDriver.java","lineNumber":575,"sourceCode":"    public Element waitForText(String locator, String expected) {\n        return waitForText(locator, expected, options.getTimeoutDuration());\n    }\n\n    @Override\n    public Element waitForText(String locator, String expected, java.time.Duration timeout) {\n        long deadline = System.currentTimeMillis() + timeout.toMillis();\n        while (System.currentTimeMillis() < deadline) {\n            try {\n                String text = text(locator);\n                if (text != null && text.contains(expected)) {\n                    return BaseElement.existing(this, locator);\n                }\n            } catch (Exception e) {\n                // Element may not exist yet\n            }\n            sleep(options.getRetryInterval());\n        }\n        throw new DriverException(\"waitForText timeout: \" + locator + \" expected: \" + expected);\n    }\n\n    @Override\n    public Element waitForEnabled(String locator) {\n        return waitForEnabled(locator, options.getTimeoutDuration());\n    }\n\n    @Override\n    public Element waitForEnabled(String locator, java.time.Duration timeout) {\n        long deadline = System.currentTimeMillis() + timeout.toMillis();\n        while (System.currentTimeMillis() < deadline) {\n            try {\n                if (enabled(locator)) {\n                    return BaseElement.existing(this, locator);\n                }\n            } catch (Exception e) {\n                // Element may not exist yet\n            }","sourceCodeStart":557,"sourceCodeEnd":593,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/driver/w3c/W3cDriver.java#L557-L593","documentation":"W3cDriver.waitForText polls the element's text, comparing it to the expected value, every retry interval until timeout; each poll swallows exceptions because the element may not exist yet. If the element's text never equals/contains the expected string within the timeout, a DriverException naming the locator and expected text is thrown.","triggerScenarios":"Calling driver.waitForText(locator, expected) where the element never exists, or exists but its text never matches expected, within options.getTimeoutDuration().","commonSituations":"Expected text differs in whitespace/casing from rendered text; text is set by async JS that finishes after timeout; localized i18n strings differ between environments; selector matches a container whose inner text updates later.","solutions":["Print the actual text (driver.text(locator)) and compare with expected to spot formatting mismatches","Increase the timeout via waitForText(locator, expected, timeout)","Normalize the expected string (trim/case) or match on a stable substring if the API supports contains semantics","Fix the selector if it resolves to the wrong element"],"exampleFix":"// before\ndriver.waitForText('#status', 'Saved!'); // actual text is 'Saved !' with space\n// after\ndriver.waitForText('#status', 'Saved', 15000); // match stable substring","handlingStrategy":"validation","validationCode":"String actual = driver.exists(locator) ? driver.text(locator) : null;\nif (actual != null && actual.contains(expected)) { /* already satisfied */ }","typeGuard":null,"tryCatchPattern":"try {\n    driver.waitForText(locator, expected, timeout);\n} catch (DriverException e) {\n    // capture driver.text(locator) into the failure report for diagnosis\n}","preventionTips":["Compare against actual rendered text (watch whitespace/casing) before asserting","Match stable substrings instead of full strings","Beware i18n: expected text must match the test environment locale"],"tags":["webdriver","timeout","wait","text-assertion"],"backgroundTag":"request-timeout","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}