Skip to content

Commit 6ee887a

Browse files
authored
Merge pull request #120 from quantori/added-clear-cache-method
Added methods to clear browser data and to close inactive windows
2 parents 9ae3ae6 + 65be96f commit 6ee887a

File tree

8 files changed

+68
-24
lines changed

8 files changed

+68
-24
lines changed

Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/AutomationConfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
"BAUTH_LOGIN": "",
99
"BAUTH_PASSWORD": "",
1010
"BAUTH_IGNORE": "true",
11-
"BROWSER_BINARY_LOCATION" : ""
11+
"BROWSER_BINARY_LOCATION" : "",
12+
"UNHANDLED_PROMPT_BEHAVIOR" : ""
1213
}

Behavioral.Automation.Selenium/Behavioral.Automation.DemoBindings/Behavioral.Automation.DemoBindings.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Selenium.Support" Version="4.1.0" />
14-
<PackageReference Include="Selenium.WebDriver" Version="4.1.0" />
13+
<PackageReference Include="Selenium.Support" Version="4.3.0" />
14+
<PackageReference Include="Selenium.WebDriver" Version="4.3.0" />
1515
<PackageReference Include="SpecFlow" Version="3.9.69" />
1616
</ItemGroup>
1717

Behavioral.Automation.Selenium/Behavioral.Automation/Behavioral.Automation.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ The whole automation code is divided into the following parts:
1616
- UI structure descriptive code
1717
- Supportive code</Description>
1818
<Copyright>Quantori Inc.</Copyright>
19-
<PackageVersion>1.13.0</PackageVersion>
19+
<PackageVersion>1.14.0</PackageVersion>
2020
<RepositoryUrl>https://github.com/quantori/Behavioral.Automation</RepositoryUrl>
2121
<PublishRepositoryUrl>true</PublishRepositoryUrl>
2222
<IncludeSymbols>true</IncludeSymbols>
@@ -36,8 +36,8 @@ The whole automation code is divided into the following parts:
3636
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
3737
</PackageReference>
3838
<PackageReference Include="NUnit" Version="3.13.3" />
39-
<PackageReference Include="Selenium.Support" Version="4.1.0" />
40-
<PackageReference Include="Selenium.WebDriver" Version="4.1.0" />
39+
<PackageReference Include="Selenium.Support" Version="4.3.0" />
40+
<PackageReference Include="Selenium.WebDriver" Version="4.3.0" />
4141
<PackageReference Include="SpecFlow" Version="3.9.69" />
4242
</ItemGroup>
4343

Behavioral.Automation.Selenium/Behavioral.Automation/Services/BrowserRunner.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ public void OpenChrome(ChromeOptions options = null)
5959
{
6060
options.BinaryLocation = Environment.ExpandEnvironmentVariables(ConfigServiceBase.BrowserBinaryLocation);
6161
}
62+
if (!string.IsNullOrWhiteSpace(ConfigServiceBase.UnhandledPromptBehavior))
63+
{
64+
options.UnhandledPromptBehavior = ConfigServiceBase.UnhandledPromptBehavior switch
65+
{
66+
"Accept" => UnhandledPromptBehavior.Accept,
67+
"Dismiss" => UnhandledPromptBehavior.Dismiss,
68+
"Ignore" => UnhandledPromptBehavior.Ignore,
69+
_ => options.UnhandledPromptBehavior
70+
};
71+
}
6272
}
6373

6474
var driver = new ChromeDriver(options);

Behavioral.Automation.Selenium/Behavioral.Automation/Services/ConfigServiceBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,11 @@ public static class ConfigServiceBase
2929
private const string AccessClipboardString = "ACCESS_CLIPBOARD";
3030

3131
private const string BrowserBinaryLocationString = "BROWSER_BINARY_LOCATION";
32+
33+
private const string UnhandledPromptBehaviorString = "UNHANDLED_PROMPT_BEHAVIOR";
3234

35+
public static string UnhandledPromptBehavior => ConfigRoot[UnhandledPromptBehaviorString];
36+
3337
public static string BaseUrl => ConfigRoot[BaseUrlString];
3438

3539
public static string BrowserParameters => ConfigRoot[BrowserParametersString];

Behavioral.Automation.Selenium/Behavioral.Automation/Services/DriverService.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,26 @@ public void SwitchToTheFirstWindow()
183183
var handle = Driver.WindowHandles.First();
184184
Driver.SwitchTo().Window(handle);
185185
}
186+
187+
/// <summary>
188+
/// Close inactive windows
189+
/// </summary>
190+
public void CloseInactiveWindows()
191+
{
192+
var currentWindowHandle = Driver.CurrentWindowHandle;
193+
var windowHandles = Driver.WindowHandles;
194+
195+
foreach (var windowHandle in windowHandles)
196+
{
197+
if (!windowHandle.Equals(currentWindowHandle))
198+
{
199+
Driver.SwitchTo().Window(windowHandle);
200+
Driver.Close();
201+
}
202+
}
203+
204+
Driver.SwitchTo().Window(currentWindowHandle);
205+
}
186206

187207
/// <summary>
188208
/// Get URI from relative URL
@@ -229,23 +249,15 @@ public void Navigate(string url)
229249
var uri = new Uri(url);
230250
_scopeContextManager.SwitchPage(uri);
231251
}
232-
233-
/// <summary>
234-
/// Switch to the last opened window
235-
/// </summary>
236-
public void SwitchToLastWindow()
237-
{
238-
Driver.SwitchTo().Window(Driver.WindowHandles.Last());
239-
}
240252

241253
/// <summary>
242254
/// Change size of opened browser window
243255
/// </summary>
244-
/// <param name="Height">Desired height</param>
245-
/// <param name="Width">Desired width</param>
246-
public void ResizeWindow(int Height, int Width)
256+
/// <param name="height">Desired height</param>
257+
/// <param name="width">Desired width</param>
258+
public void ResizeWindow(int height, int width)
247259
{
248-
Driver.Manage().Window.Size = new Size(Width, Height);
260+
Driver.Manage().Window.Size = new Size(width, height);
249261
}
250262

251263
/// <summary>
@@ -295,5 +307,11 @@ public string SaveBrowserLog()
295307

296308
return fileName;
297309
}
310+
311+
public void ClearCache()
312+
{
313+
Driver.Manage().Cookies.DeleteAllCookies();
314+
ExecuteScript("javascript: localStorage.clear()");
315+
}
298316
}
299317
}

Behavioral.Automation.Selenium/Behavioral.Automation/Services/IDriverService.cs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.ObjectModel;
22
using JetBrains.Annotations;
33
using OpenQA.Selenium;
4-
using OpenQA.Selenium.Remote;
54

65
namespace Behavioral.Automation.Services
76
{
@@ -114,9 +113,9 @@ public interface IDriverService
114113
/// <summary>
115114
/// Change size of opened browser window
116115
/// </summary>
117-
/// <param name="Height">Desired height</param>
118-
/// <param name="Width">Desired width</param>
119-
void ResizeWindow(int Height, int Width);
116+
/// <param name="height">Desired height</param>
117+
/// <param name="width">Desired width</param>
118+
void ResizeWindow(int height, int width);
120119

121120
/// <summary>
122121
/// Make screenshot
@@ -136,5 +135,15 @@ public interface IDriverService
136135
/// </summary>
137136
/// <returns>Path to saved log</returns>
138137
string SaveBrowserLog();
138+
139+
/// <summary>
140+
/// Clears browser cache
141+
/// </summary>
142+
void ClearCache();
143+
144+
/// <summary>
145+
/// Close inactive windows
146+
/// </summary>
147+
public void CloseInactiveWindows();
139148
}
140149
}

Behavioral.Automation.Selenium/CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7-
[1.13.0] - 2022-06-03
7+
[1.14.0] - 2022-07-27
88
### Added
9-
- Changed folder structure to separate Selenium and Playwright versions of the framework
9+
- Added method to clear browser data
10+
- Added method to close inactive windows
11+
- Small fixes

0 commit comments

Comments
 (0)