英文:
How to generate cucumber html report with attached failed screenshot and cucumber.json file using gradle based project
问题
build.gradle 文件
plugins {
    id 'java'
    id 'idea'
}
group 'org.example'
version '1.0-SNAPSHOT'
configurations {
    cucumberRuntime.extendsFrom testRuntime
}
task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'stepDef', 'src/test/java']
        }
    }
}
repositories {
    mavenCentral()
}
dependencies {
    implementation fileTree(dir: System.getProperty("user.dir") + '/Plugin', include: ['*.jar'])
    implementation files('junit-4.12')
    implementation files('testng-6.7.jar')
    implementation files('junit-jupiter-api-5.6.2')
    implementation files('hamcrest-all-1.3')
    // 其他依赖...
}
TestRunner 文件
package TestRunner;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources",
        glue = "StepDefs",
        plugin = {
                "pretty", "html:target/cucumber-html-report", "json:target/cucumber.json", "pretty:target/cucumber-pretty.txt"
        }
)
public class TestRunner {
}
注意:为了避免混淆,我已经根据你提供的代码内容对其进行了适当的翻译,但保留了代码结构和关键字。如果你需要进一步的帮助,请随时询问。
英文:
Using gradle, I am trying generate cucumber html report with failed screenshot attached to it for security reason I cannot have online plugins in build.gradle file so I have to download required jar and plugins and implement and configure library manually in build.gradle file.
Please suggest how can configure TestRunner file in build.gradle and generate cucumber html report with cucumber.json file
build.gradle file
plugins {
    id 'java'
    id 'idea'
}
group 'org.example'
version '1.0-SNAPSHOT'
configurations {
    cucumberRuntime.extendsFrom testRuntime
}
task cucumber() {
    dependsOn assemble, compileTestJava
    doLast {
        javaexec {
            main = "io.cucumber.api.cli.Main"
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = ['--plugin', 'pretty', '--glue', 'stepDef', 'src/test/java']
        }
    }
}
repositories {
    mavenCentral()
}
dependencies {
    implementation fileTree(dir:System.getProperty("user.dir")+'/Plugin',include:['*.jar'])
    implementation files('junit-4.12')
    implementation files('testng-6.7.jar')
    implementation files('junit-jupiter-api-5.6.2')
    implementation files('hamcrest-all-1.3')
    .....................
TestRunner file
package TestRunner;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/test/resources",
        glue = "StepDefs",
        plugin = {
                "pretty", "html:target/cucumber-html-report", "json:target/cucumber.json", "pretty:target/cucumber-pretty.txt"
        }
)
public class TestRunner {
}
答案1
得分: 1
无论 StepDefs 是什么...
通过运行 gradle cucumber --info 进行调试可能会很有用,因为错误信息 finished with non-zero exit value 1 只是表示 "错误" 或 "未成功"。
你可能需要以下这些Java依赖项作为起点:
testImplementation 'io.cucumber:cucumber-java:6.5.0'
testImplementation 'io.cucumber:cucumber-junit:6.5.0'
并且可能需要将 gradle.cucumber 添加为参数 args 中的 --glue,正如文档所建议的。任务依赖关系 compileTestJava 应改为 testClasses。
html 通常是一个插件,它需要一个输出目录,因此应该像这样设置:
task cucumber() {
    dependsOn assemble, testClasses
    doFirst {
    }
    doLast {
        javaexec {
            main = 'io.cucumber.core.cli.Main'
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = [
                '--plugin', 'pretty', 'html:target/reports',
                '--glue', 'gradle.cucumber',
                'src/test/resources'
            ]
        }
    }
}
这些 args 也可以在Java中进行注释;不确定它们中的哪一个优先级更高。可能重复定义参数毫无意义,只会制造混乱。
确保遵循第4条指示:
在
src/test/resources中添加特性.feature文件和关联的步骤映射类.java,在src/test/java中添加gradle.cucumber包。
-g,--gluePATH:加载胶水代码(步骤定义、钩子和插件)的路径。
在使用jUnit运行时,也可以通过 junit-platform.properties 文件传递选项。
最简单的方法可能是从 cucumber-java-skeleton 开始(已知可正常工作)。
英文:
Whatever StepDefs may be ...
Running with gradle cucumber --info might be useful for debugging... because the error message finished with non-zero exit value 1 just indicates "error" or "no success".
You'd probably need these Java dependencies, to begin with:
testImplementation 'io.cucumber:cucumber-java:6.5.0'
testImplementation 'io.cucumber:cucumber-junit:6.5.0'
And one might have to add gradle.cucumber as the --glue into the arguments args, as the documentation suggests. Task dependency compileTestJava should rather be testClasses.
html generally is a plugin, which expects an output directory, therefore this should look alike this:
task cucumber() {
    dependsOn assemble, testClasses
    doFirst {
    }
    doLast {
        javaexec {
            main = 'io.cucumber.core.cli.Main'
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = [
                '--plugin', 'pretty', 'html:target/reports',
                '--glue', 'gradle.cucumber',
                'src/test/resources'
            ]
        }
    }
}
These args can also be annotated in Java; not sure which of them takes precedence.<br/>It probably makes no sense and only creates a mess, when defining the arguments twice.
Make sure to follow instruction #4:
> Add feature .feature files and associated step mapping classes .java in src/test/resources and src/test/java respectively in a gradle.cucumber package.
> -g, --glue PATH Where glue code (step definitions, hooks and plugins) are loaded from.
When running with jUnit, one can also pass options with a junit-platform.properties file.
The most easy might be to start with the cucumber-java-skeleton (it is known to be working).
答案2
得分: 0
以下是您要翻译的内容:
这对我没起作用,如果我运行这个cucumber任务,它会给我报错。
任务:cucumber 失败
错误:无法找到或加载主类 io.cucumber.api.cli.Main
原因:java.lang.ClassNotFoundException: io.cucumber.api.cli.Main
错误:无法找到或加载主类 io.cucumber.api.cli.Main
我创建了一个名为cucumberRunner的任务,它执行TestRunner.java文件,它会生成cucumber.json文件和HTML报告,但HTML报告的样式不是预期的,没有图形和颜色。
我使用的 build.gradle 内容如下:
configurations {
    cucumberRuntime {
        extendsFrom testRuntime
    }
}
task cucumber() {
    dependsOn assemble, testClasses
    doFirst {
    }
    doLast {
        javaexec {
            main = 'io.cucumber.api.cli.Main'     // 尝试过 io.cucumber.core.cli.Main
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = [
                    '--plugin', 'pretty', 'html:target/reports',
                    '--glue', 'gradle.cucumber',
                    'src/test/resources'
            ]
        }
    }
}
task cucumberRunner(type: Test) {
    include '**/**TestRunner.class'
}
此外,我还添加了以下 JAR 包:
implementation files('junit-4.12')
implementation files('testng-6.0.jar')
implementation files('cucumber-core-6.0.0')
implementation files('cucumber-java-6.0.0')
implementation files('cucumber-plugin-6.0.0')
implementation files('cucumber-junit-6.0.0')
implementation files('cucumber-testng-6.0.0')
implementation files('cucumber-jvm-deps-1.0.5')
implementation files('cucumber-gherkin-6.0.0')
implementation files('cucumber-java8-6.0.0')
implementation files('cucumber-html-0.2.3')
请注意,这里只返回了翻译好的部分,没有其他内容。
英文:
It didn't work for me, If I run this cucumber task it gives me error
Task :cucumber FAILED
Error: Could not find or load main class io.cucumber.api.cli.Main
Caused by: java.lang.ClassNotFoundException: io.cucumber.api.cli.Main
Error: Could not find or load main class io.cucumber.api.cli.Main
I have created one task cucumberRunner which executes the TestRunner.java file, it is creating cucumber.json file and html report but htlm
report but HTML report is not expected is weird no graphics and colorless colorless
build.gradle I'm using:
```
configurations {
    cucumberRuntime {
        extendsFrom testRuntime
    }
}
task cucumber() {
    dependsOn assemble, testClasses
    doFirst {
    }
    doLast {
        javaexec {
            main = 'io.cucumber.api.cli.Main'     // tried with  io.cucumber.core.cli.Main
            classpath = configurations.cucumberRuntime + sourceSets.main.output + sourceSets.test.output
            args = [
                    '--plugin', 'pretty', 'html:target/reports',
                    '--glue', 'gradle.cucumber',
                    'src/test/resources'
            ]
        }
    }
}
task cucumberRunner(type: Test) {
    include '**/**TestRunner.class'
}
       Also I have added jars
           implementation files('junit-4.12')
           implementation files('testng-6.0.jar')
           implementation files('cucumber-core-6.0.0')
           implementation files('cucumber-java-6.0.0')
           implementation files('cucumber-plugin-6.0.0')
           implementation files('cucumber-junit-6.0.0')
           implementation files('cucumber-testng-6.0.0')
           implementation files('cucumber-jvm-deps-1.0.5')
           implementation files('cucumber-gherkin-6.0.0')
           implementation files('cucumber-java8-6.0.0')
           implementation files('cucumber-html-0.2.3')
       ```
</details>
				通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论