Git Product home page Git Product logo

wdio-shadowdom-service's Introduction

wdio-shadowdom-service

This is a plugin for WebDriverIO that transparently makes CSS selectors "just work" with the shadow DOM.

With this plugin, APIs like $('.foo') and $$('.foo')will automatically query inside the shadow DOM to find elements. This can help avoid complicated or hard-to-maintain test code.

Before:

// ๐Ÿ˜ž
const element = $('.foo')
  .shadow$('.bar')
  .shadow$('.baz')
  .shadow$('.quux')

After:

// ๐Ÿฅณ
const element = $('.quux') 

Features:

  • APIs like $, $$, and even some basic usage of execute all "just work" with the shadow DOM.
  • Doesn't override the global document.querySelector or document.querySelectorAll. Only touches your test code, not your production code.
  • Uses kagekiri under the hood โ€“ a rigorously-tested utility containing a full CSS selector parser.

Install

npm install wdio-shadowdom-service

Usage

Configuration

Modify your wdio.conf.js like so:

const ShadowDomService = require('wdio-shadowdom-service')

exports.config = {
    // ...
    services: [ [ShadowDomService, {}] ],
    // ...
}

Use the webdriver protocol

Due to an open bug on WebDriverIO, you will also need to use the webdriver protocol, not the devtools protocol. Set this in your wdio.conf.js:

exports.config = {
  // ...
  automationProtocol: 'webdriver',
  path: '/wd/hub',
  // ...
}

Examples

Now you can use selector queries that pierce the shadow DOM:

const element = await browser.$('.foo')
const elements = await browser.$$('.foo')

Some simple usages of document.querySelector/querySelectorAll are also supported:

const element = await browser.execute(() => document.querySelector('.foo'))
const elements = await browser.execute(() => document.querySelectorAll('.foo'))

All selectors are able to pierce the shadow DOM, including selectors like '.outer .inner' where .outer is in the light DOM and .inner is in the shadow DOM. See kagekiri for more details on how it works.

Supported APIs

* execute and executeAsync only work with simple usages of document.querySelector/querySelectorAll or element.querySelector/querySelectorAll.

Currently, WebDriverIO v6 and v7 are supported.

Contributing

To lint:

npm run lint

To fix most lint issues automatically:

npm run lint:fix

To run the tests:

npm test

To run the tests in debug mode:

DEBUG=true npm test

Then open chrome:inspector in Chrome and open the dedicated DevTools for Node.

wdio-shadowdom-service's People

Contributors

dependabot[bot] avatar nolanlawson avatar svc-scm avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

wdio-shadowdom-service's Issues

Use with webdriverIO v8 blocked.

The peerDependency setting in package.json limits the use to webdriverIO v6 and v7 only. This blocks upgrading to wdio v8 which was released recently. I have created pr #27 to fix it.

Some CSS selectors are not working when using shadow service

When I use ShadowDomService in my wdio.config.js, some css selectors are not working(not working meaning - element is not found. sample is provided in the code)
If I remove ShadowDomService from wdio config, element with the same selector is working.
Is this something to do with how kagekiri? I'm not sure. Need your help.

const { expect, browser, $ } = require('@wdio/globals')

describe("top2", () => {
  it("tests top2", async () => {
    await browser.setWindowSize(1920, 934)
    await browser.url('file:///Users/smothkuri/Downloads/test.html')
    
    const selectBox = await browser.$("div.modal-body > div.filters > div:nth-child(1) select.templateInputs")
    console.log("selectBox", selectBox)
    await selectBox.waitForDisplayed()
    await selectBox.click()
  });
});

my HTML code(test.html)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div class="modal-container slds-modal__container" data-aura-rendered-by="3220:0" xpath="1">
        <div id="content_3202:0" class="modal-body scrollable slds-modal__content slds-p-around--medium"
            data-aura-rendered-by="3229:0" data-scoped-scroll="true"><!--render facet: 3088:0-->
            <div class="filters runtime_sales_emailtemplateuiTemplateInsertDialog" data-aura-rendered-by="3135:0"
                data-aura-class="runtime_sales_emailtemplateuiTemplateInsertDialog">
                <div data-aura-rendered-by="3136:0">
                    <div data-aura-rendered-by="3243:0" class="uiInput uiInputSelect uiInput--default uiInput--select"
                        data-aura-class="uiInput uiInputSelect uiInput--default uiInput--select"><label for="3137:0"
                            class="uiLabel-top form-element__label uiLabel" data-aura-rendered-by="3238:0"
                            data-aura-class="uiLabel"><span id="3137:0-label" title="Templates" class=""
                                data-aura-rendered-by="3239:0">Templates</span><!--render facet: 3241:0--><!--render facet: 3242:0--></label><select
                            size="1" aria-describedby="" id="" class="templateInputs select" data-aura-rendered-by="3142:0"
                            data-interactive-lib-uid="14"><!--render facet: 3143:0-->
                            <option label="My Templates" value="MyLightning">My Templates</option>
                            <option label="All Templates" value="AllLightning">All Templates</option>
                        </select></div><!--render facet: 3146:0-->
                </div>
                <div class="slds-list__item slds-m-right--large" data-aura-rendered-by="3163:0">
                    <div data-aura-rendered-by="3254:0" class="uiInput uiInputSelect uiInput--default uiInput--select"
                        data-aura-class="uiInput uiInputSelect uiInput--default uiInput--select"><label for="3164:0"
                            class="uiLabel-top form-element__label uiLabel" data-aura-rendered-by="3249:0"
                            data-aura-class="uiLabel"><span id="3164:0-label" title="Template Folders" class=""
                                data-aura-rendered-by="3250:0">Template
                                Folders</span><!--render facet: 3252:0--><!--render facet: 3253:0--></label><select size="1"
                            aria-describedby="" id="3164:0" class="templateInputs select" data-aura-rendered-by="3170:0"
                            data-interactive-lib-uid="15" style="">
                            <option class="slds-m-left--xx-small uiInputSelectOption" value="" selected="selected"
                                data-aura-rendered-by="3:2466;a" data-aura-class="uiInputSelectOption">All</option>
                            <option class="slds-m-left--xx-small uiInputSelectOption" value="005x0000000fxgJ"
                                data-aura-rendered-by="8:2466;a" data-aura-class="uiInputSelectOption">Private Email
                                Templates</option>
                            <option class="slds-m-left--xx-small uiInputSelectOption" value="00Dx000000096MA"
                                data-aura-rendered-by="13:2466;a" data-aura-class="uiInputSelectOption">Public Email
                                Templates</option>
                        </select></div><!--render facet: 3174:0-->
                </div>
                <div data-aura-rendered-by="3176:0">
                    <div data-aura-rendered-by="3263:0" class="uiInput uiInputSearch uiInput--default uiInput--input"
                        data-aura-class="uiInput uiInputSearch uiInput--default uiInput--input"><label for="3177:0"
                            class="uiLabel-top form-element__label uiLabel" data-aura-rendered-by="3258:0"
                            data-aura-class="uiLabel"><span id="3177:0-label" title="Search templates..." class=""
                                data-aura-rendered-by="3259:0">Search</span><!--render facet: 3261:0--><!--render facet: 3262:0--></label>
                        <div data-aura-rendered-by="3181:0"><input size="30" aria-describedby="" id="3177:0"
                                placeholder="Search templates..." type="search" class="templateSearch input"
                                data-aura-rendered-by="3182:0" data-interactive-lib-uid="16"><button aria-live="off"
                                type="button" class="slds-button slds-button--neutral hide clear uiButton" aria-label=""
                                data-aura-rendered-by="3186:0" data-aura-class="uiButton"><!--render facet: 3187:0--><span
                                    class="assistiveText"
                                    data-aura-rendered-by="3190:0">clear</span><!--render facet: 3192:0--></button></div>
                    </div><!--render facet: 3195:0-->
                </div>
            </div>
                </div>
    </div>
</body>
</html>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.