简体   繁体   English

使用CSS选择器和javascript声明随按钮单击而更改的文本

[英]Asserting the text which changes with button click with css selector and javascript

I have to assert that with a particular button click the text changes.my javascript code is 我必须断言,使用特定按钮单击文本更改。我的JavaScript代码是

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

the html for the text 文本的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"/>

but when the test case ran the assertion fails with error Selector '.js-variant-name.variant-name' was visible after 228 milliseconds. 但是当测试用例运行时,声明失败,并显示错误选择器'.js-variant-name.variant-name',此错误在228毫秒后可见。 Debugging information for elContainsText actual text() result:[ elContainsText实际text()结果的调试信息:[
Choose an option] where "choose a option" was the default value..with html 选择一个选项],其中“选择一个选项”为默认值。

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

I'm not sure what you're trying to get at here with your question because it doesn't make much sense to me but this is what I could muster up for you. 我不确定您要提出的问题是什么,因为这对我来说意义不大,但这就是我能为您服务的内容。

I've just tried to re-do the code from what you've told me. 我只是想根据您告诉我的内容重新编写代码。

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;
    }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM