Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Automation Testing for Mobile

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 1


Lecture 03:
ADVANCED USER INTERACTIONS

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 2


Agenda

• Tap Mobile Elements


• Long press
• Swipe and Scroll
• Manage Device Orientation
• Install and Uninstall Native Apps
• Capture screenshots

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 3


Tap mobile elements
For people familiar with web automation , clicking is a common and simple
action, but in the mobile landscape, tapping is the action that replaces clicking.

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 4


Tap mobile elements

Tab by using an element


Tab by using x,y coordinates

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 5


Tap mobile elements
Tap by using an element

public void tapByElement (AndroidElement androidElement,


AndroidDriver driver) {
TouchAction action = new TouchAction(driver);

action.tap(TapOptions.tapOptions().withElement(ElementOption.ele
ment(androidElement))).perform();
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 6


Tap mobile elements
Tab by using x,y coordinates
Element.getLocation().getX()

Element.getLocation().getY()

public void tapByCoordinates (int x, int y, AndroidDriver driver) {


TouchAction action = new TouchAction(driver);
action.tap(PointOption.point(x,y));
}

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 7


Long press
Long press is a mobile gesture that is widely
used by people. It is a wonderful feature; most
people say that you should touch and hold instead
of using long press. By using long press, you can
get more information about a particular feature.
Just like a context-click on the Web, it also
enables multiselection in mobile apps.

new
TouchAction(driver).longPress(PointOption.po
int(element.getLocation().getX(),
element.getLocation().getY()))

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 8


Swipe and Scroll
In general, scrolling is the process of moving the visual portions of mobile apps
in order to see additional information.
On a mobile device, you can use your finger to swipe up or down in order to
scroll down or scroll up the screen. Swiping your finger upwards will lead to a
scroll down and swiping down will perform a scroll up

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 9


Swipe and Scroll
public void swipe(int startx, int starty, int endx, int endy) {

new TouchAction(driver).longPress(PointOption.point(startx, starty))

.moveTo(PointOption.point(endx, endy))

.release().perform();

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 10


Swipe and Scroll
public void swipeMobileUp() {

Dimension size = driver.manage().window().getSize();

int starty = (int) (size.height * 0.8);

int endy = (int) (size.height * 0.2);

int startx = size.width / 2;

swipe(startx, starty, startx, endy);

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 11


Swipe and Scroll
public void swipeMobileDown() {

Dimension size = driver.manage().window().getSize();

int starty = (int) (size.height * 0.8);

int endy = (int) (size.height * 0.2);

int startx = size.width / 2;

swipe(startx, endy, startx, starty);

}
09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 12
Swipe and Scroll
public void rightLeftSwipe() {

Dimension size = driver.manage().window().getSize();

int startx = (int) (size.width * 0.90);

int endx = (int) (size.width * 0.10);

int starty = size.height / 2;

swipe(startx, starty, endx, starty);

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 13


Swipe and Scroll
public void leftRightSwipe() {

Dimension size = driver.manage().window().getSize();

int startx = (int) (size.width * 0.90);

int endx = (int) (size.width * 0.10);

int starty = size.height / 2;

swipe(endx, starty, startx, starty);

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 14


Capturing screenshots
File screenShot=driver.getScreenshotAs(OutputType.FILE);
File location=new File("screenshots");
String
screenShotName=location.getAbsolutePath()+File.separator+"testCalcula
to r.png";
FileUtils.copyFile(screenShot,new File(screenShotName));

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 15


Manage device orientation
One convenience of using smartphones and
tablets is that when you hold the device
either horizontally or vertically, the mobile
app will adjust to the new viewport size. In
the beginning of the mobile app
development era, most defects were
discovered because of an orientation
change, so it is important to run test cases
on orientation change to make sure that the
app works fine when users change the
orientation. You want to know how to change
the orientation using Appium.

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 16


Manage device orientation

driver.rotate(ScreenOrientation.LANDSCAPE);

driver.rotate(ScreenOrientation.PORTRAIT);

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 17


Install and Uninstall Native Apps

Installing, upgrading, and deleting applications can be tricky because these


tasks require a lot of changes to the memory and cache on a device. So, testing
scenarios related to these steps are important for covering edge scenarios in
your test strategy.

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 18


Install and Uninstall Native Apps
Automate launching, closing, installing, and removing an app from an Android device.

With the following code, you are performing the following actions on an Android app:

1. Checking whether the app is launched


2. Closing the app
3. Launching the app again
4. Checking whether the app is launched
5. Checking whether app is installed
6. Removing the app from the device
7. Installing the app again
8. Checking whether the app is installed

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 19


Install and Uninstall Native Apps
Confirm If App is launched: Check If App is installed

Driver.currentActivity() Driver.isAppInstalled(io.appium.android)

Close the App: Remove App

Driver.closeApp() Driver.removeApp(io.appium.android)

Launch the App again: Install App again

Driver.launchApp() Driver.installApp(appFilePath)

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 20


Happy Coding!

09e-BM/DT/FSOFT - ©FPT SOFTWARE - Fresher Academy - Internal Use 21

You might also like