【问题标题】:Asserting the text which changes with button click with css selector and javascript使用 css 选择器和 javascript 断言随着按钮单击而改变的文本
【发布时间】:2015-10-10 01:36:11
【问题描述】:

我必须断言使用特定按钮单击文本更改。我的 javascript 代码是

.clickEl('[title="Smoke Blue / Chocolate"]')
.assert.elContainsText('.js-product-buying-table [data-id="actual_color"]',        'Smoke Blue / Chocolate');

文本的 html

</div>
<div class="js-product-variant-row product-variant-row" data-id="actual_color">
<label class="label-bold variant-label">
          Actual Color:             
<span class="js-variant-name variant-name">Smoke Blue / Chocolate</span>
<span class="js-unit-price-divider hide-content">
<i class="divider">|</i>
</span>
<span class="js-variant-unit-price variant-unit-price"/>

但是当测试用例运行时,断言失败并出现错误 选择器“.js-variant-name.variant-name”在 228 毫秒后可见。 elContainsText 的调试信息 实际 text() 结果:[
选择一个选项] 其中“选择一个选项”是默认值..with html

<span class="js-variant-name variant-name">Choose an option</span>

【问题讨论】:

    标签: javascript css-selectors automated-tests


    【解决方案1】:

    我不确定你的问题是什么意思,因为这对我来说没有多大意义,但这是我可以为你准备的。

    我刚刚尝试按照您告诉我的内容重新编写代码。

    document.addEventListener('DOMContentLoaded', function() {
        addText();
    });
    
    function addText() {
        var colour = document.getElementsByClassName('js-variant-name')[0].innerHTML;
        var result = '';
    
        if (colour === 'Smoke Red / Rose') {
            result = 'Red';
        } else if (colour === 'Smoke Blue / Chocolate') {
            result = 'Blue';
        } else {
            result = 'Choose an option';
        }
        colour = result;
        }   
    

    【讨论】:

    • 我正在使用 nighwatchjs 编写测试脚本......我试图通过 css 选择器获取元素并断言它包含在单击特定按钮时的文本......当红色单击按钮将显示红色,如果未单击按钮将显示蓝色按钮,它将显示选择一个选项。但是当我点击红色按钮并断言文本时,它给出了错误,因为它给出了文本是“选择一个选项”,尽管 html 显示文本是红色的。
    猜你喜欢
    • 1970-01-01
    • 2013-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-10-15
    • 2015-10-07
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    相关资源
    最近更新 更多