从TextArea Element中剥离HTML标记以在testcafe中执行断言

问题描述 投票:0回答:1

我有一些文本区域字段,其格式如下。像这样将其植入数据库。

<table style="line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;width: 100%;border-collapse: collapse;border-collapse: collapse;border-collapse: collapse;border-collapse: collapse;border-collapse: collapse;border-collapse: collapse;">
    <tbody>
<tr style="width: 100%;">
        <td style="padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;" valign="top">
            <p>Hello <strong>[[Contact First Name]]</strong>!</p>
            <p>Your unsubscription request to the following [[Tenant Name]] program has been declined:</p>
            <ul><li>[[Program Name]]</li></ul>
            <p>Thank you,</p>
            <p>[[Tenant Name]]</p>
        </td>
    </tr>
</tbody>
</table>

是否有人对我如何断言没有所有标签而仅包含文本的文本区域有任何见解?

我尝试过textcontent,innertext,带有w / eql的值,包含刚碰到的墙。

await t.expect(messagingDetailsPage.emailBodyHTML.textContent).contains(userdata.emailbodyhtml,"Email Body in HTML Match Not Found")
javascript automation automated-tests e2e-testing testcafe
1个回答
0
投票

这不是一个简单的任务,因为textarea元素不按预期支持innerText属性。不过,您可以使用解决方法。创建一个div元素,并从'textarea.value'设置其innerHTML属性。参见示例:

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
</head>
<body>
<textarea>
<table style="line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 
100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;line-height: 1.6em;margin:0;margin-bottom:15px;border:none;background:none;font-size: 1em;width: 100%;width: 100%;border-collapse: collapse;border-collapse: collapse;border-collapse: collapse;border-collapse: collapse;border-collapse: collapse;border-collapse: collapse;">
    <tbody>
    <tr style="width: 100%;">
        <td style="padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;padding:5px;border:1px solid #ddd;vertical-align: top;"
            valign="top">
            <p>Hello <strong>[[Contact First Name]]</strong>!</p>
            <p>Your unsubscription request to the following [[Tenant Name]] program has been declined:</p>
            <ul><li>[[Program Name]]</li></ul>
            <p>Thank you,</p>
            <p>[[Tenant Name]]</p>
        </td>
    </tr>
    </tbody>
</table>
    </textarea>
</body>
</html>

测试

import { Selector, ClientFunction } from 'testcafe';

fixture `fixture`
    .page `...`;

const getTextAreaText = ClientFunction(() => {
    var textarea = document.querySelector('textarea');
    var div      = document.createElement('div');

    div.innerHTML = textarea.value;

    return div.innerText;
});

test('test', async t => {
    const text = await getTextAreaText();

    console.log(text);
});
© www.soinside.com 2019 - 2024. All rights reserved.