Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-904: Create an example of using FrontendServer and context.ExecuteJavascriptTestAsync #421

Open
wants to merge 51 commits into
base: dev
Choose a base branch
from

Conversation

sarahelsaig
Copy link
Member

@sarahelsaig sarahelsaig commented Nov 17, 2024

OSOE-904
Fixes #408

// ensure the two work together. To make this happen, you need some custom logic to initialize the frontend process
// after setup, with a unique port number to avoid clashes. Also the frontend may need the backend URL, whose port is
// already randomized with every test.
public abstract class FrontendUITestBase : UITestBase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Explain how much of this is necessary in every case, and what's specific to this sample but can be different based on the use case.

async context =>
{
// Don't forget that if you want to interact with the frontend manually, you can just switch the context
// back to the back end and use the interactive mode extension method.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you interact with the frontend in interactive mode too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the question.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. The point is that you can explore the frontend in interactive mode, but you can't just call await context.SwitchToInteractiveAsync();. You have to switch the UI test context scope back to the backend first (context.SwitchToBackend();) or it won't work, because the await context.SwitchToInteractiveAsync(); will try to visit the page on the frontend's URL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Can't SwitchToInteractiveAsync detect where it's called from and change the URL accordingly? Having to remember this is exposing an implementation particularity. Or at least an overload that makes this easier to discover, something along the lines of SwitchToInteractiveModeFromFrontendWithSwitchToBackendAsync (this is terribly long, but you get the idea).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not just the SwitchToInteractiveAsync, but every method that uses Lombiq.Tests.UI.Shortcuts (because those controller actions are in the backend). I don't think it's viable nor worth the extra complexity.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about checking UITestContext.TenantName for FrontendPseudoTenantName and throw in something like an EnsureBackend() in ShortcutsUITestContextExtensions?


namespace Lombiq.Tests.UI.Samples.Tests;

// Suppose you want to write UI tests in Javascript. Why would you want to do that? Unlikely if you are an Orchard Core
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere it should be spelled "JavaScript".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file name would be better as something clearer like ExampleJavascriptTestShouldWork.mjs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm referencing the same file in the sandbox, so that would be confusing. Also I don't want more JS tests than just this one so it's pointless to specify the test name in the file name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Then JavaScriptTests.msj, rather.

if (args.length !== 3) throw new Error('Usage: node script.js driverPath startUrl tempDirectory');
const [driverPath, startUrl, tempDirectory] = args;

let options = new chrome.Options()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this mean that the JS test-related C# methods should throw an exception if the current browser is not Chrome, because this won't work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because they can provide their own driver config function if they want to. But I've updated the code to throw about it more explicitly in JS.

Lombiq.Tests.UI/ui-testing-toolkit.mjs Show resolved Hide resolved
.addArguments('disable-dev-shm-usage')
.addArguments('unsafely-disable-devtools-self-xss-warnings')
.addArguments('disable-search-engine-choice-screen')
.addArguments('--lang=en-US')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should rather come from the config, like it is in WebDriverFactory.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to over-complicate it because every such config has to be passed into the script through command line arguments

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing else from here needs to be passed in, however, this one I think should: this also affects the Accept-Language header, thus the UI behavior. I.e. without settings this to the same as in C#, the C# and JS tests may see a different UI (even if they test different parts of the app, e.g. the same date for the same content item may be displayed differently).

Comment on lines 83 to 95
.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')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't these all be the same as in WebDriverFactory? If they should, please fix. And in any case, comment in both places if these two need to be kept in sync in some way.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create an example of using FrontendServer and context.ExecuteJavascriptTestAsync() (OSOE-904)
2 participants