How to use if-else condition for this switch using appium java

I already used this:

WebElement switch1 = driver1.findElement(By.id("com.android.settings:id/switch_widget"));
	if (switch1.isEnabled()) {
		System.out.println("enable");
	}	else{switch1.click();
	}

but its not working.

use getAtribute(“checked”), it will return string true or false.

where I have to use this attribute…
You mean like this-
if(switch1.getAttribute(“checked”).isEmpty()){
}

As in switch1.getAttribute("checked") == "true"

1 Like

thanks bro, its working.
if (switch1.getAttribute(“checked”).equals(“true”)) {
System.out.println(“enable”);
} else{switch1.click();
}

You can use getText with switch
If switch1.getText()==“OFF/ON”

im using this function below but actually appium still perform both conditions for example if the element is displayed it will perform first part in the if() and then the part of catch ()

and if the element is not displayed it will perform only the part in catch

and this not what i want, i want if the element is displayed do this and end the test

and if the element is not displayed do this and end the test
.
public void Verify_user_can_add_and_remove_song() {
TrackList.click();
PlayList.click();
SportPlayList.click();
try {
if (element.isDisplayed()) {
System.out.println(“song is displayed in the playlist”);
mishaa.click();
//performing long press (press and hold)
TouchAction action = new TouchAction(driver);
action.longPress(longPressOptions().withElement(element(element))).release().perform();
RemoveIcon.click();
Assert.assertNotEquals(element.isDisplayed(), false);
}

    } catch(NoSuchElementException e){
                System.out.println("element is not displayed in the playlist");
                AddTrack.click();
                Folders.click();
                Download.click();
                SearchIcon.click();
                SearchField.sendKeys("misha");
                element.click();
                //conditional wait until presence of an element
                new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.id("com.sec.android.app.music:id/menu_picker_search_done"))).click();
                Assert.assertEquals(elemnet.getText(), " text (Original Mix)");
            }


    }