第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何在 Maven 中運(yùn)行具有不同配置的 UI 測(cè)試

如何在 Maven 中運(yùn)行具有不同配置的 UI 測(cè)試

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

2 回答

?
有只小跳蛙

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊

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


為了加載不同的屬性,可以根據(jù)環(huán)境,為屬性文件創(chuàng)建多個(gè)文件夾。


假設(shè)您將屬性文件存儲(chǔ)在src/main/java/dev/properties.properties


創(chuàng)建多個(gè)目錄,例如:


src/main/java/qa

src/main/java/dev

src/main/java/prod

在每個(gè)路徑中創(chuàng)建 1 個(gè)屬性文件,就像我在開始時(shí)所做的那樣。


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


給你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>


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


我希望你使用,maven-surefire-plugin因?yàn)檫@就是我將在示例中展示的內(nèi)容。


<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!讓我們關(guān)注systemPropertyVariables. 為了從 Maven 配置文件中獲取屬性,您可以通過將${configuration.paths}變量傳遞到surefire-plugin. 您可以使用相同的名稱。


現(xiàn)在,您需要將.properties文件中的屬性加載到系統(tǒng)中。我寫了一個(gè)類來做到這一點(diǎn),閱讀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);

    }

}

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


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


現(xiàn)在,除了瀏覽器之外,您可以做類似的事情。我強(qiáng)烈建議閱讀BrowserFactory或Factory設(shè)計(jì)模式。我只能為您提供有關(guān)如何執(zhí)行此操作的提示,因?yàn)槲业慕鉀Q方案可能無法如您所愿。


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


<profile>

            <activation>

                <activeByDefault>true</activeByDefault>

            </activation>

            <id>chrome</id>

            <properties>

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

            </properties>

        </profile>

調(diào)用:-Pchrome


記得將它添加到surefire-plugin:


<configuration>

                    <systemPropertyVariables>

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

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

                    </systemPropertyVariables>

                </configuration>

您現(xiàn)在要做的就是考慮,您在哪里實(shí)例化您的瀏覽器。


簡(jiǎn)單的解決方案是(非線程安全!?。。?/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();

        }

    }

}

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


希望能幫助到你!


查看完整回答
反對(duì) 回復(fù) 2023-05-17
?
慕容3067478

TA貢獻(xiàn)1773條經(jīng)驗(yàn) 獲得超3個(gè)贊

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


這是一個(gè)使用我設(shè)法開始工作的屬性的簡(jiǎn)單示例:


省略了部分代碼


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());

    }

所以當(dāng)我使用命令時(shí):


mvn -Dbrowser=chrome test


司機(jī)將導(dǎo)航至https://www.bbc.co.uk


如果我使用:


mvn -Dbrowser=firefox test


然后驅(qū)動(dòng)程序?qū)?dǎo)航到https://www.google.co.uk


如果我只是輸入:


mvn test


然后它將導(dǎo)航到 bbc 因?yàn)槟J(rèn)設(shè)置為 Chrome


如果你有多個(gè)東西,比如不同的網(wǎng)絡(luò)驅(qū)動(dòng)程序等,那么它可以用同樣的方式完成。讀入一個(gè)屬性,然后根據(jù)該屬性的值,初始化您需要的任何驅(qū)動(dòng)程序。


查看完整回答
反對(duì) 回復(fù) 2023-05-17
  • 2 回答
  • 0 關(guān)注
  • 150 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)