The post Mobile Testing for the First Time with Android, Appium, and Applitools appeared first on Automated Visual Testing | Applitools.
]]>For some of us, it’s hard to believe how long smartphones have existed. I remember when the first iPhone came out in June 2007. I was working at my first internship at IBM, and I remember hearing in the breakroom that someone on our floor got one. Oooooooh! So special! That was 15 years ago!
In that decade and a half, mobile devices of all shapes and sizes have become indispensable parts of our modern lives: The first thing I do every morning when I wake up is check my phone. My dad likes to play Candy Crush on his tablet. My wife takes countless photos of our French bulldog puppy on her phone. Her mom uses her tablet for her virtual English classes. I’m sure, like us, you would feel lost if you had to go a day without your device.
It’s vital for mobile apps to have high quality. If they crash, freeze, or plain don’t work, then we can’t do the things we need to do. So, being the Automation Panda, I wanted to give mobile testing a try! I had three main goals:
This article covers my journey. Hopefully, it can help you get started with mobile testing, too! Let’s jump in.
The mobile domain is divided into two ecosystems: Android and iOS. That means any app that wants to run on both operating systems must essentially have two implementations. To keep things easier for me, I chose to start with Android because I already knew Java and I actually did a little bit of Android development a number of years ago.
I started by reading a blog series by Gaurav Singh on getting started with Appium. Gaurav’s articles showed me how to set up my workbench and automate a basic test:
Test Automation University also has a set of great mobile testing courses that are more than a quickstart guide:
Next, I needed an Android app to test. Thankfully, Applitools had the perfect app ready: Applifashion, a shoe store demo. The code is available on GitHub at https://github.com/dmitryvinn/applifashion-android-legacy.
To do Android development, you need lots of tools:
I followed Gaurav’s guide to a T for setting these up. I also had to set the ANDROID_HOME
environment variable to the SDK path.
Be warned: it might take a long time to download and install these tools. It took me a few hours and occupied about 13 GB of space!
Once my workbench was ready, I opened the Applifashion code in Android Studio, created a Pixel 3a emulator in Device Manager, and ran the app. Here’s what it looked like:
I chose to use an emulator instead of a real device because, well, I don’t own a physical Android phone! Plus, managing a lab full of devices can be a huge hassle. Phone manufacturers release new models all the time, and phones aren’t cheap. If you’re working with a team, you need to swap devices back and forth, keep them protected from theft, and be careful not to break them. As long as your machine is powerful and has enough storage space, you can emulate multiple devices.
It was awesome to see the Applifashion app running through Android Studio. I played around with scrolling and tapping different shoes to open their product pages. However, I really wanted to do some automated testing. I chose to use Appium for automation because its API is very similar to Selenium WebDriver, with which I am very familiar.
Appium adds on its own layer of tools:
Again, I followed Gaurav’s guide for full setup. Even though Appium has bindings for several popular programming languages, it still needs a server for relaying requests between the client (e.g., the test automation) and the app under test. I chose to install the Appium server via the NPM module, and I installed version 1.22.3. Appium Doctor gave me a little bit of trouble, but I was able to resolve all but one of the issues it raised, and the one remaining failure regarding ANDROID_HOME
turned out to be not a problem for running tests.
Before jumping into automation code, I wanted to make sure that Appium was working properly. So, I built the Applifashion app into an Android package (.apk file) through Android Studio by doing Build → Build Bundle(s) / APK(s) → Build APK(s). Then, I configured Appium Inspector to run this .apk file on my Pixel 3a emulator. My settings looked like this:
Here were a few things to note:
/wd/hub
.appium: automationName
had to be uiautomator2
– it could not be an arbitrary name.I won’t lie – I needed a few tries to get all my capabilities right. But once I did, things worked! The app appeared in my emulator, and Appium Inspector mirrored the page from the emulator with the app source. I could click on elements within the inspector to see all their attributes. In this sense, Appium Inspector reminded me of my workflow for finding elements on a web page using Chrome DevTools. Here’s what it looked like:
So far in my journey, I had done lots of setup, but I hadn’t yet automated any tests! Mobile testing certainly required a heftier stack than web app testing, but when I looked at Gaurav’s example test project, I realized that the core concepts were consistent.
I set up my own Java project with JUnit, Gradle, and Appium:
My example code is hosted here: https://github.com/AutomationPanda/applitools-appium-android-webinar.
Warning: The example code I share below won’t perfectly match what’s in the repository. Furthermore, the example code below will omit import statements for brevity. Nevertheless, the code in the repository should be a full, correct, executable example.
My build.gradle file looked like this with the required dependencies:
plugins {
id 'java'
}
group 'com.automationpanda'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
testImplementation 'io.appium:java-client:8.1.1'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2'
testImplementation 'org.seleniumhq.selenium:selenium-java:4.2.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
test {
useJUnitPlatform()
}
My test case class was located at /src/test/java/com/automationpanda/ApplifashionTest.java
. Inside the class, I had two instance variables: the Appium driver for mobile interactions, and a WebDriver waiting object for synchronization:
public class ApplifashionTest {
private AppiumDriver driver;
private WebDriverWait wait;
// …
}
I added a setup method to initialize the Appium driver. Basically, I copied all the capabilities from Appium Inspector:
@BeforeEach
public void setUpAppium(TestInfo testInfo) throws IOException {
// Create Appium capabilities
// Hard-coding these values is typically not a recommended practice
// Instead, they should be read from a resource file (like a properties or JSON file)
// They are set here like this to make this example code simpler
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "android");
capabilities.setCapability("appium:automationName", "uiautomator2");
capabilities.setCapability("appium:platformVersion", "12");
capabilities.setCapability("appium:deviceName", "Pixel 3a API 31");
capabilities.setCapability("appium:app", "/Users/automationpanda/Desktop/Applifashion/main-app-debug.apk");
capabilities.setCapability("appium:appPackage", "com.applitools.applifashion.main");
capabilities.setCapability("appium:appActivity", "com.applitools.applifashion.main.activities.MainActivity");
capabilities.setCapability("appium:fullReset", "true");
// Initialize the Appium driver
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
wait = new WebDriverWait(driver, Duration.ofSeconds(30));
}
I also added a cleanup method to quit the Appium driver after each test:
@AfterEach
public void quitDriver() {
driver.quit();
}
I wrote one test case that performs shoe shopping. It loads the main page and then opens a product page using locators I found with Appium Inspector:
@Test
public void shopForShoes() {
// Tap the first shoe
final By shoeMainImageLocator = By.id("com.applitools.applifashion.main:id/shoe_image");
wait.until(ExpectedConditions.presenceOfElementLocated(shoeMainImageLocator));
driver.findElement(shoeMainImageLocator).click();
// Wait for the product page to appear
final By shoeProductImageLocator = By.id("com.applitools.applifashion.main:id/shoe_image_product_page");
wait.until(ExpectedConditions.presenceOfElementLocated(shoeProductImageLocator));
}
At this stage, I hadn’t written any assertions yet. I just wanted to see if my test could successfully interact with the app. Indeed, it could, and the test passed when I ran it! As the test ran, I could watch it interact with the app in the emulator.
My next step was to write assertions. I could have picked out elements on each page to check, but there were a lot of shoes and words on those pages. I could’ve spent a whole afternoon poking around for locators through the Appium Inspector and then tweaking my automation code until things ran smoothly. Even then, my assertions wouldn’t capture things like layout, colors, or positioning.
I wanted to use visual assertions to verify app correctness. I could use the Applitools SDK for Appium in Java to take one-line visual snapshots at the end of each test method. However, I wanted more: I wanted to test multiple devices, not just my Pixel 3a emulator. There are countless Android device models on the market, and each has unique aspects like screen size. I wanted to make sure my app would look visually perfect everywhere.
In the past, I would need to set up each target device myself, either as an emulator or as a physical device. I’d also need to run my test suite in full against each target device. Now, I can use Applitools Native Mobile Grid (NMG) instead. NMG works just like Applitools Ultrafast Grid (UFG), except that instead of browsers, it provides emulated Android and iOS devices for visual checkpoints. It’s a great way to scale mobile test execution. In my Java code, I can set up Applitools Eyes to upload results to NMG and run checkpoints against any Android devices I want. I don’t need to set up a bunch of devices locally, and the visual checkpoints will run much faster than any local Appium reruns. Win-win!
To get started, I needed my Applitools account. If you don’t have one, you can register one for free.
Then, I added the Applitools Eyes SDK for Appium to my Gradle dependencies:
testImplementation 'com.applitools:eyes-appium-java5:5.12.0'
I added a “before all” setup method to ApplifashionTest
to set up the Applitools configuration for NMG. I put this in a “before all” method instead of a “before each” method because the same configuration applies for all tests in this suite:
private static InputReader inputReader;
private static Configuration config;
private static VisualGridRunner runner;
@BeforeAll
public static void setUpAllTests() {
// Create the runner for the Ultrafast Grid
// Warning: If you have a free account, then concurrency will be limited to 1
runner = new VisualGridRunner(new RunnerOptions().testConcurrency(5));
// Create a configuration for Applitools Eyes
config = new Configuration();
// Set the Applitools API key so test results are uploaded to your account
config.setApiKey("<insert-your-API-key-here>");
// Create a new batch
config.setBatch(new BatchInfo("Applifashion in the NMG"));
// Add mobile devices to test in the Native Mobile Grid
config.addMobileDevices(
new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S21),
new AndroidDeviceInfo(AndroidDeviceName.Galaxy_Note_10),
new AndroidDeviceInfo(AndroidDeviceName.Pixel_4));
}
The configuration for NMG was almost identical to a configuration for UFG. I created a runner, and I created a config object with my Applitools API key, a batch name, and all the devices I wanted to target. Here, I chose three different phones: Galaxy S21, Galaxy Note 10, and Pixel 4. Currently, NMG supports 18 different Android devices, and support for more is coming soon.
At the bottom of the “before each” method, I added code to set up the Applitools Eyes object for capturing snapshots:
private Eyes eyes;
@BeforeEach
public void setUpAppium(TestInfo testInfo) throws IOException {
// …
// Initialize Applitools Eyes
eyes = new Eyes(runner);
eyes.setConfiguration(config);
eyes.setIsDisabled(false);
eyes.setForceFullPageScreenshot(true);
// Open Eyes to start visual testing
eyes.open(driver, "Applifashion Mobile App", testInfo.getDisplayName());
}
Likewise, in the “after each” cleanup method, I added code to “close eyes,” indicating the end of a test for Applitools:
@AfterEach
public void quitDriver() {
// …
// Close Eyes to tell the server it should display the results
eyes.closeAsync();
}
Finally, I added code to each test method to capture snapshots using the Eyes object. Each snapshot is a one-line call that captures the full screen:
@Test
public void shopForShoes() {
// Take a visual snapshot
eyes.check("Main Page", Target.window().fully());
// Tap the first shoe
final By shoeMainImageLocator = By.id("com.applitools.applifashion.main:id/shoe_image");
wait.until(ExpectedConditions.presenceOfElementLocated(shoeMainImageLocator));
driver.findElement(shoeMainImageLocator).click();
// Wait for the product page to appear
final By shoeProductImageLocator = By.id("com.applitools.applifashion.main:id/shoe_image_product_page");
wait.until(ExpectedConditions.presenceOfElementLocated(shoeProductImageLocator));
// Take a visual snapshot
eyes.check("Product Page", Target.window().fully());
}
When I ran the test with these visual assertions, it ran one time locally, and then NMG ran each snapshot against the three target devices I specified. Here’s a look from the Applitools Eyes dashboard at some of the snapshots it captured:
The results are marked “New” because these are the first “baseline” snapshots. All future checkpoints will be compared to these images.
Another cool thing about these snapshots is that they capture the full page. For example, the main page will probably display only 2-3 rows of shoes within its viewport on a device. However, Applitools Eyes effectively scrolls down over the whole page and stitches together the full content as if it were one long image. That way, visual snapshots capture everything on the page – even what the user can’t immediately see!
Capturing baseline images is only the first step with visual testing. Tests should be run regularly, if not continuously, to catch problems as soon as they happen. Visual checkpoints should point out any differences to the tester, and the tester should judge if the change is good or bad.
I wanted to try this change detection with NMG, so I reran tests against a slightly broken “dev” version of the Applifashion app. Can you spot the bug?
The formatting for the product page was too narrow! “Traditional” assertions would probably miss this type of bug because all the content is still on the page, but visual assertions caught it right away. Visual checkpoints worked the same on NMG as they would on UFG or even with the classic (e.g. local machine) Applitools runner.
When I switched back to the “main” version of the app, the tests passed again because the visuals were “fixed:”
While running all these tests, I noticed that mobile test execution is pretty slow. The one test running on my laptop took about 45 seconds to complete. It needed time to load the app in the emulator, make its interactions, take the snapshots, and close everything down. However, I also noticed that the visual assertions in NMG were relatively fast compared to my local runs. Rendering six snapshots took about 30 seconds to complete – three times the coverage in significantly less time. If I had run tests against more devices in parallel, I could probably have seen an even greater coverage-to-time ratio.
My first foray into mobile testing was quite a journey. It required much more tooling than web UI testing, and setup was trickier. Overall, I’d say testing mobile is indeed more difficult than testing web. Thankfully, the principles of good test automation were the same, so I could still develop decent tests. If I were to add more tests, I’d create a class for reading capabilities as inputs from environment variables or resource files, and I’d create another class to handle Applitools setup.
Visual testing with Applitools Native Mobile Grid also made test development much easier. Setting everything up just to start testing was enough of a chore. Coding the test cases felt straightforward because I could focus my mental energy on interactions and take simple snapshots for verifications. Trying to decide all the elements I’d want to check on a page and then fumbling around the Appium Inspector to figure out decent locators would multiply my coding time. NMG also enabled me to run my tests across multiple different devices at the same time without needing to pay hundreds of dollars per device or sucking up a few gigs of storage and memory on my laptop. I’m excited to see NMG grow with support for more devices and more mobile development frameworks in the future.
Despite the prevalence of mobile devices in everyday life, mobile testing still feels far less mature as a practice than web testing. Anecdotally, it seems that there are fewer tools and frameworks for mobile testing, fewer tutorials and guides for learning, and fewer platforms that support mobile environments well. Perhaps this is because mobile test automation is an order of magnitude more difficult and therefore more folks shy away from it. There’s no reason for it to be left behind anymore. Given how much we all rely on mobile apps, the risks of failure are just too great. Technologies like Visual AI and Applitools Native Mobile Grid make it easier for folks like me to embrace mobile testing.
The post Mobile Testing for the First Time with Android, Appium, and Applitools appeared first on Automated Visual Testing | Applitools.
]]>The post Modern Cross Device Testing for Android & iOS Apps appeared first on Automated Visual Testing | Applitools.
]]>Learn the cross device testing practices you need to implement to get closer to Continuous Delivery for native mobile apps.
Modern cross device testing is the system by which you verify that an application delivers the desired results on a wide variety of devices and formats. Ideally this testing will be done quickly and continuously.
There are many articles explaining how to do CI/CD for web applications, and many companies are already doing it successfully, but there is not much information available out there about how to achieve the same for native mobile apps.
This post will shed light on the cross device testing practices you need to implement to get a step closer to Continuous Delivery for native mobile apps.
The number of mobile devices used globally is staggering. Based on the data from bankmycell.com, we have 6.64 billion smartphones in use.
Even if we are building and testing an app which impacts a fraction of this number, that is still a very huge number.
The below chart shows the market share by some leading smartphone vendors over the years.
One of the biggest challenges for testing mobile apps is that across all manufacturers combined, there are 1000s of device types in use today. Depending on the popularity of your app, this means there are a huge number of devices your users could be using.
These devices will have variations based on:
It is clear that you cannot run our tests on each type of device that may be used by your users.
So how do you get quick feedback and confidence from your testing that (almost) no user will get impacted negatively when you release a new version of your app?
Before we think about the strategy for running your automated tests for mobile apps, we need to have a good and holistic mobile testing strategy.
Along with testing the app functionality, mobile testing has additional dimensions, and hence complexities as compared with web-app testing.
You need to understand the impact of the aspects mentioned above and see what may, or may not be applicable to you.
Here are some high-level aspects to consider in your mobile testing strategy:
Once you have figured out your Mobile Testing Strategy, you now need to think about how and what type of automated tests can give you good, reliable, deterministic and fast feedback about the quality of your apps. This will result in you identifying the different layers of your test automation pyramid.
Remember: It is very important to execute all types of automated tests, on every code change and new app that is built. The functional / end-2-end / UI tests for your app should also be run at this time.
Additionally, you need to be able to run the tests on a local developer / qa machine, as well in your Continuous Integration (CI) system. In case of native / hybrid mobile apps, developers and QAs should be able to install the app on the (local) devices they have available with them, and run the tests against that. For CI-based execution, you need to have some form of device farm available locally in your network, or cloud-based to allow execution of the tests.
This continuous testing approach will provide you with quick feedback and allow you to fix issues almost as soon as they creep in the app.
Testing and automating mobile apps have additional complexities. You need to install the app in some device before your automated tests can be run against it.
Let’s explore your options for devices.
Real devices are ideal to run the tests. Your users / customers are going to use your app using a variety of real devices.
In order to allow proper development and testing to be done, each team member needs access to relevant types of devices (which is subject to their user-base).
However, it is not as easy to have a variety of devices available for running the automated tests, for each team member (developer / tester).
The challenges of having the real devices could be related to:
Hence we need a different strategy for executing tests on mobile devices. Emulators and Simulators come to the rescue!
Before we get into specifics about the execution strategy, it is good to understand the differences between an emulator and simulator.
Android-device emulators and iOS-device simulators make it easy for any team member to easily spin up a device.
An emulator is hardware or software that enables one computer system (called the host) to behave like another computer system (called the guest). An emulator typically enables the host system to run software or use peripheral devices designed for the guest system
An emulator can mimic the operating system, software and the hardware features of the android device.
A Simulator runs on your Mac and behaves like a standard Mac app while simulating iPhone, iPad, Apple Watch, or Apple TV environments. Each combination of a simulated device and software version is considered its own simulation environment, independent of the others, with its own settings and files. These settings and files exist on every device you test within a simulation environment.
An iOS simulator mimics the internal behavior of the device. It cannot mimic the OS / hardware features of the device.
Emulators / Simulators are a great and cost-effective way to overcome the challenges of real devices. These can easily be created as per the requirements and needs by any team member and can be used for testing and also running automated tests. You can also relatively easily set up and use the emulators / simulators in your CI execution environment.
While emulators / simulators may seem like they will solve all the problems, that is not the case. Like with anything, you need to do a proper evaluation and figure out when to use real devices versus emulators / simulators.
Below are some guidelines that I refer to.
The above approach of using real-devices or emulators / simulators will help your team to shift-left and achieve continuous testing.
There is one challenge that still remains – scaling! How do you ensure your tests run correctly on all supported devices?
A classic, or rather, a traditional way to solve this problem is to repeat the automated test execution on a carefully chosen variety of devices. This would mean, if you have 5 important types of devices, and you have a 100 tests automated, then you are essentially running 500 tests.
This approach has multiple disadvantages:
We all know these disadvantages, however, there is no better way to overcome this. Or, is there?
The Applitools Native Mobile Grid for Android and iOS apps can easily help you to overcome the disadvantages of traditional cross-device testing.
It does this by running your test on 1 device, but getting the execution results from all the devices of your choice, automatically. Well, almost automatically. This is how the Applitools Native Mobile Grid works:
Below is an example of how to specify Android devices for Applitools Native Mobile Grid:
Configuration config = eyes.getConfiguration(); //Configure the 15 devices we want to validate asynchronously
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S9, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S9_Plus, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S8, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S8_Plus, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Pixel_4, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_Note_8, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_Note_9, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_Note_10, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_Note_10_Plus, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S10_Plus, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S20, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S20_PLUS, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S21, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S21_PLUS, ScreenOrientation.PORTRAIT));
config.addMobileDevice(new AndroidDeviceInfo(AndroidDeviceName.Galaxy_S21_ULTRA, ScreenOrientation.PORTRAIT));
eyes.setConfiguration(config);
Below is an example of how to specify iOS devices for Applitools Native Mobile Grid:
Configuration config = eyes.getConfiguration(); //Configure the 15 devices we want to validate asynchronously
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_11));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_11_Pro));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_11_Pro_Max));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_12));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_12_Pro));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_12_Pro_Max));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_12_mini));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_13_Pro));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_13_Pro_Max));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_XS));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_X));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_XR));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_11));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_8));
config.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_7));
eyes.setConfiguration(config);
Every call to Applitools to do a visual validation will automatically do the functional and visual validation for each device specified in the configuration above.
The Applitools Native Mobile Grid has many advantages.
Read this post on How to Scale Mobile Automation Testing Effectively for more specific details of this amazing solution!
Using the Applitools Visual AI allows you to extend coverage at the top of your Test Automation Pyramid by including AI-based visual testing along with your UI/UX testing.
Using the Applitools Native Mobile Grid for cross device testing of Android and iOS apps makes your CI loop faster by providing seamless scaling across all supported devices as part of the same test execution cycle.
You can watch my video on Mobile Testing 360deg (https://applitools.com/event/mobile-testing-360deg/) where I share many examples and details related to the above to include as part of your mobile testing strategy.
To start using the Native Mobile Grid, simply sign up at the link below to request access. You can read more about the Applitools Native Mobile Grid in our blog post or on our website.
Happy testing!
The post Modern Cross Device Testing for Android & iOS Apps appeared first on Automated Visual Testing | Applitools.
]]>The post How to Scale Mobile Automation Testing Effectively appeared first on Automated Visual Testing | Applitools.
]]>Learn the best way to scale mobile automation testing today. We’ll look at a history of traditional mobile automation testing strategies and discuss why each one is and isn’t effective, and then see why a new tool, the Native Mobile Grid, is different.
In today’s mobile testing world there are many different approaches to scaling our test automation for native mobile applications. Your options range from running locally with virtual devices (simulators/emulators) or real devices, to a local mobile grid/lab, to docker containers / virtual machines, to remote cloud test services.
As you’re probably aware, testing native mobile applications can sometimes be a difficult endeavor. There are a lot of moving parts and many points of failure involved. To successfully execute, everything needs to work in complete harmony.
For example, just executing a single Appium test you need:
Now let’s say you want to scale your tests across multiple devices for your cross-device validation needs. We’re now introducing more points of failure for each device that is tested. A test on one device may execute just fine but on another, it may fail for various unknown reasons. We then often have to spend a vast amount of time investigating and debugging these failures to find the root cause.
Let’s also consider that because we’re adding more devices we will likely need to add more conditional logic to our test code to accommodate these different devices. We may need to add conditionals for different device resolutions/screen sizes, OS versions, orientations, scroll views, locators/selectors, or maybe gestures for specific devices. This all adds more coded logic to our test suite or framework we need to maintain and thus refactor in the future when our application changes.
Because of these reasons and/or perhaps others, many people often don’t scale their mobile test coverage across different devices. Either it might introduce more test maintenance, more test flakiness, a longer test execution time, or access to different devices is not possible. Crossing fingers and hoping for the best has been the approach for many…
Now let’s cover some common mobile test scaling approaches. I’ll go over the benefits and drawbacks of each of these approaches and finally introduce you to a new modern approach and technology, the Applitools Native Mobile Grid.
The simplest but also the most inefficient and slowest approach. The example diagram below shows executing two tests (Test A & Test B) executing across three different mobile devices.
As stated before, this is the simplest approach but very inefficient and slow. Perhaps some people aren’t aware of different or better approaches. Or maybe some bad test practices are being used such as one test (Test B) is dependent on (Test A) to complete in order to proceed. Generally, a good test practice is to never have any tests dependent on another to execute. Test suites/frameworks should be architected in a way to run any test in any order by utilizing test hooks/annotations, database seeding, mocking, or API test data injection to set up each test independently.
For example, say I have an app that has a shopping cart and I have a test “Add items to shopping cart” which failed due to a bug in the app. If I relied on that test to then run my “shopping cart” specific tests, I couldn’t. That is where good test architecture comes into play by setting up each test independently by some of the methods mentioned above or other means.
We live in a fast-paced CI/CD world these days, and feedback loops that are as fast as possible are crucial. So what can you do to help get this test feedback ASAP? Parallelization!
In this scenario, we are parallelizing the devices needed for cross-device validation. It’s however still inefficient since the tests themselves are running sequentially. But still much better than the former sequential approach.
This approach to mobile automation testing is a bit more efficient as it’s parallelizing the tests in your test suite. Ultimately it should reduce the execution time and also promote good test practices keeping tests independent from one another to execute. However, the inefficiency now is that each device is sequentially tested.
This is the most efficient and fastest traditional approach as it parallelizes both your test suite and devices for cross-device validations. However, it comes at a cost of machine resources, cloud concurrency or minutes usage on license limits (if applicable), and added complexity in your test framework having to manage different sets of parallelization. Some frameworks have this architecture “baked” into them but for many others, it’s up to the developer/tester (or whomever) to implement this logic themselves.
With that said, all of the approaches above come with but are not limited to some of the challenges below:
Now that we’ve talked about the typical traditional approaches to mobile automation testing, let’s talk about the next generation of mobile cross-device testing using the Applitiools Native Mobile Grid! Our Native Mobile Grid (NMG) uses a new technological approach to asynchronously validate your native mobile application in parallel and easily across many different devices in a single execution.
What this means is the parallelization of devices is handled on the Applitools NMG. Since it’s asynchronous you are not waiting on the device to connect or the test results, which frees your tests to execute as fast as possible!
Some key benefits:
Now let’s look at some example execution architectures using the Applitools Native Mobile Grid!
This next diagram is similar to the traditional Parallel Device Execution we talked about above. However, the parallelization is offloaded to the NMG to handle and no additional logic for parallel threads or processes is required. This approach is still not the most efficient since each test is running sequentially but it’s lightyears faster than the traditional approach.
Out of all the approaches for mobile test automation we’ve discussed thus far, this next example is by far the most superior, efficient, and fastest approach possible to scaling your native mobile test coverage! The added benefit of this approach is your entire test suite execution time will now only take as long as it takes to run your slowest test in your suite!
This example is similar to the traditional Parallel Test & Parallel Device Execution above but the device parallelization is now offloaded to the NMG and handled asynchronously.
The below code is an example using Appium Java, testing an iOS native application. For those of you familiar with the Applitools Ultrafast Test Cloud for desktop and mobile web applications, this should look very similar. All we need to do in the test code is define the devices we want to validate for our cross-device coverage needs. In this case, we’ve specified 15 iOS devices we’ll validate in the same execution time as it takes to run the tests on just one device! Additional settings such as setting the OS version and orientation per device can be specified (not shown).
public class iOSNativeUFGTest {
private WebDriverWait wait;
private IOSDriver driver;
private VisualGridRunner runner;
private Eyes eyes;
private static IOSDriver startApp() throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("automationName", "XCUITest");
capabilities.setCapability("deviceName", "iPhone 12 Pro Max"); //Launch a single local simulator or real device
capabilities.setCapability("platformVersion", "15.4");
capabilities.setCapability("app", "/Users/justin/Desktop/MyApp.app");
return new IOSDriver<>(new URL("http://localhost:4723/wd/hub"), capabilities);
}
@Before
public void setup() throws Exception {
runner = new VisualGridRunner(new RunnerOptions().testConcurrency(15));
eyes = new Eyes(runner);
eyes.setApiKey(System.getenv("APPLITOOLS_API_KEY"));;
Configuration conf = eyes.getConfiguration(); //Configure the 15 devices we want to validate asynchronously
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_11));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_11_Pro));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_11_Pro_Max));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_12));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_12_Pro));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_12_Pro_Max));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_12_mini));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_13_Pro));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_13_Pro_Max));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_XS));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_X));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_XR));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_11));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_8));
conf.addMobileDevice(new IosDeviceInfo(IosDeviceName.iPhone_7));
eyes.setConfiguration(conf);
driver = startApp();
}
@Test
public void iOSNativeTest() throws Exception {
eyes.open(driver, "My Native App", "Login View"); //Start a visual test
eyes.check("Login", Target.window().fully(true)); //Capture the mobile UI view
eyes.closeAsync(); //End the visual test
}
@After
public void tearDownTest(){
/The /results object contains validation results on all 15 devices which can then be asserted for visual and accessibility checks
TestResultsSummary results = runner.getAllTestResults(false);
eyes.abortAsync();
driver.quit();
}
}
Now for the first time ever, native mobile developers can perform continuous testing, running their entire test suites on every pull request or code push across many different devices to get immediate quality feedback at once. Just like web application developers have for many years now!
Hopefully, this article illustrated the benefits and superiority of the Applitools Native Mobile Grid to other traditional approaches for mobile automation testing. As you now can see how simple and efficient it is to expand your cross-device coverage with the Native Mobile Grid there are no more excuses not to. So what’s stopping you?!
How do you get started with the Native Mobile Grid you may ask? To start using the Native Mobile Grid, simply sign up at the link below to request access. You can read more about the Applitools Native Mobile Grid in our introductory blog post or on our website.
Happy testing!
The post How to Scale Mobile Automation Testing Effectively appeared first on Automated Visual Testing | Applitools.
]]>The post Writing Your First Appium Test For iOS Devices appeared first on Automated Visual Testing | Applitools.
]]>This is the third and final post in our Hello World introduction series to Appium, and we’ll discuss how to create your first Appium test for iOS. You can read the first post for an introduction to Appium, or the second to learn how to create your first Appium test for Android.
Congratulations on having made it so far. I hope you are slowly becoming more comfortable with Appium and realizing just how powerful a tool it really is for mobile automation, and that it’s not that difficult to get started with it.
This is the final post in this short series on helping you start with Appium and write your first tests. If you need a refresher on what Appium is and writing your first Android test with it, you can read the earlier parts here:
In this post, we’ll learn how to set up your dev environment and write your first Appium based iOS test.
We’ll need some dependencies to be preinstalled on your dev machine.
Let’s go over them one by one.
Also, remember it’s completely okay if you don’t understand all the details of these in the beginning since Appium pretty much abstracts those details away and you can always dig deeper later on if you need some very specific capabilities of these libraries.
To run iOS tests, we need a machine running macOS with Xcode installed.
The below command would setup Command-line scripts that are needed for us to be able to run our first test:
xcode-select --install
You can think of Carthage as a tool to allow adding frameworks to your Cocoa applications and to build required dependencies:
brew install carthage
libimobiledevice library allows Appium to talk to iOS devices using native protocols:
brew install libimobiledevice
ios-deploy helps to install and debug iOS apps from the command line:
brew install ios-deploy
brew install ios-webkit-debug-proxy
IDB (iOS Device bridge) is a node JS wrapper over IDB that are a set of utilities made by Facebook:
brew tap facebook/fb
brew install idb-companion
pip3.6 install fb-idb
If you are curious, you could read the below reference blogs below that helped me come up with this shortlist of dependencies and are good reads for more context:
For our first iOS test, we’ll use a sample demo app provided by Appium.
You can download the zip file from here, unzip it and ensure you copy it under src/test/resources dir in the project, such that we have a TestApp.app file under the test resources folder.
If you are following these tests along by checking out the GitHub repo appium-fast-boilerplate, you’ll see the iOS app path is mentioned under a file ios-caps.json under src/main/resources/.
This file represents Appium capabilities in JSON format and you can change them based on which iOS device you want to run them on.
When we run the test DriverManager will pick these up and help create the Appium session. You can read part 2 of this blog series to know more about this flow.
{
"platformName": "iOS",
"automationName": "XCUITest",
"deviceName": "iPhone 13",
"app": "src/test/resources/TestApp.app"
}
Our app has a set of UI controls with one section representing a calculator wherein we could enter two numbers and get their sum (see below snapshot):
We would automate the below flow:
Pretty basic right?
Below is how a sample test would look like (see the code here):
import constants.TestGroups;
import org.testng.Assert;
import org.testng.annotations.Test;
import pages.testapp.home.HomePage;
public class IOSTest extends BaseTest {
@Test(groups = {TestGroups.IOS})
public void addNumbers() {
String actualSum = new HomePage(this.driver)
.enterTwoNumbersAndCompute("5", "5")
.getSum();
Assert.assertEquals(actualSum, "10");
}
}
Here, we follow the same good patterns that have served us well (like using Fluent, page objects, a base test, and driver manager) in our tests just as we did in our Android test.
You can read about these in detail in this earlier blog.
The beauty of the page object pattern is that it looks very similar regardless of the platform.
Below is the complete page object for the above test that implements the desired behavior for this test.
package pages.testapp.home;
import core.page.BasePage;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
public class HomePage extends BasePage {
private final By firstNumber = By.name("IntegerA");
private final By secondNumber = By.name("IntegerB");
private final By computeSumButton = By.name("ComputeSumButton");
private final By answer = By.name("Answer");
public HomePage(AppiumDriver driver) {
super(driver);
}
public HomePage enterTwoNumbersAndCompute(String first, String second) {
typeFirstNumber(first);
typeSecondNumber(second);
compute();
return this;
}
public HomePage typeFirstNumber(String number) {
WebElement firstNoElement = getElement(firstNumber);
type(firstNoElement, number);
return this;
}
public HomePage typeSecondNumber(String number) {
WebElement secondNoElement = getElement(secondNumber);
type(secondNoElement, number);
return this;
}
public HomePage compute() {
WebElement computeBtn = getElement(computeSumButton);
click(computeBtn);
return this;
}
public String getSum() {
waitForElementToBePresent(answer);
return getText(getElement(answer));
}
}
Let’s unpack this and understand its components.
We create a HomePage class that inherits from BasePage that has wrappers over Appium API methods.
public class HomePage extends BasePage
We define our selectors of type By, using the Appium inspector to discover that name is the unique selector for these elements, in your projects trying to depend on ID is probably a safer bet.
private final By firstNumber = By.name("IntegerA");
private final By secondNumber = By.name("IntegerB");
private final By computeSumButton = By.name("ComputeSumButton");
private final By answer = By.name("Answer");
Next, we initialize this class with a driver instance that’s passed the test and also its parent class to ensure we have the appropriate driver instance set:
public HomePage(AppiumDriver driver) {
super(driver);
}
We then create a wrapper function that takes two numbers as strings, types numbers in the two text boxes, and taps on the button.
public HomePage enterTwoNumbersAndCompute(String first, String second) {
typeFirstNumber(first);
typeSecondNumber(second);
compute();
return this;
}
We implement these methods by reusing methods from BasePage while ensuring the correct page object is returned.
Since there is no redirection happening in these tests and it’s a single screen we just return this (i.e. the current page object in Java syntax). This enables writing tests in the Fluent style that you saw earlier.
public HomePage typeFirstNumber(String number) {
WebElement firstNoElement = getElement(firstNumber);
type(firstNoElement, number);
return this;
}
public HomePage typeSecondNumber(String number) {
WebElement secondNoElement = getElement(secondNumber);
type(secondNoElement, number);
return this;
}
public HomePage compute() {
WebElement computeBtn = getElement(computeSumButton);
click(computeBtn);
return this;
}
Finally, we return the string that has the sum of two numbers in the getSum() method and let the test perform desired assertions:
public String getSum() {
waitForElementToBePresent(answer);
return getText(getElement(answer));
}
Before running the test, ensure that the Appium server is running in another terminal and that your appium 2.0 server has the XCUITest driver installed by following the below steps
# Ensure driver is installed
appium driver install xcuitest
# Start the appium server before running your test
appium
Within the project, you could run the test using the below command or use IntelliJ or equivalent editors test runner to run the desired test.
gradle wrapper clean build runTests -Dtag="IOS" -Dtarget="IOS"
With this, we come to an end to this short three-part series on getting started with Appium, from a general introduction to Appium to working with Android to this post on iOS. Hopefully, this series makes it a little bit easier for you or your friends to get set up with Appium.
Exploring the remainder of Appium’s API, capabilities and tooling is left as an exercise to you, my brave and curious reader. I’m sure pretty soon you’ll also be sharing similar posts and hopefully, I’ll learn a thing or two from you as well. Remember Appium docs, the community, and Appium Conf are great sources to go deeper into Appium ?.
So, what are you waiting for? Go for it!
Remember, you can see the entire project on Github at appium-fast-boilerplate, clone or fork it, and play around with it. Hopefully, this post helps you a little bit in starting on iOS automation using Appium. If you found it valuable, do leave a star on the repo and in case there is any feedback, don’t hesitate to create an issue.
You could also check out https://automationhacks.io for other posts that I’ve written about Software engineering and Testing and this page for a talk that I gave on the same topic.
As always, please do share this with your friends or colleagues and if you have thoughts or feedback, I’d be more than happy to chat over on Twitter or in the comments. Until next time. Happy testing and coding.
The post Writing Your First Appium Test For iOS Devices appeared first on Automated Visual Testing | Applitools.
]]>The post Writing Your First Appium Test For Android Mobile Devices appeared first on Automated Visual Testing | Applitools.
]]>This is the second post in our Hello World introduction series to Appium, and we’ll discuss how to create your first Appium test for Android. You can read the first post where we discussed what Appium is, including its core concepts and how to set up the Appium server. You can also read the next post on setting up your first Appium iOS test.
In this post, we’ll build on top of earlier basics and focus on the below areas:
We have lots to cover but don’t worry, by the end of this post, you will have run your first Appium-based Android test. Excited? ? Let’s go.
To run Android tests, we need to set up an Android SDK, ADB (Android debug bridge), and some other utilities.
The easiest way to set these up is to go to the Android site and download Android Studio (An IDE to develop Android apps), which will install all the desired libraries and also give us everything we need to run our first Android test.
Once downloaded and installed, open Android Studio, click on Configure, and then SDK Manager. Using this you can download any android API version from SDK Platforms.
Also, You can install any desired SDK Tools from here.
We’ll go with the defaults for now. This tool can also install any required updates for these tools which is quite a convenient way of upgrading.
The Appium server needs to know where the Android SDK and other tools like Emulator, Platform Tools are present to help us run the tests.
We can do so by adding the below variables in the system environment.
On Mac/Linux:
source <shell_profile_file_name>
for e.g. source.zshrc
export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$ANDROID_HOME/emulator:$PATH
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/tools/bin:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH
If you are on Windows, you’ll need to add the path to Android SDK in the ANDROID_HOME variable under System environment variables.
Once done, run the adb command on the terminal to verify ADB is set up:
➜ appium-fast-boilerplate git:(main) adb
Android Debug Bridge version 1.0.41
Version 30.0.5-6877874
Installed as /Users/gauravsingh/Library/Android/sdk/platform-tools/adb
These are a lot of tedious steps and in case you want to set these up quickly, you can execute this excellent script written by Anand Bagmar.
Our Android tests will run either on an emulator or a real Android device plugged in. Let’s see how to create an Android emulator image.
You’ll be greeted with a blank screen with no virtual devices listed. Tap on Create a virtual device to launch the Virtual device Configuration flow:
Next, select an Android device like TV, Phone, Tablet, etc., and the desired size and resolution combination.
It’s usually a good idea to set up an emulator with Play Store services available (see the Play Store icon under the column) as certain apps might need the latest play services to be installed.
We’ll go with Pixel 3a with Play Store available.
Next, we’ll need to select which Android version this emulator should have. You can choose any of the desired versions and download the image. We’ll choose Android Q (10.0 version).
You need to give this image a name. We’ll need to use this later in Appium capabilities so you can give any meaningful name or go with the default. We’ll name it Automation.
Nice, ?We have created our emulator. You can fire it up by tapping the Play icon under the Actions section.
You should see an emulator boot up on your device similar to a real phone.
While the emulator is an in-memory image of the Android OS that you can quickly spin up and destroy, it does take physical resources like Memory, RAM, and CPU. It’s always a good idea to verify your app on a real device.
We’ll see how to set up a real device so that we can run automation on it.
You need to connect your device to your machine via USB. Once done:
And thats all you need to potentially run our automation on a connected real device.
Appium comes with a nifty inspector desktop app that can inspect your Application under test and help you identify element locators (i.e. ways to identify elements on the screen) and even play around with the app.
It can easily run on any running Appium server and is really a cool utility to help you identify element locators and develop Appium scripts.
You can download it by just going to the Appium Github repo, and searching for appium-inspector.
Go to release and find the latest .dmg (on Mac) or .exe (on Windows) to download and install.
On Mac, you may get a warning stating: “Appium Inspector” can’t be opened because Apple cannot check it for malicious software.
To mitigate this, just go to System preferences > Security & Privacy > General and say Open Anyway for Appium Inspector. For more details see Issue 1217.
Once you launch Appium inspector, you’ll be greeted with the below home screen. If you see there is a JSON representation sector under Desired Capabilities section.
Think of them as properties that you want your driver’s session to have. For example, you may want to reset your app after your test, or launch the app in a particular orientation. These are all achievable by specifying a Key-Value pair in a JSON that we provide to the Appium server session.
Please see Appium docs for more idea on these capabilities.
Below are some sample capabilities we can give for Android:
{
"platformName": "android",
"automationName": "uiautomator2",
"platformVersion": "10",
"deviceName": "Automation",
"app": "/<absolute_path_to_app>/ApiDemos-debug.apk",
"appPackage": "io.appium.android.apis",
"appActivity": "io.appium.android.apis.ApiDemos"
}
For this post, we’ll use the sample apps provided by Appium. You can see them here, once you’ve downloaded them. Keep track of its absolute path and update it in-app key in the JSON.
We’ll add this JSON under JSON representation and then tap on the Save button.
It would be a good idea to save this config for the future. You can tap on ‘Save as’ and give it a meaningful name.
To start the inspection session, You need to have an Appium server run locally, run it via typing appium
on the command line:
➜ appium-fast-boilerplate git:(main) appium
[Appium] Welcome to Appium v2.0.0-beta.25
[Appium] Attempting to load driver uiautomator2...
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
[Appium] Available drivers:
[Appium] - uiautomator2@2.0.1 (automationName 'UiAutomator2')
[Appium] No plugins have been installed. Use the "appium plugin" command to install the one(s) you want to use.
Let’s make sure our emulator is also up and running. We can start the emulator via AVD Manager in Android studio, or in case you are more command-line savvy, I have written an earlier post on how to do this via CMDline as well.
Once done, tap on the Start Session button, this should bring launch the API Demos app and show the inspector home screen.
Using this you can tap on any element in the app and see its Element hierarchy, elements properties. This is very useful to author Appium scripts and I’ll encourage you to explore each section of this tool and get familiar since you’ll be using this a lot.
Phew, that seemed like a lot of setup steps but don’t worry, you only have to do this once and now we can get down to the real business of writing our first Automated test on Android.
You can download the project using Github from appium-fast-boilerplate.
We’ll also understand what the fundamental concepts of writing page objects and tests are based on these. Let’s take a look at the high-level architecture.
Before automating any test, we need to be clear on what is the purpose of that test. I’ve found the Arrange Act Assert pattern quite useful to reason about it. Read this post by Andrew Knight in case you are interested to know more about it.
Our test would perform the below:
Let’s start by seeing our test.
import constants.TestGroups;
import org.testng.Assert;
import org.testng.annotations.Test;
import pages.apidemos.home.APIDemosHomePage;
public class AndroidTest extends BaseTest {
@Test(groups = {TestGroups.ANDROID})
public void testLogText() {
String logText = new APIDemosHomePage(this.driver)
.openText()
.tapOnLogTextBox()
.tapOnAddButton()
.getLogText();
Assert.assertEquals(logText, "This is a test");
}
}
There are a few things to notice above:
public class AndroidTest extends BaseTest
Our class extends a BaseTest, this is useful since we can perform common setup and tear down functions, including setting up driver sessions and closing it once our script is done.
This ensures that the tests are as simple as possible and does not overload the reader with any more details than they need to see.
String logText = new APIDemosHomePage(this.driver)
.openText()
.tapOnLogTextBox()
.tapOnAddButton()
.getLogText();
We see our tests read like plain English with a series of actions following each other. This is called a Fluent pattern and we’ll see how this is set up in just a moment.
Let’s see our BaseTest class:
import constants.Target;
import core.driver.DriverManager;
import core.utils.PropertiesReader;
import exceptions.PlatformNotSupportException;
import io.appium.java_client.AppiumDriver;
import org.testng.ITestContext;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import java.io.IOException;
public class BaseTest {
protected AppiumDriver driver;
protected PropertiesReader reader = new PropertiesReader();
@BeforeMethod(alwaysRun = true)
public void setup(ITestContext context) {
context.setAttribute("target", reader.getTarget());
try {
Target target = (Target) context.getAttribute("target");
this.driver = new DriverManager().getInstance(target);
} catch (IOException | PlatformNotSupportException e) {
e.printStackTrace();
}
}
@AfterMethod(alwaysRun = true)
public void teardown() {
driver.quit();
}
}
Let’s unpack this class.
protected AppiumDriver driver;
We set our driver instance as protected so that all test classes will have access to it.
protected PropertiesReader reader = new PropertiesReader();
We create an instance of PropertiesReader class to read relevant properties. This is useful since we want to be able to switch our driver instances based on different test environments and conditions. If curious, please see its implementation here.
Target target = (Target) context.getAttribute("target");
this.driver = new DriverManager().getInstance(target);
We get the relevant Target
and then using that gets an instance of AppiumDriver from a class called DriverManager.
We’ll use this reusable class to:
package core.driver;
import constants.Target;
import exceptions.PlatformNotSupportException;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import static core.utils.CapabilitiesHelper.readAndMakeCapabilities;
public class DriverManager {
private static AppiumDriver driver;
// For Appium < 2.0, append /wd/hub to the APPIUM_SERVER_URL
String APPIUM_SERVER_URL = "http://127.0.0.1:4723";
public AppiumDriver getInstance(Target target) throws IOException, PlatformNotSupportException {
System.out.println("Getting instance of: " + target.name());
switch (target) {
case ANDROID:
return getAndroidDriver();
case IOS:
return getIOSDriver();
default:
throw new PlatformNotSupportException("Please provide supported target");
}
}
private AppiumDriver getAndroidDriver() throws IOException {
HashMap map = readAndMakeCapabilities("android-caps.json");
return getDriver(map);
}
private AppiumDriver getIOSDriver() throws IOException {
HashMap map = readAndMakeCapabilities("ios-caps.json");
return getDriver(map);
}
private AppiumDriver getDriver(HashMap map) {
DesiredCapabilities desiredCapabilities = new DesiredCapabilities(map);
try {
driver = new AppiumDriver(
new URL(APPIUM_SERVER_URL), desiredCapabilities);
} catch (MalformedURLException e) {
e.printStackTrace();
}
return driver;
}
}
You can observe:
Let’s take a look at the example of a page object that enables a Fluent pattern.
package pages.apidemos.home;
import core.page.BasePage;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import pages.apidemos.logtextbox.LogTextBoxPage;
public class APIDemosHomePage extends BasePage {
private final By textButton = By.xpath("//android.widget.TextView[@content-desc=\"Text\"]");
private final By logTextBoxButton = By.xpath("//android.widget.TextView[@content-desc=\"LogTextBox\"]");
public APIDemosHomePage(AppiumDriver driver) {
super(driver);
}
public APIDemosHomePage openText() {
WebElement text = getElement(textButton);
click(text);
return this;
}
public LogTextBoxPage tapOnLogTextBox() {
WebElement logTextBoxButtonElement = getElement(logTextBoxButton);
waitForElementToBeVisible(logTextBoxButtonElement);
click(logTextBoxButtonElement);
return new LogTextBoxPage(driver);
}
}
Notice the following:
Above is an example page object class:
package core.page;
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.List;
public class BasePage {
protected AppiumDriver driver;
public BasePage(AppiumDriver driver) {
this.driver = driver;
}
public void click(WebElement elem) {
elem.click();
}
public WebElement getElement(By by) {
return driver.findElement(by);
}
public List<WebElement> getElements(By by) {
return driver.findElements(by);
}
public String getText(WebElement elem) {
return elem.getText();
}
public void waitForElementToBeVisible(WebElement elem) {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOf(elem));
}
public void waitForElementToBePresent(By by) {
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated(by));
}
public void type(WebElement elem, String text) {
elem.sendKeys(text);
}
}
Every page object inherits from a BasePage that wraps Appium methods.
Congratulations, you’ve written your first Appium Android test. You can run this either via the IDE or via a Gradle command
./gradlew clean build runTests -Dtag="ANDROID" -Ddevice="ANDROID"
You can see the entire project on the Github appium-fast-boilerplate, clone it and play around with it. Hopefully, this post helps you a little bit in starting on Android automation using Appium.
In the next post, we’ll dive into the world of iOS Automation with Appium and write our first hello world test.
You could also check out https://automationhacks.io for other posts that I’ve written about software engineering and testing.
As always, do share this with your friends or colleagues and if you have thoughts or feedback, I’d be more than happy to chat over at Twitter or elsewhere. Until next time. Happy testing and coding.
The post Writing Your First Appium Test For Android Mobile Devices appeared first on Automated Visual Testing | Applitools.
]]>The post What’s New in Appium Java Client 8.0.0 appeared first on Automated Visual Testing | Applitools.
]]>Learn about the latest updates in the Appium Java Client 8.0.0 release, and how it affects your Appium mobile testing today. Sai Krishna and Srinivasan are members of the Appium team.
The Selenium team released Selenium 4 in September 2021 to much anticipation with tons of great features. Since then, the Appium team has been working on upgrading the Selenium dependency in all the clients of Appium to provide a seamless experience. Most of the Appium clients are updated and now in early beta, but with a lot of breaking changes. Let’s go through the changes introduced in Appium Java client 8.0.0 beta2 and what you would need to do.
With Appium 2.0, we on the Appium team are making sure we are strictly W3C compliant. Since the Java client supports Selenium 4.1.1, it’s strictly W3C compliant too. Methods that are non-w3c complaint are removed and the details can be seen here.
It is now recommended to use driver-specific W3C option classes instead of Desired Capabilities. Below is the list of driver-specific classes.
XCUITestOptions
to create an XCUITestDriver instance UiAutomator2Options
to create a UIAutomator2Driver instanceEspressoOptions
to create an EspressoDriver instanceWindowsOptions
to create a WindowsDriver instanceMac2Options
to create a Mac2Driver instanceGeckoOptions
to create a GeckoDriver instanceSafariOptions
to create a SafariDriver instanceSee the below example for changes required:
// From Appium Java Client version 8.0.0 Beta
UiAutomator2Options options = new UiAutomator2Options()
.setDeviceName("Android Emulator")
.setApp(System.getProperty("user.dir") + "/VodQA.apk")
.eventTimings();
driver = new AndroidDriver(service.getUrl(), options);
// Older approach: Java Client version 7.X.X
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 700000);
findBy
methods.MobileBy
with AppiumBy
and introduced camelCase naming conventions. For example, MobileBy.AccessibilityId
is now AppiumBy.accessibilityId
. MobileBy
is now deprecated.windowsAutomation
locator strategy is deprecated.AppiumServiceBuilder
to start a node server programmatically. With the updated version, it works slightly differently with different Appium server versions./wd/hub
by default.–base-path
explicitly while building the AppiumServiceBuilder
.// From Appium 2.0 Beta
AppiumDriverLocalService service;
service = new AppiumServiceBuilder()
.withIPAddress("127.0.0.1")
.usingPort(4723)
.build();
service.start();
// Older approach: Appium 1.22.X
AppiumDriverLocalService service;
service = new AppiumServiceBuilder()
.withIPAddress("127.0.0.1")
.withArgument(GeneralServerFlag.BASEPATH, "/wd/hub")
.usingPort(4723)
.build();
service.start();
SelendroidDriver
class is removedGeckoDriver
and SafariDriver
are newly introduced driversGeckoDriver
is an officially supported Appium driver to automate Mobile browsers and web views based on the gecko engineSafariDriver
is also an official driver for automating Safari on macOS and iOSMobileElement
classes including AndroidElement
and iOSElement
classes are removed. It is recommended to use WebElement
instead.replaceValue
method is now called replaceElementvalue
in AndroidDriver
classsetValue
method is removed in favor of the existing sendKeys
method.The TouchActions
and MultiTouchActions
classes for automating gestures from your client code have been deprecated. Support for these actions will be removed from future Appium versions. It is recommended to use W3C Actions instead or the corresponding extension methods for the driver (if available).
Point source = slider.getLocation();
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence sequence = new Sequence(finger, 1);
sequence.addAction(finger.createPointerMove(ofMillis(0),
PointerInput.Origin.viewport(), source.x, source.y));
sequence.addAction(finger.createPointerDown(PointerInput.MouseButton.MIDDLE.asArg()));
sequence.addAction(new Pause(finger, ofMillis(600)));
sequence.addAction(finger.createPointerMove(ofMillis(600),
PointerInput.Origin.viewport(), source.x + 400, source.y));
sequence.addAction(finger.createPointerUp(PointerInput.MouseButton.MIDDLE.asArg()));
driver.perform(singletonList(sequence));
Refer to the links below to know more about how to work with gestures:
AppiumDriver methods like resetApp
, closeApp
and launchApp
have been deprecated as they are going to be removed from the future Appium versions. Alternatively, the suggested approach is to use removeApp
, installApp
, and activateApp
methods available here. The non-standard way for App Management in iOS is using the mobile:
methods to remove the app then install the new application and launch it using the methods here. For Android UIAutomator2 backend, the non-standard mobile:
methods like clearApp
deletes all data associated with a package, and installMultipleApks
allows users to install multiple applications at the same time.
The current event firing mechanism that Appium Java Client uses has been deprecated in favor of the one that Selenium 4 provides natively. Read The-event_firing.md for more details on how to use it.
There are a lot of great new changes in this latest Appium Java Client update. You can check out a detailed migration guide here in Appium’s Java Client for reference. Do let us know where there’s been a problem, and thank you. We only have a chance to improve when we know that there’s something that needs work!
The post What’s New in Appium Java Client 8.0.0 appeared first on Automated Visual Testing | Applitools.
]]>The post How to Test a Mobile Web App in Cypress appeared first on Automated Visual Testing | Applitools.
]]>Before we start, we need to clear up some areas that tend to confuse people when it comes to mobile testing. If you’d like to test a mobile application built for iOS or Android, Cypress will not be efficient and you should probably reach for a tool like Appium (although there are some interesting examples by Gleb Bahmutov for React Native). Cypress is best known as a tool for testing anything that runs in a browser. So in order to use Cypress for your mobile testing, your app needs to be able to run in a browser. That brings forward a question: “What’s the difference between desktop and mobile web app?”
To answer this question, it’s best to look at the application in test from a perspective of a developer. As someone who creates a web application, you might want to consider a couple of traits that make the mobile app different from a desktop one:
Let’s go through them one by one and see how we can write a Cypress test that would ensure our application works as expected. If you want to follow along, you can clone my repository with the application and attached Cypress tests. The simple example application shows different messages based on viewport width, presence of touch interface or user agent.
We might want to test a responsive CSS on different screen widths to see if the application renders correctly. This can be easily done by using the cy.viewport()
command. You can specify width and height of the viewport, or choose from one of the predefined devices:
Using these commands will trigger visibility of a “it’s getting pretty narrow here” message:
<
CSS responsiveness can hide or show different content, like buttons or modals. A nice trick to test this is to run your existing tests with different resolution. To do that, pass a --config
flag to your CLI command:
npx cypress run --config viewportWidth=320 viewportHeight=480
You can also set the viewport width and viewport height in cypress.json
or programmatically via Cypress plugin API. I write about this on my personal blog in a slightly different context, but it still might be helpful in this case.
Depending on your application, you might want to consider testing responsiveness of your application by visual tests using a tool like Applitools. Functional tests might have a tendency to become too verbose if their sole purpose is to check for elements appearing/disappearing on different viewports.
Your application might react differently or render slightly different content based on whether it is opened on a touch screen device or not. Manually you can test this with Chrome DevTools:
Notice how we can have a touch device, that is actually not a mobile. This is a nice case to consider when testing your app.
In Chrome DevTools, we are simply switching a mode of our browser. It’s like changing a setting to enable viewing our app as if it was opened on a touch device.
With Cypress we can do something very similar. There is an ontouchstart
property which is present on a mobile browser. This is usually a very good clue for a web application to “know” that it is being opened on a touch device. In Cypress, we can add this property manually, and make our application “think” it is being opened on a touch device:
With the onBeforeLoad
function, we tap into the window object and add the property manually, essentially creating a similar situation as we did in DevTools, when we toggled the “touch” option:
To go even further with testing a touch interface I recommend using Dmitryi Kovalenko’s cypress-real-events plugin that fires events using Chrome devtools protocol. Your Cypress API will now get augmented with cy.realTouch()
and cy.realSwipe()
commands, which will help you test touch events.
Some applications use information from user agent to determine whether an app is being viewed on mobile device. There are some neat plugins out there that are commonly used in web applications to help with that.
Although User Agent might sound like a super complicated thing, it is actually just a string that holds information about the device that is opening a web application. Cypress allows you to change this information directly in the cypress.json
file. The following setup will set the user agent to the exact same value as on an iPhone:
However, you might not want to change the user agent for all of your tests. Instead of adding the user agent string to cypress.json
you can again tap into the onBeforeLoad
event and change the viewport on your browser window directly:
The reason why we are not changing win.navigator.userAgent
directly via the assignment operator (=
) is that this property is not directly configurable, so we need to use the defineProperty
method. Opening our application in this way will, however, cause our application to render a message that it is being viewed on mobile device:
You cannot test native mobile apps with Cypress, but you can get pretty close with mobile web apps. Combining these three methods will help you narrowing the gap between desktop and mobile testing. To understand what makes an app behave differently, it is nice to look into your app as a developer at least for a little while.
If you enjoyed this blogpost, make sure to head over to my personal blog for some more Cypress tips.
The post How to Test a Mobile Web App in Cypress appeared first on Automated Visual Testing | Applitools.
]]>The post Hello Appium, Part 1: What is Appium? An Introduction to Appium and its Tooling appeared first on Automated Visual Testing | Applitools.
]]>In the first article in this series, learn how to get started with Appium. We’ll dig into what Appium is, the essentials of the Appium architecture and how you can use Appium to solve your mobile test automation issues today. You can also skip ahead to read about how to set up your first Appium Android test and how to set up your first Appium iOS test.
I recently had the privilege to speak at Appium Conf 2021 on a topic quite near to my heart: Getting started with Appium.
If you are an intermediate or advanced Appium user, then sure, ?? you may be familiar with this already.
However, I’m a fan of a concept called Shoshin (loosely translated as “Beginner’s mind”) and believe that relearning something opens up new ways of solidifying concepts and often enlightens you about the gaps in your knowledge.
When I started learning Appium back in 2018, I remember I had to struggle a lot to figure out where to start, what setups are needed to run my first test, or just even the underlying architecture.
This was a lot of initial friction and I remember thinking to myself:
It shouldn’t be this hard just to get started, eh?
Appium is a great automation tool and now having used it for a few years and having gone through the trenches I wanted to improve this experience for beginners. Hence the reason for my talk and this blog series. If you are curious about the Appium talk, the video is out on YouTube and slides over a static site.
In this 3 part series we’ll deep dive into:
Let’s begin.
Appium is an open source ecosystem of tools and libraries that help you drive your mobile apps (native, hybrid, and mobile web on platforms such as Android, iOS), desktop apps (Windows, Mac), and now even on platforms like Smart TV and much more ?.
Language agnostic: With Appium you can choose the language binding of your choice (Such as Java, Kotlin, Python, JavaScript, Ruby, C# … ?) and don’t have to be tied to the programming language of the platform vendors (Kotlin, Swift, etc.).
Does not reinvent the wheel: Appium provides a convenient API compliant with Web driver protocol that wraps automation libraries from vendors (Google UIAutomator2/Espresso, Apple XCUITest/UIAutomation, Windows WinApp) and allows you to write cross-platform tests.
No requirement of SDK or recompilation of the app: Appium can drive the app from a grey box user perspective but does not need any app compilation step.
Cross-platform: Drives apps on different platforms and provided apps have the same business logic, could drive them with a single cross-platform test.
Before diving into practical setup and tests, it’s important to understand the Appium ecosystem in a birds-eye so that you can make sense of all the different components at play
The below high-level architecture diagram gives the perspective:
The core components are:
A typical request in Appium loosely looks like below:
{
"orientation": "LANDSCAPE|PORTRAIT"
}
Now that we understand the high-level components at play, let’s install some of these to set up our environment regardless of the platform that we choose to automate.
To set up the Appium server and other utilities, we’ll need to install npm, node. These could be easily installed with a tool like homebrew (mac), Linux brew (Linux), or chocolatey (windows)
We’ll assume using a Mac/Linux or WSL (Windows Subsystem for Linux) environment for this series but you’ll be able to find equivalent steps for the Windows platform by googling.
We’ll need it to install npm and node.
To install on macOS:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
On Linux or WSL (Windows Subsystem for Linux), you may need to install Linux brew.
You can download and install node (10+) for your platform. Prefer choosing LTS (Long term support version) since that would be the stable version.
On macOS:
brew install node
Verify running the below command returns desired version:
node -v
And verify that npm is installed as well:
npm -v
To install Appium server < 2.0
npm install -g appium
Verify Appium server is installed by typing appium command in your terminal and see all server logs.
➜ appium-fast-boilerplate git:(main) appium
[Appium] Welcome to Appium v1.17.0
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
Appium 1.2x.x would be the last version supported by Appium devs and an EOL (End of Life) plan is already published on the Appium Github repo. Read it here. Since Appium 2.0 is the future of the project, let’s see how to install the new server and some key commands.
To install Appium 2.0:
npm install -g appium@next
You’ll get an output like:
/usr/local/bin/appium -> /usr/local/lib/node_modules/appium/build/lib/main.js
> appium@2.0.0-beta.16 postinstall /usr/local/lib/node_modules/appium
> node ./postinstall.js
Not auto-installing any drivers or plugins
+ appium@2.0.0-beta.16
added 113 packages from 587 contributors, removed 316 packages, updated 136 packages and moved 5 packages in 81.611s
With Appium 2.0, drivers are decoupled and not bundled into the main server. This makes sense since if your app is only on Android, you wouldn’t need iOS, desktop, and TV drivers anyways right?
We’ll install Android and iOS drivers for this project using the below commands:
appium driver install xcuitest
appium driver install uiautomator2
You can list all available drivers using the command:
appium driver list
Or list only installed drivers using:
appium driver list --installed
After running the appium command, you’ll see an output like this and as you can see our installed drivers:
[Appium] Welcome to Appium v2.0.0-beta.16
[Appium] Non-default server args:
[Appium] tmpDir: /var/folders/5d/flg6q03n3j7769kffzly2x_r0000gp/T
[Appium] Attempting to load driver xcuitest...
[Appium] Attempting to load driver uiautomator2...
[Appium] Appium REST http interface listener started on 0.0.0.0:4723
[Appium] Available drivers:
[Appium] - xcuitest@3.53.1 (automationName 'XCUITest')
[Appium] - uiautomator2@1.67.0 (automationName 'UiAutomator2')
You can install Java from the Oracle site or the OpenJDK version:
On macOS: If you choose to install OpenJDK, then execute the below command:
brew install openjdk@8
sudo ln -sfn /usr/local/opt/openjdk@8/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk-8.jdk
Add JDK home path as JAVA_HOME variable. Since we would use the Java client binding for this series, this is a needed step.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/openjdk-8.jdk/Home
export PATH=$JAVA_HOME/bin:$PATH
Appium doctor is a CLI that provides insights on what dependencies are missing as well as how to install them. It even provides very helpful suggestions or notes on how exactly a dependency might be useful.
Make sure all required dependencies are installed:
npm install -g appium-doctor
Run below commands:
# For both android and iOS
appium-doctor
# For only android
appium-doctor --android
# For only iOS
appium-doctor --ios
If I run it at this point in time, it intelligently warns me about the required dependencies that are installed, optional dependencies that could be installed, and how to go about installing them.
So hopefully you are set up with Appium, have a conceptual understanding of the different components, and are now ready to dive into writing our first tests for Android and iOS. Stay tuned for the next part. In the meantime, you can read the below docs to get further context.
Looking for more? We’ve got other great content to help you dig in and get started with Appium. Here are a few places to help you get started with Appium today.
The post Hello Appium, Part 1: What is Appium? An Introduction to Appium and its Tooling appeared first on Automated Visual Testing | Applitools.
]]>The post How to Automate Gesture Testing with Appium appeared first on Automated Visual Testing | Applitools.
]]>In the previous blog posts in this Appium series, we discussed step-by-step instructions for creating your own Appium 2.0 Plugins as well as Appium 2.0 Drivers and Plugins usage and its installation. This article discusses the evolution of touch gestures and how you can simplify the way you perform automated tests for gestures using a new plugin in Appium 2.0.
Gestures are the new clicks. Gesture-driven devices changed the way we think about interaction. The success of mobile applications depends on how well the gestures are implemented into the user experience. The definitive guide for gestures by Luke Wroblewski details a lot of different actions and how they actually work.
Animations, when paired with Gestures, make users believe in interacting with tangible objects. Appium handles these gestures using the Actions API defined in the W3C WebDriver Specification. It’s great that the API was designed thinking of all interfaces like touch, pen, mouse, etc., but it is hard to understand. Since Appium 1.8, Appium supports the W3C Actions API for building any kind of gesture in mobile devices.
Let’s consider a situation where the application sets specific values based on the slider movements.
Let’s see using Actions API how this situation can be handled.
In the above code snippet, we have three actions to look at which are to get the element location using location API and create a sequence with actions which includes pointerMove
, pointerDown
, pause
, pointerUp
, and then perform the sequence. So in this case we need to add these actions in the right order to get the gesture to work. Let’s break down the above in detail to understand what happens under the hood.
PointerInput
object of type TOUCH
with a unique id as a finger.PointerInput
object.Refer to the below image to understand how the element location can be calculated.
This way any complex gestures can be automated using Actions API.
Let’s look at how the gestures plugin from Appium 2.0 simplifies this entire process:
Internally appium-gesture-plugin
finds the given element location and does the target location calculation based on the given percentage. It also creates the sequence of actions to perform the gestures on both iOS and Android platforms.
Refer here for a working example of the above swipe gesture using the gestures plugin. Follow the instruction specified here to install the Appium gestures plugin.
For any simple gesture actions (swipe, drag and drop, long press, double-tap, etc.) we can use the gestures plugin. For more custom actions, like if your application has a feature on digital signature, we could still use the Actions API.
Apart from the Actions API, Appium also supports native gesture API which is exposed by Android and iOS platforms through non-standard endpoints like below:
For more such APIs, check out Android non-standard gesture APIs and iOS non-standard gesture APIs.
The post How to Automate Gesture Testing with Appium appeared first on Automated Visual Testing | Applitools.
]]>The post Getting Started with Visual UI Testing for Android Apps with Applitools and Bitrise appeared first on Automated Visual Testing | Applitools.
]]>A hands-on guide for getting started with visual UI testing for mobile apps with Applitools and Bitrise easily.
Mobile apps play a critical role nowadays in our life, especially during the pandemic period, like e-commerce, groceries, medical, or banking apps. As we know there are different mobile apps, technologies, SDKs, and platforms that make mobile testing have a lot of challenges and which need special technical skills from the testing team to tackle.
And because most of the mobile companies aim to release their apps on a weekly or bi-weekly release cadence based on the team decision to deploy new features to the customer before their competitors, they are using different techniques. One of them is test automation.
Based on that, in this article, I’ll walk you through the most important aspects of automating the Visual UI testing for mobile apps and running it via CI/CD pipelines using Applitools and Bitrise.
Visual Testing is a testing activity that helps us to verify that the GUI (Graphical User Interface) appears correctly and as expected to customers, by checking that each element on a mobile app appears in the right size and position on different devices and OS versions.
Mobile companies nowadays are applying Mobile DevOps and implementing CI/CD (Continuous Integration and Continuous Delivery/Deployment) pipelines to deploy their mobile apps consistently and frequently.
One of the vital parts of this process is test automation, to make sure that app functionalities are working as expected and test that there are no blockers or critical issues in the early stages (Fail Fast) by running these tests on every pull request or as a nightly build. And if we add Visual UI testing with this pipeline we will make sure that there are no issues in the GUI and all the features are displayed correctly.
In this tutorial, I will add the Visual UI Testing to my Android UI tests with Appium and then run my tests on Bitrise as CI/CD pipeline, covering the following steps:
To get started with Appium for Android applications, you should prepare your local machine with the following requirements:
More information about installing Appium can be found in the free Appium Course on Test Automation University or in this blog post.
If you don’t have an Android application, you can fork the Sunflower app from GitHub. Sunflower app is a gardening app illustrating Android development best practices with Android Jetpack.
With Appium, you can test automation scripts or projects that are stand-alone. I created a Maven project (you can create a Gradle one if you’d like) with the following structure:
The full project can be found on GitHub.
And now we can run the test using the following command:
> mvn clean test -Pandroid
And the results will be like the following image:
Now it’s time to add the Visual UI validation using Applitools Eyes SDK for Appium.
Applitools Eyes is powered by Visual AI, the only AI-powered computer vision that replicates the human eyes and brain to quickly spot functional and visual regressions.
If you don’t have an account you can create a free account from here.
Eyes eyes = new Eyes();
eyes.setApiKey("YOUR_API_KEY");
eyes.setForceFullPageScreenshot(false);
eyes.open(driver, "Sunflower", "Add My Plant");
eyes.checkWindow("Plant list",false);
eyes.checkWindow("My Empty Garden",false);
eyes.checkWindow("Avocado",false);
eyes.close();
eyes.abortIfNotClosed();
10. Now we can run our test again with the same Maven command and check the results in the Applitools Test manager with the steps view.
Or the Batch Summary View:
11. Try to run the test again and check if the results are the same or if there is an issue.
In my case when I run the tests again the test failed because the Eyes detected differences between the base images or the first run/batch and the 2nd run.
And I checked the dashboard to check the results and the differences:
And I found that I have a difference in the first image which was already highlighted. You can click on the image for more details or compare it with the base image to check the difference.
Because in my case sometimes the images loaded slowly, there was a difference between the two images.
And also you can check the Batch Summary View for more details.
TIP: If you need to run on different mobile devices and OS versions in parallel, you can use Applitools Ultrafast Test Cloud. More info on that can be found here.
After we successfully run our tests locally, it’s time to integrate our tests with our Android CI using Bitrise.
But first, we need to do a small change in the application path, Applitools API Key, and the Batch ID. Locally, we’re using the absolute path of the app and hardcoded values, but with Bitrise or our CI server, it should be as an environment variables with the following steps:
caps.setCapability("app", System.getenv("BITRISE_APK_PATH"));
Now it’s time to integrate our Android app and the automation project with Bitrise.
14. The final step is the Webhook setup. We just need to click on Register a Webhook for me! button and you will trigger your first Android build for your project
15. You will be redirected to the build log where you can check the Steps and the build status.
16. In the end, you will find a summary of all the tasks you ran during the build and how much time they took, along with the Step names. You can download logs or check the apps and artifacts here.
17. Now let’s open the Workflow Editor to configure the CI Workflow with the new steps required for Appium and Applitools integration.
You can rename the workflow as you like, for example from primary to appium-ui-tests:
18. Add a Script step to clone the Appium repository by clicking on the + button and it will open the steps screen, then you can search about the script then select it.
19. As we know, we are using Appium Desktop as a server and inspector locally, but with CI servers we can’t use it, so we need to install and run Appium Server from the command line. Here I’ll add another Script step to install and run the Appium server in the background.
20. You will notice that I’m passing –log appium.log with the Appium command to export the server log as a .txt file for more debugging purposes if any tests failed.
21. Add the AVD Manager Step to create an Android Emulator to use with your tests. You can choose any API level. For example, you can change it to 28 but you need to change it in the Appium desired capabilities and commit the change to your GitHub repository as well.
22. Add the Wait for the Android emulator Step and wait for the emulator to be ready before running our tests on it.
23. Add another Script Step to run the UI tests. You need to switch inside the automation project to be able to run the maven command.
24. Add the Script Step to copy the Appium log file to the Deploy directory of Bitrise to be able to find it in the artifacts tab.
25. Add a Step to Export the test results to the Bitrise Test Report Add-on and specify the test results search pattern like this: (*/target/surefire-reports/junitreports/*)
26. Add the Applitools API Key and the Batch ID as environment variables, by click on the Secrets menu option and click add button then add your APPLITOOLS_API_KEY and APPLITOOLS_BATCH_ID like the following image:
The final Workflow will look like the following:
To run the Workflow, go to the application page and click on the Start/Schedule Build button and select the Workflow then click on the Start Build button.
To check the test results and the Appium log file you can click on the Test Report add-on, and for the appium log and the app.apk file, you can click on Apps & Artifacts tab.
Finally, we successfully added the Visual UI testing for our Android app and we can run the tests on every pull request or as a nightly build to make sure that we don’t have something breaking our codebase and to keep our master branch always green.
I hope you found this article useful and thank you for reading it!
The post Getting Started with Visual UI Testing for Android Apps with Applitools and Bitrise appeared first on Automated Visual Testing | Applitools.
]]>The post How to Build Your Own Appium Plugin appeared first on Automated Visual Testing | Applitools.
]]>In the previous blog post we discussed Appium 2.0 Drivers and Plugins usage and its installation. This article is aimed at creating your own Appium Plugin and its use cases.
Appium plugins are here to help with the various use cases that require a change to Appium’s default behavior.
There are already official Appium plugins created by the project, as well as community-created Appium plugins.
Let’s say we want to improve the way we write appium tests, for example, let appium server take care of element waits and we focus on the user journey in tests rather than element waits all over it.
Below are the steps to create a new plugin to wait for the presence of an element:
Initialize a new node project
npm init
Install Appium base plugin as a dependency inside the new project created in above step
npm i @appium/base-plugin
Navigate to the package.json and add an appium object as described below:
"appium": {
"pluginName": "element-wait",
"mainClass": "WaitCommandPlugin"
}
Create a new file called plugin.js in the root directory of the project
In plugin.js, create a class which extends appium base plugin.
export default class WaitCommandPlugin extends BasePlugin {}
Here is the list of supported Appium commands. As per our use case, we need to override the appium findElement command.
Override the appium command implementation in our new class
async findElement(next, driver, ...args) {
// Custom Implementation of Appium’s FindElement command
}
We can extract strategy and selector from the arguments of the findElement command and then make an API call to the /element endpoint to check if it exists in the DOM (document object model):
const response = await fetch(
`${baseUrl}wd/hub/session/${this.driver.sessionId}/element`,
{
body: JSON.stringify({
strategy: this.strategy,
selector: this.selector,
context: '',
multiple: false,
}),
method: 'POST',
headers: { 'Content-Type': 'application/json' },
}
);
Keep polling the endpoint consistently for a constant period of time until the element exists, if it exists check for element displayed status as below:
const response = await fetch(
`${baseUrl}wd/hub/session/${this.driver.sessionId}/element/${this.element}/attribute/displayed`,
{
method: 'GET',
headers: { 'Content-Type': 'application/json' },
}
);
return await response.json();
}
Call await next() to execute the default behaviour of the findElement command. Refer here for more details on plugin.js
Export the plugin
Create a new file called index.js in the root directory of the project to export the plugin
export default WaitCommandPlugin;
Install the plugin
Transpile and build the plugin using babel or Typescript. Install the plugin from local source using the below command:
appium plugin install --source=local <path to plugin>
Activate the plugin
The plugin can be activated using the following command while starting the Appium server. In this example, “element-wait” is the name of the plugin.
appium --plugins=element-wait
Plugin usage
The element wait plugin is activated while starting the appium server so the client code can be simplified without using webdriver waits.
Without Wait plugin
wait = new WebDriverWait(driver, 30);
wait.until(presenceOfElementLocated(MobileBy.AccessibilityId("login"))).click();
wait.until(presenceOfElementLocated(MobileBy.AccessibilityId("slider")));
driver.findElementByAccessibilityId("slider").click();
With Wait plugin
driver.findElementByAccessibilityId("login").click();
driver.findElementByAccessibilityId("slider").click();
In the example above, we altered the existing appium command to find an element, but Appium plugins can also be used to create our own custom commands on top of Appium supported commands.
To implement custom commands or adding new routes in appium we need to implement on both the Appium server and client-side.
A server-side plugin includes adding a new method map where we define the route and the new Appium command and its implementation.
static newMethodMap = {
'/session/:sessionId/fake_data': {
GET: {command: 'getFakeSessionData'}
},
};
async getFakeSessionData (next, driver) {
await B.delay(1);
return driver.fakeSessionData || null;
}
As of today custom commands can be added only in Appium Ruby, Appium Python and in WebdriverIO. Below is an example from Appium Ruby client:
@driver.add_command(
method: :post,
url: 'session/:session_id/path/to/custom/bug/report,
name: :bug_report
) do
def bug_report(argument)
execute(:bug_report, {}, { sample: argument })
end
end
@driver.bug_report(1)
Using this approach, the Appium plugin adds custom commands and overrides or modifies the existing Appium commands.
Decoupled Appium 2.0 architecture will help create new plugins to solve many interesting use cases. For more on Appium 2.0, check out our talk at the Future of Testing event.
The post How to Build Your Own Appium Plugin appeared first on Automated Visual Testing | Applitools.
]]>The post A Guide to Appium – Our Top 10 Appium Tutorials in 2021 appeared first on Automated Visual Testing | Applitools.
]]>In this collection of free Appium tutorials, guides and courses, find our most popular Appium articles so far in 2021 to help you improve your mobile test automation skills.
Are you looking to up your mobile test automation game? Appium is a powerful open-source testing framework that you should be acquainted with. At Applitools, we love all things test automation, and we’ve been thinking and writing about Appium for years. In this guide, we’ve collected all our best free tutorials, comparisons and courses in one place for you.
The pieces are ranked by the traffic they’ve received so far in 2021 – collectively they’ve been viewed by many thousands of you in the last six months or so.
Whether you’re a beginner or an experienced Appium user, you’re sure to find something new and useful here. We hope you enjoy this list, and if there’s something we missed that you wish we’d cover in the second half of 2021, let us know @applitools!
First, a brief introduction.
Appium is one of the most popular open-source test automation frameworks for mobile testing – the testing of native mobile apps, mobile web apps and hybrid apps for Android, iOS and Windows. It is cross-platform and compatible with numerous development languages, allowing you to write tests against multiple platforms in the language of your choice and reuse the code. It was developed in 2011 by Dan Cuellar and Jason Huggins, and today has about 14K stars on GitHub with very regular updates (the last commit was 4 hours ago at time of writing).
You can get an overview of what Appium is all about here in their docs.
In this on-demand webinar (with accompanying slides), Applitools Senior Director of R&D, Daniel Puterman, dives deep into the internals of Appium’s code. He and his team submitted a major pull request when they added a feature to Appium, and Daniel shares his experiences doing so. This webinar took place a few years ago but it’s still a relevant and fascinating look into the structure and architecture of Appium.
Jonathan Lipps, who leads the Appium project (among other things), led this on-demand webinar to help you understand how to easily parallelize visual testing with Appium across all Android devices at once using Genymotion Cloud and Applitools. If you’re wondering how you can perform visual testing for mobile apps at scale, this is a great one to check out.
When I said we’ve been talking about Appium for years, I meant it, and this classic post is now approaching 7 years young. That this post remains among our more popular Appium posts after all that time is a testament to the enduring relevance of the techniques it describes and the topic itself. Take a look at this post and the demo video to get a sense of how wearable devices can be tested with Appium.
Getting started with Appium for the first time can be a little daunting, as there are many dependencies to keep track of and set up. In this post, Anand Bagmar shares a custom script he wrote to automate the process for you so that you don’t have to do each of these individually. Check it out for a simple and easy way to get going with Appium quickly.
A few months ago Applitools hosted a great “Future of Testing: Mobile” event. In this recap, you can read about (and watch) key talks about Appium 2.0, the state of mobile frameworks generally, and much more. You can watch all the videos here (and sidenote: our next “Future of Testing: Mobile” event takes place on August 10th, so register now to catch it live!).
Mobile testing can be a challenge. Android in particular can seem daunting, with its fractured nature yielding numerous devices, form factors and operating system versions that need to be tested to achieve full coverage. In this tutorial, you’ll learn how you can use Genymotion’s cloud-based Android emulation to run Appium tests rapidly on a range of Android devices, and how you can easily combine it with Applitools as well to give you full visual coverage.
In this recap of a popular Test Automation University course, Automated Visual Testing with Appium, which is taught by Jonathan Lipps (who we also talked about in #9), you’ll get a crash course on visual testing with Appium. You’ll learn the basic of Appium testing and explore multiple alternatives for visual testing. This is a great written recap that is easy to follow along with, so check it today to get started with visual testing, and don’t forget to check out the full course if you’re curious for more.
Remember back at #5 when we talked about the challenge of testing on mobile across all devices, form factors and operating system versions? In this popular article, you’ll learn another way to tackle the issue using Amazon’s AWS Device Farm. The solution is cross-platform too, so it goes beyond just Android. This tutorial is on the technical side, and you’ll find a number of helpful and detailed code samples and screenshots to guide you through the process step by step.
Appium 2.0 is coming soon, and the betas have been coming out quickly as the official release draws closer. If you’re looking to get started with Appium 2.0 early, this guide is for you. You’ll find tips for installing the Appium 2.0 server, working with the newly decoupled drivers, incorporating the latest plugins and more. Though things move fast and a beta version or two has been released since it was published in February, it’s still extremely relevant and one of our most popular Appium posts for a reason.
Our most popular post around Appium, this one seeks to answer a question that’s been around since the dawn of mobile apps: What mobile test automation framework should I use for my app? This article compares Appium (cross-platform, open-source) with two of the most widely-used test automation frameworks in Espresso (just Android, developed by Google) and XCUITest (just iOS, developed by Apple). It provides a detailed overview of the pros and cons for each of these frameworks. If you’re looking to understand these frameworks better or just to figure out how to test your own app, this highly-visited post is for you.
Looking to expand your knowledge around Appium but want something more in-depth than an individual article or tutorial? Why don’t you take a free course at the Test Automation University? Here are the top three courses you may want to consider:
Appium is a popular open-source framework for mobile test automation. It’s a powerful and versatile tool, and definitely one that we’re watching closely as it develops. How do you use Appium today, and what are you looking forward to in the next release? Let us know @applitools.
The post A Guide to Appium – Our Top 10 Appium Tutorials in 2021 appeared first on Automated Visual Testing | Applitools.
]]>