1

I'm figuring what is in Karate testing the best practice to reuse some features containing selenium steps in other features. In other words I'd like to use some scenarios containing the web browsing part and recall them in other features.

This is important from my point of view for:

  • keeping together the selenium parts that otherwise the will be spread in all the features
  • having one single point in case something changes in the web ui (a selector, etc)

I've tried defining a feature "A" that:

  • sets the url to open with the browser (in a variable, let's say my_url)
  • calls a scenario in a feature B

The feature "B" opens the url in a browser and does something, for example clicks on a button and waits for a div:

Given driver my_url
When waitForEnabled(<button selector>).click()
Then match waitFor(<div selector>).text == 'Signing complete'

The problem with this solution is that running the feature A, it calls the feature B and it works all fine but it doesn't close the webdriver at the end of the feature. Probably using a hook, closing the webdriver after the scenario in feature B, can be a solution but I'm not sure that is the best practice to do it.

I tried launching the feature in my IDE (IntelliJ Idea), not with maven.

So have you tried other solutions for this? Have you hints/tips to suggest?

Francesco F.
  • 79
  • 1
  • 8

1 Answers1

1

I think explicitly closing the browser within the test flow is fine.

Or you can use an afterScenario hook from the top-level calling feature: https://github.com/intuit/karate#hooks

You can also choose to call Java code: https://stackoverflow.com/a/47233301/143475

Do note that Karate has a replacement for Webdriver now, and Karate will auto-close the browser: https://github.com/intuit/karate/tree/master/karate-core

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thanks, I used the Karate driver but I've had the suspect that in case of called feature the driver is not automatically closed in a particular case. I'll try to investigate a bit more on this. – Francesco F. Apr 30 '20 at 11:04
  • @FrancescoF. yes, if you can replicate it - we can fix it :) – Peter Thomas Apr 30 '20 at 11:28