Switch between Android Driver and iOS Driver in a single test (Appium)

I’m just trying to get a single test for both of Mobile Operative System

I’m using Cucumber as test framework for example that feature:

@test

Scenario Outline: Test app

When Start Test "<Platform>"
Then Do the test

Examples:
  | Platform |
  | Android  |
  | IOS      |

Calls to my Test.java:

public class Test {
  public <typeofvariable> driver;
  @When("Start Test {string}")
  public Start_test (String platform){
  if (platform.equals("Android"))
    {
        driver = new InitAndroidDriver();

    }
    else { driver = new InitIOSDriver();}
     
  }
  @When("Do the test")
  public Do_the_test{
    driver.context("NATIVE_APP");
  }

}

But I don’t know what type of variable needs to be “driver” to accept AndroidDriver and also IOSDriver, I tried putting AppiumDriver in but It not works for me, because in the “Do the test” section I need to switch between context and AppiumDriver dont seems to have that function.

Appreciate the help.

But I don’t know what type of variable needs to be “driver” to accept AndroidDriver and also IOSDriver, I tried putting AppiumDriver in but It not works for me, because in the “Do the test” section I need to switch between context and AppiumDriver dont seems to have that function.

This is exactly what interfaces were invented for. Simply cast your common class to the desired interface type, for example ((SupportsContextSwitching) driver).getContext()

1 Like

Appreciate it, it worked for me :slight_smile:

I didi this:

public class Test {
  public AppiumDriver driver;
  @When("Start Test {string}")
  public Start_test (String platform){
  if (platform.equals("Android"))
    {
        driver = new InitAndroidDriver();

    }
    else { driver = new InitIOSDriver();}
     
  }
  @When("Do the test")
  public Do_the_test{
   switchContext(driver,"NATIVE_APP",platform);
   //Some test here
   switchContext(driver,"WEBVIEW",platform);
 
  }


public static void switchContext (AppiumDriver driver, String context, String platform) throws InterruptedException {
        Thread.sleep(2000);
        int context_position = 0;
        if (context.equals("NATIVE_APP")) {context_position = 0;} else {context_position = 1;}
        if (platform.equals("Android"))
        {
                Set<String> contextNames = ((AndroidDriver)driver).getContextHandles();
                for (String contextName : contextNames) {
                    System.out.println("Context available Android: " + contextName);
                }
                System.out.println("Context selected: " + contextNames.toArray()[context_position]);
                ((AndroidDriver)driver).context((String) contextNames.toArray()[context_position]);
        }
        else {
            Set<String> contextNames = ((IOSDriver)driver).getContextHandles();
            for (String contextName : contextNames) {
                System.out.println("Contextos available IOS: " + contextName);
            }
            System.out.println("Context selected: " + contextNames.toArray()[context_position]);
            ((IOSDriver)driver).context((String) contextNames.toArray()[context_position]);
        }

    }
}