playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[Feature] Support for Component Tests in Angular apps

Open jhanink opened this issue 2 years ago • 57 comments

Please support Component Testing for Angular.

In v1.22.0, there is experimental support for react, vue, and svelte Component Tests

https://github.com/microsoft/playwright/releases/tag/v1.22.0

F997E557-0366-4AF7-BB76-1BCDA37C0611

jhanink avatar May 13 '22 21:05 jhanink

@jhanink please provide how your see API in ideal world for component testing angular app.

vitalics avatar May 14 '22 04:05 vitalics

@mxschmitt @jhanink I thing is a good idea to support angular applications. But let me put some notes.

React/vue has a simple API (aka ReactDOM.render function) to put the component in your DOM node. Angular applications has not such API. The minimal repro:

index.html

<body>
  <app-root></app-root>
// angular runtime
<script src="runtime.js" type="module"></script> 
<script src="polyfills.js" type="module"></script>
// compiled app
<script src="main.js" type="module"></script>
</body>

main.ts

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule, Component } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

@Component({
  selector: 'app-root',
  template:'<span>{{ title }} app is running!</span>' 
})
export class AppComponent {
  title = 'my-app';
}

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }


platformBrowserDynamic().bootstrapModule(AppModule)

vitalics avatar May 14 '22 06:05 vitalics

Does Angular v14 help resolve and make this simpler? With standalone components coming I think this could be similar to the React/Vue/Svelte examples.

RFC from Anular for context.

tytusplanck-8451 avatar May 19 '22 14:05 tytusplanck-8451

Does Angular v14 help resolve and make this simpler? With standalone components coming I think this could be similar to the React/Vue/Svelte examples.

RFC from Anular for context.

About the new setup with standalone component angular docs

ExFlo avatar Jun 30 '22 11:06 ExFlo

That will be great. In order for this to work with traditional component (read: not standalone), you will need to do something like Testbed which is the compilation context for an angular component. Only that will give you a renderable component. You might want to look at storybook angular library, which is dealing with the same thing basically (render a single component in isolation).

Tallyb avatar Sep 06 '22 10:09 Tallyb

Playwright is so amazing that I want to replace all our e2e tests, including tests in libraries, with Playwright. Would be amazing to have Angular components support, at least standalone components.

e-oz avatar Sep 06 '22 14:09 e-oz

Support for Angular would be great! Cypress has proven it is technically possible, even without standalone components, if I'm not mistaken :).

Waterstraal avatar Sep 19 '22 07:09 Waterstraal

Another obstacle is that Playwright transpiles the components with Vite and Angular has no Vite support as far as i know.

sand4rt avatar Nov 15 '22 21:11 sand4rt

Just released Angular support here: https://github.com/jscutlery/devkit/tree/main/packages/playwright-ct-angular but you'll have to follow the Versatile Angular Style: standalone, inject(), inline templates & styles.

yjaaidi avatar Dec 07 '22 16:12 yjaaidi

I hope PW team will not postpone this feature because of previous comment :)

e-oz avatar Dec 07 '22 23:12 e-oz

While the current implementation only works with Versatile Angular Style. Using this angular vite plugin might help supporting most use cases. https://github.com/analogjs/analog/tree/main/packages/vite-plugin-angular

yjaaidi avatar Dec 09 '22 13:12 yjaaidi

I also opened an issue to merge efforts.

https://github.com/microsoft/playwright/issues/19380

yjaaidi avatar Dec 09 '22 13:12 yjaaidi

@brandonroberts May I ask if you think it is realistic/desirable to use your Vite plugin for Angular? Saw you said something about it earlier in: https://github.com/analogjs/analog/issues/20#issuecomment-1221413098 😄

sand4rt avatar Dec 20 '22 16:12 sand4rt

@sand4rt yes, it's possible to use it. I've tested a working solution with Playwright Component Tests and Angular using the Vite plugin. Supports inline/external styles and constructor DI

brandonroberts avatar Dec 20 '22 17:12 brandonroberts

Awesome! Do you still have the solution? Perhaps it can help with the integration if the Playwright team decides to use it?

sand4rt avatar Dec 20 '22 17:12 sand4rt

I don't have the solution I worked on before, but I remember the steps to get it working. I'll recreate it and push up a repo and we can decide what to do from there

brandonroberts avatar Dec 20 '22 22:12 brandonroberts

Here's the repo! https://github.com/brandonroberts/analog-playwright-component-tests

Of course it will need more work to override providers and such, but its a start

brandonroberts avatar Dec 22 '22 21:12 brandonroberts

Hi all, i'm also building a library for testing Angular components with Playwright due to the absence of @yjaaidi. It is still work in progress: https://github.com/sand4rt/playwright-ct-angular.

edit: It is ready for use. If you have any feedback, let me know. @brandon thanks for the repo, managed to integrate your Vite plugin!

sand4rt avatar Mar 29 '23 14:03 sand4rt

Hi all, i'm also building a library for testing Angular components with Playwright due to the absence of @yjaaidi.

@sand4rt Why have you not opened issues against our repository? We could have joined the effort instead of splitting the work with multiple community solutions.

edbzn avatar Apr 22 '23 10:04 edbzn

@edbzn I worked on it before @yjaaidi released the lib. Tried to collaborate and combine efforts several times without luck see: https://github.com/microsoft/playwright/pull/19395 for example. So then i decided to publish a separate one.

sand4rt avatar Apr 22 '23 10:04 sand4rt

Hi @sand4rt, I was off for a while and catching up now. As mentioned by @edbzn, we were ok with merging efforts in Playwright, not another library. In addition to this, I am not the only maintainer of @jscutlery/devkit and @edbzn could have helped.

Also, we are sad to see that there is no credit for our work in your library.

yjaaidi avatar Apr 22 '23 10:04 yjaaidi

Also, we are sad to see that there is no credit for our work in your library.

Which part do you think i should have give credit for? I think that the library i made looks/works nothing like yours right?

Edit: Not sure if the Playwright team is focusing on other things, but im still open to collaborate on official support.

sand4rt avatar Apr 22 '23 11:04 sand4rt

Any news on combining/merging any of the above Angular Component Testing solutions in Playwright?

the-ult avatar Jun 20 '23 12:06 the-ult

Hey! Sorry everyone for catching up so late. I just had a quick chat with @sand4rt, and we definitely have to provide official Angular support.

@sand4rt would you be willing to recreate a fresh PR?

Also, I have a couple of suggestions concerning the integration and customization of analog's vite plugin.

@sand4rt @mxschmitt what would be the most efficient approach?

  1. Start the PR and discuss the details on the PR? (my favorite)
  2. Discuss the details on @sand4rt's repo?
  3. Create a playwright-community repo? (that would be temporary and act as an incubator because Angular plugin deserves its place next to its fellow libs/fmwks)

yjaaidi avatar Oct 23 '23 21:10 yjaaidi

I think official Vite support is coming to Angular 17 next month.

Tallyb avatar Oct 24 '23 10:10 Tallyb

That would be awesome news... counting down to the 6th of november!

the-ult avatar Oct 24 '23 11:10 the-ult

Hey! Sorry everyone for catching up so late. I just had a quick chat with @sand4rt, and we definitely have to provide official Angular support.

@sand4rt would you be willing to recreate a fresh PR?

Also, I have a couple of suggestions concerning the integration and customization of analog's vite plugin.

@sand4rt @mxschmitt what would be the most efficient approach?

  1. Start the PR and discuss the details on the PR? (my favorite)
  2. Discuss the details on @sand4rt's repo?
  3. Create a playwright-community repo? (that would be temporary and act as an incubator because Angular plugin deserves its place next to its fellow libs/fmwks)

Would like to create a PR, but it is up to the Playwright team. It's also an option to discuss it at my repo or create a community repo, all fine with me :)

sand4rt avatar Oct 24 '23 13:10 sand4rt

@pavelfeldman @mxschmitt can we go ahead and open a PR?

yjaaidi avatar Oct 24 '23 13:10 yjaaidi

I think official Vite support is coming to Angular 17 next month.

@Tallyb was this discussed somewhere? Official support would be great.

brandonroberts avatar Oct 24 '23 13:10 brandonroberts

I think official Vite support is coming to Angular 17 next month.

Yes @Tallyb! Actually, you can already opt-in to vite since v16 by replacing @angular-devkit/build-angular:browser with @angular-devkit/build-angular:browser-esbuild

That said, this doesn't change things concerning the integration with Playwright etc... as Angular will not expose a vite plugin yet. But Brandon is here to bridge the gap.

yjaaidi avatar Oct 24 '23 13:10 yjaaidi