亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Maven 中運行具有不同配置的 UI 測試

如何在 Maven 中運行具有不同配置的 UI 測試

鳳凰求蠱 2023-05-17 15:46:08
我是自動化 UI 測試的新手,我正在使用 Cucumber 和 Selenium 進行 UI 自動化。所以在我的項目中,我創建了一個 Hook 類來設置用于測試的 web 驅動程序。是這樣的:System.setProperty("webdriver.chrome.driver", "driver path");driver = new chrome driver;但是如果我想用不同的瀏覽器和不同的環境運行相同的測試。例如,我想用 chrome 和環境 A 運行測試,并用 firefox 和環境 B 運行相同的測試。我計劃為不同的環境創建兩個屬性文件env=qabaseURl = http://qa......username = testpassword = test和env=devbaseURl = http://dev......username = test1password = test1我只想放一個像這樣的maven命令mvn clean test broswer=chrome env=qa從正確的文件中選擇屬性并根據瀏覽器參數設置網絡驅動程序。有可能這樣做嗎?這種情況的任何例子?
查看完整描述

2 回答

?
有只小跳蛙

TA貢獻1824條經驗 獲得超8個贊

使用屬性是個好主意。你走在正確的軌道上。


為了加載不同的屬性,可以根據環境,為屬性文件創建多個文件夾。


假設您將屬性文件存儲在src/main/java/dev/properties.properties


創建多個目錄,例如:


src/main/java/qa

src/main/java/dev

src/main/java/prod

在每個路徑中創建 1 個屬性文件,就像我在開始時所做的那樣。


現在,您想使用 Maven 加載正確的屬性。你可以用maven profiles它來做到這一點。


給你pom.xml添加:


</dependencies>

    <profiles>

        <profile>

            <activation>

                <activeByDefault>true</activeByDefault>

            </activation>

            <id>dev</id>

            <properties>

                <configuration.path>src/main/dev</configuration.path>

            </properties>

        </profile>

        <profile>

            <activation>

                <activeByDefault>false</activeByDefault>

            </activation>

            <id>prod</id>

            <properties>

                <configuration.path>src/main/prod</configuration.path>

            </properties>

        </profile>

    </profiles>

</project>

就在下面</dependencies>


如您所見,我創建了 2 個配置文件。他們的 ID 是dev和prod。兩個配置文件都有一個configuration.path指向您的.properties文件的屬性。要使用 maven commend 運行配置文件,您只需鍵入-PprofileId. -Pdev例如


我希望你使用,maven-surefire-plugin因為這就是我將在示例中展示的內容。


<plugin>

                <groupId>org.apache.maven.plugins</groupId>

                <artifactId>maven-surefire-plugin</artifactId>

                <version>2.22.1</version>

                <configuration>

                    <systemPropertyVariables>

                        <configuration.path>${configuration.path}</configuration.path>

                    </systemPropertyVariables>

                </configuration>

            </plugin>

以上只是部分配置surefire-plugin!讓我們關注systemPropertyVariables. 為了從 Maven 配置文件中獲取屬性,您可以通過將${configuration.paths}變量傳遞到surefire-plugin. 您可以使用相同的名稱。


現在,您需要將.properties文件中的屬性加載到系統中。我寫了一個類來做到這一點,閱讀configuration.path。您可以使用其他解決方案。


public class Properties {

    private static java.util.Properties props;


    static {

        props = new java.util.Properties();


        String pathWithPropertiesFiles = System.getProperty("configuration.path");

        String[] paths = pathWithPropertiesFiles.split("[;]");


        Arrays.asList(paths).forEach(propertyPath -> Arrays.asList(Objects.requireNonNull(new File(propertyPath).listFiles())).forEach(propertyFile -> {

            InputStream input;

            try {

                input = new FileInputStream(propertyFile);

                props.load(input);

            } catch (IOException e) {

                throw new RuntimeException(e);

            }

        }));

    }


    public static String getValue(String key) {

        String envProperty = System.getenv(key);

        if (envProperty != null && !envProperty.equals("null")) {

            return envProperty;

        }


        String systemProperty = System.getProperty(key);

        if (systemProperty != null && !systemProperty.equals("null")) {

            return systemProperty;

        }


        return props.getProperty(key);

    }

}

上述解決方案允許您將多個路徑傳遞到configuration.path屬性中,以 . 分隔;。


如果您想打開正確的 URL,您只需使用Properties.getValue("baseURL");它會根據您選擇的配置文件從正確的路徑獲取 URL。


現在,除了瀏覽器之外,您可以做類似的事情。我強烈建議閱讀BrowserFactory或Factory設計模式。我只能為您提供有關如何執行此操作的提示,因為我的解決方案可能無法如您所愿。


創建其他配置文件(您可以將多個配置文件與 Maven 一起使用)但用于瀏覽器。


<profile>

            <activation>

                <activeByDefault>true</activeByDefault>

            </activation>

            <id>chrome</id>

            <properties>

                <browser.type>chrome</browser.type>

            </properties>

        </profile>

調用:-Pchrome


記得將它添加到surefire-plugin:


<configuration>

                    <systemPropertyVariables>

                        <configuration.path>${configuration.path}</configuration.path>

                        <browser.type>${browser.type}</browser.type>

                    </systemPropertyVariables>

                </configuration>

您現在要做的就是考慮,您在哪里實例化您的瀏覽器。


簡單的解決方案是(非線程安全?。。。?/p>


public class Browser {


    public static WebDriver getDriver() {

        String browserType = Properties.getValue("browser.type"); //it will get the `chrome` for profile `chrome`

        switch(browserType) {

            case "chrome": return new ChromeDriver();

        }

    }

}

您現在要做的就是在您的框架中實施解決方案。switch添加配置文件,使用所有使用的瀏覽器填寫聲明。


希望能幫助到你!


查看完整回答
反對 回復 2023-05-17
?
慕容3067478

TA貢獻1773條經驗 獲得超3個贊

對不起,如果代碼看起來很垃圾,我傾向于使用 Scala


這是一個使用我設法開始工作的屬性的簡單示例:


省略了部分代碼


private String getBaseUrl() {

        String base = "";

        String foo = System.getProperty("browser", "chrome");

        switch (foo) {

            case "firefox":

                base = "https://www.google.co.uk";

                break;

            case "chrome":

                base = "https://www.bbc.co.uk";

                break;

        }

        return base;

    }




public void goTo() {

        this.driver.get(getBaseUrl());

    }

所以當我使用命令時:


mvn -Dbrowser=chrome test


司機將導航至https://www.bbc.co.uk


如果我使用:


mvn -Dbrowser=firefox test


然后驅動程序將導航到https://www.google.co.uk


如果我只是輸入:


mvn test


然后它將導航到 bbc 因為默認設置為 Chrome


如果你有多個東西,比如不同的網絡驅動程序等,那么它可以用同樣的方式完成。讀入一個屬性,然后根據該屬性的值,初始化您需要的任何驅動程序。


查看完整回答
反對 回復 2023-05-17
  • 2 回答
  • 0 關注
  • 157 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號