“Android can use ADB to help with automated testing, but IOS is not so easy to use. Is there any way?

For IOS, we can use LibiMobileDevice to do some work. We can use xcRun simctl to operate the emulator.

What is a libimobiledevice

LipimBielEdter is a cross-platform software library for supporting protocols on MAC devices such as iPhone. Unlike other projects, it does not rely on using any existing proprietary libraries and does not require jailbreaking. It allows other software to easily access the device’s file system and retrieve information about the device and its internal devices. Website: www.libimobiledevice.org Github address: github.com/libimobiled…

Install For MacOS

"$$ruby - e (curl - fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" # if MAC does not homebrew execute this command, Install libiMobileDevice $brew install ideviceInstaller # ipaCopy the code

1. Print app list and information

ideviceinstaller -l
Copy the code

2. View the UUID of the connected device

idevice_id -l
Copy the code

3. Obtain device information

ideviceinfo
Copy the code

4. Obtain the equipment time

idevicedate
Copy the code

5. Restart the device

Idevicediagnostics restart IdeviceDiagnostics Shutdown IdeviceDiagnostics Sleep SleepCopy the code

6. Install the IPA package and uninstall the application

The ideviceinstaller -i xxx.ipa // command installs an IPA file on the mobile phone. If it is signed by the enterprise, it can be installed directly on non-jailbroken machines. Ideviceinstaller -u [bundleID] // Command to uninstall the application. You need to know the bundleID of the applicationCopy the code

7. View system logs

Idevicesyslog >> iphone-.log &// This command is used to import logs to iphone-.log and is executed in the background. / / then use tail -f and grep view log tail -f iphone logtail - f the iphone. The log | grep 'WeChat' # at WeChat lineCopy the code

Idevicescreenshot // If you get an error message while using a screenshot, copy the two files of the corresponding DeveloperDiskImage version under the libiMobileDevice file.

Path: / Applications/Xcode. App/Contents/Developer/Platforms/iPhoneOS/platform/DeviceSupport/corresponding version

Get version number command:

ideviceinfo -k ProductVersion
Copy the code

Install the DeveloperDiskImage command:

Ideviceimagemounter developerDiskimage.dmg // Then you can take a normal snapshotCopy the code

9, encountered an error

Solution: Uninstall and reinstall

brew uninstall ideviceinstallerbrew uninstall libimobiledevicebrew install --HEAD libimobiledevicebrew install ideviceinstaller
Copy the code

Problem 1 Run the ideviceinfo command

ERROR: Could not connect to lockdownd, ERROR code -21Copy the code

Fault 2 An error occurs during installation:

Requested ‘libusbmuxd >= 1.1.0’ but version of libusbmuxd is 1.0.10
Copy the code

Solution:

brew updatebrew uninstall --ignore-dependencies libimobiledevicebrew uninstall --ignore-dependencies usbmuxdbrew install  --HEAD usbmuxdbrew install --HEAD libimobiledeviceCopy the code

Problem 3: Run ideviceInstaller -i ‘Installation package path’. Throws the following error:

29385 abort  ideviceinstaller -i
Copy the code

Solution: Uninstall the ideviceInstaller and install the latest version of ideviceInstaller

Operating the iOS emulator command (xcrun simctl)

When doing automated tests, simulators are sometimes used instead of real computers, which has several advantages. One is that there are not necessarily so many real devices, which can save resources, and there is no battery running out.

We need to understand the difference between the simulator and the real machine: the simulator is i386 processor and the real machine is arm series. Arm is the CPU of embedded devices, and it is theoretically less accurate. Take these factors into account when writing mobile devices.

Hardware Limitations iOS emulators have no hardware limitations, such as memory. So there will be cases where applications appear fast on the emulator and slow on the real machine because the real machine has run out of memory.

We are doing basic functional automation, which can be replaced by emulators. We can use commands to manipulate the emulator, so let’s see what xcRun simctl does!

  1. screenshots

    xcrun simctl io booted screenshot /pictures/test.png

  2. Record the screen command

    Xcrun simctl IO Booted recordVideo/Videos /test.mp4# Press Ctrl+C in terminal to stop recording

  3. View the installed devices

    Xcrun simctl list# lists installed available emulators xcrun instruments -s check installed emulator ios-sim showdevicetypes

  4. Start simulator

    This is used to start the emulator, where the UUID parameter is the UUID in the previous list. Xcrun simctl boot $UUIDxcrun Instruments -w “iPhone 8(11.2)”

  5. Closing the simulator

    xcrun simctl shutdown $UUID

  6. Resetting the simulator

    xcrun simctl erase $UUID

  7. Clean up unavailable emulators

    This command may help you regain disk space when your Mac is running out of space. xcrun simctl delete unavailable

  8. Install the specified app

    Xcrun simctl install booted path > < app ios – sim launch/Users/nali/Desktop/ting. The app – devicetypeid iPhone – X, Xcrun simctl install <app path >

  9. Run the specified app

    Xcrun simctl launch Booted # XcRun simctl launch for multiple devices

  10. Close an open application

    Xcrun simctl Terminate Booted # XcRun simctl terminate for multiple devices

  11. Uninstalling a specific application

    Xcrun simctl uninstall Booted # Xcrun simctl uninstall for multiple devices

  12. Copy and paste pbCopy & PBpaste between emulator and Mac device

    Pbcopy Copies the contents to the Mac clipboard. Pbpaste Pastes the contents on the Mac clipboard. Xcrun simctl pbCopy Booted Copies the contents on the Mac clipboard to the emulator. Direction: Mac= simulator xcrun simctl pbpaste Booted Copies the contents of the simulator clipboard to the Mac clipboard: Direction: Emulator = “Macxcrun simctl pbsync sourceDevice destDevice Synchronizes the contents on the clipboard on the Source device to the clipboard on the Dest device. Direction: source = dest, where host indicates the Mac device

  13. More functions view

    xcrun simctl -h

These commands, you can write a package to call, for example:

import osimport reimport subprocessimport time current_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time())) class iosinformation(): def exec_command(self, cmd): result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) (stdoutdata, stderrdata) = result.communicate() # print("Result is:%s") % stdoutdata if (re.search("No device found", str(stdoutdata)) or re.search("Could not connect", str(stdoutdata))): print ("Please connet it agian, or add permission like: brew install libimobiledevice --HEAD,sudo chmod -R 777 /var/db/lockdown/") else: return stdoutdata def Get_UUID(self): cmd = 'idevice_id -l' uuid = self.exec_command(cmd) return uuid def Get_Device_Name(self): cmd = 'ideviceinfo -k DeviceName' device_name = self.exec_command(cmd) return device_name def Get_Device_information(self): cmd = 'ideviceinfo -k ProductVersion' device_information = self.exec_command(cmd) return device_information def Get_Device_Product_type(self): cmd = 'ideviceinfo -k ProductType' product_type = self.exec_command(cmd) return product_type def List_All_Pakages(self, uuid): cmd = 'ideviceinstaller -u {0}'.format(uuid) all_pakages = self.exec_command(cmd) return all_pakages def List_All_Logs(self, uuid): all_logs = "idevicesyslog -u {0}".format(uuid) return all_logs def Take_Screenshot(self): current_dir = os.path.split(os.path.realpath(__file__))[0] cmd1 = "idevicescreenshot {0} + '/' + screenshot-DATE.tiff".format(current_dir) cmd2 = "sips - s format png {0}.tiff - -out {1}.png".format(current_time, current_time) self.exec_command(cmd1) # self.exec_command(cmd2) def Install_Ipa(self, ipa): cmd = 'ideviceinstaller -i {0}'.format(ipa) result = self.exec_command(cmd) return result def Uninstall_Ipa(self, appid): cmd1 = 'ideviceinstaller -l' cmd2 = 'ideviceinstaller -U {0}'.format(appid) result = self.exec_command(cmd1) appids=[] for id in result.split('\n'): if re.search('-',id): str = id[0:id.find("-")].strip() appids.append(str) else: pass if appid in appids: result = self.exec_command(cmd2) else: print ("The appid dosen't exit in the devices") # cmd2 = 'ideviceinstaller -u appid'.format(appid) # result = self.exec_command(cmd) # return result def main(): ios = iosinformation() uuid = ios.Get_UUID() print (" uuid is {0}".format(uuid)) device = ios.Get_Device_Name() print ("  device is {0}".format(device)) device_info = ios.Get_Device_information() print (" device_info is {0}".format(device_info)) product_type = ios.Get_Device_Product_type() print (" product_type is {0}".format(product_type)) # all_pakagas = ios.List_All_Pakages(uuid) # print " all_pakagas is {0}".format(all_pakagas) ios.Take_Screenshot() if __name__ == '__main__': main()Copy the code

Experience lies in accumulation, in daily work, bit by bit accumulation, may be able to greatly improve work efficiency!

IOS Technology Group: [563513413](Jq.qq.com/? \_wv= 1027&… , share BAT, Ali interview questions, interview experience, discuss technology, we exchange learning and growth together!