【问题标题】:How to connect Selenium Cucumber results to TestRail using JUnit如何使用 JUnit 将 Selenium Cucumber 结果连接到 TestRail
【发布时间】:2019-11-14 16:51:11
【问题描述】:

我的问题主要是知道如何在 Cucumber 场景运行后填充 TestRail 结果。我正在尝试将我的 JUnit 测试运行的结果设置在现有的 TestRail 运行上。根据this project,我有 APIClient 和 APIException。然后我创建了this JUnit class 也复制了同一个项目。作为第一次使用 Cucumber 和 JUnit,现在不确定如何进行。如果有帮助,我们的项目还有一个 Hooks 类和一个 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";
}

【问题讨论】:

    标签: selenium selenium-webdriver junit cucumber testrail


    【解决方案1】:

    现在工作。必须在 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的帖子

    【讨论】:

    • String[] res = s.split("(\\(.*?)"); caseIdSplit = res[1].substring(0, res[1].length() -1); // Removing the last parenthesis 正则表达式现在就在这里,如果对其他人有用的话。
    猜你喜欢
    • 1970-01-01
    • 2019-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多