-1

My attribute value if condition is not working correctly in Selenium. It is printing string value true to console, but the following if condition supposed to print SMS number checkbox is disabled. Printing else output

String value=customerSectionSMSNumber.getAttribute("readonly");
        System.out.println("Value = "+ value);

        if ( value == "true") {
            System.out.println("SMS number checkbox is disabled");
        }`  else {
            System.out.println(customerSectionSMSNumber.getAttribute("readonly"));
        }
Amruta
  • 1,128
  • 1
  • 9
  • 19

2 Answers2

0

try this code

String value=customerSectionSMSNumber.getAttribute("readonly");
        System.out.println("Value = "+ value);
   if(value!=null){
        if ( value.contains("true")) {
            System.out.println("SMS number checkbox is disabled");
          }`  else {
            System.out.println(customerSectionSMSNumber.getAttribute("readonly"));
         }
   }
Amruta
  • 1,128
  • 1
  • 9
  • 19
0

You cann't compare string value with equals operator. You have to use equals or contains method of string class given below.

    String value=customerSectionSMSNumber.getAttribute("readonly");
    System.out.println("Value = "+ value);

    if ( value.equals("true")) {
        System.out.println("SMS number checkbox is disabled");
    }`  else {
        System.out.println(customerSectionSMSNumber.getAttribute("readonly"));
    }

otherwise convert the string value into boolean values and use it if clause as given below.

String value=customerSectionSMSNumber.getAttribute("readonly");
        System.out.println("Value = "+ value);

        if ( Boolean.parseBoolean(value)) {
            System.out.println("SMS number checkbox is disabled");
        }`  else {
            System.out.println(customerSectionSMSNumber.getAttribute("readonly"));
        }
Murthi
  • 5,299
  • 1
  • 10
  • 15