how to add 'global wait' to mocha test - electron

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);
});

Related

Keep browser open for all tests in a test suite

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)

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.

how can I solve this error in playwright tests?

I have this code for testing my form
import { expect, test } from '#playwright/test'
test('Form test', async ({ page }) => {
await expect(page.locator('#feedback_form'))
await page.locator('input#name[type="text"]').fill('John Doe')
await page.locator('input#email[type="email"]').fill('troum#outlook.com')
await page.locator('input#phone[type="text"]').fill('+47 89 89678 90')
await page.locator('input#subject[type="text"]').fill('Test subject')
await page.locator('input#purpose[type="text"]').fill('Test purpose')
await page.locator('input#contactWay[type="text"]').fill('Via e-mail')
await page
.locator('textarea#message')
.fill('Contrary to popular belief, Lorem Ipsum is not simply random text')
await page.click('button[type="submit"]', { force: true })
await page.screenshot({ path: 'scrapingant.png' })
})
but I've got this problems for each browsers
it's my screenshort
Complete the assertion first:
await expect(page.locator('#feedback_form')).toBeVisible()
Then You can modify your selector a bit like this and try:
await page.locator('input#name').fill('John Doe')
Or, you can do a click before fill and try:
await page.locator('input#name').click().fill('John Doe')
try to use different selectors if its still not working,go for playwright Codegen's generated locator using command npx playwright codegen as i was facing the same issue and it worked for me even though i was using unique locator.

Waiting for async to be completed

I currently have something like this
Future<bool> checkAvailability(String email) async {
var client = new http.Client();
var response = await client.get(host);
bool result;
if (response.statusCode == 404) {
result= true;
}
else if (response.statusCode == 200) {
result= false;
}
client.close();
return result;
}
I am calling the above method from a regular non-aysnc function
in this way
void test() {
checkAvailability(email).then((result){....}
);
}
The problem with the above code is that its async. From what I understand is that Once checkAvailability is called its launched in a different thread ? and the ui (main) thread continues? Am I correct?
What I would like to do is to have test function wait for the result of checkAvailability. I know I can use await but then the method test will need to be marked as async and when this method is called it will be launched in a different thread. What I want is for the call to checkAvailability be synchronous and I don't mind waiting for a response.
Once checkAvailability is called its launched in a different thread ?
Async execution is not related to threads, async works with an event queue https://webdev.dartlang.org/articles/performance/event-loop
test will need to be marked as async and when this method is called it will be launched in a different thread.
As mentioned before, it won't be another thread. Using async on a test method usually works fine. Why do you try to avoid it?
What I want is for the call to checkAvailability be synchronous and I dont mind waiting for a response.
If you don't care about the result, just don't await it, although this would also cause the calling code to continue before checkAvailability was completed.
What I want is for the call to checkAvailability be synchronous and I dont mind waiting for a response.
There is no way to go back from async execution to sync execution. Once an async call is made, it's completion and the result will always be a Future and needs to be awaited or handled by .then(...).
If you don't care when checkAvailability completes and don't need a result from async calls it makes, then you don't need to await or use then(...). Just call the method and that's it.

mvc async await link clicks wait till task finished

I have a MVC website where I am trying to call a method with async. My code is like below:
Views
Submit Download
$(document).on('click', '.submit-download', function (evt) {
var submit_url = '/SubmitDownloads/24589;
$.ajax({
type: 'POST',
url: submit_url,
success: function (data) {
if (data.Item1)
location.reload();
}
});
});
Controller
[HttpPost]
public async Task<JsonResult> SubmitDownloads(int id)
{
var respository = new WorkflowRepository();
var result = await respository.SubmitAsync(id);
return Json(result, JsonRequestBehavior.AllowGet);
}
Repository Method
//db service call which will take much longer time
public async Task<Tuple<bool, string>> SubmitAsync(id)
{
//long running method here
await Task.Delay(20000);
return new Tuple<bool, string>(true, "done with " + id);
}
When user clicks on the 'Submit Download' link in Views, it complete the entire function quickly as its supposed to do and page shows responsive like scrollable, menu shows fine. But when I click on any link in the page, it waits till the entire operation is finished (20 seconds) and then redirect to respective URL.
If I change the Task.Delay to 50 seconds, link click takes 50 seconds to redirect.
Can you please guide me what I am missing here?
But when I click on any link in the page, it waits till the entire operation is finished (20 seconds) and then redirect to respective URL.
Asynchronous controller methods don't make the HTTP interaction async, just the interaction from the web server to the web application async. The idea is that a high volume web server can free up threads to service other requests while long-running requests are doing their thing.
When you click on a link, the browser needs to wait for a response before, well, processing that response (displaying the page). There's no way to display a page without waiting for that page to be sent from the web server.

Resources