本文整理汇总了Java中org.openqa.selenium.WebDriver类的典型用法代码示例。如果您正苦于以下问题:Java WebDriver类的具体用法?Java WebDriver怎么用?Java WebDriver使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebDriver类属于org.openqa.selenium包,在下文中一共展示了WebDriver类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getFirefoxDriver
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
public static WebDriver getFirefoxDriver()
throws Exception
{
baseDir = new File(".").getPath();
System.out.println("******** " + baseDir);
String path = "src/test/resources/geckodriver";
System.out.println("******** " + path);
System.setProperty("webdriver.gecko.driver", path);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
capabilities.setCapability("networkConnectionEnabled", true);
capabilities.setCapability("browserConnectionEnabled", true);
WebDriver driver = new RemoteWebDriver(
new URL("http://localhost:4444"),
capabilities);
// WebDriver driver = new MarionetteDriver(capabilities);
return driver;
}
示例2: checkEmulators
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
private static WebDriver checkEmulators(String browserName, DesiredCapabilities caps, ProjectSettings settings,
Boolean isGrid, String remoteUrl) {
Emulator emulator = settings.getEmulators().getEmulator(browserName);
if (emulator != null) {
switch (emulator.getType()) {
case "Duplicate":
return create(emulator.getDriver(), caps, settings, isGrid, remoteUrl);
case "Emulator":
if (emulator.getDriver().equals("Chrome")) {
return checkAndSetSize(
create(emulator.getDriver(), getChromeUAECaps(caps, emulator), settings, isGrid, remoteUrl),
emulator.getSize());
} else {
return checkAndSetSize(
create(emulator.getDriver(), getFFUAECaps(caps, emulator), settings, isGrid, remoteUrl),
emulator.getSize());
}
case "Chrome Emulator":
return create("Chrome", getChromeEmulatorCaps(caps, emulator.getDriver()), settings, isGrid, remoteUrl);
case "Remote URL": {
return createRemoteDriver(emulator.getRemoteUrl(), caps, settings.getDriverSettings().useProxy(), settings.getDriverSettings());
}
}
}
return null;
}
示例3: mockElement
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
/**
* Create mocked {@link WebElement} object.
*
* @param type element type
* @param value element value
* @param isCheckbox 'true' is checkbox is desired; otherwise 'false'
* @return mocked WebElement object
*/
private static WebElement mockElement(String type, String value, boolean isCheckbox) {
WebElement element = mock(WebElement.class, withSettings().extraInterfaces(WrapsDriver.class));
when(element.getTagName()).thenReturn(type);
if (isCheckbox) {
when(element.getAttribute("type")).thenReturn("checkbox");
when(element.getAttribute("value")).thenReturn("isSelected: " + value);
when(element.isSelected()).thenReturn(Boolean.parseBoolean(value));
} else {
when(element.getAttribute("type")).thenReturn("text");
when(element.getAttribute("value")).thenReturn(value);
when(element.isSelected()).thenReturn(false);
}
WebDriver driver = mockDriver();
when(((WrapsDriver) element).getWrappedDriver()).thenReturn(driver);
return element;
}
示例4: create
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
@Override
public WebDriver create(final Map<String, Object> options) {
new Initialization(() -> {
beforeInitialization(options);
final File workDirectory = getWorkDirectory(options);
final String expectedVersion = (String) options.get(EXPECTED_VERSION);
final boolean forceUpdate = Boolean.parseBoolean((String) options.get(FORCE_UPDATE));
final File webDriverExecutable =
new DownloadWebDriverExecutable(workDirectory, getWebDriverExecutableFromWorkDirectory,
getLatestVersion, downloadExpectedVersion).get(expectedVersion, forceUpdate);
new SystemPropertyWebDriverExecutableSetup(systemPropertyForExecutable, webDriverExecutable)
.setup();
webDriver = instantiateWebDriver(options);
new WebDriverShutdownHook(webDriver).install();
afterInitialization(options);
}).initialize();
return webDriver;
}
示例5: getElements
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
private <T extends TeasyElement> List<T> getElements(By locator, Function<WebDriver, List<WebElement>> condition, TeasyElementType type) {
List<WebElement> webElements;
try {
webElements = fluentWait.waitFor(condition);
} catch (AssertionError ignoredToReturnEmptyList) {
return new ArrayList<>();
}
if (webElements == null) {
return new ArrayList<>();
}
if (context == null) {
return wrapList(webElements, locator, type);
} else {
return wrapList(context, webElements, locator, type);
}
}
示例6: load
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
public void load(SeleniumAction... actions){
WebDriver driver=null;
try {
driver=webDriverPool.get();
WebDriver.Options manage = driver.manage();
manage.window().maximize();
for(SeleniumAction action:actions){
action.execute(driver);
}
} catch (InterruptedException e) {
e.printStackTrace();
log.error("",e);
}finally{
if(driver!=null){
webDriverPool.returnToPool(driver);
}
}
}
示例7: captureScreenshot
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
/**
* This method captures a screenshot
**/
public static void captureScreenshot(WebDriver driver, String screenshotName) {
try {
TakesScreenshot ts = (TakesScreenshot) driver;
File source = ts.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File(dirPath + "/ " + screenshotName
+ "_" + strDateStamp
+ ".png"));
String ESCAPE_PROPERTY = "org.uncommons.reportng.escape-output";
System.setProperty(ESCAPE_PROPERTY, "false");
URL path = new File(dirPath + "/ " + screenshotName + "_"
+ strDateStamp + ".png").toURI().toURL();
String test = "<a href=" + path + "> click to open screenshot of "
+ screenshotName + "</a>";
Reporter.log(screenshotName + test + "<br>");
Reporter.log("<br>");
}
catch (Exception e) {
System.out.println("Exception while taking screenshot "
+ e.getMessage());
}
}
示例8: isAlertPresent
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
private boolean isAlertPresent(WebDriver Driver) {
try {
Driver.switchTo().alert();
return true;
} catch (NoAlertPresentException e) {
Logger.getLogger(this.getClass().getName()).log(Level.OFF, e.getMessage(), e);
return false;
}
}
示例9: getDriverUsingIf
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
public static WebDriver getDriverUsingIf(DesiredCapabilities desiredCapabilities) {
if (desiredCapabilities == null) {
throw new IllegalStateException("DesiredCapabilities are missing!");
}
final String browser = desiredCapabilities.getBrowserName();
if (CHROME.equalsIgnoreCase(browser)) {
return new ChromeDriver(desiredCapabilities);
} else if (FIREFOX.equalsIgnoreCase(browser)) {
return new FirefoxDriver(desiredCapabilities);
} else if (browser.isEmpty()) {
throw new IllegalStateException("'browser' capability is missing!");
}
throw new IllegalArgumentException(desiredCapabilities.getBrowserName() + " browser is not supported!");
}
示例10: apply
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
@Nullable
@Override
public Boolean apply(@Nullable WebDriver webDriver) {
List<String> actualTexts = new ArrayList<>();
errorElements = new ArrayList<>();
boolean isCorrect = true;
for (TeasyElement el : elements) {
actualTexts.add(el.getText());
if (!text.equals(el.getText())) {
isCorrect = false;
errorElements.add(el);
}
}
return isCorrect;
}
示例11: createDriver
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
@Override
public WebDriver createDriver(final Browser browser, final XmlConfig config) {
final RemoteWebDriver driver = mock(RemoteWebDriver.class);
when(driver.getScreenshotAs(OutputType.BYTES)).thenReturn(new byte[]{1, 2, 3});
doReturn(new SessionId(randomAlphanumeric(14))).when(driver).getSessionId();
return driver;
}
示例12: getChromeDriver
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
public static WebDriver getChromeDriver()
{
String path = "src/test/resources/chromedriver";
System.setProperty("webdriver.chrome.driver", path);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("networkConnectionEnabled", true);
capabilities.setCapability("browserConnectionEnabled", true);
return new ChromeDriver(capabilities);
}
示例13: esperaCargaPaginaNoTexto
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
static public void esperaCargaPaginaNoTexto(WebDriver driver, String texto, int timeout)
{
Boolean resultado =
(new WebDriverWait(driver, timeout)).until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("//*[contains(text(),'" + texto + "')]")));
assertTrue(resultado);
}
示例14: canGetArtifact
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
/**
* Determine if the specified driver is capable of taking screenshots.
*
* @param optDriver optional web driver object
* @param logger SLF4J logger object
* @return 'true' if driver can take screenshots; otherwise 'false
*/
public static boolean canGetArtifact(Optional<WebDriver> optDriver, Logger logger) {
if (optDriver.isPresent()) {
WebDriver driver = optDriver.get();
if (driver instanceof TakesScreenshot) {
return true;
} else {
String message = "This driver is not capable of taking a screenshot. If a screenshot is desired, use"
+ " a WebDriver implementation that supports screenshots. For more information, see https://"
+ "seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/TakesScreenshot.html";
logger.warn(message);
}
}
return false;
}
示例15: testEventListenerWithError
import org.openqa.selenium.WebDriver; //导入依赖的package包/类
@Test
public void testEventListenerWithError() {
WebDriver webDriver = rule.getWebDriver();
webDriver.get("https://github.com");
WebElement searchInput = webDriver.findElement(By.cssSelector(".header-search-input"));
searchInput.sendKeys("will", "test");
searchInput.sendKeys(Keys.ENTER);
String searchKeyword = webDriver.findElement(By.cssSelector(".header-search-input")).getAttribute("value");
assertThat(searchKeyword, is("foooooo"));
}