如何使用 JUnit 将 Selenium Cucumber 结果连接到 TestRail

How to connect Selenium Cucumber results to TestRail using JUnit

我的问题主要是了解如何在 Cucumber 场景 运行 之后填充 TestRail 结果。我正在尝试将我的 JUnit 测试 运行 的结果设置在现有的 TestRail 运行 上。我有 APIClient 和 APIException 根据 this project. I then created this JUnit class 也复制同一个项目。不确定现在如何继续使用 Cucumber 和 JUnit。如果有帮助,我们的项目还有一个 Hooks class 和一个 MainRunner?

public class Hooks {

public static WebDriver driver;

@Before
public void initializeTest() {

    System.out.println("Testing whether it starts before every scenario");

    driver = DriverFactory.startDriver();
}

}


import java.io.File;
@RunWith(Cucumber.class)
@CucumberOptions(
    features = {"src/test/java/clinical_noting/feature_files/"},
    glue = {"clinical_noting.steps", "clinical_noting.runner"},
    monochrome = true,
    tags = {"@current"},
    plugin = {"pretty", "html:target/cucumber", 
"json:target/cucumber.json", 
"com.cucumber.listener.ExtentCucumberFormatter:target/cucumber- 
reports/report.html"}
)

public class MainRunner {

@AfterClass
public static void writeExtentReport() {
    Reporter.loadXMLConfig(new File(FileReaderManager.getInstance().getConfigReader().getReportConfigPath())) 
;
}

}

感谢您的帮助。

更新

在 运行 单独进行 JUnit 测试时更新 TestRail。仍然不确定在 Cucumber 场景 运行 之后该怎么做?这就是它现在的工作方式:

public class JUnitProject {
private static APIClient client = null;
private static Long runId = 3491l;
private static String caseId = "";
private static int FAIL_STATE = 5;
private static int SUCCESS_STATE = 1;
private static String comment = "";
@Rule
public TestName testName = new TestName();

@BeforeClass
public static void setUp() {
    //Login to API
    client = testRailApiClient();
}

@Before
public void beforeTest() throws NoSuchMethodException {
    Method m = JUnitProject.class.getMethod(testName.getMethodName());
    if (m.isAnnotationPresent(TestRails.class)) {
        TestRails ta = m.getAnnotation(TestRails.class);
        caseId = ta.id();
    }
}

@TestRails(id = "430605")
@Test
public void validLogin() {
    comment = "another comment";
    Assert.assertTrue(true);
}

@Rule
public final TestRule watchman = new TestWatcher() {
    Map data = new HashMap();

    @Override
    public Statement apply(Statement base, Description description) {
        return super.apply(base, description);
    }

    @Override
    protected void succeeded(Description description) {
        data.put("status_id", SUCCESS_STATE);
    }

    // This method gets invoked if the test fails for any reason:
    @Override
    protected void failed(Throwable e, Description description) {
        data.put("status_id", FAIL_STATE);
    }

    // This method gets called when the test finishes, regardless of status
    // If the test fails, this will be called after the method above
    @Override
    protected void finished(Description description) {
        try {
            data.put("comment", comment);
            client.sendPost("add_result_for_case/" + runId + "/" + caseId, data);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (APIException e) {
            e.printStackTrace();
        }
    };
};
}

和注释

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD) //on method level
public @interface TestRails {

String id() default "none";
}

现在工作。必须在 before 方法 中添加 scenario 参数并从那里进行 TestRail 连接。

@regressionM1 @TestRails(430605)
Scenario: Verify the user can launch the application
Given I am on the "QA-M1" Clinical Noting application
Then I should be taken to the clinical noting page

public class Hooks {

private static APIClient client = null;
private static Long runId = 3491l;
private static String caseId = "";
private static int FAIL_STATE = 5;
private static int SUCCESS_STATE = 1;
private static String SUCCESS_COMMENT = "This test passed with Selenium";
private static String FAILED_COMMENT = "This test failed with Selenium";
@Rule
public TestName testName = new TestName();

public static WebDriver driver;

@Before
public void initializeTest() {
    client = testRailApiClient();

    System.out.println("Testing whether it starts before every scenario");

    driver = DriverFactory.startDriver();
}

@After()
public void tearDown(Scenario scenario) {
    String caseIdSplit = "";

    for (String s : scenario.getSourceTagNames()) {
        if (s.contains("TestRail")) {
            caseIdSplit = s.substring(11, 17); // Hardcoded for now as all the ids have 6 characters
            System.out.println("Testing whether the browser closes after every scenario" + caseIdSplit);
        }
    }

    caseId = caseIdSplit;
    Map data = new HashMap();

    if (!scenario.isFailed()) {
        data.put("status_id", SUCCESS_STATE);
        data.put("comment", SUCCESS_COMMENT);

    } else if (scenario.isFailed()) {

        data.put("status_id", FAIL_STATE);
        data.put("comment", SUCCESS_COMMENT);
    }

    try {
        client.sendPost("add_result_for_case/" + runId + "/" + caseId, data);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (APIException e) {
        e.printStackTrace();
    }
}

}

更新 在这个 here

上写了一个 post