Permalink
Browse files

Added acceptance test for custom driver usage

  • Loading branch information...
1 parent 3969333 commit 25cd3cd77caf8accca7441d0a8e38a6f1b4d9b2b @amolenaar amolenaar committed Nov 22, 2012
@@ -0,0 +1,41 @@
+This example demonstrates how easy it is to configure a web driver apart from the Xebium Selenium Driver Fixture.
+
+First set up an alternative web-driver provider:
+
+!| script | single web driver supplier |
+| set browser | firefox |
+| set as default |
+
+Then run the scripts. Note the url next to the fixture name.
+
+!| script | selenium driver fixture | http://localhost:${FITNESSE_PORT}/ |
+| ensure | do | open | on | ${PAGE_PATH}.${PAGE_NAME} |
+| check | is | verifyTitle | ${PAGE_PATH}.${PAGE_NAME} |
+| check | is | verifyTitle | on | ${PAGE_PATH}.${PAGE_NAME} |
+| ensure | do | verifyTextPresent | on | Xebium |
+| check | is | verifyText | on | css=span.page_title | ${PAGE_NAME} |
+| check | is | verifyText | on | css=span.page_title | with | ${PAGE_NAME} |
+| note | do not ''stop browser'' as it will prevent the selenium driver from restarting. |
+
+You can do the same through a library. Note that the same webdriver instance will be used.
+
+| library |
+| selenium driver fixture | http://localhost:${FITNESSE_PORT}/ |
+
+!| script |
+| ensure | do | open | on | ${PAGE_PATH}.${PAGE_NAME} |
+| check | is | verifyTitle | ${PAGE_PATH}.${PAGE_NAME} |
+| check | is | verifyTitle | on | ${PAGE_PATH}.${PAGE_NAME} |
+| ensure | do | verifyTextPresent | on | Xebium |
+| check | is | verifyText | on | css=span.page_title | ${PAGE_NAME} |
+| check | is | verifyText | on | css=span.page_title | with | ${PAGE_NAME} |
+
+Tear down:
+| script |
+| stop browser |
+
+
+Reset to default (normally you don't need to do this):
+
+!| script | default web driver supplier |
+| set as default |
@@ -0,0 +1,14 @@
+<?xml version="1.0"?>
+<properties>
+ <Edit/>
+ <Files/>
+ <Help>Use a custom provider for web drivers.</Help>
+ <Properties/>
+ <RecentChanges/>
+ <Refactor/>
+ <Search/>
+ <Suites/>
+ <Test/>
+ <Versions/>
+ <WhereUsed/>
+</properties>
@@ -32,6 +32,13 @@
private File profileDirectory;
+ public DefaultWebDriverSupplier() {
+ }
+
+ public void setAsDefault() {
+ SeleniumDriverFixture.configureWebDriverSupplier(this);
+ }
+
public WebDriver get() {
return newWebDriver();
}
@@ -97,7 +104,7 @@ private WebDriver newWebDriver() {
driver = new OperaDriver(capabilities);
} else {
try {
- driver = new RemoteWebDriverBuilder(browser).get();
+ driver = new RemoteWebDriverSupplier(browser).get();
} catch (Exception e) {
throw new RuntimeException("Unknown browser type. Should be one of 'firefox', 'iexplore', 'chrome', " +
"'opera', 'opera-mobile-tablet', 'opera-mobile-phone', 'htmlUnit' or 'htmlUnit+js'", e);
@@ -1,105 +0,0 @@
-/*
- * Copyright 2010-2012 Xebia b.v.
- * Copyright 2010-2012 Xebium contributers
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- */
-
-package com.xebia.incubator.xebium;
-
-import static org.openqa.selenium.remote.CapabilityType.PLATFORM;
-
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.Map;
-
-import org.json.JSONException;
-import org.json.JSONObject;
-import org.openqa.selenium.Capabilities;
-import org.openqa.selenium.WebDriver;
-import org.openqa.selenium.remote.DesiredCapabilities;
-import org.openqa.selenium.remote.RemoteWebDriver;
-
-import com.google.common.base.Supplier;
-
-public class RemoteWebDriverBuilder implements Supplier<WebDriver> {
-
- private static final String REMOTE = "remote";
-
- private String remote;
- private Map<String, String> capabilities;
-
- public RemoteWebDriverBuilder(String json) {
- JSONObject jsonObject;
- try {
- jsonObject = new JSONObject(json);
- } catch (JSONException e) {
- throw new RuntimeException("Unable to interpret browser information", e);
- }
-
- try {
- remote = jsonObject.getString(REMOTE);
- jsonObject.remove(REMOTE);
- capabilities = jsonObjectToMap(jsonObject);
- } catch (JSONException e) {
- throw new RuntimeException("Unable to fetch required fields from json string", e);
- }
- }
-
- private Map<String, String> jsonObjectToMap(JSONObject jsonObject) throws JSONException {
- // Assume you have a Map<String, String> in JSONObject
- @SuppressWarnings("unchecked")
- Iterator<String> nameItr = jsonObject.keys();
- Map<String, String> outMap = new HashMap<String, String>();
- while(nameItr.hasNext()) {
- String name = nameItr.next();
- outMap.put(name, jsonObject.getString(name));
- }
-
- String platform = outMap.get(PLATFORM);
- if (platform != null) {
- outMap.put(PLATFORM, platform.toUpperCase());
- }
-
- return outMap;
- }
-
-
- public URL getRemote() {
- try {
- return new URL(remote);
- } catch (MalformedURLException e) {
- throw new RuntimeException("URL '" + remote + "' is not a valid URL");
- }
- }
-
- public Capabilities getCapabilities() {
- return new DesiredCapabilities(capabilities);
- }
-
- /**
- * Create a new remote-webdriver. It can be configured according to the specs on
- * https://saucelabs.com/docs/ondemand/additional-config.
- *
- * @return a fresh RemoteWebDriver instance
- * @throws RuntimeException in case of any error
- */
- public WebDriver get() {
- return new RemoteWebDriver(getRemote(), getCapabilities());
- }
-
-
-}
@@ -48,7 +48,7 @@
private static final String ALIAS_PREFIX = "%";
- private static Supplier<WebDriver> webDriverSupplier = new DefaultWebDriverSupplier();
+ private static Supplier<WebDriver> webDriverSupplier = new DefaultWebDriverSupplier();;
private CommandProcessor commandProcessor;
@@ -67,6 +67,7 @@
private Map<String, String> aliases = new HashMap<String, String>();
public static void configureWebDriverSupplier(Supplier<WebDriver> webDriverSupplier) {
+ LOG.info("Configured WebDriver supplier to {}", webDriverSupplier);
SeleniumDriverFixture.webDriverSupplier = webDriverSupplier;
}
@@ -81,9 +82,13 @@ public SeleniumDriverFixture(String browserUrl) {
private CommandProcessor startWebDriverCommandProcessor(String browserUrl) {
browserUrl = removeAnchorTag(browserUrl);
- return new WebDriverCommandProcessor(browserUrl, webDriverSupplier);
+ return new WebDriverCommandProcessor(browserUrl, webDriverSupplier.get());
}
+ /**
+ * @param filename
+ * @deprecated Call on the web driver supplier directly.
+ */
public void loadCustomBrowserPreferencesFromFile(String filename) {
if (webDriverSupplier instanceof DefaultWebDriverSupplier) {
((DefaultWebDriverSupplier) webDriverSupplier).setCustomProfilePreferencesFile(new File(filename));
@@ -92,6 +97,10 @@ public void loadCustomBrowserPreferencesFromFile(String filename) {
}
}
+ /**
+ * @param directory
+ * @deprecated Call on the web driver supplier directly.
+ */
public void loadFirefoxProfileFromDirectory(String directory) {
if (webDriverSupplier instanceof DefaultWebDriverSupplier) {
((DefaultWebDriverSupplier) webDriverSupplier).setProfileDirectory(new File(directory));
@@ -100,6 +109,9 @@ public void loadFirefoxProfileFromDirectory(String directory) {
}
}
+ /**
+ * @param browser Name of the browser, as accepted by the DefaultWebDriverSupplier.
+ */
private void setBrowser(String browser) {
if (webDriverSupplier instanceof DefaultWebDriverSupplier) {
((DefaultWebDriverSupplier) webDriverSupplier).setBrowser(browser);
@@ -130,6 +142,7 @@ public void startBrowserOnUrl(final String browser, final String browserUrl) {
*
* @param browser
* @param browserUrl
+ * @deprecated This call requires a Selenium 1 server. It is adviced to use WebDriver.
*/
public void startBrowserOnUrlUsingRemoteServer(final String browser, final String browserUrl) {
startBrowserOnUrlUsingRemoteServerOnHost(browser, browserUrl, "localhost");
@@ -143,6 +156,7 @@ public void startBrowserOnUrlUsingRemoteServer(final String browser, final Strin
* @param browser
* @param browserUrl
* @param serverHost
+ * @deprecated This call requires a Selenium 1 server. It is adviced to use WebDriver.
*/
public void startBrowserOnUrlUsingRemoteServerOnHost(final String browser, final String browserUrl, final String serverHost) {
startBrowserOnUrlUsingRemoteServerOnHostOnPort(browser, browserUrl, serverHost, 4444);
@@ -157,6 +171,7 @@ public void startBrowserOnUrlUsingRemoteServerOnHost(final String browser, final
* @param browserUrl
* @param serverHost
* @param serverPort
+ * @deprecated This call requires a Selenium 1 server. It is adviced to use WebDriver.
*/
public void startBrowserOnUrlUsingRemoteServerOnHostOnPort(final String browser, final String browserUrl, final String serverHost, final int serverPort) {
setCommandProcessor(new HttpCommandProcessorAdapter(new HttpCommandProcessor(serverHost, serverPort, browser, removeAnchorTag(browserUrl))));
@@ -420,7 +435,10 @@ private String executeCommand(final ExtendedSeleniumCommand command, final Strin
}
if (commandProcessor == null) {
- throw new IllegalStateException("Command processor not running. First start it by invoking startBrowserOnUrl");
+ throw new IllegalStateException("Command processor not running. " +
+ (webDriverSupplier instanceof DefaultWebDriverSupplier
+ ? "First start it by invoking startBrowserOnUrl"
+ : "Provide a url on construction of the driver"));
}
// Handle special cases first
@@ -12,23 +12,23 @@
@Test
public void loadSettingsFromJson() throws MalformedURLException {
- RemoteWebDriverBuilder builder = new RemoteWebDriverBuilder("{ \"remote\": \"http://localhost\", \"platform\": \"vista\", \"browserName\": \"firefox\" }");
+ RemoteWebDriverSupplier builder = new RemoteWebDriverSupplier("{ \"remote\": \"http://localhost\", \"platform\": \"vista\", \"browserName\": \"firefox\" }");
assertEquals(new URL("http://localhost"), builder.getRemote());
assertEquals(Platform.VISTA, builder.getCapabilities().getPlatform());
assertEquals("firefox", builder.getCapabilities().getBrowserName());
}
@Test
public void loadSettingsFromJsonWithFullblownUrl() throws MalformedURLException {
- RemoteWebDriverBuilder builder = new RemoteWebDriverBuilder("{ \"remote\": \"http://amolenaar:12345678-90ab-cdef-1234-etcetc@ondemand.saucelabs.com:80/wd/hub\", \"platform\": \"vista\", \"browserName\": \"firefox\" }");
+ RemoteWebDriverSupplier builder = new RemoteWebDriverSupplier("{ \"remote\": \"http://amolenaar:12345678-90ab-cdef-1234-etcetc@ondemand.saucelabs.com:80/wd/hub\", \"platform\": \"vista\", \"browserName\": \"firefox\" }");
assertEquals(new URL("http://amolenaar:12345678-90ab-cdef-1234-etcetc@ondemand.saucelabs.com:80/wd/hub"), builder.getRemote());
assertEquals(Platform.VISTA, builder.getCapabilities().getPlatform());
assertEquals("firefox", builder.getCapabilities().getBrowserName());
}
@Test(expected=RuntimeException.class)
public void loadInvalidSettings() throws MalformedURLException {
- RemoteWebDriverBuilder builder = new RemoteWebDriverBuilder("\"remote\": \"http://localhost\", \"platform\": \"vista\", \"browserName\": \"firefox\"");
+ RemoteWebDriverSupplier builder = new RemoteWebDriverSupplier("\"remote\": \"http://localhost\", \"platform\": \"vista\", \"browserName\": \"firefox\"");
assertEquals(new URL("http://localhost"), builder.getRemote());
assertEquals(Platform.VISTA, builder.getCapabilities().getPlatform());
assertEquals("firefox", builder.getCapabilities().getBrowserName());

0 comments on commit 25cd3cd

Please sign in to comment.