量角器-检查是否选中了单选按钮 [英] Protractor - Check if a radio-button is checked or not

查看:67
本文介绍了量角器-检查是否选中了单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我在运行量角器/硒测试时遇到了这个问题,有时在测试过程中已经检查了单选按钮,有时却没有.

So I have this problem where I run a protractor/selenium test and sometimes a radio-button is already checked during the test and sometimes its not.

等:

<div id="TRUCK" class="radio-item checked" data-gtm="truck">

<div id="TRUCK" class="radio-item" data-gtm="deliveryOpt-truck">

您可以在其中看到类有时具有已检查"的init,有时没有.

where you can see the class sometimes have the "checked" init and sometimes not.

我要解决的问题是,如果没有选中单选按钮,那么我想创建一个单击该函数的函数;如果已经选中,则我们继续.

What I am trying to do solve is that I want to make a function that clicks IF the radio-button is not checked and if its already checked then we just continue.

我设法做到的是:

it('Clicking Truck button', function (done) {

    browser.driver
        .then(() => browser.wait(EC.presenceOf(element(by.id("TRUCK")))), 30000, "Timed out button")
        .then(() => browser.executeScript("arguments[0].click();",element(by.id("TRUCK")).getWebElement()))
        .then(() => done());

但是问题是,如果已经选中单选按钮,它将取消选中它,这是不好的.再次如此.

however the problem is that it will uncheck it if the radio-button is already checked which is not good. So again.

我正在尝试创建一个单击功能.如果未选中单选按钮,则单击.如果已经检查过,那么我们继续.

I am trying to make a function that clicks. If the radio-button is not checked then we click. If its already checked then we continue.

我该怎么做?

推荐答案

您似乎已经关闭.要在 class 属性不包含 checked 值时标识元素,可以使用以下任一

Seems you were close. To identify an element when the class attribute doesn't contain the value checked you can use either of the following Locator Strategies:

  • xpath :

//div[@id='TRUCK' and not(contains(@class,'checked'))]

  • css :

    div#TRUCK:not(.checked)
    

  • 有效地,如果您的用例是在 class 属性的值未选中的情况下调用 click(),而不是 presenceOf(),您需要使用 ExpectedConditions elementToBeClickable() ,您可以使用以下 Locator Strategies (定位器策略)之一:

    Effectively, if your use case is to invoke click() when the class attribute is not having the value checked, instead of presenceOf() you need to use the ExpectedConditions elementToBeClickable() and you can use either of the following Locator Strategies:

    it('Clicking Truck button', function (done) {
    
        browser.driver
        .then(() => browser.wait(EC.elementToBeClickable(element(by.xpath("//div[@id='TRUCK' and not(contains(@class,'checked'))]")))), 30000, "Timed out button")
    

  • css :

    it('Clicking Truck button', function (done) {
    
        browser.driver
        .then(() => browser.wait(EC.elementToBeClickable(element(by.css("div#TRUCK:not(.checked)")))), 30000, "Timed out button")
    

  • 这篇关于量角器-检查是否选中了单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆