How to open incognito/private window with Selenium WD for different browser types?

后端 未结 9 898
感情败类
感情败类 2020-12-05 08:18

I want to test my test cases in private window or incognito window.

How to do the same in various browsers:

9条回答
  •  北海茫月
    2020-12-05 08:59

    public static void OpenBrowser() {
        if (Browser.equals("Chrome")) {
            System.setProperty("webdriver.chrome.driver", "E:\\Workspace\\proj\\chromedriver.exe");
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            ChromeOptions options = new ChromeOptions();
            options.addArguments("incognito");
            capabilities.setCapability(ChromeOptions.CAPABILITY, options);
            driver = new ChromeDriver(capabilities);
        } else if (Browser.equals("IE")) {
    
            DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
            capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, false);
    // if you get this exception "org.openqa.selenium.remote.SessionNotFoundException: " . uncomment the below line and comment the above line
    // capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true);
            System.setProperty("webdriver.ie.driver", "E:\\Workspace\\proj\\IEDriverServer32.exe");capabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
            driver = new InternetExplorerDriver(capabilities);
        } else {
            FirefoxProfile firefoxProfile = new FirefoxProfile();
            firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
            driver = new FirefoxDriver(firefoxProfile);
        }
    

提交回复
热议问题