首页 > 解决方案 > 如何使用量角器切换没有 id 的复选框

问题描述

我是量角器 e2e 的新手。我正在尝试切换没有 id 的复选框。如果复选框被选中,我如何获取值?

<div class="bootstrap-switch bootstrap-switch-wrapper bootstrap-switch-mini bootstrap-switch-on bootstrap-switch-animate" style="width: 62px;">
    <div class="bootstrap-switch-container" style="width: 90px; margin-left: 0px;">
        <span class="bootstrap-switch-handle-on bootstrap-switch-success" style="width: 30px;">Yes</span>
        <span class="bootstrap-switch-label" style="width: 30px;">&nbsp;</span>
        <span class="bootstrap-switch-handle-off bootstrap-switch-danger" style="width: 30px;">No</span>
        <input type="checkbox" data-bind="bootstrapSwitchOn: IsActive, v1:$root.isenable" data-on-text="Yes" data-off-text="No" data-size="mini" data-handle-width="20px" data-label-width="20px" data-on-color="success" data-off-color="danger">
    </div>
</div>
<label class="checkbox-inline" data-bind="text: window.LabelVM.workflow_details_active,attr: { 'data-title': window.LabelVM.workflow_details_active, 'data-con': window.LabelVM.Active_popover, 'data-popover': 'popover' }" data-tab="3" data-orientation="right" data-title="Is Active" data-con="Activate your workflow for it to appear in Self Service. If a Workflow isn&amp;#39;t active, it cannot be accessed by a staff member" data-popover="popover">Is Active</label>

标签: selenium-webdriverprotractore2e-testing

解决方案


You should try making your check to be based on the presence of the bootstrap-switch-on class that I assume indicates that the checkbox is set/checked.

Something along these lines:

function checkValue (checkbox) {
    checkbox.getAttribute("class").then(function (classes) {
        var switchOn = classes.indexOf("bootstrap-switch-on") >= 0;
        if (switchOn) {
            console.log("Switch is on");
        } else {
            console.log("Switch is off");
        }
    });
}

var checkbox = $(".bootstrap-switch");
checkValue(checkbox);

checkbox.click();
checkValue(checkbox);

(not tested)


推荐阅读