WebDriver 由用户输入选择,用于使用 TestNG 套件进行的 Selenium 测试

问题描述 投票:0回答:0

我正在尝试将

Scanner()
类集成到我现有的 Selenium 测试用例中,以便用户可以选择 WebDriver 来运行测试,并且相信该值需要传递给 TestNG 套件。

以下是我已经具备的以及我尝试实施的内容:

选择司机

public static String selectDriver() {

    Scanner reader = new Scanner(System.in);
    System.out.println("Please select a platform and a device to run the tests on:\n" +
            "Windows\n" +
            "\t1. Google Chrome\n" +
            "\t2. Firefox\n" +
            "\t3. Microsoft Edge\n\n" +
            "Android\n" +
            "\t4. Google Chrome");

    int driverSelection = reader.nextInt();

    if (driverSelection == 1) {
        return "chromeWindows";
    } else if (driverSelection == 2) {
        return "firefoxWindows";
    } else if (driverSelection == 3) {
        return "edgeWindows";
    } else if (driverSelection == 4) {
        return "chromeAndroid";
    }
    reader.close();
    return null;
}

网络驱动程序

@Parameters("browser")
@BeforeClass
public void webDriver(String browser) throws IOException {

    if (browser.equalsIgnoreCase("chromeWindows")) {

        List<String> driverArguments = List.of("start-maximized", "remote-debugging-port=9222", "disable-popup-blocking", "disable-notifications", "--remote-allow-origins=*"); 
        System.setProperty("webdriver.chrome.driver", currentUserDirectory + Config.config("chromeDriverPath"));
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("excludeSwitches", new String[]{"enable-automation"});
        options.addArguments(driverArguments);
        setWebDriver(new ChromeDriver(options));

TestSuite.xml

    !DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd"
suite name="Desktop - Selenium Tests"

    parameter name="browser" value="chrome"/

    <test name="Test1" >
        <classes>
            <class name="tests.desktop.homePageTest"/>
        </classes>
    </test>
</suite>

如果没有更简单的方法来实现我想要的,我将不得不考虑根据用户的输入编辑 XML 文件以提供 browser 值。

java selenium-webdriver testng
© www.soinside.com 2019 - 2024. All rights reserved.