Closed Manojms1997 closed 4 years ago
As far as I can tell, this is not a kagekiri issue. What's happening in this test is that, somehow, the module is being executed in a Node.js environment rather than a browser environment. Element
is undefined in Node.js (as is document
, window
, etc.), so the test fails. (I can tell this is true because I can add console.log
s to kagekiri.cjs.js
showing that it is a Node environment, e.g. console.log(typeof process)
.)
You could test this by not using kagekiri at all, but adding some very simple assertion to your component such as:
if (typeof window === 'undefined') {
throw new Error('no window!')
}
This should still fail your test.
I'm not an expert in Angular or Protractor so I can't really help with the configuration, but it needs to either execute in a browser environment or jsdom
, etc. kagekiri is not designed to run in Node.js but rather in a browser environment.
Hope that helps, closing the issue for now!
BTW thank you for including a reproducible test case; that is very helpful! 🙂
I believe I see the issue now. It looks like this line of code is importing kagekiri
in a Node.js environment. It looks like these Protractor tests run in Node.js but send commands to the browser. So browser code cannot run directly in these files, but rather has to be injected (e.g. with executeScript()
).
@nolanlawson Thank you for giving more insight on the issue. I tried using executeScript() to run querySelector(), but I am facing some more errors. I will continue looking into the issue. It would be helpful, if I can get a sample application using kagekiri in protractor e2e.
Hello @nolanlawson , Thank you for suggesting executeScript() to load kagekiri on browser side. It worked. I am using browser.executeScript() to load kagekiri file, in this line of code. But in order to load kagekiri, it looks like I should have kagekiri.umd.js file accessible from my application (right now I have added it in assets folder). Is there any better way to inject kagekiri.js on the browser side. Also I have updated https://github.com/Manojms1997/KagekiriEndToEnd.git for reference.
PS: We are actually looking for shadow DOM piercing approach for our component harness. So on the suggestion from this issue we are using kagekiri as query function for our harness loader.
Yes, loading kagekiri
into the browser is the best way to use it in this kind of webdriver-testing approach. Based on the angular/components
issue you linked to, it looks like they are injecting kagekiri
like this.
The technique you are using looks like it should work too. :) Dropping in kagekiri.umd.js
is an easy way to access the global kagekiri.querySelector
/kagekiri.querySelectorAll
functions. Glad you got it working. :)
Issue:
E2E testing with protractor, using kagekiri's querySelector() and querySelectorAll() is giving "Element is not defined" error and pointing to kagekiri's CommonJS file. This issue is not present in karma and jasmine based unit test.
Reproduction:
The DOM structure of angular's AppComponent is:
Contents of AppComponent is inside shadow DOM as encapsulation is set to ViewEncapsulation.ShadowDom.
E2E test:
In app.e2e.spec.ts file, querySelector and querySelectorAll methods are imported
The following test case is written in e2e test:
When running end to end test for this application, I am getting the following error:
But the same test in karma based unit test is passing. This same issue was faced when using test harness which contains kagekiri, being used in protractor e2e tests.
The dependencies and dev-dependencies are:
You can find the sample application, having this issue, in the following github link: https://github.com/Manojms1997/KagekiriEndToEnd.git