簡體   English   中英

如何在 Java 中動態設置 CucumberOptions

[英]How to set dynamically CucumberOptions in Java

我正在開發一個將執行自動化測試用例的桌面應用程序。 用戶可以從 GUI 中選擇執行類型:按功能/按場景/按標簽名稱。 現在我正在使用 maven 並在 CMD 中執行命令來執行測試用例,我想切換到 TestNG 執行,但我需要從配置文件動態設置 CucumberOptions,

@CucumberOptions(features = { "src/test/resources/features/" }, tags = { "@Test3" }, glue = { "stepdefinitions" }, plugin = { "listeners.ExtentCucumberFormatter:" })
public class TestNGRunner extends AbstractTestNGCucumberTests {
}

但是 CucumberOptions 需要一個常量表達式,我無法設置它們。

有什么辦法可以做到嗎?

您可以通過命令行參數傳遞 Cucumber 選項:

您可以列出可用於您正在使用的 Cucumber 版本的選項。

通過 --help 選項打印出所有可用的配置選項:

java cucumber.api.cli.Main --help

或者:

mvn test -Dcucumber.options="--help"

您還可以使用標簽來指定要運行的內容。

配置選項也可以被覆蓋並通過cucumber.options Java 系統屬性傳遞給任何運行程序。

例如,如果您正在使用 Maven 並希望運行帶有 @smoke 標記的場景子集:

mvn test -Dcucumber.options="--tags @smoke"

提供將選項傳遞給 Cucumber 的額外機制。一些跑步者

黃瓜選項文檔

注意:這是來自 Java,我從 Eclipse 運行,而不是命令行。 如果你知道自己在做什么,並不是說會有很大的不同,但仍然......

我有一個 Excel 電子表格,其中每一行都包含一個角色和一個屏幕子集,並且我已經完成了所有帶有測試一個屏幕的 @tag 場景(及其相應的 Java/Selenium 代碼片段)的 Gherkin 功能文件。 有些角色只有 4 個屏幕; 有些多達 46 個。我不想創建 24 個(可能更多)不同的 Cucumber Runners(Excel 電子表格中的每一行一個)。 我首先嘗試通過編輯 CucumberOptions 注釋的 tags 屬性來動態更改現有 Cucumber Runner 上的標簽(在 Java 中;這是可能的!),但我無法讓它接受多個標簽的字符串數組; 只有個別標簽。 :-( 然后我偶然發現了cucumber.api.cli.Main 是如何工作的,這讓一切變得更容易。最重要的類是cucumber.runtime.Runtime 和cucumber.runtime.RuntimeOptions。我將跳過所有POI 代碼用於讀取 Excel 文件,但重要的部分如下:

import org.junit.Test;
import org.junit.internal.TextListener;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;

import cucumber.api.CucumberOptions;
import cucumber.api.SnippetType;
import cucumber.runtime.ClassFinder;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;

public class MyRunner {

    /** Constructor */
    public MyRunner (String thisPath) {
           // This does all the POI stuff, plus making the data available to
           //   getCellData(sheet, columnName, row) (which you get to define). 
           // Any way that makes it return a dynamic value will work here.
    }   
 
    public static void main(String[] args) {
        MyRunner myRunner = new MyRunner ("src/main/resources/User Roles.xlsx");
        myRunner.runRunner();
    }

    public void runRunner() {
        String[] newTags = { "@Login" }; 
        String sheetName = "Sheet1";
        int rowCount = this.getRowCount(sheetName);
        String role = "";
        String userId = "";
        String password = "";
        //For each row in the Excel file (row 1 is column names)
        for (int i=2; i<rowCount; i++) { 
            System.out.println("\n run loop i: " + i);
            int rowNumber = i-1;
            role = this.getCellData(sheetName, "User Role", rowNumber).trim();
            userId = this.getCellData(sheetName, "UserID", rowNumber).trim();
            password = this.getCellData(sheetName, "Password", rowNumber).trim();
            String screens = this.getCellData(sheetName, "Screens", rowNumber).trim(); 
            System.out.println(rowNumber + ". runRbac role: '" + role + 
                    "', userId: '" + userId + "', password: '" + password + 
                    "', screens: '" + screens + "'.");
            // I had to do some indirection here because customer who edits the
            // Excel file does not want to see Tags, but uses Screen titles.
            // So I had to map it.
            ArrayList<String> tagList = this.screenTitlesToTags(screens);
            System.out.println(rowNumber + ". Check " + tagList.size() + " screens.");
            runOneRow(rowNumber, role, userId, password, tagList);                          
        }
        String[] finalTags = { "@LogoutCloseBrowser" }; 
        runOneRow(rowCount+1, role, userId, password, finalTags);
    }


    public static void runOneRow(int iteration, String role, String userId, 
                      String password, ArrayList<String> tags) {
        System.out.println("Starting runOneRow with iteration=" + iteration + 
           ", userid=" + userId + ", " + password + ", role=" + role);
        // I couldn't figure out a way to inject the UserId and password into JUnit,
        // so I did it old-school: as a file that the snippet reads. Yuck!
        Utils.writeFile("target/credential.txt", userId + ", " + password); 
        // !!!!!! Here is the heart of the matter!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        JUnitCore junit = new JUnitCore();
        junit.addListener(new TextListener(System.out));
        System.out.println("runOneRow- JUnit/Cucumber test with tags: " + tags);
        String tagString = tags.toString().replace("[", "").replace("]", "").trim();
        tagString = StringUtils.removeEnd(tagString, ",");
        // The tagString value should look something like: 
        // "@Login, @WelcomePage, @FirstScreen, @SecondScreen, @ThirdScreen, @Screen4"
        String[] argv = { "--plugin",  "com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/MyCukeReport.html",
                "--snippets",  "camelcase",
                "--glue", "com.myorg.cukes.stepDefinitions", // or wherever 
                "--tags",  tagString,
                "src/main/java/com/hhs/cms/cukes/features" };
        RuntimeOptions runtimeOptions = new RuntimeOptions(new ArrayList<String>(asList(argv)));
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        ResourceLoader resourceLoader = new MultiLoader(classLoader);
        ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
        Runtime runtime = new Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
        try {
            runtime.run();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("Finished runOneRow with exitStatus=" + runtime.exitStatus() + ", iteration=" + iteration + ", userid=" + userId + ", " + password + ", role=" + role);
        Result jUnitResult = junit.run(cukeRunnerClass);
        resultReport(jUnitResult);
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM