Skip to content

Commit

Permalink
Add GetDriverRemoteUri.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Sep 14, 2024
1 parent 90083b9 commit 3282c20
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions Lombiq.Tests.UI/Extensions/FrontendUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,20 @@ namespace Lombiq.Tests.UI.Extensions;

public static class FrontendUITestContextExtensions
{
public static Uri GetDriverRemoteUri(this UITestContext context)
{
if (context.Driver is not WebDriver { CommandExecutor: DriverServiceCommandExecutor executor } driver)
{
throw new InvalidOperationException(
$"The {nameof(GetDriverRemoteUri)} method requires a driver that inherits from {nameof(WebDriver)} " +
$"and a command executor of type {nameof(DriverServiceCommandExecutor)}.");
}

return (Uri)typeof(HttpCommandExecutor)
.GetField("remoteServerUri", BindingFlags.Instance | BindingFlags.NonPublic)?
.GetValue(executor.HttpExecutor) ?? throw new InvalidOperationException("Couldn't get driver executor URI.");
}

/// <summary>
/// Executes the provided file via <c>node</c> with command line arguments containing the necessary information for
/// Selenium JS to take over the browser.
Expand All @@ -22,19 +36,10 @@ public static async Task ExecuteJavascriptTestAsync(
string scriptPath,
ITestOutputHelper testOutputHelper)
{
if (context.Driver is not WebDriver { CommandExecutor: DriverServiceCommandExecutor executor } driver)
{
throw new InvalidOperationException(
$"The {nameof(ExecuteJavascriptTestAsync)} requires a driver that inherits from {nameof(WebDriver)} " +
$"and a command executor of type {nameof(DriverServiceCommandExecutor)}.");
}

const string command = "node";
var pipe = testOutputHelper.ToPipeTarget($"{nameof(ExecuteJavascriptTestAsync)}({command})");

var remoteServerUri = (Uri)typeof(HttpCommandExecutor)
.GetField("remoteServerUri", BindingFlags.Instance | BindingFlags.NonPublic)?
.GetValue(executor.HttpExecutor) ?? throw new InvalidOperationException("Couldn't get driver executor URI.");
var remoteServerUri = context.GetDriverRemoteUri();

try
{
Expand Down

0 comments on commit 3282c20

Please sign in to comment.