将空手道用作我的Java @Tests的库 [英] Using Karate as Library to my Java @Tests

查看:58
本文介绍了将空手道用作我的Java @Tests的库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用空手道的最佳方法是在功能文件中使用空手道DSL.但是,我只想检查是否可以将空手道用作基于Java的框架的库.

The best way using karate is with Karate DSL in feature files. However I just wants to check if I can utilize karate as a library to my java based framework.

例如,我知道我可以使用下面的代码并使用Chrome DevTools自动执行Chrome.

For example I know I can use below code and automate Chrome using Chrome DevTools.

public class Test {
    public static void main(String[] args) {
        Chrome chrome = Chrome.startHeadless();
        chrome.setLocation("https://github.com/login");
        ......
        chrome.quit();
    } 
}

我是否可以对com.intuit.karate.driver.WebDriver进行类似操作,以自动化任何基于WebDriver的浏览器(例如ChromeWebDriver,GeckoWebDriver等)而无需功能文件?

Can I do similar for com.intuit.karate.driver.WebDriver for automating any WebDriver based Browsers (like ChromeWebDriver, GeckoWebDriver etc.) without having feature files?

我可以使用类似的方法通过自己的@Test方法自动执行API测试,但可以在测试方法中使用空手道库吗?

Can I use similar way to automate API tests with my own @Test methods but using karate library underneath in test method?

谢谢

推荐答案

与空手道创作者Peter Thomas讨论之后,我找到了一种方法. 这是通过空手道启动Chrome Webdriver Browser的示例代码.

After discussion with Peter Thomas (The Karate Creator) I figured out a way to do it. Here is sample code to launch Chrome webdriver Browser via karate.

HashMap<String,Object> map = new HashMap<String,Object>();
map.put("type","chromedriver");
map.put("executable","/Users/vxt82/Apps/chromedriver");
HashMap httpConfig = new HashMap<String,Object>();
httpConfig.put("readTimeout", 120000);
map.put("httpConfig",httpConfig);
ChromeWebDriver driver = new ChromeWebDriver(new DriverOptions(null, map, null, 9515, "chromedriver"));

然后您可以调用类似的方法

And then you can call methods like

driver.setUrl("https://github.com/login");
driver.input("#login_field", "dummy");
driver.input("#password", "world");
driver.submit().click("input[name=commit]");

P.S. :如Thomas所言,不建议使用空手道的方法,而是发布答案,以防万一有人试图将空手道用于自动化测试并作为依赖项并使用Java编写测试而不是使用功能文件.

P.S. : As said by Thomas this is not recommended way of using Karate but posting answer just in case anyone is trying to use karate for test automation as a dependency and write test in java instead of using feature file.

这篇关于将空手道用作我的Java @Tests的库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆