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

[Question] Tests executed in NodeJS env and not browser? #14469

Closed
jensenhertz opened this issue May 29, 2022 · 6 comments
Closed

[Question] Tests executed in NodeJS env and not browser? #14469

jensenhertz opened this issue May 29, 2022 · 6 comments

Comments

@jensenhertz
Copy link

I'm trying to use playwright test for unit tests, but I'm having trouble getting it to run the tests in the browser context.

For example, if I do a simple expect(window).toBeTruthy(), I get window is not defined error. However, global does exist, so I'm guessing for some reason tests are executed in NodeJS environment.

I've tried --browser chromium, but doesn't seem to work.

Any ideas how can I get the browser env to work?

@Bessonov
Copy link

If I understand correctly, there is a difference. Your tests are executed inside a node environment, but actions of your tests are executed inside a real browser. Probably, this will help you understand what's going on and how you can access window.

@jensenhertz
Copy link
Author

@Bessonov Ok, that's truly unfortunate and makes me question the whole point of using Playwright. I'm currently trying to test code that has a dependency which checks navigator.userAgentData. The test doesn't even go to execution because it fails with navigator is not defined before that.

@Bessonov
Copy link

Like I sad, you can access it. But not like expect(window).toBeTruthy(). If your all use cases rely on browser execution environment, then look at cypress. But be aware of some cypress' gotchas, because it runs inside a browser.

@pavelfeldman
Copy link
Member

Looks like this can be closed. If you are looking for an in-browser test runner, you can refer to Karma.

@wakaztahir
Copy link

wakaztahir commented Jul 27, 2023

I think playwright should allow the ability to do this as well, Since most people prefer not to do page.evaluate everytime they need access to document , karma is deprecated , I am going to use cypress but as I have used playwright, its test results were in a good UI, playwright downloaded the browsers nicely

Playwright should have a simple toggle in its configuration like "runAllInBrowserContext" : true when set to true , it runs all tests in browser by default , or if a toggle like that is too simple, maybe test.runEachWrapping((t)=> { page.evaluate(t) })

I'd leave it to you guys to figure out the API but I think this would be a huge addition

I would also reference jestjs/jest#139 (comment) jestjs/jest#139 (comment) jestjs/jest#848

I tried jest jsdom, turns out it has many issues, It fails if you use innerText property, and I can't change my library to use textContent just because jsdom fails

I also tried puppeteer, it does same thing like playwright, I tried karma at last, It worked, Karma reporting sucks, I used typescript-reporter with karma and html string comparisons cutoff in the middle, I can't see them and therefore can't debug my test

I want playwright to just work out of the box, The tests should be performed, Logs from chrome browser console should be captured, maybe another toggle captureLogsFromConsole and displayed in the developer's console, The results should be launched in html or displayed in console

@wakaztahir
Copy link

wakaztahir commented Jul 28, 2023

I require this issue to be reopened, as a feature request, rename title to "Execute all tests in the browser by default" or should I create a new one

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

No branches or pull requests

4 participants