如何使用量角器来切换没有ID的复选框

时间:2018-08-23 02:17:37

标签: selenium-webdriver protractor e2e-testing

我是量角器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>

1 个答案:

答案 0 :(得分:0)

您应该尝试根据bootstrap-switch-on类的存在进行检查,我认为这表明该复选框已设置/选中。

遵循以下原则:

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

(未经测试)