public static WebDriver selectLocalBrowser() { switch (browser) { case Chrome: return new ChromeDriver(); case Firefox: return new FirefoxDriver(); case IE: return new InternetExplorerDriver(); case Opera: return new OperaDriver(); case Safari: return new SafariDriver(); default: throw new WebDriverException("No browser specified"); } }
public SeleniumExtension() { handlerMap.put(ChromeDriver.class, ChromeDriverHandler.class); handlerMap.put(FirefoxDriver.class, FirefoxDriverHandler.class); handlerMap.put(EdgeDriver.class, EdgeDriverHandler.class); handlerMap.put(OperaDriver.class, OperaDriverHandler.class); handlerMap.put(SafariDriver.class, SafariDriverHandler.class); handlerMap.put(RemoteWebDriver.class, RemoteDriverHandler.class); handlerMap.put(AppiumDriver.class, AppiumDriverHandler.class); handlerMap.put(List.class, ListDriverHandler.class); handlerMap.put(PhantomJSDriver.class, OtherDriverHandler.class); templateHandlerMap.put("chrome", ChromeDriver.class); templateHandlerMap.put("firefox", FirefoxDriver.class); templateHandlerMap.put("edge", EdgeDriver.class); templateHandlerMap.put("opera", OperaDriver.class); templateHandlerMap.put("safari", SafariDriver.class); templateHandlerMap.put("appium", AppiumDriver.class); templateHandlerMap.put("phantomjs", PhantomJSDriver.class); templateHandlerMap.put("chrome-in-docker", RemoteWebDriver.class); templateHandlerMap.put("firefox-in-docker", RemoteWebDriver.class); templateHandlerMap.put("opera-in-docker", RemoteWebDriver.class); }
@Override public void resolve() { try { Optional<Object> testInstance = context.getTestInstance(); Optional<Capabilities> capabilities = annotationsReader .getCapabilities(parameter, testInstance); SafariOptions safariOptions = (SafariOptions) getOptions(parameter, testInstance); if (capabilities.isPresent()) { safariOptions.merge(capabilities.get()); } object = new SafariDriver(safariOptions); } catch (Exception e) { handleException(e); } }
@Override public WebDriver getDefaultDriver() { String seleniumBrowser = System.getProperty( "selenium.browser" ); if ( StringUtils.contains( seleniumBrowser, "chrome" ) ) { return new ChromeDriver(); } if ( StringUtils.contains( seleniumBrowser, "safari" ) ) { return new SafariDriver(); } if ( StringUtils.contains( seleniumBrowser, "iexplore" ) ) { return new InternetExplorerDriver(); } return new FirefoxDriver(); }
WebDriver start(Capabilities capabilities) { switch (this) { case FIREFOX: return new FirefoxDriver(capabilities); case HTML_UNIT: return new HtmlUnitDriver(capabilities); case CHROME: return new ChromeDriver(capabilities); case EDGE: return new EdgeDriver(capabilities); case INTERNET_EXPLORER: return new InternetExplorerDriver(capabilities); case SAFARI: return new SafariDriver(capabilities); default: throw new AssertionError(); } }
@Override public WebDriver createWebDriver() { DesiredCapabilities cap = new SafariCapabilitiesFactory().createCapabilities(webDriverConfig); System.out.println("Begin Safari"); synchronized (this.getClass()) { driver = new SafariDriver(cap); } System.out.println("safari started"); this.setWebDriver(driver); // Implicit Waits handles dynamic element. setImplicitWaitTimeout(webDriverConfig.getImplicitWaitTimeout()); if (webDriverConfig.getPageLoadTimeout() >= 0) { TestLogging.log("Safari doesn't support pageLoadTimeout"); } return driver; }
@Test public void dragAndDropSafari() throws InterruptedException { WebDriver driver = new SafariDriver(); driver.get("http://www.w3schools.com/html/html5_draganddrop.asp"); WebElement source = driver.findElement(id("drag1")); System.out.println(source.getAttribute("src")); WebElement target = driver.findElement(id("div2")); System.out.println(target.getTagName() + "=" + target.toString()); Actions builder = new Actions(driver); Action dragAndDrop = builder.clickAndHold(source) .moveToElement(target) .release(source) .build(); dragAndDrop.perform(); }
public static WebDriver getDriver(String browserType){ if ( StringUtils.isEmpty(browserType) || FIREFOX.equals(browserType)) { return new FirefoxDriver(SeleniumBrowserFactory.getBrowserCapabilities(browserType)); } else if (CHROME.equals(browserType)) { return new ChromeDriver(SeleniumBrowserFactory.getBrowserCapabilities(browserType)); } else if (IE.equals(browserType)) { return new InternetExplorerDriver(SeleniumBrowserFactory.getBrowserCapabilities(browserType)); } else if (PHANTOMJS.equals(browserType)) { return new PhantomJSDriver(); } else if (SAFARI.equals(browserType)) { return new SafariDriver(); } else if (EDGE.equals(browserType)) { return new EdgeDriver(); } else { throw new RuntimeException(String.format("Unknown browser type: \"%s\"", browserType)); } }
/** * This method initialises the ThreadLocal WebDriver object according to the * supplied parameters to enable tests to run on a users local machine. */ private void setLocalWebdriver() { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setJavascriptEnabled(true); capabilities.setCapability("handlesAlerts", true); switch (getBrowserId(browser)) { case 0: capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, Boolean.valueOf(System.getProperty("IGNORE_SECURITY_DOMAINS", "false"))); driver.set(new InternetExplorerDriver(capabilities)); break; case 1: driver.set(new FirefoxDriver(capabilities)); break; case 2: driver.set(new SafariDriver(capabilities)); break; case 3: driver.set(new ChromeDriver(capabilities)); break; default: throw new WebDriverException("Browser not found: " + browser); } }
/** * This method initialises the ThreadLocal WebDriver object according to the * supplied parameters to enable tests to run on a users local machine. * * @param browser This can be set to "chrome", "internet explorer" or "firefox" * @param version This should be set to reflect the browser required. It is only * used when matching capabilities for a RemoteWebDriver object * however. * @param platform This should be set to whatever platform the test is required * to run on e.g. "WINDOWS", "MAC", "LINUX". It is only used when * matching capabilities for a RemoteWebDriver object however. */ public void setLocalWebdriver(String browser, String version, String platform) { DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setJavascriptEnabled(true); capabilities.setCapability("handlesAlerts", true); switch (getBrowserId(browser)) { case 0: capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, Boolean.valueOf(System.getProperty("IGNORE_SECURITY_DOMAINS", "false"))); driver.set(new InternetExplorerDriver(capabilities)); break; case 1: driver.set(new FirefoxDriver(capabilities)); break; case 2: driver.set(new SafariDriver(capabilities)); break; case 3: driver.set(new ChromeDriver(capabilities)); break; default: throw new WebDriverException("Browser not found: " + browser); } }
@Override public SafariDriver getObject() throws BeansException { if (properties.getSafari().isEnabled()) { try { return new SafariDriver(); } catch (WebDriverException e) { e.printStackTrace(); // swallow the exception } } return null; }
@Test public void testWithMockedSafari() { load(new Class[]{MockSafariConfiguration.class}, "com.greglturnquist.webdriver.firefox.enabled:false", "com.greglturnquist.webdriver.chrome.enabled:false"); WebDriver driver = context.getBean(WebDriver.class); assertThat(ClassUtils.isAssignable(TakesScreenshot.class, driver.getClass())).isTrue(); assertThat(ClassUtils.isAssignable(SafariDriver.class, driver.getClass())).isTrue(); }
static Stream<Arguments> forcedTestProvider() { return Stream.of( Arguments.of(AppiumDriverHandler.class, ForcedAppiumJupiterTest.class, AppiumDriver.class, "appiumNoCapabilitiesTest"), Arguments.of(AppiumDriverHandler.class, ForcedAppiumJupiterTest.class, AppiumDriver.class, "appiumWithCapabilitiesTest"), Arguments.of(ChromeDriverHandler.class, ForcedBadChromeJupiterTest.class, ChromeDriver.class, "chromeTest"), Arguments.of(FirefoxDriverHandler.class, ForcedBadFirefoxJupiterTest.class, FirefoxDriver.class, "firefoxTest"), Arguments.of(RemoteDriverHandler.class, ForcedBadRemoteJupiterTest.class, RemoteWebDriver.class, "remoteTest"), Arguments.of(EdgeDriverHandler.class, ForcedEdgeJupiterTest.class, EdgeDriver.class, "edgeTest"), Arguments.of(OperaDriverHandler.class, ForcedOperaJupiterTest.class, OperaDriver.class, "operaTest"), Arguments.of(SafariDriverHandler.class, ForcedSafariJupiterTest.class, SafariDriver.class, "safariTest")); }
@ParameterizedTest @MethodSource("testClassProvider") @SuppressWarnings("deprecation") void testSafariOptions(Class<?> testClass) throws Exception { Parameter parameter = testClass .getMethod("safariTest", SafariDriver.class).getParameters()[0]; Optional<Object> testInstance = Optional.of(testClass.newInstance()); SafariOptions safariOptions = (SafariOptions) annotationsReader .getOptions(parameter, testInstance); assertTrue(safariOptions.getUseCleanSession()); assertFalse(safariOptions.getUseTechnologyPreview()); }
/** * {@inheritDoc} */ @Override public SafariDriver start(Capabilities other) { Capabilities capabilities = this.mergeCapabilities(other); if (capabilities == null) { return new SafariDriver(); } return new SafariDriver(capabilities); }
/** * Load the default configuration. It will not erase previous non-default configurations. * * @return Itself. */ public SeleniumConfiguration loadDefaultConfiguration() { this.getConfiguration().put(ChromeDriver.class, new ChromeConfiguration()); this.getConfiguration().put(EdgeDriver.class, new EdgeConfiguration()); this.getConfiguration().put(FirefoxDriver.class, new FirefoxConfiguration()); this.getConfiguration().put(HtmlUnitConfiguration.class, new HtmlUnitConfiguration()); this.getConfiguration().put(InternetExplorerConfiguration.class, new InternetExplorerConfiguration()); this.getConfiguration().put(OperaConfiguration.class, new OperaConfiguration()); this.getConfiguration().put(PhantomJSConfiguration.class, new PhantomJSConfiguration()); this.getConfiguration().put(SafariDriver.class, new SafariConfiguration()); return this; }
private WebDriver initSafariDriver(String appURL) { System.out.println("Launching Safari browser.."); WebDriver driver = new SafariDriver(); driver.manage().window().maximize(); driver.navigate().to(appURL); return new WebDriverProxy().protect(driver); }
@Before public void prepare() { testUrl = "https://leftstick.github.io/"; driver = new SafariDriver(); driver.manage().window().maximize(); driver.get(testUrl); }
@Parameters(name = "{index}: {0} {1}") public static Collection<Object[]> data() { return asList(new Object[][] { { SafariDriver.class, WebDriverException.class }, { EventFiringWebDriver.class, InstantiationException.class }, { HtmlUnitDriver.class, null }, { RemoteWebDriver.class, IllegalAccessException.class } }); }
/** * <p> * remote.public.driver set to chrome or firefox (null assumes firefox). * </p><p> * if remote.public.hub is set a RemoteWebDriver is created (Selenium Grid) * if proxy.host is set, the web driver is setup to use a proxy * </p> * * @return WebDriver or null if unable to create */ public static WebDriver getWebDriver() { String driverParam = System.getProperty(HUB_DRIVER_PROPERTY); String hubParam = System.getProperty(HUB_PROPERTY); String proxyParam = System.getProperty(PROXY_HOST_PROPERTY); // setup proxy if specified as VM Arg DesiredCapabilities capabilities = new DesiredCapabilities(); WebDriver webDriver = null; if (StringUtils.isNotEmpty(proxyParam)) { capabilities.setCapability(CapabilityType.PROXY, new Proxy().setHttpProxy(proxyParam)); } if (hubParam == null) { if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) { FirefoxProfile profile = new FirefoxProfile(); profile.setEnableNativeEvents(false); capabilities.setCapability(FirefoxDriver.PROFILE, profile); return new FirefoxDriver(capabilities); } else if ("chrome".equalsIgnoreCase(driverParam)) { return new ChromeDriver(capabilities); } else if ("safari".equals(driverParam)) { System.out.println("SafariDriver probably won't work, if it does please contact Erik M."); return new SafariDriver(capabilities); } } else { try { if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) { return new RemoteWebDriver(new URL(getHubUrlString()), DesiredCapabilities.firefox()); } else if ("chrome".equalsIgnoreCase(driverParam)) { return new RemoteWebDriver(new URL(getHubUrlString()), DesiredCapabilities.chrome()); } } catch (MalformedURLException mue) { System.out.println(getHubUrlString() + " " + mue.getMessage()); mue.printStackTrace(); } } return null; }
@Override public WebDriver getWebDriver(Capabilities capabilities) { if (Driver.isMobile()) { throw new IllegalArgumentException( "seleniumGridURL or sauceUser and sauceKey must be specified when running on iOS"); } else { return new SafariDriver(new SafariOptions(capabilities)); } }
/** * @return true when current driver is connected to either a local or remote Safari. */ public boolean connectedToSafari() { boolean result = false; WebDriver driver = driver(); if (driver instanceof SafariDriver) { result = true; } else if (driver instanceof RemoteWebDriver) { result = checkRemoteBrowserName(driver, "safari"); } return result; }
private static WebDriver createLocalDriver(Capabilities capabilities) { String browserType = capabilities.getBrowserName(); if (browserType.equals(BrowserType.FIREFOX)) return new FirefoxDriver(capabilities); if (browserType.equals(BrowserType.IE)) return new InternetExplorerDriver(capabilities); if (browserType.equals(BrowserType.CHROME)) return new ChromeDriver(capabilities); if (browserType.equals(BrowserType.SAFARI)) return new SafariDriver(capabilities); throw new Error("Unsupported browser type: " + browserType); }
@Override public RemoteWebDriver createWebDriver() { SafariDriver safari = new SafariDriver(); if (OS.isFamilyMac()) { try { // put the browser in the foreground: String cmdline = "open -a safari"; SeleniumJavaRobot.log("Executing: " + cmdline); Runtime.getRuntime().exec(cmdline); } catch (Exception e) { } } return safari; }
@Test public void shouldBeInstanceOfUntargetedSafariDriver() { WebDriverBrowserFactory browserFactory = new SafariBrowserFactory(); browser = (WebDriverBrowser) browserFactory.newBrowser(); ForwardingTargetedWebDriver webDriver = (ForwardingTargetedWebDriver) browser.getWrappedDriver(); CachingTargetLocator targetLocator = (CachingTargetLocator) webDriver.getTargetLocator(); assertThat(targetLocator.getUntargetedDriver(), instanceOf(SafariDriver.class)); }
@Test public void findHowItWorksLinkUsingPartialLinkText() { WebDriver driver = new SafariDriver(); driver.get("http://ticketfly.com/careers"); WebElement element = driver.findElement(By.linkText("How It Works")); System.out.println("element=" + element); System.out.println("element.getTagName()=" + element.getTagName()); System.out.println("element.getText()=" + element.getText()); }
/** * initialization SafariDriver * * @param capabilities - desired capabilities */ public static void initSafariDriver(Capabilities capabilities) { ReporterNGExt.logTechnical("Initialization Safari Driver"); setWebDriver(new SafariDriver(capabilities)); setTimeout(TIMEOUT); getDriver().manage().window().maximize(); }
/** * initialization SafariDriver */ public static void initSafariDriver() { ReporterNGExt.logTechnical("Initialization Safari Driver"); setWebDriver(new SafariDriver()); setTimeout(TIMEOUT); getDriver().manage().window().maximize(); }
@Override public WebDriver getDefaultDriver() { String browser = System.getProperty("fluentlenium.browser"); LOGGER.debug("Selecting Selenium Browser using " + browser); if (browser == null) { LOGGER.debug("Using default HTML Unit Driver"); return new HtmlUnitDriver(); } if ("chrome".equalsIgnoreCase(browser)) { LOGGER.debug("Using Chrome"); return new ChromeDriver(); } if ("firefox".equalsIgnoreCase(browser)) { LOGGER.debug("Using Firefox"); return new FirefoxDriver(); } if ("ie".equalsIgnoreCase(browser) || "internetexplorer".equalsIgnoreCase(browser)) { LOGGER.debug("Using Internet Explorer"); return new InternetExplorerDriver(); } if ("safari".equalsIgnoreCase(browser)) { LOGGER.debug("Using Safari"); return new SafariDriver(); } throw new IllegalArgumentException("Unknown browser : " + browser); }
public static void init(WebDriver driver) { if (driver != null) { LOGGER.info("==============================================================="); LOGGER.info("| Open Selenium Web Driver "); LOGGER.info("===============================================================\n"); WebDriverConfig.driver = driver; WebLocator.setDriverExecutor(driver); if (driver instanceof InternetExplorerDriver) { isIE = true; } else if (driver instanceof ChromeDriver) { isChrome = true; } else if (driver instanceof FirefoxDriver) { isFireFox = true; } else if (driver instanceof SafariDriver) { isSafari = true; } else if (driver instanceof OperaDesktopDriver) { isOpera = true; } driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(WebLocatorConfig.getInt("driver.implicitlyWait"), TimeUnit.MILLISECONDS); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { if (WebLocatorConfig.getBoolean("driver.autoClose")) { initSeleniumEnd(); } } }); } }
/** * remote.public.driver set to chrome or firefox (null assumes firefox) * if remote.public.hub is set a RemoteWebDriver is created (Selenium Grid) * if proxy.host is set, the web driver is setup to use a proxy * @return WebDriver or null if unable to create */ public static WebDriver getWebDriver() { String driverParam = System.getProperty(ITUtil.HUB_DRIVER_PROPERTY); String hubParam = System.getProperty(ITUtil.HUB_PROPERTY); String proxyParam = System.getProperty(PROXY_HOST_PROPERTY); // setup proxy if specified as VM Arg DesiredCapabilities capabilities = new DesiredCapabilities(); WebDriver webDriver = null; if (StringUtils.isNotEmpty(proxyParam)) { capabilities.setCapability(CapabilityType.PROXY, new Proxy().setHttpProxy(proxyParam)); } if (hubParam == null) { if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) { FirefoxProfile profile = new FirefoxProfile(); profile.setEnableNativeEvents(false); capabilities.setCapability(FirefoxDriver.PROFILE, profile); return new FirefoxDriver(capabilities); } else if ("chrome".equalsIgnoreCase(driverParam)) { return new ChromeDriver(capabilities); } else if ("safari".equals(driverParam)) { System.out.println("SafariDriver probably won't work, if it does please contact Erik M."); return new SafariDriver(capabilities); } } else { try { if (driverParam == null || "firefox".equalsIgnoreCase(driverParam)) { return new RemoteWebDriver(new URL(ITUtil.getHubUrlString()), DesiredCapabilities.firefox()); } else if ("chrome".equalsIgnoreCase(driverParam)) { return new RemoteWebDriver(new URL(ITUtil.getHubUrlString()), DesiredCapabilities.chrome()); } } catch (MalformedURLException mue) { System.out.println(ITUtil.getHubUrlString() + " " + mue.getMessage()); mue.printStackTrace(); } } return null; }
@Bean SafariDriverFactory safariDriverFactory() { SafariDriverFactory factory = mock(SafariDriverFactory.class); given(factory.getObject()).willReturn(mock(SafariDriver.class)); return factory; }
@Override public WebDriver getWebDriver() { WebDriver driver = new SafariDriver(); return driver; }
@Override public WebDriver createDriver(Capabilities capabilities) { return new SafariDriver((SafariOptions) capabilities); }
@Test public void safariTest(SafariDriver driver) { assumeFalse(IS_OS_MAC); assertThat(driver, nullValue()); }
@Test public void safariTest(SafariDriver driver) { driver.get("https://bonigarcia.github.io/selenium-jupiter/"); assertThat(driver.getTitle(), containsString("A JUnit 5 extension for Selenium WebDriver")); }
@Test public void test(SafariDriver driver) { driver.get("http://www.seleniumhq.org/"); assertThat(driver.getTitle(), containsString("A JUnit 5 extension for Selenium WebDriver")); }
@Test @Ignore public void testSafariDriver() throws Exception { assumeTrue(System.getenv("Path").contains("Safari")); assertTrue(Drivers.SAFARI.get() instanceof SafariDriver); }