22# -*- coding: cp1252 -*-
33
44""" Name: Built In Functions - Appium
5- Description: Contains all Sequential Actions related to automating Android and IOS using Appium
5+ Description: Contains all Sequential Actions related to automating Android, IOS and MacOS using Appium
66"""
77
88#########################
1414from appium import webdriver
1515from appium .options .android import UiAutomator2Options
1616from appium .options .ios import XCUITestOptions
17+ from appium .options .mac import Mac2Options
1718import traceback
1819import socket
1920import os , sys , datetime , time , inspect , subprocess , re , signal , _thread , requests , copy
@@ -340,7 +341,7 @@ def find_correct_device_on_first_run(serial_or_name, device_info):
340341
341342@logger
342343def unlock_android_device (data_set ):
343- """ Unlocks an androi device with adb commands"""
344+ """ Unlocks an android device with adb commands"""
344345
345346 sModuleInfo = inspect .currentframe ().f_code .co_name + " : " + MODULE_NAME
346347
@@ -459,10 +460,6 @@ def launch_application(data_set):
459460
460461 # Parse data set
461462 try :
462- desiredcaps = {}
463- desiredcaps ['unicodeKeyboard' ] = False
464- desiredcaps ['resetKeyboard' ] = False
465-
466463 browserstack_run = False
467464 aws_run = False
468465
@@ -493,6 +490,7 @@ def launch_application(data_set):
493490 platform_version = ""
494491 device_name = ""
495492 ios = ""
493+ macos = ""
496494 no_reset = False
497495 work_profile = False
498496
@@ -505,6 +503,8 @@ def launch_application(data_set):
505503 activity_name = right
506504 elif left in ("ios" , "ios simulator" ) and mid == "element parameter" :
507505 ios = right
506+ elif "macos" in left .lower ().strip ():
507+ macos = right .strip ()
508508 elif left == "work profile" and right .strip ().lower () in ("yes" , "true" ):
509509 work_profile = True
510510 elif left in ("no reset" , "no_reset" , "noreset" ) and mid == "element parameter" :
@@ -520,9 +520,12 @@ def launch_application(data_set):
520520 desiredcaps ['unicodeKeyboard' ] = False
521521 desiredcaps ['resetKeyboard' ] = False
522522 # Set the global variable for the preferred connected device
523- if find_correct_device_on_first_run (serial , device_info ) in failed_tag_list :
523+ if find_correct_device_on_first_run (serial , device_info ) in failed_tag_list and macos == "" :
524524 return "zeuz_failed"
525-
525+ elif macos != "" :
526+ device_id = "device 1"
527+ appium_details [device_id ] = {"driver" :None , "server" :None , "serial" :"" , "type" :"macos" , "iemi" :"" , "platform_version" :"" , "device_name" :"" }
528+
526529 device_type = appium_details [device_id ]["type" ].lower ().strip ()
527530
528531 for left , mid , right in data_set :
@@ -592,14 +595,15 @@ def launch_application(data_set):
592595 platform_version = platform_version ,
593596 device_name = device_name ,
594597 ios = ios ,
598+ macos = macos ,
595599 no_reset = no_reset ,
596600 work_profile = work_profile ,
597601 desiredcaps = desiredcaps ,
598602 )
599603 if result == "zeuz_failed" :
600604 return "zeuz_failed"
601605
602- if launch_app : # if ios simulator then no need to launch app again
606+ if launch_app and macos == "" : # if ios simulator then no need to launch app again
603607 appium_driver .activate_app (package_name ) # Launch program configured in the Appium capabilities
604608 CommonUtil .ExecLog (sModuleInfo , "Launched the application successfully." , 1 )
605609 return "passed"
@@ -662,6 +666,7 @@ def start_appium_server():
662666 while is_port_in_use (appium_port ) and tries < 20 :
663667 appium_port += 2
664668 wdaLocalPort += 2
669+ tries += 1
665670
666671 if tries >= 20 :
667672 CommonUtil .ExecLog (
@@ -759,6 +764,7 @@ def start_appium_driver(
759764 platform_version = "" ,
760765 device_name = "" ,
761766 ios = "" ,
767+ macos = "" ,
762768 no_reset = False ,
763769 work_profile = False ,
764770 desiredcaps = None ,
@@ -867,17 +873,21 @@ def start_appium_driver(
867873 # saving simulator path for future use
868874 Shared_Resources .Set_Shared_Variables ("ios_simulator_folder_path" , str (app ))
869875
870- app = os .path .join (app , ios )
871- encoding = "utf-8"
872- bundle_id = str (
873- subprocess .check_output (
874- ["osascript" , "-e" , 'id of app "%s"' % str (app )]
875- ),
876- encoding = encoding ,
877- ).strip ()
878-
879- desired_caps ["app" ] = app # Use set_value() for writing to element
880- desired_caps ["bundleId" ] = bundle_id .replace ("\\ n" , "" )
876+ ios_part = ios .split ('.' )[0 ]
877+ if ios_part == 'com' :
878+ desired_caps ["bundleId" ] = ios
879+ else :
880+ app = os .path .join (app , ios )
881+ encoding = "utf-8"
882+ bundle_id = str (
883+ subprocess .check_output (
884+ ["osascript" , "-e" , 'id of app "%s"' % str (app )]
885+ ),
886+ encoding = encoding ,
887+ ).strip ()
888+
889+ desired_caps ["app" ] = app # Use set_value() for writing to element
890+ desired_caps ["bundleId" ] = bundle_id .replace ("\\ n" , "" )
881891
882892 desired_caps ["platformName" ] = "iOS" # Read version #!!! Temporarily hard coded
883893 desired_caps ["platformVersion" ] = platform_version
@@ -910,6 +920,13 @@ def start_appium_driver(
910920 desired_caps ["deviceName" ] = "iPhone" # Read model (only needs to be unique if using more than one)
911921 desired_caps ["bundleId" ] = ios
912922 desired_caps ["udid" ] = appium_details [device_id ]["serial" ] # Device unique identifier - use auto if using only one phone
923+ elif str (appium_details [device_id ]["type" ]).lower () == "macos" :
924+ CommonUtil .ExecLog (sModuleInfo , "Setting up with MacOS" , 1 )
925+ desired_caps ["platformName" ] = "mac"
926+ desired_caps ["automationName" ] = "mac2"
927+ desired_caps ["wdaLocalPort" ] = wdaLocalPort
928+ desired_caps ["bundleId" ] = macos
929+ desired_caps ["newCommandTimeout" ] = 6000
913930 else :
914931 CommonUtil .ExecLog (sModuleInfo , "Invalid device type: %s" % str (appium_details [device_id ]["type" ]), 3 )
915932 return "zeuz_failed" , launch_app
@@ -936,6 +953,8 @@ def start_appium_driver(
936953
937954 if appium_driver : # Make sure we get the instance
938955 appium_details [device_id ]["driver" ] = appium_driver
956+ if appium_details [device_id ]["type" ] == "macos" :
957+ Shared_Resources .Set_Shared_Variables ("screen_capture" , "desktop" )
939958 Shared_Resources .Set_Shared_Variables ("appium_details" , appium_details )
940959 CommonUtil .set_screenshot_vars (Shared_Resources .Shared_Variable_Export ())
941960 CommonUtil .ExecLog (sModuleInfo , "Appium driver created successfully." , 1 )
0 commit comments