Run one script for both iOS & Android

Hey,

I have a app to test on iOS & Android platform. So I can use same script on both platform ,so can anybody please help me for this

Thanks

1 Like

Hi @nayu

The app you are testing is native or web app.
if web app then you can use same script.
for native app it may change in locating techniques.

Thanks,
-Mani

Hi,@mani
Thanks for reply ,but i am trying through localization ,so can it be possible for native app.
Thanks

Sure you can,

in your test you have to implement such class which holds Appium Driver object.
once you check whether its IOS or Android you create a corresponded instance.

you use System.getProperty(“os.name”); to retrieve the os you are running on
you will receive “Mac” or “Windows” , this way you get the platform.(Appium IOS works only on Mac, it needs the Xcode ).
For Windows the platform = “android” for Mac platform = “IOS”
then you use it

AppiumDriver _driver;
if (platform.equals("android"))
	_driver = new AndroidDriver(new URL(mAppiumRemoteAccess), mCapabilities);
else
	_driver = new IOSDriver(new URL(mAppiumRemoteAccess), mCapabilities);

then it does not matter what platform you use, you have one driver. now write you methods using the driver. use these methods within a test class.

1 Like

In addition to the comment above, you can also stack iOSFindby and AndroidFindBy in the same file. If your app is complicated, you’ll need to insert a lot of if/else conditions. (if android, do this. If ios, do that. etc) I can tell you from my experience that you can probably share ~70 to 80 % of your code between iOS and Android.

Hi, @Mayu_Kataoka & @TamirS
Can you people please show it through some example for mobile apps

Thanks in advance

You can use same script for both android and iOS, provided your app is identical in both platforms.
You can initiate drivers as per above explanations. Let come to locator part. So in that case you can use page object model to make it work. let me give below example. For login screen in iOS and Android are same.

public LoginScreen extends AbstractScreen{
@iOSFindBy(xpath="")
@AndroidFindBy(uiautomator="")
private MobileElement userNameTextEdit;

public void fillUserName(){    
userNameTextEdit.sendKeys("bs");
    }
}

Hope this helps.

Thanks,
Priyank Shah

Hi @TamirS
Do you mean that, for android I need to run Appium on windows and for iOS it should be run on Mac??
As I am getting “Mac OS x” as OS name, can you please provide me a way to find device OS type on single machine?

In my opinion, its kinnda lame to write code to detect your phone platform. you know what device you run the test on. dont waste your time to detect such device.

I use JSON file which contains devices details including platform and extra info which i need along the test.
I parse the file prior running the tests.

assuming my JSON file contains both SGS3 and iPhone6,
if I run on window and the current device is SGS3(which runs Android) I do the test otherwise I skip it. same thing for iPhone.

if its not clear, let me know.

cheers.
Tamir

Hi Tamir,

I have gone through your reply for run one script for both ios and Android and looking meaningful to me.
I am new to Appium can you please help me regarding this topic.

Thanks,
Rajeev