Customize TestCafe Selector in TypeScript for Shadow root Element

ぐ巨炮叔叔 提交于 2020-01-21 14:38:10

问题


Please help me to define Selector part in TypeScript

import { Selector, t } from 'testcafe'

fixture `Scenario Name : Validation`
    .page `https://chrisbateman.github.io/guide-to-web-components/demos/shadow-dom.htm`;


const demoPage  = Selector('#demo1');
const paragraph = Selector(() => {
    return demoPageSelector().shadowRoot.querySelectorAll('p');
}, { dependencies: { demoPageSelector: demoPage } });


test('Test ShadowDom', async t => {
    await t
        .expect(paragraph.value).eql('Some text');
});

回答1:


Hi Debashish Samanta,

Dependencies are added to the function's scope at runtime, so TypeScript cannot find them during compilation. You can suppress this validation using the // @ts-ignore comment.

As for the "Argument of type '{ dependencies: { demoPageSelector: Selector; }; }' is not assignable to parameter of type 'SelectorOptions'." error, it seems like the dependencies property is somehow missing in the SelectorOptions type declaration. You can work around this using the <SelctorOptions> type assertion.

import { Selector, t } from 'testcafe'

fixture `Scenario Name : Validation`
    .page `https://chrisbateman.github.io/guide-to-web-components/demos/shadow-dom.htm`;


const demoPage  = Selector('#demo1');
const paragraph = Selector(() => {
    // @ts-ignore: Cannot find name 'demoPageSelector'.
    return demoPageSelector().shadowRoot.querySelectorAll('p');
}, <SelectorOptions> { dependencies: { demoPageSelector: demoPage } });


test('Test ShadowDom', async t => {
    await t
        .expect(paragraph.innerText).eql('These paragraphs are in a shadow root.');
});


来源:https://stackoverflow.com/questions/57983950/customize-testcafe-selector-in-typescript-for-shadow-root-element

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!