How to switch between the iOS and Android platform

Hi Team,
When i was working on previous version of Appium when i wanted to switch between two different platforms i use to use isPlatformAndroid and isPlatformIOS but after upgrading to Appium 2 thats seems to be not working and my code is existing out of the condition.

Below is the code

public HomeScreen GrantPermissionForUserWithTiles() {
		System.out.println("in the android");
		if (isPlatformIOS) {
			System.out.println("in the iOS");
			Assert.assertTrue(waitForElementToBeDisplayed(15, allowBluetoothButton),
					"Element 'allowBluetoothButton' was not found.");
			allowBluetoothButton.click();
			Assert.assertTrue(waitForElementToBeDisplayed(10, okButton), "Element 'okButton' was not found.");
			okButton.click();
			Assert.assertTrue(waitForElementToBeDisplayed(10, allowPushNotificationButton),
					"Element 'allowPushNotificationButton' was not found.");
			allowPushNotificationButton.click();
			Assert.assertTrue(waitForElementToBeDisplayed(10, allowButton), "Element 'allowButton' was not found.");
			allowButton.click();

			Assert.assertTrue(waitForElementToBeDisplayed(10, continueButton),
					"Element 'continueButton' was not found.");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			continueButton.click();
			Assert.assertTrue(BaseInteractions.waitForElementToBeDisplayed(5, allowOnceButton),
					"System popup not present?");
			allowOnceButton.click();

			Assert.assertTrue(waitForElementToBeDisplayed(10, continueButton),
					"Element 'continueButton' was not found.");
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			continueButton.click();
			Assert.assertTrue(waitForElementToBeDisplayed(15, locationAsk), "Element 'locationAsk' was not found.");
			locationAsk.click();
			Assert.assertTrue(waitForElementToBeDisplayed(10, alwaysButton), "Element 'alwaysButton' was not found.");
			alwaysButton.click();

			tap(driver,20,20);
		} else if (isPlatformAndroid) {
			System.out.println("in the android");
			clickElementIfDisplayed(enableBluetoothBtn);
			driver.switchTo().alert().accept();
			if (!driver.getCapabilities().is("autoGrantPermissions")) {
				Assert.assertTrue(waitForElementToBeDisplayed(2, locationNextBtn),
						"Element 'locationNextBtn' was not found.");
				locationNextBtn.click();
				driver.switchTo().alert().accept();
				Assert.assertTrue(waitForElementToBeDisplayed(2, locationNextBtn),
						"Element 'locationNextBtn' was not found.");
				if (Integer.parseInt(String.valueOf(driver.getCapabilities().getCapability("platformVersion"))) > 10) {
					locationNextBtn.click();
					Assert.assertTrue(waitForElementToBeDisplayed(5, allowAlwaysLocationAccessRadioBtn),
							"Element 'allowAlwaysLocationAccessRadioBtn' was not found.");
					allowAlwaysLocationAccessRadioBtn.click();
					driver.navigate().back();
				}
				if (clickElementIfDisplayed(nextBtn))
					driver.switchTo().alert().accept();
				Assert.assertTrue(waitForElementToBeDisplayed(2, allowPushNotificationButton),
						"Element 'btn_allow' was not found.");
				allowPushNotificationButton.click();
				driver.switchTo().alert().accept();
			}
			clickElementIfDisplayed(getElementByText("ALLOW"));
		}
		clickElementIfDisplayed(new HomeScreen(driver).closeBannerButton);
		return new HomeScreen(driver);
	}

Can you open an IDE and identify where these live? I don’t think they are Appium commands. Also it would be helpful if you shared what programming language. I think it’s Java, but I looked through the Java Client repo and don’t see these commands anywhere.

Sorry i just noticed that those variables where declared like this

public static boolean isPlatformAndroid;
public static boolean isPlatformIOS;

1 Like