iPhone and Android

In this lesson, we will learn how to write a cross-platform script. We will use the demo script from the third tutorial and enhance it by adding support for iOS, Android, and Windows devices. Let’s start by connecting two devices: Android and iOS. Then add two iterations to our Datatable. For now, we can just duplicate the first row. The first operation in this script is the Launch method. It can launch apps on a local or a remote computer. This method also supports mobile devices. To learn more about this method, please check out the relevant documentation on our Portal. We have connected an iOS and an Android device. If you do not have two devices, you can still follow the instructions for one of the devices. For the iOS device, modify the launch command argument to “ios” followed by the browser that is being used, for example, the zap browser. For the Android device, we can use the chrome browser, so we type in “android chrome”. This parameter will launch the Chrome browser on the first connected Android device. Before running the script, we will learn a few tricks which are very useful when working with iterations. There is the “Iteration” variable. With it we can set the Datatable to skip to a specific row or Iteration. The “StopTest” command stops the execution of the test. And the “StopIteration” advances to the next row in the Datatable and restarts the execution of the script. We know this script works on a Windows machine so we don’t need to run the first row, or Iteration, in the Datatable. To skip it, add the “StopIteration” command when the script runs the first iteration, and add a StopIteration command after the Launch command. Run the script. ZAPTEST will launch the browsers on both iOS and Android devices. Notice, the views look quite different. Let’s enhance our script to support it! The first enhancement will be with the Combo Box item: Since each device can have a different resolution, the distance between the label and the Combo Box can appear different. The same problem occurs when we create the script on a standard DPI and then try to run the script on a High DPI monitor. Not only the size is affected, but also the spacing will be different. ZAPTEST can solve this problem using the scalability property. If we set our Combo Box’ scalability property to Parent Size, ZAPTEST will recalculate the distance based on the size of the parent label object. Let’s run the second iteration and then stop the script. To do so, remove the first If-statement, set the Iteration variable to two, and add StopTest command after the Click method on the Combo Box object. Now, run the script. The selection dialog looks quite different. This is the platform-dependent part of the script. Each platform can have its own implementation of the Combo Box. Using the ZAPTEST Enterprise Edition, we can write a custom function which would perform the item selection, but in case of the ZAPTEST Free Edition, we should repeat this step for each Combo Box in the application, which we are going to do now. Let’s scan this view: Open the Workbench and scan it. Here we will use the Area and the OCR objects to click on the menu item. Remove all objects except the “Done” button and one of the list items. Set the color property for the list item and check how the OCR works. Then, add the Area object and set all the relative objects like we did in previous lessons. The Area object will help us specify the search region and improve reliability. Now we can save and close the Workbench. For the Area object, we can also set the “Parent Size” Scalability option which adds support for different resolutions. So how do we tell ZAPTEST to perform certain operations on the iOS but not on the other platforms? The main Application object has a neat method: GetPlatformType. This method retrieves the platform type of the current device that is used in the script. This method works only if the Launch method was used prior to it at least once in this script. We can use the GetPlatformType method in If-Else statements to add support for iOS and Android. Let’s do that, with the iOS device first: pre-set the list object text, click on the list item, and then click the “Done” button to finish the selection process. Also, add ParentSize scalability option for the List object which we have used for the windows system. It will make this object cross-DPI. Now, the Android device. Let’s change our Iteration and check how it looks like. This is the new view. Here too, scan it, but keep only one object that will Click on the item. Don’t forget to set the color of the object, it will improve the recognition. Save and close the Workbench, then, add a new Else-If condition, where we preset the real text of the object and click on it. It’s quite simple with Android because here the Combo Box object covers the entire screen. However, in other cases, a combo-box could get a tad more complicated on an Android device. That’s it. We have written a platform dependent code for selecting the combo box items in various platforms. We should use the same objects for the second Combo Box item, but with different preset text from the Datatable. As we have already learned, in ZAPTEST Enterprise Edition, we can create a custom function which will automatically detect the platform and reuse this procedure multiple times in multiple scripts, so we would only have to write it once. Let’s check how the DatePicker control looks like. Move the StopTest command behind the DatePicker line and then run the second iteration. We can see that this control is hiding the “Date” label; use another parent object with the scalability option to solve the cross-resolution issue. For our parent object of the table, use the days names, Monday to Thursday. Open the Workbench and add a new object, “Names”, update the text property and set Partial OCR recognition. It will allow us to find parts of the text. Also, update the parent object and the scalability option for our Table object. Set the new parent object to “Names” and select “Parent Size” as the scalability type. That’s it. Now we can save and close the Workbench. This will work on windows and iOS platforms. Now, let’s check how it looks on the Android device. We will switch to the third iteration and check how the DatePicker control looks on the Android device. We can see that the edit cursor and the android keyboard hide part of the DatePicker control. This means that we should add some additional steps to support android platforms. First, scroll the view down to move the whole control up, then, - using the RightMouse click - hide the keyboard. Add the code which will do just that. Add an If-statement for the Android platform and scroll down the view before activating the control. ZAPTEST has a method called “ScrollWheel”. To scroll down, provide a negative number. This number represents the number of pixels that ZAPTEST will scroll down the page. Now, set it to negative 300 and add a Wait command for 1 second. Otherwise, ZAPTEST might click on a wrong object position because ZAPViewer needs some time to update the view. After clicking on the control, perform a right mouse click using the RightClick method. First write the If-statement and then use the Date label for the RightClick operation. Run the script and check how the DatePicker control looks like after these updates. Now we can see that the view is identical to the windows and the iOS platforms. The last thing to update is the result page. In case of a mobile device, scroll the page down to read the flight information. To obtain all flight numbers, use programmatic loops, which are available only with the ZAPTEST Enterprise Edition. With the Free Edition, we get only one flight number from the result table. Let’s check how the results look like on an Android device. Move the StopTest command behind the clicking on the Search button, and update our table method, since we changed the parent object. Run the script and check the result page. Scan this page and keep only one object, the flight label. We will add the related Flight Number object which we will use to get the flight information. Also, update the text color for our flight label and set the scalability of the child Flight Number object to ‘ParentSize’. Now we can save and close the Workbench. It’s time to add the code. We will use If-Else statements and add special steps for the mobile devices. Use our new object to get the flight information. Also, before retrieving the text, add the ScrollWheel command to scroll the page for 200 pixels. Our script is now ready for cross platform execution. Remove the StopTest command and the Iteration variable. Then run the script. First iteration is running on the local Chrome browser. Now that we have improved the script, we can zoom in on the page or change the DPI setting while the script is still working. Second iteration is running on the iOS device. We added conditional steps that work only with the iOS Combo Box controls. Other than the objects in these steps, everything is the same as for the Windows system. The third iteration is running on the Android device. In addition to the Combo Box controls, we wrote code to handle a device-dependent issue with DatePicker control, which scrolls the page and hides the keyboard. All 3 iterations are successfully completed. We can now check the results to obtain more information about the execution process. Thank you for using ZAPTEST.

Contact Us

1425 Market Blvd Suite 530-230

Roswell, GA 30076, USA

(800) 795-3552