atu*_*sia 10 java browser selenium-webdriver
我想在私人窗口或隐身窗口中测试我的测试用例.
如何在各种浏览器中做同样的事情:
怎么实现呢?
aga*_*rys 19
铬:
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("incognito");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
Run Code Online (Sandbox Code Playgroud)火狐:
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.privatebrowsing.autostart", true);
Run Code Online (Sandbox Code Playgroud)IE浏览器:
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true);
capabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
Run Code Online (Sandbox Code Playgroud)歌剧:
DesiredCapabilities capabilities = DesiredCapabilities.operaBlink();
OperaOptions options = new OperaOptions();
options.addArguments("private");
capabilities.setCapability(OperaOptions.CAPABILITY, options);
Run Code Online (Sandbox Code Playgroud)小智 9
在chrome中你可以尝试-incognito在选项中使用命令行开关,不确定自动化扩展是否会出现问题,但值得一试.
ChromeOptions options = new ChromeOptions();
options.addArguments("incognito");
Run Code Online (Sandbox Code Playgroud)
对于FireFox,配置文件中的特殊标志可用于此目的
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.setPreference("browser.private.browsing.autostart",true);
Run Code Online (Sandbox Code Playgroud)
对于IE
setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
Run Code Online (Sandbox Code Playgroud)
在页面上找到主体元素,然后为您想要的浏览器触发一个关键和弦。在下面的示例中,我尝试将浏览器抽象为一个枚举,该枚举概述了newTab、newWindow和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")));
}
}
Run Code Online (Sandbox Code Playgroud)
这样,我们就可以提供参数化测试,该测试将运行每个配置并执行视觉验证的行为。您可能想将任何您想要的断言添加到测试中。
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
祝你好运。
| 归档时间: |
|
| 查看次数: |
28469 次 |
| 最近记录: |