-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bugs and factoring; upgrade HtmlUnitDriver (#186)
- Loading branch information
Showing
13 changed files
with
140 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
src/main/java/com/nordstrom/automation/selenium/plugins/PluginUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.nordstrom.automation.selenium.plugins; | ||
|
||
import java.io.IOException; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
import org.apache.commons.lang3.reflect.ConstructorUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.google.common.reflect.ClassPath; | ||
import com.google.common.reflect.ClassPath.ClassInfo; | ||
import com.nordstrom.automation.selenium.DriverPlugin; | ||
|
||
/** | ||
* This static utility class contains support methods for <b>Local Selenium Grid</b> plug-ins. | ||
*/ | ||
public class PluginUtils { | ||
|
||
private static final String PLUGIN_PACKAGE_NAME = PluginUtils.class.getPackage().getName(); | ||
private static final Logger LOGGER = LoggerFactory.getLogger(PluginUtils.class); | ||
|
||
private PluginUtils() { | ||
throw new AssertionError("PluginUtils is a static constants class that cannot be instantiated"); | ||
} | ||
|
||
/** | ||
* Get "personalities" from the plug-in that supports the specified browser. | ||
* <p> | ||
* <b>NOTE</b>: This method uses the {@link ClassLoader} of the current thread to search for candidate classes, | ||
* and only plug-in classes in the <i>com.nordstrom.automation.selenium.plugins</i> package are | ||
* considered. | ||
* | ||
* @param browserName browser name | ||
* @return map: "personality" → desired capabilities (JSON); empty if plug-in for browser not found | ||
*/ | ||
public static Map<String, String> getPersonalitiesForBrowser(String browserName) { | ||
try { | ||
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); | ||
ClassPath classPath = ClassPath.from(classLoader); | ||
for (ClassInfo classInfo : classPath.getTopLevelClasses(PLUGIN_PACKAGE_NAME)) { | ||
DriverPlugin driverPlugin = pluginForName(classInfo.getName()); | ||
if ((driverPlugin != null) && (driverPlugin.getBrowserName().equals(browserName))) { | ||
return driverPlugin.getPersonalities(); | ||
} | ||
} | ||
} catch (IOException e) { | ||
throw new IllegalArgumentException("Failed building class path model", e); | ||
} | ||
LOGGER.warn("No plugin for browser '{}' found in package: {}", browserName, PLUGIN_PACKAGE_NAME); | ||
return Collections.emptyMap(); | ||
} | ||
|
||
/** | ||
* Create an instance of the specified plug-in class. | ||
* | ||
* @param candidateName candidate class name | ||
* @return {@link DriverPlugin} object; {@code null} if instantiation attempt fails | ||
*/ | ||
private static DriverPlugin pluginForName(String candidateName) { | ||
try { | ||
Class<?> clazz = Class.forName(candidateName); | ||
if (DriverPlugin.class.isAssignableFrom(clazz)) { | ||
return (DriverPlugin) ConstructorUtils.invokeConstructor(clazz); | ||
} | ||
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | | ||
InvocationTargetException | InstantiationException e) { | ||
// nothing to do here | ||
} | ||
return null; | ||
} | ||
|
||
} |