如何解析Java或groovy中的Cucumber功能文件? [英] How to parse Cucumber feature file in java or groovy?

查看:71
本文介绍了如何解析Java或groovy中的Cucumber功能文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析.feature文件并生成Json数据.我目前能够使用正则表达式读取步骤,但是我想解析整个功能文件.

I need to parse .feature files and produce Json data. I am currently able to read the steps using regular expressions, But i want to parse entire feature file.

Path path= Paths.get("path to feature file").toAbsolutePath();
    try {
        File file = new File(path.toString())
                    try {
                        List<String> data = Files.readAllLines(file);

                        data.forEach(line -> {
                            Matcher stepMatcher = Pattern.compile("\\b(Given|When|Then|And|But)(.*)").matcher(line);
                            if (stepMatcher.find()) {
                                System.out.println(line);
                            }
                        });
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

推荐答案

我以google形式获得了答案.答案是

I got the answer in google forms. the Answer is

public void readFeatureFile() {
    Path fpath= Paths.get("path to file").toAbsolutePath();
    String path = fpath.toString();
    String gherkin;
    try {

        gherkin = FixJava.readReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
        System.out.println("gherkin...\n" + gherkin);
        StringBuilder json = new StringBuilder();
        System.out.println("json: '" + json + "'");
        JSONFormatter formatter = new JSONFormatter(json);
        System.out.println("formatter: " + formatter.toString());
        Parser parser = new Parser(formatter);
        System.out.println("parser: " + parser.toString());
        parser.parse(gherkin, path, 0);
        System.out.println("json: '" + json + "'"); 
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    }
}

查看此URL进行讨论讨论链接

check out this url for the discussion discussion link

这篇关于如何解析Java或groovy中的Cucumber功能文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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