2015-10-19 208 views
9

Ben veya pencerede gizli özel pencerede benim test durumları test etmek istiyorum.Farklı tarayıcı türleri için Selenium WD ile gizli/özel pencere nasıl açılır?

çeşitli tarayıcılarda aynı Nasıl yapılır?

  • firefox (tercih)
  • krom (tercih)
  • IE
  • safari
  • opera

Nasıl elde edilir?

+1

google biliyor ... sordun mu? – JeffC

+0

Olası kopyası [Selenyum'da inconginto Modunda krom tarayıcı çalıştır] (http://stackoverflow.com/questions/19026295/run-chrome-browser-in-inconginto-mode-in-selenium) – JeffC

+1

@JeffC Google'a sordum ve beni buraya getirdi - arsız piç: p – hd1

cevap

0

Sayfadaki gövde elemanını bulun ve sonra istediğiniz tarayıcı için bir Anahtar Akorunu ateşleyin. Aşağıdaki örnekte, tarayıcıları newTab, newWindow ve newIncognitoWindow davranışlarını özetleyen bir numaralandırma için tarayıcıları soyutlamaya çalıştım. FF, IE, Chrome, Safari ve Opera içerik yaptım; Ancak, bilgi eksikliğim nedeniyle tamamen uygulanamayabilirler.

sonra önüne aldığımızda
/** 
    * 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"))); 
     } 

    } 

, biz yapılandırmaların her üzerinden çalışmasını ve görsel doğrulama için davranışlarını çalıştırır parametreli testi sunabilir. Test yapmak istediğiniz her şeyi eklemek isteyebilirsiniz.

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

Şansın En İyisi.

+1

Fikirler almaktan nefret ediyorum, bu muhtemelen işe yarayabilir, ancak birkaç eksi oyla ve bu konuda neyin yanlış olduğu hakkında yorum yapmam. – parvuselephantus

8

Kromda, seçenekler arasında -incognito komut satırı anahtarını kullanmayı deneyebilirsiniz, otomasyon uzantısında bir sorun olup olmayacağından emin olun, ancak bir denemeye değer.

FireFox için
ChromeOptions options = new ChromeOptions(); 
options.addArguments("incognito"); 

, profildeki özel bayrak amaçla kullanılabilecek IE

için

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

setCapability(InternetExplorerDriver.IE_SWITCHES, "-private"); 
+1

Firefox için yukarıdaki kod benim durumumda çalışmıyor. – Ronak

16
  • Krom:

    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); 
    
  • Internet Explorer:

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

    DesiredCapabilities capabilities = DesiredCapabilities.operaBlink(); 
    OperaOptions options = new OperaOptions(); 
    options.addArguments("private"); 
    capabilities.setCapability(OperaOptions.CAPABILITY, options); 
    
    Chrome için
+1

Gelecek ziyaretçi için bu selenium 2.53 ile çalışmaz, 2.52 ile çalışır. – pr4bh4sh

+0

Hangi noktalar çalışmıyor? Selenium 3.0.1 kullanıyoruz ve her şey doğru çalışıyor. – agabrys

+1

Evet, 3.0.1 ile çalışıyor, ne yazık ki en son sürüme yükseltmek için (izin verilen) kullanamıyorum. 2 saatten fazla israf ettim Bu yüzden aynı sorunla karşılaşan biri için yararlı olabileceğini düşündüm. – pr4bh4sh

0

gizli modda tarayıcıyı açmak için bu kodu kullanın:

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