Skip to content

Commit

Permalink
Pass all arguments from the C# driver.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Nov 19, 2024
1 parent b01363e commit 2b6d92c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 15 deletions.
6 changes: 6 additions & 0 deletions Lombiq.Tests.UI/Extensions/FrontendUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System;
using System.IO;
using System.Reflection;
using System.Text.Json;
using System.Threading.Tasks;
using Xunit.Abstractions;

Expand Down Expand Up @@ -116,6 +117,11 @@ public static async Task ExecuteJavaScriptTestAsync(

try
{
var browserArguments = context.Configuration.BrowserConfiguration.Arguments;
await File.WriteAllTextAsync(
context.GetTempSubDirectoryPath("BrowserArguments.json"),
JsonSerializer.Serialize(browserArguments));

await Cli.Wrap(command)
.WithArguments(arguments)
.WithStandardOutputPipe(pipe)
Expand Down
6 changes: 6 additions & 0 deletions Lombiq.Tests.UI/Services/BrowserConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Lombiq.Tests.UI.Models;
using SixLabors.ImageSharp;
using System;
using System.Collections.Generic;
using System.Globalization;

namespace Lombiq.Tests.UI.Services;
Expand Down Expand Up @@ -43,4 +44,9 @@ public class BrowserConfiguration
/// Gets or sets the fake camera video source information. See: <see cref="FakeBrowserVideoSource"/>.
/// </summary>
public FakeBrowserVideoSource FakeVideoSource { get; set; }

/// <summary>
/// Gets a list of command line arguments that were passed to the driver during the driver instance creation.
/// </summary>
public IList<string> Arguments { get; } = [];
}
11 changes: 11 additions & 0 deletions Lombiq.Tests.UI/Services/WebDriverFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace Lombiq.Tests.UI.Services;
Expand Down Expand Up @@ -55,6 +57,8 @@ Task<Func<ChromeDriver>> CreateDriverInnerAsync(string driverPath = null) =>
// Helps with misconfigured hosts.
if (chromeConfig.Service.HostName == "localhost") chromeConfig.Service.HostName = "127.0.0.1";
configuration.Arguments.SetItems(chromeConfig.Options.Arguments);
return new ChromeDriver(chromeConfig.Service, chromeConfig.Options, pageLoadTimeout)
.SetCommonTimeouts(pageLoadTimeout);
});
Expand Down Expand Up @@ -89,6 +93,8 @@ public static Task<Func<EdgeDriver>> CreateEdgeDriverAsync(BrowserConfiguration
var service = EdgeDriverService.CreateDefaultService();
service.SuppressInitialDiagnosticInformation = true;
configuration.Arguments.SetItems(options.Arguments);
return () => new EdgeDriver(service, options).SetCommonTimeouts(pageLoadTimeout);
});

Expand All @@ -112,6 +118,11 @@ public static Task<Func<FirefoxDriver>> CreateFirefoxDriverAsync(BrowserConfigur
configuration.BrowserOptionsConfigurator?.Invoke(options);
var arguments = typeof(FirefoxOptions)
.GetField("firefoxArguments", BindingFlags.Instance | BindingFlags.NonPublic)?
.GetValue(options) as IList<string> ?? [];
configuration.Arguments.SetItems(arguments);
return new FirefoxDriver(options).SetCommonTimeouts(pageLoadTimeout);
}));

Expand Down
29 changes: 14 additions & 15 deletions Lombiq.Tests.UI/ui-testing-toolkit.mjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import path from 'path';
import process from 'process';
import fs from 'node:fs/promises';
import { By, WebDriver, WebElement } from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome.js';
import { writeFile } from 'node:fs/promises'

function _exists(path) {
return fs.stat(path).then(() => true, () => false);
}

async function _logSource(driver) {
const html = await driver.getPageSource();
console.log('HTML:', html.replace(/\s*[\n\r]+\s*/g, ' '));
Expand Down Expand Up @@ -81,21 +86,15 @@ async function runTest(test, configureOptions = null) {

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

let options = new chrome.Options()
.addArguments('disable-dev-shm-usage')
.addArguments('unsafely-disable-devtools-self-xss-warnings')
.addArguments('disable-search-engine-choice-screen')
.addArguments('--lang=en-US')
.addArguments('disable-accelerated-2d-canvas')
.addArguments('disable-gpu')
.addArguments('force-color-profile=sRGB')
.addArguments('force-device-scale-factor=1')
.addArguments('high-dpi-support=1')
.addArguments('disable-smooth-scrolling')
.addArguments('ignore-certificate-errors')
.addArguments('--ignore-certificate-errors')
.addArguments('--no-sandbox')
;
let options = new chrome.Options();

const argumentsPath = path.join(tempDirectory, 'BrowserArguments.json');
if (await _exists(argumentsPath)) {
JSON.parse(await fs.readFile(argumentsPath, { encoding: 'utf8' }))
.forEach(argument => {
options = options.addArguments(argument);
});
}

if (process.env.GITHUB_ENV) options = options.addArguments('headless');
if (configureOptions) options = configureOptions(options) ?? options;
Expand Down

0 comments on commit 2b6d92c

Please sign in to comment.