Skip to content

Commit

Permalink
Merge pull request #13 from RohitM-IN/dev
Browse files Browse the repository at this point in the history
Enhancements
  • Loading branch information
RohitM-IN authored Aug 27, 2023
2 parents 2e5850d + 34673f8 commit 04efa87
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 47 deletions.
6 changes: 3 additions & 3 deletions SeleniumManager.Core/Configuration/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"UserName": "",
"Password": "",
"statistics": {
"Chrome": 2,
"MicrosoftEdge": 1,
"Firefox": 1,
"chrome": 0.47,
"MicrosoftEdge": 0.38,
"firefox": 0.15,
"Internet Explorer": 0
},
"endpoints": {
Expand Down
2 changes: 1 addition & 1 deletion SeleniumManager.Core/DataContract/ConfigurationSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public string GridHost
_GridHost = value;
}
}
public Dictionary<string, int> statistics { get; set; }
public Dictionary<string, double> statistics { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public Endpoints Endpoints { get; set; }
Expand Down
6 changes: 3 additions & 3 deletions SeleniumManager.Core/SeleniumManager.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
This include managing Queues, Parallel Tests, etc.</Description>
<Copyright>MIT License</Copyright>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<Version>1.2.0.0</Version>
<AssemblyVersion>1.2.1.0</AssemblyVersion>
<Version>1.2.1.0</Version>
<GenerateDocumentationFile>True</GenerateDocumentationFile>
<FileVersion>1.2.0.0</FileVersion>
<FileVersion>1.2.1.0</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
40 changes: 25 additions & 15 deletions SeleniumManager.Core/SeleniumManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,31 +356,41 @@ private bool IsBrowserAvailable(string browserName)

private async Task<string> FindBestAvailableBrowser()
{
var statistics = RatioDictionary.GetRatioDictionary(_configSettings.statistics, MaxSessions);

foreach (var kvp in statistics.OrderByDescending(x => x.Value))
try
{
await _availableStereotypesSemaphore.WaitAsync();
var browserName = kvp.Key;
var maxInstances = kvp.Value;
ConcurrentStereotypes.TryGetValue(kvp.Key.ToLower(), out var concurrentInstances);
AvailableStereotypes.TryGetValue(kvp.Key.ToLower(), out var availableInstances);
var statistics = RatioDictionary.GetRatioDictionary(_configSettings.statistics, MaxSessions);

if (maxInstances >= concurrentInstances && !string.IsNullOrEmpty(browserName))
foreach (var kvp in statistics.OrderByDescending(x => x.Value))
{
var browserName = kvp.Key;
var maxInstances = kvp.Value;
ConcurrentStereotypes.TryGetValue(kvp.Key, out var concurrentInstances);
AvailableStereotypes.TryGetValue(kvp.Key, out var availableInstances);

AdjustInstance(browserName.ToLower(), AdjustType.Create);
if (maxInstances > concurrentInstances && !string.IsNullOrEmpty(browserName))
{

_availableStereotypesSemaphore.Release();
return browserName.ToString();
AdjustInstance(browserName, AdjustType.Create);
return browserName.ToString();
}

continue;
}
_availableStereotypesSemaphore.Release();
continue;

// If no available browser is found, return the browser with the highest instances count
return statistics.OrderByDescending(x => x.Value).FirstOrDefault().Key ?? WebDriverType.Chrome.GetDescription();

}
catch (System.Exception)
{

// If no available browser is found, return the browser with the highest instances count
return statistics.OrderByDescending(x => x.Value).FirstOrDefault().Key ?? WebDriverType.Chrome.GetDescription();
throw;
}
finally
{
_availableStereotypesSemaphore.Release();
}
}

private void AdjustInstance(string key, AdjustType type)
Expand Down
37 changes: 22 additions & 15 deletions SeleniumManager.Core/Utils/RatioDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@
{
public class RatioDictionary
{
public static Dictionary<string, int> GetRatioDictionary(Dictionary<string, int> dict, int maxNumber)
public static Dictionary<string, int> GetRatioDictionary(Dictionary<string, double> dict, int maxNumber)
{
// Calculate the total sum of values in the input dictionary
double totalValue = dict.Values.Sum();

// Calculate a ratio factor based on the total number and sum of values
double ratioFactor = maxNumber / totalValue;

Dictionary<string, int> share = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> item in dict)
int totalAllocated = 0;

foreach (KeyValuePair<string, double> item in dict)
{
share[item.Key] = (int)(item.Value * maxNumber / sum(dict.Values));
// Calculate the number of instances for the current browser type
int instances = (int)Math.Floor(item.Value * ratioFactor);

// Store the instances count in the share dictionary
share[item.Key] = instances;

// Keep track of the total allocated instances
totalAllocated += instances;
}

int remainingValue = maxNumber - sum(share.Values);
int remainingValue = maxNumber - totalAllocated;

// Distribute the remaining instances to the highest value
if (remainingValue > 0)
{
string highestValueKey = "";
int highestValue = 0;
foreach (KeyValuePair<string, int> item in share)
{
if (item.Value > highestValue)
{
highestValue = item.Value;
highestValueKey = item.Key;
}
}

string highestValueKey = share.OrderByDescending(x => x.Value).First().Key;
share[highestValueKey] += remainingValue;
}

Expand Down
10 changes: 7 additions & 3 deletions SeleniumManager.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.5.33516.290
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeleniumManager.Core", "SeleniumManager.Core\SeleniumManager.Core.csproj", "{B7FAAF9B-A860-4949-9605-A069E1B05BFB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeleniumManager.Core", "SeleniumManager.Core\SeleniumManager.Core.csproj", "{B7FAAF9B-A860-4949-9605-A069E1B05BFB}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{7D9E9DA4-B073-42C7-B197-E91494FB65CE}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
doc\API_REFERENCE.md = doc\API_REFERENCE.md
doc\CODE_OF_CONDUCT.md = doc\CODE_OF_CONDUCT.md
doc\Configuration.md = doc\Configuration.md
doc\CONTRIBUTING.md = doc\CONTRIBUTING.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeleniumManager.ConsoleApp", "SeleniumManager.ConsoleApp\SeleniumManager.ConsoleApp.csproj", "{BE559C8A-13DC-4C04-B959-8C968BF6A6DD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeleniumManager.ConsoleApp", "SeleniumManager.ConsoleApp\SeleniumManager.ConsoleApp.csproj", "{BE559C8A-13DC-4C04-B959-8C968BF6A6DD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SeleniumManager.Tests", "SeleniumManager.Tests\SeleniumManager.Tests.csproj", "{04E33D40-4601-4D08-B0F8-A837CE94C350}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SeleniumManager.Tests", "SeleniumManager.Tests\SeleniumManager.Tests.csproj", "{04E33D40-4601-4D08-B0F8-A837CE94C350}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
15 changes: 8 additions & 7 deletions doc/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ The `ConfigurationSettings` class acts as a configuration hub for Selenium Manag
- **Description**: A collection of endpoint URLs used by Selenium Manager to communicate with different parts of the Selenium Grid infrastructure. These endpoints include URLs for retrieving status, session details, and more.

### Options
- **Type**: Options
- **Type**: `Options`
- **Description**: A collection of browser-specific options used to configure the behavior of WebDriver instances. This property holds sub-properties for each browser type (e.g., Chrome, Firefox) to allow you to set browser-specific options.

## Usage
Expand All @@ -62,9 +62,9 @@ var configSettings = new ConfigurationSettings
GridHost = "http://localhost:4444/wd/hub",
statistics = new Dictionary<string, int>
{
{ "chrome", 1 },
{ "firefox", 1 },
{ "MicrosoftEdge", 1 }
{ "chrome", 0.5 },
{ "firefox", 0.2 },
{ "MicrosoftEdge", 0.3 }
},
UserName = "your-username",
Password = "your-password",
Expand Down Expand Up @@ -92,15 +92,16 @@ var configSettings = new ConfigurationSettings
"UserName": "",
"Password": "",
"statistics": {
"Chrome": 2,
"MicrosoftEdge": 1,
"Firefox": 1,
"chrome": 0.47,
"MicrosoftEdge": 0.38,
"firefox": 0.15,
"Internet Explorer": 0
},
"endpoints": {
"status": "/status"
}
}

```

Using above config file
Expand Down

0 comments on commit 04efa87

Please sign in to comment.