iOS/Android Driver casting on a shared test code base

Curious as to how people are solving the problem of managing whether to instantiate IOSDriver or AndroidDriver based on which platform the test is running on. We share test code between iOS and Android and this seems to make our framework more complex.

On that note, why have these branched off? It seems to be moving away from cross-platform compatibility. There are certainly interactions that don’t have an analogue in one platform vs. the other, but I personally found it easier to jut deal with “unsupported platform” exceptions in the test logic than try to dynamically type cast our driver.

1 Like

Hi!

Hmmm… There a lot of ways to manage AppiumDriver instantiation. I think Selenium user experience will be useful (there are FireFoxDriver, ChromeDriver, InternetExplorerDriver and so on).

I think test parametrization will help you. Examles: TestNG, JUnit, JBehave.

You can declare AppiumDriver field in the test code. It will be correct because AndroidDriver and IOSDriver both extend AppiumDriver which is abstract now.

Sample:

//it is parameterized test
public class AppiumJUnitTest{

//this is set up by given parameters before test is started
private AppiumDriver driver;

@Test
public void test() throws Exception{
//code which works agains Android and iOS
}

}

1 Like

Thanks for the explanation! I think I understand better how to approach the problem.

I’m also interested in learning about how the shared test between Android and ios works. It would be great if someone could share a sample code.

@SergeyTichomirov

The Code which you have referenced that will not work in new Appium Version as it comes with generic type like AppiumDriver <``W>

if we will create an instance like

public AppiumDriver<``AppiumWebElement> driver;

based on Android or iOS we will try to assign

driver = new IOSDriver<IOSElement> (new uri ("url"), new DesiredCapabilites ()); or, driver = new AndroidDriver<AndroidElement>(new Uri(""), new DesiredCapabilities());

then it will throw error saying “cannot convert source type IOSDriver<IOSElement> to target type AppiumDriver<AppiumWebElement>”.

How you are suggesting to approach this scenario ?

Thanks,
Mihir

It is not the issue of .Net client. It is the problem (maybe feature) of C# :smile:

http://igoro.com/archive/fun-with-c-generics-down-casting-to-a-generic-type/

There are tons of stuff which you can find.

I think the choosing of the generic parameter of AppiumDriver should depend on your purposes.
If you need to design tests for both Android/IOS then it is better solution to use AppiumWebElement. This works, code is compiled

private AppiumDriver<AppiumWebElement> driver;

driver = new IOSDriver<AppiumWebElement>(serverUri, capabilities, Env.INIT_TIMEOUT_SEC);

If your test are Android-only/IOS-only then you are free to use AndroidElement/IOSElement as the parameter.

Thanks @SergeyTichomirov
Yes, AppiumWebElement works fine, but I was thinking to use full feature of AndroidElement or IOSElement for the tests; that’s why trying different ways to achieve it.
looks like that’s not possible :frowning:

C# generics are not so flexible like generics of Java :frowning: