Удаление тегов HTML из элемента TextArea для выполнения утверждения в testcafe - PullRequest
0 голосов
/ 30 января 2020

У меня есть несколько полей текстовой области, которые отформатированы следующим образом. Это добавлено в БД следующим образом.

<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, значение в сочетании с / eql, содержит только попадание в кирпичную стену.

await t.expect(messagingDetailsPage.emailBodyHTML.textContent).contains(userdata.emailbodyhtml,"Email Body in HTML Match Not Found")

1 Ответ

2 голосов
/ 07 февраля 2020

Это не типичная задача, поскольку элемент textarea не поддерживает свойство innerText, как ожидалось. Вы можете использовать обходной путь, хотя. Создайте элемент div и установите его свойство innerHTML из 'textarea.value'. Смотрите пример:

<!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>

test

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);
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...