【问题标题】:Testcafe unable to filter <tspan> using Selector.withText()Testcafe 无法使用 Selector.withText() 过滤 <tspan>
【发布时间】:2021-02-20 15:29:29
【问题描述】:

我正在尝试使用选择器过滤svg 图表上的元素:Selector('.joint-type-mapping-record .v-line').withText('testElement')。但结果是收到一条消息,提示在 DOM 中找不到元素。

我检查了innerText 可用于&lt;tspan&gt;,但它不是。然后我尝试添加 customDOMProperties (addCustomDOMProperties({ innerText: (el) =&gt; el.innerText });),但出现错误:1) TypeError: Cannot redefine property: innerText

有什么方法可以使用withText() 处理&lt;tspan&gt;?

这里是页面代码:

<g model-id="6c1b0506-321a-4c27-8a06-7f2bfa5851ed" data-type="mapping.Record" id="j_488" class="joint-cell joint-type-mapping joint-type-mapping-record joint-element joint-theme-default" magnet="false" transform="translate(-171,99)">
<text joint-selector="headerLabel" id="v-11781" font-size="20" xml:space="preserve" font-family="Galano Grotesque" font-weight="500" text-anchor="middle" text-vertical-anchor="middle" fill="#333333" transform="matrix(1,0,0,1,109,15)">
<tspan dy="0.3em" class="v-line">testElement</tspan></text></g>

【问题讨论】:

    标签: svg automated-tests e2e-testing testcafe tspan


    【解决方案1】:

    默认情况下,TestCafe 不会在 svg 元素中搜索 HTML 元素。为此,您可以使用带有客户端功能的选择器initialized。看看下面的例子:

    import { Selector } from 'testcafe';
    
    fixture `New Fixture`
        .page `https://xg35y.csb.app/`;
    
    test('New Test', async t => {
        const getVlineElement = Selector(() => {
            const allVlineElements = document.querySelectorAll('.joint-type-mapping-record .v-line');
    
            return [].slice.call(allVlineElements)
                .filter(tspan => tspan.textContent === 'testElement')[0];
        });
    
        await t
            .expect(Selector(getVlineElement).textContent).eql('testElement');
    });
    

    【讨论】:

    • 感谢@aleks-pro 的回答。这个对我有用。你能解释一下为什么你为const getVlineElement调用Selector,然后在t.expect内部调用?
    • 看起来像是错字。我认为没有必要将 getVlineElement 选择器再次包装在 Selector 函数中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 2019-03-10
    • 2018-12-23
    • 2019-01-22
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多