问题
How do I define my own StepActions class in a Karate test?
All I need is one working example (apparently nobody has an example of this anywhere; I looked and couldn't find anything and so I am asking here).
For example, how would I implement this helper step action in Java? In this case, how do I get access to the WebDriver driver
instance from within Java context? Then, how do I call the embed?
@Slf4j
public class SeleniumStepActions extends StepActions {
public SeleniumStepActions(FeatureContext featureContext, CallContext callContext, Scenario scenario, LogAppender appender) {
super(featureContext, callContext, scenario, appender);
}
@When("^screenshot$")
public void takeAScreenShot()
{
// goals is to simulate this in a karate js test
// * def bytes = driver.screenshot()
// * karate.embed(bytes, 'image/png')
log.info("Testing my own custom action.");
}
}
It is possible the above won't work. I am just looking to be pointed in the right direction by someone who knows. I wish there was such an example in the karate demo.
回答1:
You can't. Which is why there ain't any demo :P
For a detailed discussion, please read this thread: https://github.com/intuit/karate/issues/398
The summary:
- Karate does not support the "Step Definitions" that Cucumber does
- There are 2 ways to inject custom logic, a) JS, b) Java interop
- These are more than sufficient to implement something close to custom "keywords" - see this example: https://twitter.com/KarateDSL/status/1128170638223364097
- and another: https://twitter.com/KarateDSL/status/1144458169822806016
If you insist on making your test read like "plain english" (which IMHO is not worth it) - then Karate may not be the best choice for your team.
来源:https://stackoverflow.com/questions/59149245/how-do-i-define-my-own-stepactions-class-in-a-karate-test