0

我正在寻找在 INCOGNITO 中打开 chrome 的选项,以便使用 SelenIDE/JUNIT/Maven 进行自动化测试。

我不确定我需要在这里提供什么样的信息。我将添加一个用于控制 WebdriverRunner 的 LocalTest 页面的副本。

我在测试类本身中调用了实际的 Url,因为该站点有不同的要测试的起点。

这就是我调用 URL 的方式:

@Before
public void OpenCarUrl(){WebDriverRunner.getWebDriver().get("MyUrl");}

我希望这已经足够了,但如果我需要提供更多信息,我随时准备回答。

我们的 LocalTest 的副本

public class LocalTest extends SelenideHelperClass {

private static Logger logger = LoggerFactory.getLogger(LocalTest.class);


private static boolean osIs(String os) {
    return System.getProperty("os.name").toLowerCase().contains(os);
}


@BeforeClass
public static void setupDriver() {
    if (System.getProperty("CHROMEDRIVER_HOME") != null) {
        System.setProperty("webdriver.chrome.driver", System.getProperty("CHROMEDRIVER_HOME") + "/chromedriver");
    } else if (System.getenv("CHROMEDRIVER_HOME") != null) {
        System.setProperty("webdriver.chrome.driver", System.getenv("CHROMEDRIVER_HOME") + "/chromedriver");
    } else {
        String driver;
        if (osIs("mac")) {
            driver = "chromedriver-mac";
        } else if (osIs("windows")) {
            driver = "chromedriver.exe";
        } else if (osIs("linux")) {
            driver = "chromedriver-linux";
        } else {
            throw new RuntimeException("OS could not be determined, chromedriver cannot be set or is unavailable");
        }
        System.setProperty("webdriver.chrome.driver", System.getProperty("java.io.tmpdir") + "/chromedriver/" + driver);
    }

    System.setProperty("selenide.browser", "Chrome");
    logger.info("Set selenide.browser to Chrome");
    logger.info("Set webdriver.chrome.driver to " + System.getProperty("webdriver.chrome.driver"));

    // ----------------


}

@AfterClass
public static void closeDriver() {
WebDriverRunner.getWebDriver().quit();
}
}
4

1 回答 1

0

我找到了解决方案:

...
 } else {
                throw new RuntimeException("OS could not be determined, chromedriver cannot be set or is unavailable");
            }


            ChromeOptions option = new ChromeOptions();
            option.addArguments("start-maximized");
            option.addArguments("incognito");
            System.setProperty("webdriver.chrome.driver", System.getProperty("java.io.tmpdir") + "/chromedriver/" + driver);

            ChromeDriver webdriver = new ChromeDriver(option);
            WebDriverRunner.setWebDriver(webdriver);


        }

        System.setProperty("selenide.browser", "Chrome");
        logger.info("Set selenide.browser to Chrome");
        logger.info("Set webdriver.chrome.driver to " + System.getProperty("webdriver.chrome.driver"));
    }

....
于 2018-10-10T11:23:08.793 回答