Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to programmatically switch between ios and android enhanced driver #12

Open
jerimiah797 opened this issue Jul 27, 2021 · 3 comments

Comments

@jerimiah797
Copy link

jerimiah797 commented Jul 27, 2021

When I write appium tests, it looks like I can only declare driver as either an EnhancedAndroidDriver type, or an EnhancedIOSDriver type. I want to run a simple test in a single file that will run on both platforms, but it seems I must choose either android or ios for that file to run on. How do I avoid duplicating all my test files? I am using react native and my app is basically identical on both platforms. I have done something similar with the regular AppiumDriver.

Any suggestions to programmatically switch which 'EnhancedIOS/AndroidDriver' the variable driver refers to in Java??

With the regular AppiumDriver, I can do this:

private static AppiumDriver<MobileElement> driver;

public AppiumDriver<MobileElement> getDriver() throws IOException {
    if (PLATFORM_NAME.equals("Android")) {
       driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    } else if (PLATFORM_NAME.equals("iOS")) {
       driver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
    }
    return driver;
}

But it seems impossible to take this approach with the Enhanced drivers because they don't share a common type. Please advise!

@jerimiah797
Copy link
Author

jerimiah797 commented Aug 10, 2021

I can kind of get it to seem like it might work if I declare the driver as an AppiumDriver, but then the driver.label screenshot capability breaks, because 'label' isn't part of AppiumDriver.

public AppiumDriver<MobileElement> driver;

if(true) {
            driver = Factory.createAndroidDriver(new URL("http://localhost:4723/wd/hub"), caps);
        } else {
            driver = Factory.createIOSDriver(new URL("http://localhost:4723/wd/hub"), caps);
        }
driver.label("Stopping App");

---> Error: Cannot resolve method 'label' in 'AppiumDriver'

But this doesn't actually work at all because App Center wants the driver declared like this:
public EnhancedAndroidDriver<MobileElement> driver; or this:
public EnhancedIOSDriver<MobileElement> driver;

and then of course the driver is supposed to be instantiated later like this:
EnhancedAndroidDriver<MobileElement> driver = Factory.createAndroidDriver(new URL("http://localhost:4723/wd/hub"), caps);
or this:
EnhancedIOSDriver<MobileElement> driver = Factory.createIOSDriver(new URL("http://localhost:4723/wd/hub"), caps);

But in Java, these three variables can not be the same, despite sharing the same name:

    public AppiumDriver<MobileElement> driver;
    public EnhancedAndroidDriver<MobileElement> driver;
    public EnhancedIOSDriver<MobileElement> driver;

Whichever one is declared first is used; the others are ignored. The variable driver can never be a variable that can be any of type enhancedAndroidDriver , enhancedIOSDriver or AppiumDriver type as far as I understand the Java language -- which I admit is limited. Please, I'd love to know what I'm doing wrong.

@albert17
Copy link

albert17 commented Dec 9, 2022

@jerimiah797 Did you find a solution for this?

@jerimiah797
Copy link
Author

Sorry for the delay, @albert17 . I did find a solution. I had to create an interface in Java that let me abstract each call in the driver.. I can post some code here if you are interested.. it's not in a public repo at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants