web-dev-qa-db-ja.com

さまざまなブラウザタイプのSelenium WDでシークレット/プライベートウィンドウを開く方法

private windowまたはincognitoでテストケースをテストしたいwindow.

さまざまなブラウザーで同じことを行う方法:

  • firefox(推奨)
  • クロム(推奨)
  • IE
  • サファリ
  • opera

それを達成する方法?

10
atul chaurasia
  • クロム:

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions options = new ChromeOptions();
    options.addArguments("incognito");
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    
  • FireFox:

    FirefoxProfile firefoxProfile = new FirefoxProfile();    
    firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
    
  • インターネットエクスプローラ:

    DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
    capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true); 
    capabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
    
  • オペラ:

    DesiredCapabilities capabilities = DesiredCapabilities.operaBlink();
    OperaOptions options = new OperaOptions();
    options.addArguments("private");
    capabilities.setCapability(OperaOptions.CAPABILITY, options);
    
18
agabrys

chromeでは、-incognitoオプションのコマンドラインスイッチ。自動化拡張機能に問題があるかどうかはわかりませんが、試してみる価値はあります。

ChromeOptions options = new ChromeOptions();
options.addArguments("incognito");

FireFoxの場合、プロファイル内の特別なフラグを目的に使用できます

FirefoxProfile firefoxProfile = new FirefoxProfile();    
firefoxProfile.setPreference("browser.private.browsing.autostart",true);

IEの場合

setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
9
rac2030

次の更新後にのみ、リモートIEをプライベートモードで実行できました。

InternetExplorerOptions options = new InternetExplorerOptions()
                    .ignoreZoomSettings()
                    .useCreateProcessApiToLaunchIe()
                    .addCommandSwitches("-private");

DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
            capabilities.setCapability("se:ieOptions", options);
return new RemoteWebDriver(url, capabilities);

上記のすべては、RemoteWebDriverでは機能しませんでした。

0
ModX
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);
    }
0

ページでbody要素を見つけて、目的のブラウザのKey Chordを起動します。以下のサンプルでは、​​ブラウザをnewTabnewWindowの動作の概要を示す列挙に抽象化しようとしました。 およびnewIncognitoWindow。コンテンツFF、IE、Chrome、Safari、Operaを作成しました。ただし、知識が不足しているため、完全に実装されていない場合があります。

  /**
     * Enumeration quantifying some common keystrokes for Browser Interactions.
     * 
     * @see "http://stackoverflow.com/questions/33224070/how-to-open-incognito-private-window-through-Selenium-Java"
     * @author http://stackoverflow.com/users/5407189/jeremiah
     * @since Oct 19, 2015
     *
     */
    public static enum KeystrokeSupport {
        CHROME,
        FIREFOX {
            @Override
            protected CharSequence getNewIncognitoWindowCommand() {
                return Keys.chord(Keys.CONTROL, Keys.SHIFT, "p");
            }
        },
        IE {
            @Override
            protected CharSequence getNewIncognitoWindowCommand() {
                return Keys.chord(Keys.CONTROL, Keys.SHIFT, "p");
            }
        },
        SAFARI {
            @Override
            protected CharSequence getNewTabCommand() {
                throw new UnsupportedOperationException("Author does not know this keystroke");
            }

            @Override
            protected CharSequence getNewWindowCommand() {
                throw new UnsupportedOperationException("Author does not know this keystroke");
            }

            @Override
            protected CharSequence getNewIncognitoWindowCommand() {
                throw new UnsupportedOperationException("Author does not know this keystroke");
            }
        },
        OPERA {
            @Override
            protected CharSequence getNewIncognitoWindowCommand() {
                throw new UnsupportedOperationException("Author does not know this keystroke");
            }
        };

        public final void newTab(WebDriver driver) {
            WebElement target = getKeystrokeTarget(driver);
            target.sendKeys(getNewTabCommand());
        }

        public final void newWindow(WebDriver driver) {
            WebElement target = getKeystrokeTarget(driver);
            target.sendKeys(getNewWindowCommand());
        }

        public final void newIncognitoWindow(WebDriver driver) {
            WebElement target = getKeystrokeTarget(driver);
            target.sendKeys(getNewIncognitoWindowCommand());
        }

        protected CharSequence getNewTabCommand() {
            return Keys.chord(Keys.CONTROL, "t");
        }

        protected CharSequence getNewWindowCommand() {
            return Keys.chord(Keys.CONTROL, "n");
        }

        protected CharSequence getNewIncognitoWindowCommand() {
            return Keys.chord(Keys.CONTROL, Keys.SHIFT, "t");
        }

        protected final WebElement getKeystrokeTarget(WebDriver driver) {
            WebDriverWait wait = new WebDriverWait(driver, 10);
            return wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("body")));
        }

    }

その後、各構成を実行し、視覚的検証の動作を実行するパラメーター化されたテストを提供できます。必要なアサートをテストに追加できます。

package stackoverflow.proof.Selenium;

import Java.util.Collection;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.openqa.Selenium.By;
import org.openqa.Selenium.Keys;
import org.openqa.Selenium.WebDriver;
import org.openqa.Selenium.WebElement;
import org.openqa.Selenium.firefox.FirefoxDriver;
import org.openqa.Selenium.support.ui.ExpectedConditions;
import org.openqa.Selenium.support.ui.WebDriverWait;


import com.google.common.base.Supplier;
import com.google.common.collect.Lists;



/**
 * Test to try out some various browser keystrokes and try to get the environment to do what we want.
 * 
 * @see "http://stackoverflow.com/questions/33224070/how-to-open-incognito-private-window-through-Selenium-Java"
 * @author http://stackoverflow.com/users/5407189/jeremiah
 * @since Oct 19, 2015
 *
 */
@RunWith(Parameterized.class)
public class KeyStrokeTests {


    @Parameters(name="{0}")
    public static Collection<Object[]> buildTestParams() {
        Collection<Object[]> params = Lists.newArrayList();
        Supplier<WebDriver> ffS = new Supplier<WebDriver>() {
            public WebDriver get() {
                return new FirefoxDriver();
            }
        };
        params.add(new Object[]{KeystrokeSupport.FIREFOX, ffS});

      /* I'm not currently using these browsers, but this should work with minimal effort.
        Supplier<WebDriver> chrome = new Supplier<WebDriver>() {
            public WebDriver get() {
                return new ChromeDriver();
            }
        };
        Supplier<WebDriver> ie = new Supplier<WebDriver>() {
            public WebDriver get() {
                return new InternetExplorerDriver();
            }
        };
        Supplier<WebDriver> safari = new Supplier<WebDriver>() {
            public WebDriver get() {
                return new SafariDriver();
            }
        };
        Supplier<WebDriver> opera = new Supplier<WebDriver>() {
            public WebDriver get() {
                return new OperaDriver();
            }
        };

        params.add(new Object[]{KeystrokeSupport.CHROME, chrome});
        params.add(new Object[]{KeystrokeSupport.IE, ie});
        params.add(new Object[]{KeystrokeSupport.SAFARI, safari});
        params.add(new Object[]{KeystrokeSupport.OPERA, opera});
        */
        return params;
    }
    Supplier<WebDriver> supplier;
    WebDriver driver;
    KeystrokeSupport support;

    public KeyStrokeTests(KeystrokeSupport support,Supplier<WebDriver> supplier) {
        this.supplier = supplier;
        this.support = support;
    }

    @Before
    public void setup() {
        driver = supplier.get();
        driver.get("http://google.com");
    }

    @Test
    public void testNewTab() {
        support.newTab(driver);
    }
    @Test
    public void testNewIncognitoWindow() {
        support.newIncognitoWindow(driver);
    }

    @Test
    public void testNewWindow() {
        support.newWindow(driver);
    }

    @After
    public void lookAtMe() throws Exception{
        Thread.sleep(5000);    
          for (String handle : driver.getWindowHandles()) {
            driver.switchTo().window(handle);
            driver.close();
        }
    }
  }

最高の幸運。

0
Jeremiah

Chromeこのコードを使用して、シークレットモードでブラウザを開きます。

public WebDriver chromedriver;
ChromeOptions options = new ChromeOptions();
options.addArguments("-incognito");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver chromedriver=new ChromeDriver(capabilities);
chromedriver.get("url");
0
FirefoxOptions opts = new FirefoxOptions();
opts.addArguments("-private");
FirefoxDrive f = new FirefoxDriver(opts);

これは、Seleniumバージョン3.14.0およびgeckodriver-v0.22.0で正常に動作します

0
amitkumar
public class gettext {
  static WebDriver driver= null;

  public static void main(String args[]) throws InterruptedException {

    //for private window

    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    ChromeOptions option = new ChromeOptions();
    option.addArguments("incognito");
    capabilities.setCapability(ChromeOptions.CAPABILITY,option);
    System.setProperty("webdriver.chrome.driver", "D:\\Tools\\chromedriver.exe");       
    driver= new ChromeDriver(capabilities);

    String url = "https://www.google.com/";
    driver.manage().window().maximize();
    driver.get(url);
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    gettextdata();
  } 
}
0
palak