Mobile Automation Using Appium + Robot Framework | Part 2
Great job finishing the workspace setup! I know it was lengthy and cumbersome. But now that it is out of the way, we can focus on the exciting part: writing mobile automation scripts.
For web automation, you need to capture page elements first before you can write a script. The same is true for mobile automation. Appium for Windows has a built-in tool that will help you determine the element locators of the app fields.
Getting element locators using Appium for Windows
Follow these steps first. There are screenshots below.
- Launch Android Emulator
- Launch Appium
- Start Server
- Start Inspector Session
- Enter Desired Capabilities*
- Start Session
*For the desired capabilities, use the values below:
- platformName = Android
- deviceName = Android Emulator
- app = (download this apk. Place the APK file in a folder. Use the location of that folder as the value here)
- appPackage = com.loginmodule.learning
- appActivity = com.loginmodule.learning.activities.LoginActivity
For this example, we will interact with 3 elements: email, password, login. To get the element locator, just click on the element.
Appium will already provide you with an xpath that you can use. The provided xpath isn’t the best and shortest, but this will do for now.
Open notepad and type the following:
${email} xpath=[value from appium]
${password} xpath=[value from appium]
${login} xpath=[value from appium]
Remember to change the xpath value using the one you have gathered from Appium inspector.
Once you have completed capturing the xpaths, save the file as “All types” and put the extension as “.robot” Give it the name “objectmaps.”
Writing the Automation Script in Robot Framework
Now we’ll create a short and simple script that will…
- Enter values for Email and Password field
- Click the login button
Open the notepad and paste the text below and save it as a robot file. Give it the name “test.” Make sure to put the APK in the same folder as this file.
*** Settings ***
Library AppiumLibrary*** Variables ***
${APPIUM_SERVER} http://localhost:4723/wd/hub
${PLATFORM_NAME} Android
${DEVICE_NAME} Android Emulator
${APP} ${CURDIR}\\Sample Android App Login Test_v4.0_apkpure.com.apk
${APP_PACKAGE} com.loginmodule.learning
${APP_ACTIVITY} com.loginmodule.learning.activities.LoginActivity*** Test cases ***
Mobile Test
Launch Mobile Application
Login User test@test.com password@1234*** Keywords ***
Launch Mobile Application
Open Application ${APPIUM_SERVER}
... platformName=${PLATFORM_NAME}
... deviceName=${DEVICE_NAME}
... app=${APP}
... appPackage=${APP_PACKAGE}
... appActivity=${APP_ACTIVITY}Login User
[Arguments] ${i_email} ${i_pw}
Input Text ${Form.Login.EmailAddress.Txt} ${i_email}
Input Text ${Form.Login.Password.Txt} ${i_pw}
Click Element ${Form.Login.Login.Btn}
The “Launch Mobile Application” keyword simply creates an Appium session and calls the specified application. The “Login User” interacts with the elements in the app by entering values for email and password then clicking the login button.
If you are new to Robot Framework and having some difficulty understanding how the code is structured, it’s okay. You can do a deep dive later on. For this tutorial, I want to help you to be able to run and see for yourself a working mobile automation script. Because that is making progress.
Before we proceed, quick check. In a folder, you must have 3 files:
- objectmaps.robot
- test.robot
- Sample Android App Login Test_v4.0_apkpure.com.apk
If those are present in the same folder, we can now run it.
To run the automation script:
- Start Appium
- Launch Android emulator
- Open cmd and run this command:
cd path\to\your\folder
python -m robot.run --suite [folder name].test "path to your folder"
Test execution and reading reports
After execution, you should see report.html and log.html in your folder. Open the report.html file and you will see a simple report showing the status of the test execution, whether pass or fail, execution time, and more. Open log.html and you can drill down to see step by step, keyword per keyword logs.
There you go. You were able to write and execute a simple mobile automation script. Well done!
Now that you have a starting point, I encourage you to explore further and practice some more. If you are serious about learning mobile automation, I have an exercise for you. Try to extend the script and automate the registration part. You can do it.
But I still have your back in case you need help, the complete mobile automation demo code is the link below. 😉
GitHub repo -> cloud-vr/mobile-automation-demo (github.com)