webdriverio / query-selector-shadow-dom

querySelector that can pierce Shadow DOM roots without knowing the path through nested shadow roots. Useful for automated testing of Web Components. Production use is not advised, this is for test environments/tools such as Web Driver, Playwright, Puppeteer
MIT License
242 stars 25 forks source link

How to reference the path for addScriptTag from a different file #22

Closed alpriya87 closed 4 years ago

alpriya87 commented 4 years ago

Hi,

At present I have multiple test cases in a single file and i end up defining the library for each case.

Can you please show us some example of how to reference the library from a different file to use it inside the actual code. Probably a function

I'm new to javascript. This would definitely help a lot to most I believe!!

await this.page.addScriptTag({ path: path.join(__dirname, '../../node_modules/query-selector-shadow-dom/dist/querySelectorShadowDom.js') });

Reference the above code in the actual test case.

I tried to use it from different file like below :

helper.js async function path() { await this.page.addScriptTag({ path: path.join(__dirname, '../../../node_modules/query-selector-shadow-dom/dist/querySelectorShadowDom.js') }); } module.exports = { path };

code : const pathFor = require('./lib/helper.js')

await pathFor.path(); // this is the code in question

try { const nextbtn = (await this.page.waitForFunction(() => { const nextbtn = querySelectorShadowDom.querySelectorDeep(".something") return nextbtn; })).asElement(); await nextbtn.click(); console.log("clicking the button is a success")

}catch(e) {
    throw new Error("clicking button is not a success")
}

Thanks in advance

Georgegriff commented 4 years ago

I'm a little surprised what you've done in helper.js doesn't work I'll try to find some time later next week to look at this. I want to make using the this lib with puppeteer better, you may have seen the updates to introduce playwright support which is done I a much nicer way I'd like to do that same with puppeteer if it supported it

alpriya87 commented 4 years ago

Great, looking forward for the update on this!! will be much useful for my current project.

Georgegriff commented 4 years ago

One thing to check with your code example you are importing path but also have function named path in the same scope, this wont work image

Georgegriff commented 4 years ago

also you re not passing in the page object?

alpriya87 commented 4 years ago

I'm passing it in the page object

alpriya87 commented 4 years ago

Georgegriff, i do not have any other function named path.

I also see that few click and tags are not recognized with deepqueryselector for ex: below is the complete path document.querySelector("body > web").shadowRoot.querySelector("search").shadowRoot.querySelector("#search > div.search-text > dn-search").shadowRoot.querySelector("#helpers-1234").shadowRoot.querySelector("div > span")

deep selector to get the inner text is not working in this case

Georgegriff commented 4 years ago

Happy to look into if there are issues with the library, what are those web and search elements? Can't be web components they have no dash

Georgegriff commented 4 years ago

Doesn't seem valid that an element of web can be a direct child of body which is what your above snippet suggests, I'm not aware of an element called web?

Georgegriff commented 4 years ago

Georgegriff, i do not have any other function named path. What I meant was your function is named path and you're using the path node module path.join..., this doesn't seem right, please look closer at my screenshot

Georgegriff commented 4 years ago

Closing due to inactivity