Skip to content

Commit f78e4eb

Browse files
s_roes_roe
authored andcommitted
updates
1 parent 1da9437 commit f78e4eb

2 files changed

Lines changed: 99 additions & 2 deletions

File tree

src/csharp/Gravity.Core/Gravity.Core/Extensions/WebDriverExtensions.cs

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
* on-line resources
55
*/
66
using Newtonsoft.Json;
7+
78
using OpenQA.Selenium.Common;
89
using OpenQA.Selenium.Remote;
910
using OpenQA.Selenium.Support.UI;
11+
1012
using System;
1113
using System.Collections.Generic;
1214
using System.Diagnostics.CodeAnalysis;
@@ -15,6 +17,7 @@
1517
using System.Net.Http;
1618
using System.Reflection;
1719
using System.Text;
20+
using System.Text.RegularExpressions;
1821
using System.Threading;
1922
using System.Threading.Tasks;
2023

@@ -934,6 +937,30 @@ private static IWebDriver DoSwitchToFrame(IWebDriver driver, By by, TimeSpan tim
934937
}
935938
#endregion
936939

940+
#region *** Switch to Window ***
941+
/// <summary>
942+
/// Switches the focus of future commands for this driver to the window with the given name.
943+
/// </summary>
944+
/// <param name="driver">This <see cref="IWebDriver"/> instance.</param>
945+
/// <param name="windowName">The handle of the window to select.</param>
946+
/// <returns>Self <see cref="IWebDriver"/> reference.</returns>
947+
public static IWebDriver SwitchTo(this IWebDriver driver, string windowName)
948+
{
949+
// commands
950+
var command = GetCommandApi(driver, "/window");
951+
952+
// switch window parameters
953+
var body = @"{""handle"":""" + windowName + @"""}";
954+
var content = new StringContent(body, Encoding.UTF8, mediaType: "application/json");
955+
956+
// executes
957+
client.PostAsync(command, content).GetAwaiter().GetResult();
958+
959+
// results
960+
return driver;
961+
}
962+
#endregion
963+
937964
/// <summary>
938965
/// Switches the focus of future commands for this driver to the window with the given index.
939966
/// </summary>
@@ -965,6 +992,33 @@ public static Uri GetEndpoint(this IWebDriver driver)
965992
return DoGetEndpoint(driver);
966993
}
967994

995+
/// <summary>
996+
/// Returns an indication if this IWebDriver" implementation is an AppiumDriver implementation.
997+
/// </summary>
998+
/// <param name="d">The IWebDriver to evaluate.</param>
999+
/// <returns>True if AppiumDriver or False if not.</returns>
1000+
public static bool IsAppiumDriver(this IWebDriver d)
1001+
{
1002+
// initialize conditions
1003+
var isName = false;
1004+
var isAppium = false;
1005+
1006+
// setup name condition
1007+
var type = d.GetType();
1008+
while (!isName && type != null && type.Name != "RemoteWebDriver" && type.Name != "Object")
1009+
{
1010+
isName = Regex.IsMatch(type.Name, "AppiumDriver`1");
1011+
if (isName)
1012+
{
1013+
isAppium = typeof(IWebElement).IsAssignableFrom(type?.GenericTypeArguments?.First());
1014+
}
1015+
type = type.BaseType;
1016+
}
1017+
1018+
// assertion
1019+
return isAppium;
1020+
}
1021+
9681022
#region *** Send Command ***
9691023
/// <summary>
9701024
/// Sends POST command directly to this <see cref="IWebDriver"/> instance.
@@ -1031,17 +1085,20 @@ public static string SendDeleteCommand(this IWebDriver driver, string route)
10311085
// results
10321086
return response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
10331087
}
1088+
#endregion
10341089

1090+
#region *** Utilities ***
10351091
private static string GetCommandApi(IWebDriver driver, string route)
10361092
{
1093+
// setup
10371094
var endpoint = DoGetEndpoint(driver).AbsoluteUri;
10381095
var session = DoGetSession(driver);
10391096
route = route.StartsWith("/") ? route : $"/{route}";
1097+
1098+
// get
10401099
return $"{endpoint}session/{session}{route}";
10411100
}
1042-
#endregion
10431101

1044-
#region *** Utilities ***
10451102
[SuppressMessage("Major Code Smell", "S3011:Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "Designed to increase accessibility.")]
10461103
private static Uri DoGetEndpoint(IWebDriver driver)
10471104
{

src/csharp/Gravity.Core/Gravity.Core/Extensions/WebElementExtensions.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
* on-line resources
55
*/
66
using Newtonsoft.Json;
7+
78
using OpenQA.Selenium.Interactions;
89
using OpenQA.Selenium.Internal;
910
using OpenQA.Selenium.Remote;
1011
using OpenQA.Selenium.Support.UI;
12+
1113
using System;
1214
using System.Collections.Generic;
1315
using System.Diagnostics.CodeAnalysis;
@@ -265,6 +267,44 @@ public static IWebElement MoveToElement(this IWebElement toElement)
265267
// keep the fluent
266268
return toElement;
267269
}
270+
271+
/// <summary>
272+
/// Scrolls the specified element into the visible area of the browser window.
273+
/// </summary>
274+
/// <param name="onElement"><see cref="IWebElement"/> to scroll into view.</param>
275+
/// <returns>An <see cref="IWebElement"/> interface through which the user controls elements on the page.</returns>
276+
public static IWebElement TryScrollIntoView(this IWebElement onElement)
277+
{
278+
// exit conditions
279+
if (!(onElement is IWrapsDriver))
280+
{
281+
return onElement;
282+
}
283+
284+
// extract driver
285+
var driver = ((IWrapsDriver)onElement).WrappedDriver;
286+
287+
// try scroll into view
288+
try
289+
{
290+
// check for irrelevant drivers
291+
if (driver.IsAppiumDriver())
292+
{
293+
new Actions(driver).MoveToElement(onElement).Build().Perform();
294+
return onElement;
295+
}
296+
else
297+
{
298+
((IJavaScriptExecutor)driver)
299+
.ExecuteScript("arguments[0].scrollIntoView(false);", onElement);
300+
}
301+
}
302+
catch (Exception e) when (e != null)
303+
{
304+
// ignore exceptions
305+
}
306+
return onElement;
307+
}
268308
#endregion
269309

270310
#region *** Delayed Send Keys ***

0 commit comments

Comments
 (0)