|
4 | 4 | * on-line resources |
5 | 5 | */ |
6 | 6 | using Newtonsoft.Json; |
| 7 | + |
7 | 8 | using OpenQA.Selenium.Common; |
8 | 9 | using OpenQA.Selenium.Remote; |
9 | 10 | using OpenQA.Selenium.Support.UI; |
| 11 | + |
10 | 12 | using System; |
11 | 13 | using System.Collections.Generic; |
12 | 14 | using System.Diagnostics.CodeAnalysis; |
|
15 | 17 | using System.Net.Http; |
16 | 18 | using System.Reflection; |
17 | 19 | using System.Text; |
| 20 | +using System.Text.RegularExpressions; |
18 | 21 | using System.Threading; |
19 | 22 | using System.Threading.Tasks; |
20 | 23 |
|
@@ -934,6 +937,30 @@ private static IWebDriver DoSwitchToFrame(IWebDriver driver, By by, TimeSpan tim |
934 | 937 | } |
935 | 938 | #endregion |
936 | 939 |
|
| 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 | + |
937 | 964 | /// <summary> |
938 | 965 | /// Switches the focus of future commands for this driver to the window with the given index. |
939 | 966 | /// </summary> |
@@ -965,6 +992,33 @@ public static Uri GetEndpoint(this IWebDriver driver) |
965 | 992 | return DoGetEndpoint(driver); |
966 | 993 | } |
967 | 994 |
|
| 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 | + |
968 | 1022 | #region *** Send Command *** |
969 | 1023 | /// <summary> |
970 | 1024 | /// Sends POST command directly to this <see cref="IWebDriver"/> instance. |
@@ -1031,17 +1085,20 @@ public static string SendDeleteCommand(this IWebDriver driver, string route) |
1031 | 1085 | // results |
1032 | 1086 | return response.Content.ReadAsStringAsync().GetAwaiter().GetResult(); |
1033 | 1087 | } |
| 1088 | + #endregion |
1034 | 1089 |
|
| 1090 | + #region *** Utilities *** |
1035 | 1091 | private static string GetCommandApi(IWebDriver driver, string route) |
1036 | 1092 | { |
| 1093 | + // setup |
1037 | 1094 | var endpoint = DoGetEndpoint(driver).AbsoluteUri; |
1038 | 1095 | var session = DoGetSession(driver); |
1039 | 1096 | route = route.StartsWith("/") ? route : $"/{route}"; |
| 1097 | + |
| 1098 | + // get |
1040 | 1099 | return $"{endpoint}session/{session}{route}"; |
1041 | 1100 | } |
1042 | | - #endregion |
1043 | 1101 |
|
1044 | | - #region *** Utilities *** |
1045 | 1102 | [SuppressMessage("Major Code Smell", "S3011:Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "Designed to increase accessibility.")] |
1046 | 1103 | private static Uri DoGetEndpoint(IWebDriver driver) |
1047 | 1104 | { |
|
0 commit comments