下面列出了怎么用org.openqa.selenium.remote.CapabilityType的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
// Tests that a platform of 'ANY' matches another otherwise non-matching OS
public void testOsMatchesAnyRequests() {
String uuid = "testUuid";
String browser = "firefox";
String browserVersion = "25";
String os = "ANY";
Map<String,Object> map = new HashMap<>();
map.put(CapabilityType.BROWSER_NAME,browser);
map.put(CapabilityType.VERSION,browserVersion);
map.put(CapabilityType.PLATFORM,os);
AutomationRunRequest first = new AutomationRunRequest(uuid,null,browser,browserVersion,"linux");
AutomationRunRequest second = AutomationRunRequest.requestFromCapabilities(map);
Assert.assertTrue("Requests should be equal", first.matchesCapabilities(second));
Assert.assertTrue("Requests should be equal", second.matchesCapabilities(first));
}
@Test
public void shouldConvertAProxyCorrectly() {
Proxy proxy = new Proxy();
proxy.setHttpProxy("localhost:4444");
MutableCapabilities caps = new DesiredCapabilities("foo", "1", Platform.LINUX);
caps.setCapability(CapabilityType.PROXY, proxy);
Map<String, ?> asMap = ImmutableMap.of("desiredCapabilities", caps);
Command command = new Command(new SessionId("empty"), DriverCommand.NEW_SESSION, asMap);
String json = convert(command.getParameters());
JsonObject converted = new JsonParser().parse(json).getAsJsonObject();
JsonObject capsAsMap = converted.get("desiredCapabilities").getAsJsonObject();
assertThat(capsAsMap.get(CapabilityType.PROXY).getAsJsonObject().get("httpProxy").getAsString())
.isEqualTo(proxy.getHttpProxy());
}
private void setChromeDriver() {
ChromeOptions options = new ChromeOptions();
List<String> chromeOptions = new ArrayList<>();
LoggingPreferences logPrefs = new LoggingPreferences();
chromeOptions.add(String.format("--proxy-server=%s", proxies.getCurrentProxyModel().getIp()));
chromeOptions.add(String.format("--user-agent=%s", userAgent.randomUA()));
chromeOptions.add("--mute-audio");
logPrefs.enable(LogType.BROWSER, Level.ALL);
logPrefs.enable(LogType.PERFORMANCE, Level.INFO);
options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
options.addArguments(chromeOptions);
options.setBinary(this.driverLocation);
options.setHeadless(true);
// options.setProxy(this.proxies.getCurrentProxy());
options.setCapability("proxy", this.proxies.getCurrentProxy());
this.webDriver = new ChromeDriver(options);
Log.WINFO(this.workerName, this.workerColor, "Chrome Driver Set.");
}
@Test
// Test to make sure an in progress counts against the free node count
public void testRequestNewRunNotStarted() throws IOException, ServletException{
String browser = "firefox";
String nodeId = "nodeId";
// Add a node that is not running to make sure its not included in the available calculation
AutomationDynamicNode node = new AutomationDynamicNode("testUuid",nodeId,null,null,new Date(),50);
AutomationContext.getContext().addNode(node);
String runId = "runId";
AutomationContext.getContext().addRun(new AutomationRunRequest(runId,10,"firefox"));
ProxySet proxySet = new ProxySet(false);
MockRemoteProxy proxy = new MockRemoteProxy();
proxy.setMaxNumberOfConcurrentTestSessions(50);
proxy.setCapabilityMatcher(new AutomationCapabilityMatcher());
Map<String,Object> config = new HashMap<String, Object>();
config.put(AutomationConstants.INSTANCE_ID, nodeId);
proxy.setConfig(config);
Map<String,Object> capabilities = new HashMap<String,Object>();
capabilities.put(CapabilityType.BROWSER_NAME,"firefox");
TestSlot testSlot = new TestSlot(proxy, SeleniumProtocol.WebDriver,null,capabilities);
proxy.setMultipleTestSlots(testSlot, 10);
proxySet.add(proxy);
AutomationContext.getContext().setTotalNodeCount(50);
int freeThreads = new AutomationRequestMatcher().getNumFreeThreadsForParameters(proxySet,new AutomationRunRequest(browser));
Assert.assertEquals("No nodes should be free since existing run hasn't started",0,freeThreads);
}
private static WebDriver setupIeDriver() throws MalformedURLException {
LOG.i("Setting up Internet Explorer driver...");
setupDriver(iedriver());
final InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.destructivelyEnsureCleanSession();
ieOptions.setCapability("requireWindowFocus", true);
ieOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
final InternetExplorerDriverService ieService = InternetExplorerDriverService.createDefaultService();
if (!OS.isWindows()) {
Assert.fail("IE is not supported.");
}
if (appSetting().isHeadlessMode()) {
LOG.w("IE does not support headless mode. Hence, ignoring the same...");
}
return new InternetExplorerDriver(ieService, ieOptions);
}
@Test
public void usingAProxyToTrackNetworkTrafficStep2() {
BrowserMobProxy browserMobProxy = new BrowserMobProxyServer();
browserMobProxy.start();
Proxy seleniumProxyConfiguration = ClientUtil.createSeleniumProxy(browserMobProxy);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setCapability(CapabilityType.PROXY, seleniumProxyConfiguration);
driver = new FirefoxDriver(firefoxOptions);
browserMobProxy.newHar();
driver.get("https://www.google.co.uk");
Har httpArchive = browserMobProxy.getHar();
assertThat(getHTTPStatusCode("https://www.google.co.uk/", httpArchive))
.isEqualTo(200);
}
@Test
// Happy path that browsers matching shows correct free node count
public void testRequestMatchingBrowsers() throws IOException, ServletException{
String browser = "firefox";
String nodeId = "nodeId";
// Add a node that is not running to make sure its not included in the available calculation
AutomationDynamicNode node = new AutomationDynamicNode("testUuid",nodeId,null,null,new Date(),50);
AutomationContext.getContext().addNode(node);
ProxySet proxySet = new ProxySet(false);
MockRemoteProxy proxy = new MockRemoteProxy();
proxy.setMaxNumberOfConcurrentTestSessions(50);
proxy.setCapabilityMatcher(new AutomationCapabilityMatcher());
Map<String,Object> config = new HashMap<String, Object>();
config.put(AutomationConstants.INSTANCE_ID,nodeId);
proxy.setConfig(config);
Map<String,Object> capabilities = new HashMap<String,Object>();
capabilities.put(CapabilityType.BROWSER_NAME,browser);
TestSlot testSlot = new TestSlot(proxy, SeleniumProtocol.WebDriver,null,capabilities);
proxy.setMultipleTestSlots(testSlot, 10);
proxySet.add(proxy);
AutomationContext.getContext().setTotalNodeCount(50);
int freeThreads = new AutomationRequestMatcher().getNumFreeThreadsForParameters(proxySet,new AutomationRunRequest(browser));
Assert.assertEquals("Thread count should be correct due to matching browser", 10, freeThreads);
}
@Test
public void doesNotRedirectForNewSessionsRequestedViaCrossDomainRpc()
throws IOException, ServletException {
Map<String, Object> json = ImmutableMap.of(
"method", "POST",
"path", "/session",
"data", ImmutableMap.of(
"desiredCapabilities", ImmutableMap.of(
CapabilityType.BROWSER_NAME, BrowserType.FIREFOX,
CapabilityType.VERSION, true)));
FakeHttpServletResponse response = sendCommand("POST", "/xdrpc", json);
assertEquals(HttpServletResponse.SC_OK, response.getStatus());
assertEquals("application/json; charset=utf-8",
response.getHeader("content-type"));
Map<String, Object> jsonResponse = this.json.toType(response.getBody(), MAP_TYPE);
assertEquals(ErrorCodes.SUCCESS, ((Number) jsonResponse.get("status")).intValue());
assertNotNull(jsonResponse.get("sessionId"));
Map<?, ?> value = (Map<?, ?>) jsonResponse.get("value");
// values: browsername, version, remote session id.
assertEquals(value.toString(), 3, value.entrySet().size());
assertEquals(BrowserType.FIREFOX, value.get(CapabilityType.BROWSER_NAME));
assertTrue((Boolean) value.get(CapabilityType.VERSION));
}
@Before
public void setUp() throws Exception {
// set up appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Selendroid");
capabilities.setCapability(CapabilityType.VERSION, "4.2.2");
capabilities.setCapability("device", "Android");
capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
capabilities.setCapability("app", "https://s3.amazonaws.com/voodoo2/SugarCRM.apk.zip");
capabilities.setCapability("app-package", "com.sugarcrm.nomad");
capabilities.setCapability("app-activity", "NomadActivity");
driver = new SwipeableWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
values = new ArrayList<>();
}
@Test
// Tests that a node in the Terminated state without an instance id is still considered a valid resource
public void testRequestNodeTerminatedNoInstanceId() throws IOException, ServletException{
String browser = "firefox";
String nodeId = "nodeId";
// Add a node that is not running to make sure its not included in the available calculation
AutomationDynamicNode node = new AutomationDynamicNode("testUuid",nodeId,null,null,new Date(),10);
node.updateStatus(AutomationDynamicNode.STATUS.TERMINATED);
AutomationContext.getContext().addNode(node);
ProxySet proxySet = new ProxySet(false);
MockRemoteProxy proxy = new MockRemoteProxy();
proxy.setCapabilityMatcher(new AutomationCapabilityMatcher());
proxy.setMaxNumberOfConcurrentTestSessions(5);
Map<String,Object> config = new HashMap<>();
proxy.setConfig(config);
List<TestSlot> testSlots = new ArrayList<TestSlot>();
Map<String,Object> capabilities = new HashMap<String,Object>();
capabilities.put(CapabilityType.BROWSER_NAME,browser);
testSlots.add(new TestSlot(proxy, SeleniumProtocol.WebDriver, null, capabilities));
proxy.setTestSlots(testSlots);
proxySet.add(proxy);
AutomationContext.getContext().setTotalNodeCount(50);
AutomationRequestMatcher requestMatcher = new AutomationRequestMatcher();
int freeThreads = requestMatcher.getNumFreeThreadsForParameters(proxySet,new AutomationRunRequest(browser));
Assert.assertEquals("Node should be available since instance id was not on the node", 1, freeThreads);
}
private static void setupFirefox() {
final DesiredCapabilities capabilities = new DesiredCapabilities();
final String proxyHost = System.getProperty("http.proxyHost");
final String proxyPort = System.getProperty("http.proxyPort");
if (proxyHost != null) {
System.out
.println("Configuring Firefox Selenium web driver with proxy "
+ proxyHost
+ (proxyPort == null ? "" : ":" + proxyPort)
+ " (requires Firefox browser)");
final Proxy proxy = new Proxy();
final String proxyString = proxyHost
+ (proxyPort == null ? "" : ":" + proxyPort);
proxy.setHttpProxy(proxyString).setSslProxy(proxyString);
proxy.setNoProxy("localhost");
capabilities.setCapability(CapabilityType.PROXY, proxy);
} else {
System.out
.println("Configuring Firefox Selenium web driver without proxy (requires Firefox browser)");
}
driver = new FirefoxDriver(capabilities);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
@Test
// Tests that for an old run, the in progress tests are counted
public void testOldRunInProgress() {
String uuid = "testUuid";
AutomationRunContext runContext = new AutomationRunContext();
runContext.setTotalNodeCount(10);
ProxySet proxySet = new ProxySet(false);
MockRemoteProxy proxy = new MockRemoteProxy();
proxy.setCapabilityMatcher(new AutomationCapabilityMatcher());
Map<String,Object> capabilities = new HashMap<>();
capabilities.put(CapabilityType.PLATFORM,"linux");
capabilities.put(CapabilityType.BROWSER_NAME,"chrome");
capabilities.put(AutomationConstants.UUID,uuid);
AutomationRunRequest request = new AutomationRunRequest(uuid,10,"chrome","23","linux", AutomationUtils.modifyDate(new Date(),-5,Calendar.MINUTE));
runContext.addRun(request);
TestSlot testSlot = new TestSlot(proxy, SeleniumProtocol.WebDriver,null,capabilities);
testSlot.getNewSession(capabilities);
int inProgressTests = 5;
proxy.setMultipleTestSlots(testSlot,inProgressTests);
proxySet.add(proxy);
int freeThreads = runContext.getTotalThreadsAvailable(proxySet);
Assert.assertEquals("Free threads should reflect in progress test count",inProgressTests,freeThreads);
}
@Test
@NotYetImplemented(value = MARIONETTE, reason = "https://bugzilla.mozilla.org/show_bug.cgi?id=1415067")
public void testFirefoxCanNativelyClickOverlappingElements() {
FirefoxOptions options = new FirefoxOptions();
options.setCapability(CapabilityType.OVERLAPPING_CHECK_DISABLED, true);
localDriver = new FirefoxDriver(options);
localDriver.get(appServer.whereIs("click_tests/overlapping_elements.html"));
localDriver.findElement(By.id("under")).click();
assertThat(localDriver.findElement(By.id("log")).getText())
.isEqualTo("Log:\n"
+ "mousedown in over (handled by over)\n"
+ "mousedown in over (handled by body)\n"
+ "mouseup in over (handled by over)\n"
+ "mouseup in over (handled by body)\n"
+ "click in over (handled by over)\n"
+ "click in over (handled by body)");
}
@Override
public VividusWebDriver create()
{
VividusWebDriver vividusWebDriver = createVividusWebDriver(bddRunContext.getRunningStory());
WebDriver webDriver;
DesiredCapabilities desiredCapabilities = vividusWebDriver.getDesiredCapabilities();
if (proxy.isStarted())
{
desiredCapabilities.setCapability(CapabilityType.PROXY, createSeleniumProxy(remoteExecution));
desiredCapabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
}
if (remoteExecution)
{
webDriver = webDriverFactory.getRemoteWebDriver(desiredCapabilities);
vividusWebDriver.setRemote(true);
}
else
{
webDriver = webDriverFactory.getWebDriver(desiredCapabilities);
}
EventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(webDriver);
webDriverEventListeners.forEach(eventFiringWebDriver::register);
vividusWebDriver.setWebDriver(eventFiringWebDriver);
webDriverManager.resize(vividusWebDriver.getWrappedDriver(),
browserWindowSizeProvider.getBrowserWindowSize(remoteExecution));
return vividusWebDriver;
}
@Test
@Ignore(IE)
@NotYetImplemented(value = SAFARI, reason = "does not support insecure SSL")
@NotYetImplemented(EDGE)
public void testShouldBeAbleToAccessPagesWithAnInsecureSslCertificate() {
createNewDriver(new ImmutableCapabilities(
CapabilityType.ACCEPT_INSECURE_CERTS, Boolean.TRUE));
driver.get(appServer.whereIsSecure("simpleTest.html"));
shortWait.until(titleIs("Hello WebDriver"));
}
@Test
// Tests a request can be fulfilled when chrome nodes have to be spun up
public void testRequestCanFulfillSpinUpNodesChrome() throws IOException, ServletException{
MockVmManager manageEc2 = new MockVmManager();
MockRequestMatcher matcher = new MockRequestMatcher();
matcher.setThreadsToReturn(0);
MockAutomationTestRunServlet servlet = new MockAutomationTestRunServlet(null,false, manageEc2,matcher);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setParameter("uuid","testUuid");
request.setParameter("browser","chrome");
request.setParameter("threadCount","7");
String nodeId = "nodeId";
// Add a node that is not running to make sure its not included in the available calculation
AutomationDynamicNode node = new AutomationDynamicNode("testUuid",nodeId,null,null,new Date(),50);
AutomationContext.getContext().addNode(node);
ProxySet proxySet = new ProxySet(false);
MockRemoteProxy proxy = new MockRemoteProxy();
proxy.setMaxNumberOfConcurrentTestSessions(50);
proxy.setCapabilityMatcher(new AutomationCapabilityMatcher());
Map<String,Object> config = new HashMap<String, Object>();
config.put(AutomationConstants.INSTANCE_ID,nodeId);
proxy.setConfig(config);
Map<String,Object> capabilities = new HashMap<String,Object>();
capabilities.put(CapabilityType.BROWSER_NAME,"chrome");
TestSlot testSlot = new TestSlot(proxy, SeleniumProtocol.WebDriver,null,capabilities);
proxy.setMultipleTestSlots(testSlot, 10);
proxySet.add(proxy);
servlet.setProxySet(proxySet);
AutomationContext.getContext().setTotalNodeCount(50);
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.doGet(request, response);
Assert.assertEquals("Hub should be able to fulfill request",
HttpServletResponse.SC_CREATED,response.getStatusCode());
}
static void addDefaultCapabilities(MutableCapabilities capabilities) {
Set<String> capabilityNames = capabilities.getCapabilityNames();
if (capabilityNames.contains(CapabilityType.BROWSER_NAME)
&& !capabilityNames.contains(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR)) {
capabilities.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
}
}
@Override
public <X> X getScreenshotAs(OutputType<X> target) throws WebDriverException {
Object takeScreenshot = getCapabilities().getCapability(CapabilityType.TAKES_SCREENSHOT);
if (null == takeScreenshot || (Boolean) takeScreenshot) {
String base64Str = execute(DriverCommand.SCREENSHOT).getValue().toString();
return target.convertFromBase64Png(base64Str);
}
return null;
}
/**
* Задает options для запуска Firefox драйвера
* options можно передавать, как системную переменную, например -Doptions=--load-extension=my-custom-extension
*
* @return FirefoxOptions
*/
private FirefoxOptions getFirefoxDriverOptions(DesiredCapabilities capabilities) {
log.info("---------------Firefox Driver---------------------");
FirefoxOptions firefoxOptions = !options[0].equals("") ? new FirefoxOptions().addArguments(options) : new FirefoxOptions();
capabilities.setVersion(loadSystemPropertyOrDefault(CapabilityType.BROWSER_VERSION, VERSION_LATEST));
firefoxOptions.setHeadless(getHeadless());
firefoxOptions.merge(capabilities);
return firefoxOptions;
}
@Test
public void shouldReturnFalseWithDefaultCapabilitiesNotMatch_2() {
Map<String, Object> nodeCapabilities = nodeCapabilities();
Map<String, Object> requestedCapabilities = requestedCapabilities();
requestedCapabilities.put(CapabilityType.PLATFORM, Platform.VISTA);
assertFalse(capabilityMatcher.matches(nodeCapabilities, requestedCapabilities));
}
@Test
void testIsMobileFalse()
{
Map<String, Object> capabilities = Map.of(
CapabilityType.PLATFORM_NAME, Platform.LINUX.toString(),
BROWSER_TYPE, BrowserType.CHROME
);
mockWebDriverHavingCapabilities(capabilities);
assertFalse(webDriverManager.isMobile());
}
@Test
void shouldBeAndroidNativeApp()
{
webDriverManager.setNativeApp(true);
mockWebDriverHavingCapabilities(Map.of(CapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID));
assertTrue(webDriverManager.isAndroidNativeApp());
}
@Test
void shouldNotBeAndroidNativeAppAsItIsIOS()
{
webDriverManager.setNativeApp(true);
mockWebDriverHavingCapabilities(Map.of(CapabilityType.PLATFORM_NAME, MobilePlatform.IOS));
assertFalse(webDriverManager.isAndroidNativeApp());
}
@Test
void shouldBeIOSNativeApp()
{
webDriverManager.setNativeApp(true);
mockWebDriverHavingCapabilities(Map.of(CapabilityType.PLATFORM_NAME, MobilePlatform.IOS));
assertTrue(webDriverManager.isIOSNativeApp());
}
private Browsers(DesiredCapabilities desiredCapabilities) {
this.desiredCapabilities = desiredCapabilities;
this.desiredCapabilities.setJavascriptEnabled(true);
this.desiredCapabilities.setCapability(CapabilityType.TAKES_SCREENSHOT, true);
this.desiredCapabilities.setCapability(CapabilityType.SUPPORTS_FINDING_BY_CSS, true);
}
@BeforeMethod
public void setUpProxy() throws Exception {
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(CapabilityType.PROXY, getProxy());
//or
//capabilities.setCapability(CapabilityType.PROXY, server.seleniumProxy());
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
}
@ParameterizedTest
@MethodSource("orientationProvider")
void testIsOrientation(ScreenOrientation actualOrientation, ScreenOrientation orientationToCheck, String platform)
{
when(webDriverProvider.getUnwrapped(Rotatable.class)).thenReturn(mobileDriver);
when(webDriverManagerContext.getParameter(WebDriverManagerParameter.ORIENTATION)).thenReturn(null);
when(mobileDriver.getOrientation()).thenReturn(actualOrientation);
mockWebDriverHavingCapabilities(Map.of(CapabilityType.PLATFORM_NAME, platform));
assertEquals(actualOrientation == orientationToCheck, webDriverManager.isOrientation(orientationToCheck));
verify(webDriverManagerContext).putParameter(WebDriverManagerParameter.ORIENTATION, actualOrientation);
}
@Test
// Test to make sure an in progress run only counts against the free nodes if the in progress run's browser
// matches the requested browser
public void testRequestNewRunNotStartedDifferentBrowser() throws IOException, ServletException{
String nonMatchingBrowser = "firefox";
String nodeId = "nodeId";
// Add a node that is not running to make sure its not included in the available calculation
AutomationDynamicNode node = new AutomationDynamicNode("testUuid",nodeId,null,null,new Date(),50);
AutomationContext.getContext().addNode(node);
String runId = "runId";
AutomationContext.getContext().addRun(new AutomationRunRequest(runId,10,"firefox"));
ProxySet proxySet = new ProxySet(false);
MockRemoteProxy nonMatchingProxy = new MockRemoteProxy();
nonMatchingProxy.setMaxNumberOfConcurrentTestSessions(50);
nonMatchingProxy.setCapabilityMatcher(new AutomationCapabilityMatcher());
Map<String,Object> config = new HashMap<String, Object>();
config.put(AutomationConstants.INSTANCE_ID,nodeId);
nonMatchingProxy.setConfig(config);
Map<String,Object> nonMatchingCapabilities = new HashMap<String,Object>();
nonMatchingCapabilities.put(CapabilityType.BROWSER_NAME, nonMatchingBrowser);
TestSlot nonMatchingTestSlot = new TestSlot(nonMatchingProxy, SeleniumProtocol.WebDriver,null,nonMatchingCapabilities);
nonMatchingProxy.setMultipleTestSlots(nonMatchingTestSlot, 10);
proxySet.add(nonMatchingProxy);
String matchingBrowser = "chrome";
MockRemoteProxy matchingProxy = new MockRemoteProxy();
proxySet.add(matchingProxy);
matchingProxy.setMaxNumberOfConcurrentTestSessions(50);
matchingProxy.setCapabilityMatcher(new AutomationCapabilityMatcher());
config.put(AutomationConstants.INSTANCE_ID,nodeId);
matchingProxy.setConfig(config);
Map<String,Object> matchingCapabilities = new HashMap<String,Object>();
matchingCapabilities.put(CapabilityType.BROWSER_NAME, matchingBrowser);
TestSlot matchingTestSlot = new TestSlot(nonMatchingProxy, SeleniumProtocol.WebDriver,null,matchingCapabilities);
matchingProxy.setMultipleTestSlots(matchingTestSlot, 10);
AutomationContext.getContext().setTotalNodeCount(50);
int freeThreads = new AutomationRequestMatcher().getNumFreeThreadsForParameters(proxySet,new AutomationRunRequest(matchingBrowser));
Assert.assertEquals("Nodes should be free even though run is in progress as browsers do not match",10,freeThreads);
}
@Test
// 5 firefox, 5 chrome slots, (10 node total) 5 in progress chrome tests, firefox should still show 5 free
public void testMultipleBrowsersInUse() throws IOException, ServletException{
String browser = "firefox";
String nodeId = "nodeId";
// Add a node that is not running to make sure its not included in the available calculation
AutomationDynamicNode node = new AutomationDynamicNode("testUuid",nodeId,null,null,new Date(),50);
AutomationContext.getContext().addNode(node);
ProxySet proxySet = new ProxySet(false);
MockRemoteProxy proxy = new MockRemoteProxy();
proxy.setMaxNumberOfConcurrentTestSessions(10);
proxy.setCapabilityMatcher(new AutomationCapabilityMatcher());
Map<String,Object> config = new HashMap<String, Object>();
config.put(AutomationConstants.INSTANCE_ID,nodeId);
proxy.setConfig(config);
Map<String,Object> nonMatchingCapabilities = new HashMap<String,Object>();
nonMatchingCapabilities.put(CapabilityType.BROWSER_NAME, "chrome");
TestSlot testSlot = new TestSlot(proxy, SeleniumProtocol.WebDriver,null,nonMatchingCapabilities);
testSlot.getNewSession(nonMatchingCapabilities);
proxy.setMultipleTestSlots(testSlot, 5);
proxySet.add(proxy);
Map<String,Object> matchingCapabilities = new HashMap<String,Object>();
matchingCapabilities.put(CapabilityType.BROWSER_NAME, browser);
TestSlot testSlot2 = new TestSlot(proxy, SeleniumProtocol.WebDriver,null,matchingCapabilities);
proxy.setMultipleTestSlots(testSlot2, 5);
proxySet.add(proxy);
AutomationContext.getContext().setTotalNodeCount(50);
int freeThreads = new AutomationRequestMatcher().getNumFreeThreadsForParameters(proxySet,new AutomationRunRequest(browser));
Assert.assertEquals("No nodes should be free since existing run is still new",5,freeThreads);
}
/**
* Generates a AutomationRunRequest object from the capabilities passed in
* @param capabilities
* @return
*/
public static AutomationRunRequest requestFromCapabilities(Map<String,Object> capabilities) {
String capabilityBrowser = (String)capabilities.get(CapabilityType.BROWSER_NAME);
String capabilityBrowserVersion = null;
if(capabilities.containsKey(CapabilityType.VERSION)) {
capabilityBrowserVersion = (String)capabilities.get(CapabilityType.VERSION);
}
String capabilityOs = (String)capabilities.get(CapabilityType.PLATFORM);
return new AutomationRunRequest(null,null,capabilityBrowser,capabilityBrowserVersion,capabilityOs);
}