Skip to content

Commit

Permalink
Rewrite to start from working directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Nov 18, 2024
1 parent ae5b7c3 commit b01363e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 32 deletions.
37 changes: 10 additions & 27 deletions Lombiq.Tests.UI.Samples/Tests/JavaScriptTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Lombiq.Tests.UI.Extensions;
using Lombiq.Tests.UI.Services.GitHub;
using System.IO;
using System.Threading.Tasks;
using Xunit;
Expand All @@ -24,44 +23,28 @@ public JavaScriptTests(ITestOutputHelper testOutputHelper)
public Task ExampleJavaScriptTestShouldWork() =>
ExecuteTestAfterSetupAsync(context =>
{
// Don't forget to mark the script files as "Copy if newer", so they are available to the test. It's best to
// include something like the following in your csproj file:
// Don't forget to mark the script files as "Copy if newer", so they are available to the test. If you
// include something like the following in your csproj file, then you only have to do this once:
// <None Update="Tests\*.mjs" CopyToOutputDirectory="PreserveNewest" />
var workingDirectory = "Tests";
var scriptPath = Path.Join(workingDirectory, "JavaScriptTests.msj");
var scriptPath = Path.Join("Tests", "JavaScriptTests.mjs");
// Set up the JS dependencies in the test's temp directory to ensure there are no clashes, then run the
// script. This method has an additional parameter to list further NPM dependencies beyond
// "selenium-webdriver", if the script requires it. We will check out this script file in the next station.
return context.SetupSeleniumAndExecuteJavaScriptTestAsync(_testOutputHelper, scriptPath, workingDirectory);
return context.SetupSeleniumAndExecuteJavaScriptTestAsync(_testOutputHelper, scriptPath);
});

// To best debug the JavaScript code, you may want to set up the site and then invoke node manually. This is not a
// real test, but it sets up the site in interactive mode (see Tests/InteractiveModeTests.cs for more) with
// information on how to start up test scripts from your GUI. It's an example of some tooling that can improve the
// test developer's workflow.
[Fact]
public Task Sandbox()
{
// This "test" will wait indefinitely, so it's important to skip it in CI.
if (GitHubHelper.IsGitHubEnvironment) return Task.CompletedTask;

return ExecuteTestAfterSetupAsync(
async context =>
{
var workingDirectory = "Tests";
var scriptPath = Path.Join(workingDirectory, "JavaScriptTests.msj");
await context.SetupNodeSeleniumAsync(_testOutputHelper, workingDirectory);
await context.SwitchToInteractiveWithJavaScriptTestInfoAsync(scriptPath, workingDirectory);
},
configuration =>
{
// Since this is an interactive "test", make sure the browser is always displayed.
configuration.BrowserConfiguration.Headless = false;
return Task.CompletedTask;
});
}
public Task Sandbox() =>
SandboxAfterSetupAsync(async context =>
{
await context.SetupNodeSeleniumAsync(_testOutputHelper);
await context.SwitchToInteractiveWithJavaScriptTestInfoAsync(Path.Join("Tests", "JavaScriptTests.mjs"));
});
}

// NEXT STATION: Head over to Tests/JavaScriptTests.mjs.
13 changes: 9 additions & 4 deletions Lombiq.Tests.UI/Extensions/FrontendUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static async Task SetupSeleniumAndExecuteJavaScriptTestAsync(
this UITestContext context,
ITestOutputHelper testOutputHelper,
string scriptPath,
string workingDirectory,
string workingDirectory = null,
params string[] otherDependencies)
{
await context.SetupNodeSeleniumAsync(testOutputHelper, workingDirectory, otherDependencies);
Expand Down Expand Up @@ -205,13 +205,18 @@ await Cli.Wrap("pnpm")
public static Task SetupNodeSeleniumAsync(
this UITestContext context,
ITestOutputHelper helper,
string workingDirectory,
string workingDirectory = null,
params string[] otherDependencies)
{
workingDirectory ??= Environment.CurrentDirectory;

// First, copy out helper script if the working directory isn't already the current directory.
const string uiTestingToolkitScript = "ui-testing-toolkit.mjs";
if (File.Exists(uiTestingToolkitScript))
var copyFrom = Path.GetFullPath(uiTestingToolkitScript);
var copyTo = Path.GetFullPath(Path.Join(workingDirectory, uiTestingToolkitScript));
if (copyFrom != copyTo && File.Exists(uiTestingToolkitScript))
{
File.Copy(uiTestingToolkitScript, Path.Join(workingDirectory, uiTestingToolkitScript), overwrite: true);
File.Copy(copyFrom, copyTo, overwrite: true);
}

return context.SetupNodeDependenciesAsync(helper, workingDirectory, ["selenium-webdriver", .. otherDependencies]);
Expand Down
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI/ui-testing-toolkit.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ async function navigate(driver, url, maxAttempts = 10) {
*/
async function runTest(test, configureOptions = null) {
const args = process.argv.slice(2);
if (args.length !== 3) throw new Error('Usage: node script.js driverPath startUrl tempDirectory browserName');
if (args.length !== 4) throw new Error('Usage: node script.js driverPath startUrl tempDirectory browserName');
const [driverPath, startUrl, tempDirectory, browserName] = args;

if (browserName !== 'Chrome') throw new Error("Only Chrome is supported at this time");
Expand Down

0 comments on commit b01363e

Please sign in to comment.