How to set driver cookie from the Java Execution hook in Karate

317 Views Asked by At

Hi We have configured driver in the feature file. I am able to set the zaleniumTestStatus cookie from the feature file using And cookie({ name: 'zaleniumTestPassed', value: 'true' }) . I want to do same from the Java Hook. How do I get driver object in execution hook. I tried as below but not luck..

@Override public void afterScenario(ScenarioResult scenarioResult, ScenarioContext scenarioContext) {

    Cookie cookie = new Cookie("zaleniumTestPassed", "false");
    scenarioContext.cookie("zaleniumTestPassed", "true");
   
}

I want to do something like this.. driver.get().manage().addCookie(cookie);

1

There are 1 best solutions below

2
On

Why don't you do it in 2 steps. First set a variable. And in your test, if that variable is set, set a cookie.

Because unfortunately a driver is not initialized until the first URL navigation is made. If you explain what you are trying to do - this may be a potential feature request.