Presentation is loading. Please wait.

Presentation is loading. Please wait.

Writing My First Test.

Similar presentations


Presentation on theme: "Writing My First Test."— Presentation transcript:

1 Writing My First Test

2 Agenda Introduction – Selenium & Perfecto Mobile
Get up and running with the MobileCloud WebDriver plugin Download & setup Write our first test Get introduced to some basic commands (14-22) Add a validation at the end of the test (23) Run, view and share report (images, run view, video etc.) (24-28) References & Samples (29)

3 MobileCloud Web Driver Architecture
HTTP HSS Mobile web driver Java code Running in Eclipse HTTP command level API MCM Before we get started on the MobileCloud Plugin and all the technical details of the WebDriver, let’s take a quick peek at the architecture that defines this solution. First, we should notice that we have a real device that is connected, via a USB cable, to the Perfecto Mobile server (also known as the Handset Server or HSS). Through this USB cable we get a live streaming from the device and are able to control the device; including any device real user commands/gestures (such as tap, swipe, click etc.). The HSS, which is responsible for managing all device connectivity, gets its commands from the MobileCloud manager (MCM) server. The MCM is the component responsible for executing all of the scripts as well as storing within its repository and reports. The MCM gets its commands via the HTTP command level API, which are locally generated by the MobileCloud WebDriver. Now, let’s translate this to our work with the MobileCloud WebDriver. Each line in the code is translated into an HTTP command(s) level API, which are communicated to the MCM, then communicated to the HSS, and finally our devices. In short, scripts are saved within the Java machine, call the MCM who calls the HSS who calls the devices. *Represents a classic deployment type. Other deployment types may be slightly different.

4 What’s new? The Selenium WebDriver has been extended to mobile
The new extension is called MobileDriver This is the initial access point to the MobileCloud Automation The IMobileDriver interface provides methods for: Getting devices Uploading/downloading items to/from the media repository Downloading the execution report Use the quit method of the driver at the end of your test Part of the code template that’s created as part of the MobileCloud project Download the execution report after quitting the driver Java implementation Implement relevant Selenium interfaces Two new layers: MobileDriver – represents a MobileCloud MobileDevice – represents a single device

5 MobileCloud Class Relations Diagram
MobileCloud Web Driver Mobile Device Web Driver DOM Native Visual Keyboard Virtual Network Touch Screen Vitals Web Element Timers Reservation Here is a visual representation of the MobileCloud WebDriver extension, including its components and capabilities.

6 Eclipse Working environment MobileCloud for Eclipse plugin
Pre-requisites Eclipse Working environment MobileCloud for Eclipse plugin Before you begin, you will need to have Eclipse installed, and you will need to download the MobileCloud for Eclipse plugin. Prerequisite 1 – Eclipse The list of supported platforms is available and continuously updated here: Prerequisite 2 – MobileCloud for Eclipse plugin Instructions on how to download the plugin are available here: The plugin includes: Two project views (discussed in more details in the following slides) Project template Note: the plugin should be updated regularly according to Perfecto Mobile version updates. This is done just like any other plugins (From within Eclipse, go to Help > Check for updates)

7 MobileCloud Plugin and Credentials
Setup your MobileCloud credentials New project wizard Workspace settings There are two ways to set your MobileCloud details and credentials: Initially setup when creating your MobileCloud project. Go to File > New > Project > MobileCloud Project In your MobileCloud workspace settings. Go to Window > Preferences > MobileCloud Login. These are your default credentials, and can be overridden from within your script (more will be explained about this later on in this module).

8 MobileCloud for Eclipse Plugin Interface
How do I start? The MobileCloud project template - MobileTest Getting to know the MobileCloud project views Opening devices Record mode Perfecto Mobile commands - an extension of WebDriver

9 MobileCloud Project Template MobileTest
The MobileCloud project in Eclipse provides a template for writing the code, called MobileTest.java. Go to File > New > Project > MobileCloud Project This template includes a section where to include your code. The code can be developed manually, using code samples and the javadoc documentation, or it can be generated by using the record feature. Some parts can be recorded and others added or edited manually. [Trainer Notes: show how to open a new MobileCloud Project]

10 The MobileCloud Project views
The MobileCloud project contains two views: MobileCloud Recording used for: Opening devices Recording operations MobileCloud Dashboard used for: Viewing devices during execution [Images of the two different project views will be added to this slide, slightly different from the IDE] Note: The device does not have to be open explicitly in the MobileCloud Recording view in order to see it in the dashboard. [Trainer Notes: show the different views in Eclipse plugin]

11 Select Device 1 3 2 View devices while executing a script
To start developing your code, you will need to open a device. This will populate the common and full functions list (left panel of the Recording view). Click on Select Device icon Open a device Develop your code, using common and full list of functions Here are some advantages of working in this mode. Opening a device: View a device while writing a test Simulate device actions while being automated Record directly onto the test for quick use - Actions on the device can be recorded using the record icon located in the top bar. Once the record is activated, touch commands/keys and type text are recorded as script lines in the script canvas. This is the quickest way to write a test. Activity is recorded as hard-coded touch coordinates. Although this is inaccurate, it is useful when device touch coordinates are required. For example when using the drag command. View devices while executing a script Functions List: Common Functions – this tab enables the user to quickly include commonly used functions within a script Full List Functions – this tab includes all of the available functions, organized in categories. An enriched Function Reference is available in the Knowledge Base.

12 Writing a test on a MobileCloud device
Record mode Code refactoring For example: Rename (Alt + Shift + r) - rename every instance of a variable within your script Extract local variable (Alt Shift + l) Extract constant Now that your device is open, you can use the Record mode to easily write your test. This is a quick way to write your code with the correct syntax. Also, especially useful when just starting out and getting familiar with the MobileCloud WebDriver. However, it is important to re-factor the recorded code to improve its readability and maintainability. Search online for useful code refactoring techniques. How to record: To start recording, click on the 'start record' icon and perform the required operations. The recorded code will be inserted into your editor where your cursor is located. Click on the 'stop recording' icon when done. Note: While recording, every recorded phrase is independent, i.e. it doesn't depend on local variables or any code that appears earlier, except for the existence of the driver variable.

13 Writing a test on a MobileCloud device
Select Device according to device attributes, instead of a specific device ID Device may be busy or unavailable Easy to switch [Trainer Notes: Create a new MobileCloud project (File > New > Project > MobileCloud Project) Select a device (to populate the common functions and full function list tabs) Show the available functions Click record icon to start recording Show the Select device function and its input variable in the script variables window. Add it to the script (disable “Execute on add”). Explain that the system selects at runtime an available device that fits the criteria and runs script on that device. This allows unattended executions and removes dependency on device by hardware. Explain that the open device command that follows the select command takes the selected device and allocates it to script. Add it to the script (disable “Execute on add”). The close device at the end of script ensures the allocation terminates and the device is ready for the next user/script. Add it to the script(disable “Execute on add”). ] How to do it Select Device function finds a device according to criteria Accepts multiple criteria & regex Value provided to open device command that makes device available for script Close device at end of script

14 Sample Script Create a new MobileCloud Project in Eclipse
Add the sample java file to your project Available here: Open the dashboard to view the device Run the test Review the run results in report ("C:\test\report.pdf") and Eclipse console Now that we have some theoretical understanding of how the MobileCloud for Eclipse plugin interface works, let’s take a look at a sample test demonstrating some basic functions. The test will dynamically select a device according to device attributes, navigate to the PM demo webpage, enter data into two input fields (user and password), click submit, and validate the result. In future lessons, we will build on to this test, but for now, let’s look at the basics. [Trainer Notes: stop here and run the sample test. Important Note: Alternatively to adding the sample java file to your new project file, you can simply Copy/Paste the test code into the MobileTest.java that is automatically created and opened in Eclipse when you create a new project.]

15 Understanding the test
Select Device Open device Home Browser go to Edit set text X2 Button click Text checkpoint Close device Let’s review, in more detail, what the sample test does. In the following slides, we will learn the specific functions that were used. The following slides include code snippets from the sample script. Additional code snippets and details are available here, Before we get started on the code, let’s review the general test logic as a whole. Six simple and logical steps, in the forms of functions, that will: select and open a device, browse to a particular website, verify that it is on that website and then close the device. 1. Select Device – what devices do you want to run your test on? 2. Open device – if its on its Lock screen, this will open the device 3. Home - Sets the device to its Home Screen- if it is not on its HOME screen, this will put it there and now the device is now ready to take the next function 4. Browser Go To – this tells the device to open the browser and go to a specified website. 5. Text checkpoint - Verify that the device is on that website home page – this ensures the success of the test case 6. Close device – this ends the test case. [Trainer Notes: Sample script should still be open in Eclipse, and to demonstrate the functions in the script, open a device]

16 Defining the MobileDriver
Two ways to define a MobileDriver Use the host, username and password that were provided in the project preferences Use a different host, user and password Ensure the device is closed in the recording view when the latter constructor is used MobileDriver recap: The new MobileDriver extension is the initial access point to the MobileCloud Automation. Essentially, this is the fist starting point of your script. It is included as part of the MobileCloud project template (MobileTest.java). Alternatively, you can define a MobileDriver that uses a different host, user and password with the alternative constructor. Important Note: When using this constructor within the MobileCloud perspective in Eclipse, the device cannot be open in the MobileCloud recording view while the test runs; it can be viewed in the defined host MobileCloud device dashboard. // The default empty constructor of MobileDriver should be used when running the code inside Eclipse. // The user must have the ECLIPSE role in this case. // Otherwise, use the constructor that receives the host, user and password. E.g. // MobileDriver driver = new MobileDriver(host, user, password); MobileDriver driver = new MobileDriver();

17 Selecting devices for your test
Use IMobileDriver Method 1: Get a specific device, using the device ID Method 2: Find a device according to device attributes IMobileDevice device = driver.getDevice("72B4463CF6DC2AFEF876A6B6F0131BD91C82650C"); device.open(); // work on the device device.close(); [Trainer Notes: the blue man icon on Method 2 represents the Perfecto Mobile best practice.] Depending upon what point you are at, there are two methods for selecting a device. Developing Code mode: When writing a test with an open device, the easiest way to run the script is on that open device. SELECT DEVICE is not needed. This is a quick and handy way to validate the test while it is being developed. Simply specify the open device as the DUT (Device Under Test) in your Script Variables. Production mode: Once your test is ready for production, SELECT DEVICE makes it more powerful, by using 'any' device that meets your specifications, in the event that a particular device is not available at the precise time that it is required. This is essential for unattended testing. MobileDeviceFindOptions options = new MobileDeviceFindOptions(); options.setOS(MobileDeviceOS.IOS); IMobileDevice device = driver.findDevice(options);

18 Open device and Navigate Home
Navigates device to idle screen Unlocks device Recommended to always use before actual script begins We’ve selected our device and now we need to allocated it to our test by using the Open device function; and navigate to its Home screen. The Home function ensures a successful run of the test by setting the device to its home screen. If it is not on its HOME screen, this will set it there Now the device is ready to take the next function, regardless of which device you use and which screen it was last left at, this will reset it to its ready starting point. [Trainer Notes: In Eclipse, show the common functions tab and where the Home function is located the full list of functions tab and where the Open device function is located] //Open the selected device device.open(); //Navigate device to home screen device.home();

19 The functions in our test
Browser go to Opens a URL Browser parameter options OS – Chrome/Safari/Explorer Default – According to what is set on the device Perfecto Mobile – Legacy browser [Instructor: the blue man next to the OS method is the Perfecto Mobile best practice] Browser go to is one of the easier commands to use, telling the device to open its device specific browser and navigate to a specified website. [Trainer Notes: Open the Browser go to function Make sure record is disabled (the purpose is to demonstrate how we can interactively work with the device without recording the code in our test) Enable “Execute on ok” (in function window) Input the demo site url ( and click ok to execute on the device (without recording onto the script)] **First time only – Perfecto Mobile installs Chrome on the device – this is done for additional security (installed from scratch) First time use – pop up agreements have to be accepted: Do You Accept Google’s T&Cs: Pop-Ups: (Yes / Yes) Afterwards, it works Despite limitation, it’s still the recommended option //Browse to " with OS native browser IMobileWebDriver domDriver = device.getDOMDriver(MobileBrowserType.OS); domDriver.get("

20 The functions in our test
Open the Object spy Webpage.Element.Set Find the edit field element on the webpage using the name property Set the text Now that we have navigated to the demo webpage, we need to enter the login credentials (username and password) into the text field. To do this quickly, we can use the Object spy, and identify the username and password elements on the page. In this case using the name property of the object. Once the elements are identified, we can set the text to “John” and “Perfecto1”. [Trainer Notes: demo inserting this command using the Object spy in Eclipse Navigate to Open the object spy Filter to see only edit fields Click on the username object from the preview panel Click on the Webpage.Element.Set command Show that the element has property of name with value username Set the parameters: Text = John, Element identifier = username, By = HTML name ] Important Note: The Object spy and objects in general (for web and native applications) will be reviewed in following modules (Understanding XML and XPath, Leveraging Objects with XPath, and Advanced Objects). For now, we can focus on the logic of identifying the username and password elements on the page. *This also applies to the following slide dealing with Button click, using the button element on the page. //Login to web application - set username and password WebElement nameElement = domDriver.findElement(By.name("username")); nameElement.sendKeys("John"); WebElement passwordElement = domDriver.findElement(By.name("password")); passwordElement.sendKeys("Perfecto1");c

21 The functions in our test
Open the Object spy Webpage.Element.Click Find the button element on the webpage using the text property [Trainer Notes: Optionally demo the button click using the Object spy. The step-by-step is the same as the previous slide for setting the credentials. The important difference here, that can be highlighted, is that the element is identified using the Text property with value Sign in. We will cover the Object spy and working with objects later on in the course. So, there's no need to spend too much time here at this stage of the course. ] //click "Sign in" button WebElement textElement = domDriver.findElement(By.linkText("Sign in")); textElement.click();

22 The functions in our test
Text Checkpoint Validates text appears on screen Uses OCR to find text Timeout defines how long to wait for text to appear Now that we have completed some actions on the device, we should validation that we have successfully reached the Welcome page of the web application. In other words, what if proof is required that the script successfully made it to a specified website page? How can you prove this? A Checkpoint – a Text Checkpoint in this example – can be used. When the test run is complete, a text checkpoint will deliver a screenshot image of the actual value ‘seen’ on the screen at that moment in the script run. It verifies that the device is on that website page, ensuring the success of the test case. //Text checkpoint on "Welcome back John" IMobileWebDriver visualDriver = device.getVisualDriver(); visualDriver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS); visualDriver.findElement(By.linkText("Welcome back John"));

23 Validations and Validation Parameters
Sync and Check are Validations Timeout is a Validation Parameter Validations use the same mechanism as a click command, but do not perform any actions. They are useful for making tests pass/fail (example: did the login succeed?) Some useful validations are: -SYNC -CHECK A useful validation parameter is: -TIMEOUT SYNC and CHECK are the same command. SYNC has a default timeout of 60 seconds and CHECK does not. TIMEOUT is the duration in which the system will search continually for the 'needle'. TIMEOUT is a parameter within SYNC and CHECK One additional note about validations…they don’t perform any actions….they are verifications of a test’s success or failure only. There are two different types: - Sync and Check – virtually the same with one difference. (Sync has a default timeout of 60 seconds) Timeout works by continually searching for the needle. If the needle is not found, the system searches for it again and again, until the time set by the timeout expires. (For example, a timeout of zero means one attempt only. Higher values will provide multiple attempts. Note that the analysis takes a few seconds as it requires taking a fresh screenshot and then analyzing it, so the retries are NOT every millisecond.

24 How to work with functions
Javadoc documentation Question mark links to online function reference Each command has full documentation of all parameters and usage Advanced parameters Use only when needed Allow customizations There are two ways to reference the MobileCloud Functions: Javadoc documentation The QUESTION MARK icon can be found within every command window. It links to the functions reference. Each image below contains the "Sample" script commands. Each of the image "?" links to full documentation of all parameters and usage for that specific command. Advanced Parameter [Trainer Notes: Show an example of a command, such as OPEN Device – without Advanced Parameters enabled and then with Advanced Parameters enabled to show the difference]

25 Each run generates a MobileCloud report
Reports Each run generates a MobileCloud report Can be shared and downloaded Contain line by line breakdown of execution Some commands (e.g. checkpoint) automatically generate screenshots in report [Trainer Notes: View the report generated from the previous run of the sample script, in ("C:\\test\\report.pdf"), show the value of the selected device, and the screenshot. Show the video] Reports can be exported in many formats. They each provide a Pass/Fail summary, device list that the test case was run on including a test case video on each device, an execution sequence as well as a breakdown of each step in the sequence, when it started, how long it took, the expected needle it was looking for, as well as the actual value found.

26 downloadReport() method
Reports cont. downloadReport() method Used after calling driver.quit() ... MobileDriver driver = new MobileDriver(); try { // write your code here } catch (Exception e) { e.printStackTrace(); } finally { driver.quit(); downloadReport(driver); } private static void downloadReport(IMobileDriver driver) { InputStream reportStream = driver.downloadReport(MediaType.PDF); if (reportStream != null) { File reportFile = new File("C:\\test\\report.pdf"); FileUtils.write(reportStream, reportFile); So how do we generate, save and download the report? In the sample script, the downloadReport method demonstrates how to download the report to a specified location. It should be used after calling driver.quit(). Note: This method is not part of the MobileCloud project template, it is part of the sample script as a how to example.

27 Saving your Media files
Repository folders Public Group Private The MobileCloud Repository is a storage area for your media files. ( It is broken down into three areas: SHARED Repository – known as PUBLIC is accessible by all users in the MobileCloud…anything in this folder type can be seen and accessed by all. GROUP Repository – known as GROUP is a bit more restricted – files within this area are accessible only to users within that specified group PRIVATE repository – beginning with MY – is accessible only by that owner. Files cannot be seen by anyone except for that user. Note: In a shared MCM such as the public cloud, it is not possible to save into the public repository to preserve security. Customers on a shared MCM can use the group repository to securely share information within their organization. In dedicated MCM’s the public and group are basically identical, providing access to all the users of the organization. [Show the repository storage area]

28 Media files cont. downloadMedia() uploadMedia() deleteMedia()
String repositoryKey = "PRIVATE:myImage.jpg"; InputStream fileDownload = driver.downloadMedia(repositoryKey); if (fileDownload != null) { File file = new File("C:\\test\\images\\myImage.jpg"); FileUtils.write(fileDownload, file); } String repositoryKey = "PRIVATE:myImage.jpg"; File file = new File("C:\\test\\images\\myImage.jpg"); driver.uploadMedia(repositoryKey, file); The following examples demonstrate how to: download a media file from the MobileCloud repository upload a local file to the MobileCloud media repository (if the file already exists, it will be overwritten) delete a media file from the MobileCloud repository String repositoryKey = "PRIVATE:myImage.jpg"; driver.deleteMedia(repositoryKey);

29 References Setup guides http://help.perfectomobile.com/category/259
Video - Intro to MobileCloud WebDriver Setup and Configuration Troubleshooting Developing MobileCloud Automation Code Javadoc GitHub repository Additional references: MobileCloud Repository Eclipse documentation on installing new software

30 Thank You


Download ppt "Writing My First Test."

Similar presentations


Ads by Google