Keep browser open for all tests in a test suite - playwright

The typical structure of my tests is as follows:
import {test} from '#playwright/test';
test.describe('suite', () => {
test('test1', async ({page}) => {
await page.goto('test1');
});
test('test2', async ({page}) => {
await page.goto('test2');
});
});
This works perfectly but I noticed that Playwright opens and closes the browser window for each test and was wondering, why the browser window cannot stay open for all tests and if this could/should be optimised?

Based on my feedback from a playwright contributor:
If you use the VSCode extension, the browser stays open: https://playwright.dev/docs/getting-started-vscode
You want to ensure full test isolation on your CI, what Playwright does it guarantees that so no test will infer with each other. (less reasons to flake)

Related

Microsoft.Azure.Functions.Worker.Middleware.IFunctionsWorkerMiddleware Unit Testing

Looking for some examples on how to setup unit testing around IFunctionsWorkerMiddleware. All the examples I've found are for MVC and not function apps.
Example for MVC, but looking for .net 6 using function apps
using var host = await new HostBuilder() .ConfigureWebHost(webBuilder => { webBuilder .UseTestServer() .ConfigureServices(services => { services.AddMyServices(); }) .Configure(app => { app.UseMiddleware<MyMiddleware>(); }); }) .StartAsync(); var response = await host.GetTestClient().GetAsync("/")
Thanks in advance.
In order to test the middleware we need to spin up a host using HostBuilder; as part of configuring host builder we can stipulate it needs to use the test server (an in-memory webserver).  This is what we will make request against an what should be executing the middleware. Every single example I've found and tried are all for MVC  and nothing for function apps.  and every attempt pretty much results in a gRPC issue (gRPC is spun uo by the function app process by default, I cannot find where/how to not set it up).

Need suggestion/solution on how to 'Reuse signed in state' in my tests - playwright

I am working on a project where I am trying to achieve 'login one time to complete all my tests instead of logging in everytime for each test' I followed this document. My global setup looks exactly like how it is in the said document.
My test spec looks like this,
//sample.test.ts
test.describe("Login Tests", () => {
test.use({ storageState: "state.json" });
test(`TEST 1`, async ({ page }) => {
//browser launches in authenticated state
});
test(`Test 2`, async ({ page}) => {
//browser launches in unauthenticated state
});
});
//playwright.config.ts
globalSetup: require.resolve("./src/utility/globalSetup.ts"),
use: {
storageState: "state.json",
}
//globalSetup.ts
const { storageState } = config.projects[0].use;
const browser = await chromium.launch();
const page = await browser.newPage();
//code for login goes here....
await page.context().storageState({ path: storageState as string });
await browser.close();
The issue is my first test (TEST1) works fine - the browser launches in authenticated state. But my second test (TEST2) does not launch in authenticated state.
I tried running one test at a time. TEST1, TEST2 passes in isolated runs.
I swapped the test order, made TEST2 run first, in that case TEST2 launches in authenticated state TEST1 failed.
What could be the problem? Any help is highly appreciated.

Trying to connect to external .net browser hosted in desktop application

I am trying to use playwright to interact with an external .net browser hosted inside a desktop application.
I'm able to connect to the application using connectOverCDP but wasn't able to interact with the elements.
import { test} from '#playwright/test';
const pw = require("playwright");
test('test', async ({ page }) => {
const browser = await pw.chromium.connectOverCDP("http://localhost:9222");
await page.locator('[aria-label="Screencast view of debug target"]').click(); // keeps failing here
});

how to add 'global wait' to mocha test

I'm trying to test my electron app with spectron and mocha;my application will close the first window after user login,so i need add some "wait action" to wait the second window to appear. But it seems setTimeout's callback works abnormal.
I think the settimeout function works asynchronous, so the promise chain will continue after you started the settimeout. So somehow you have to await the settimeout - have you tried wrapping it in a promise, and then return the promise?
return new Promise((resolve, reject) => {
setTimeout(async () => {
await this.app.client.windowByIndex(0); //I'm not even sure you need to await this
resolve();
}, 3000);
});

How best to stub/mock rest API calls in Flutter Integration tests

I have a Flutter app that displays data after the user logs in.
I have unit and widget tests and now would like to write my first Integration/end-to-end test to test an entire 'happy path' workflow where the user logs in and views the data.
When the app goes to call the login API (GET login_api_path) I want to return some predefined JSON for what to show on the screen rather than making a real request to the server.
Is this a sensible approach, and if so what is the best way to do this? Most resources I found were specifically for unit testing.
Here's the approach I went with:
Create a mock client that uses the Dart http MockClient:
import 'package:http/testing.dart';
import 'package:http/http.dart';
MockClient integrationTestMockClient = MockClient((request) async {
switch (request.url.toString()) {
case 'https://staging.company.com/api/customer/123':
return Response('{"customer": "123", "name": "Jane Jimmy"}', 200);
case 'https://staging.company.com/api/customer/155':
return Response('{"customer": "155", "name": "Gregor"}', 200);
}
}
Now you need to pass your mock client into your app, when you start up your app in integration tests e.g. test_driver/app.dart
import 'mock_client.dart';
void main() async {
enableFlutterDriverExtension();
final app = await initializeApp(
integrationMockClient,
);
runApp(app);
}
You may need to refactor your non-test code so that when the app starts up, you can inject a client. Either a real client or a mock client when testing.
import 'package:http/http.dart';
void main() => initializeApp(Client());

Resources