Using Karate as Library to my Java @Tests

可紊 提交于 2021-01-28 05:30:16

问题


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.

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();
    } 
}

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

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

Thanks


回答1:


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. : 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.




回答2:


You can, but we won't support or document it (maybe you can :). Not recommended because you would lose things like reports and the ability to do debugging.

Here is an example for browser automation.

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


来源:https://stackoverflow.com/questions/61326328/using-karate-as-library-to-my-java-tests

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!