Automation QA Testing Course Content

Appium-Mobile Elements

Find Element:

Search for an element on the page 

Get the first element that matches a locator strategy


MobileElement elementOne = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
MobileElement elementTwo = (MobileElement) driver.findElementByClassName("SomeClassName");

OR

WebElement elementOne =  driver.findElementByAccessibilityId("SomeAccessibilityID");
WebElement elementTwo = driver.findElementByClassName("SomeClassName");

Find Elements:

Search for multiple elements

List<MobileElement> elementsOne = (List<MobileElement>) driver.findElementsByAccessibilityId("SomeAccessibilityID");
List<MobileElement> elementsTwo = (List<MobileElement>) driver.findElementsByClassName("SomeClassName");

OR

List<WebElement> elementsOne = driver.findElementsByAccessibilityId("SomeAccessibilityID");
List<WebElement> elementsTwo = driver.findElementsByClassName("SomeClassName");

Actions on Elements:

Click

Click element at its center point.

MobileElement el = driver.findElementByAccessibilityId("SomeId");
el.click();

driver.findElementByAccessibilityId("SomeId").click();

OR

WebElement el = driver.findElementByAccessibilityId("SomeId");
el.click();
driver.findElementByAccessibilityId("SomeId").click();

SendKeys:

Send a sequence of key strokes to an element

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
element.sendKeys("Hello world!");

OR

WebElement element = driver.findElementByAccessibilityId("SomeAccessibilityID");
element.sendKeys("Hello world!");

Clear:

Clear an element's value in Edit Box.

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
element.clear();

OR
WebElement element = driver.findElementByAccessibilityId("SomeAccessibilityID");
element.clear();

Get Element Text:

fetch the text of an Element

MobileElement element = (MobileElement) driver.findElementByClassName("SomeClassName");
String elText = element.getText();

Get TagName :

Get an element's tag name

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
String tagName = element.getTagName();
ORString tagName =driver.findElementByAccessibilityId("SomeAccessibilityID").getTagName();

Get Attribute Value:

Get the value of an element's attribute

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
String value = element.getAttribute("content-desc");
ORString value = driver.findElementByAccessibilityId("SomeAccessibilityID");

Is Element Selected:

Determine if a form or form-like element (checkbox, select, etc...) is selected or not.

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
boolean isSelected = element.isSelected();
ORboolean isSelected =  driver.findElementByAccessibilityId("SomeAccessibilityID").isSelected();
Is Element Enabled

Determine if an element is currently enabled or not

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
boolean isEnabled = element.isEnabled();
ORboolean isEnabled = driver.findElementByAccessibilityId("SomeAccessibilityID").isEnabled();
Is Element Displayed

Determine if an element is currently displayed or not

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
boolean isDisplayed = element.isDisplayed();
ORboolean isDisplayed = driver.findElementByAccessibilityId("SomeAccessibilityID").isDisplayed();
Get Element Location:

Determine an element's location on the page or screen.

The point (0, 0) refers to the upper-left corner of the page. The element's coordinates are returned as a JSON object with x and y properties

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
Point location = element.getLocation();

Get Element Size:

Determine an element's size in pixels

The size will be returned as an object with width and height properties.

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
Dimension elementSize = element.getSize();

Get Element Rect:

Gets dimensions and coordinates of an element

MobileElement element = (MobileElement) driver.findElementByAccessibilityId("SomeAccessibilityID");
Rectangle rect = element.getRect();

Get Css value:

The CSS property to query should be specified using the CSS property name, not the JavaScript property name (e.g. background-color instead of backgroundColor).

MobileElement element = (MobileElement) driver.findElementById("SomeId");
String cssProperty = element.getCssValue("style");

Submit Form:

The submit command may also be applied to any element that is a descendant of a FORM element (Web only).

MobileElement element = (MobileElement) driver.findElementByClassName("SomeClassName");
element.submit();

Get Active Element:

Gets the active element of the current session

WebElement currentElement = driver.switchTo().activeElement();

Elements Equal or not:

Test if two element IDs refer to the same element

// Overrides the Java Object .equals method
MobileElement elementOne = (MobileElement) driver.findElementByClassName("SomeClassName");
MobileElement elementTwo = (MobileElement) driver.findElementByClassName("SomeOtherClassName");
boolean isEqual = elementOne.equals(elementTwo);

APPIUM DEVICE COMMANDS

 Activity:

Start Activity:

Start an Android activity by providing package name and activity name

Sytax:
driver.startActivity(new Activity("com.example", "ActivityName"));

Get Current Activity:

Get the name of the current Android activity

Syntax:

String activity = driver.currentActivity();

Get Current Packkage :

Get the name of the current Android package

String package = driver.getCurrentPackage();


APP:

Install App:

Install the given app onto the device

driver.installApp("/Users/johndoe/path/to/app.apk");

Is App Installed:

Check whether the specified app is installed on the device

driver.isAppInstalled("com.example.AppName");

Launch App:

Launch the app-under-test on the device

driver.launchApp();

BackGround App:

Send the currently running app for this session to the background

driver.runAppInBackground(Duration.ofSeconds(10));

Close App:

Close an app on device

driver.closeApp();


Reset App:

Reset the currently running app for this session

driver.resetApp();

Remove App:

Remove an app from the device

driver.removeApp("com.example.AppName");

Activiate App:

Activate the given app onto the device

For Android:
driver.activateApp('com.apple.Preferences');
For IOS: driver.activateApp('io.appium.android.apis');

Terminate App:

Terminate the given app on the device

For Android:driver.terminateApp('com.apple.Preferences');For IOS:
driver.terminateApp('io.appium.android.apis');

Get App State:

Get the given app status on the device

For Android:driver.queryAppState('com.apple.Preferences');For IOS:
driver.queryAppState('io.appium.android.apis');

Get app strings

Map<String, String> appStrings = driver.getAppStringMap("en", "/path/to/file");

End Test Coverage:

Get test coverage data

driver.endTestCoverage("Intent", "/path");

Get Clipboard:

Get the content of the system clipboard

driver.getClipboard(ClipboardContentType.PLAINTEXT); // get plaintext
driver.getClipboardText();

Set Clipboard:

Set the content of the system clipboard

// base64Content is Base64-encoded content
driver.setClipboard("label", ClipboardContentType.PLAINTEXT, base64Content);
driver.setClipboardText("happy testing");

Emulate Power State

Emulate power state change on the connected emulator.

driver.setPowerAC(PowerACState.OFF);

Emulate power capacity:

Emulate power capacity change on the connected emulator.

driver.setPowerCapacity(100);

Store Files on Device:

Push File:

Place a file onto the device in a particular place

driver.pushFile("/data/local/tmp/foo.bar", new File("/Users/johndoe/files/foo.bar"));

Pull File:

Retrieve a file from the device's file system

byte[] fileBase64 = driver.pullFile("/path/to/device/foo.bar");

Pull Folder:

Retrieve a folder from the device's file system

byte[] folder = driver.pullFolder("/path/to/device/foo.bar");

Shake:

Perform a shake action on the device

driver.shake();

Lock:

Lock the device

driver.lockDevice();

Unlock:

Unlock the device

driver.unlockDevice();

Is Device Locked:

Check whether the device is locked or not

boolean isLocked = driver.isDeviceLocked();

Rotate:

Rotate the device in three dimensions

driver.rotate(new DeviceRotation(10, 10, 10));

Press keycode

Press a particular key on an Android Device

driver.pressKeyCode(AndroidKeyCode.SPACE, AndroidKeyMetastate.META_SHIFT_ON);

See https://developer.android.com/reference/android/view/KeyEvent.html for reference of available Android key code values


Long Press KeyCode:

Press and hold a particular key code on an Android device

driver.longPressKeyCode(AndroidKeyCode.HOME);

Hide keyboard:

Hide soft keyboard

driver.hideKeyboard();


Is Keyboard Shown:
Check whether soft keyboard is shown or not

boolean isKeyboardShown = driver.isKeyboardShown();

NetWork Commands:

Toggle Airplane Mode:

Toggle airplane mode on device

driver.toggleAirplaneMode();

Toggle Data:

Switch the state of data service

driver.toggleData();
Toggle Wifi:

Switch the state of the WiFi service

driver.toggleWifi();

Toggle Location service:

Switch the state of the location service

driver.toggleLocationServices();

Send SMS:

Simulate an SMS message (Emulator only)

driver.sendSMS("555-123-4567", "Hey lol");

GSM Call:

Make GSM call (Emulator only)

driver.makeGsmCall("5551234567", GsmCallActions.CALL);

GSM Signal:

Set GSM signal strength (Emulator only)

driver.setGsmSignalStrength(GsmSignalStrength.GOOD);

GSM Voice:

Set GSM voice state (Emulator only)

driver.setGsmVoice(GsmVoiceState.HOME);

NetWork Speed:

Set network speed (Emulator only)

driver.setNetworkSpeed(NetworkSpeed.LTE);


Get Performance Data:

Returns the information of the system state which is supported to read as like cpu, memory, network traffic, and battery

List<List<Object>> performanceData = driver.getPerformanceData("my.app.package", "cpuinfo", 5);

Get performance Datatypes:

Returns the information types of the system state which is supported to read as like cpu, memory, network traffic, and battery

List<String> performanceTypes = driver.getSupportedPerformanceDataTypes();

Screen Recording:

Start recording screen

driver.startRecordingScreen();
driver.startRecordingScreen(new BaseStartScreenRecordingOptions(....));

Stop Recording screen:
driver.stopRecordingScreen();
driver.stopRecordingScreen(new BaseStopScreenRecordingOptions(....));

Perform Touch ID:

Simulate a touch id event (iOS Simulator only)

driver.performTouchID(false); // Simulates a failed touch
driver.performTouchID(true); // Simulates a passing touch

Toggle TouchID Enrollment:

Toggle the simulator being enrolled to accept touchId (iOS Simulator only)

driver.toggleTouchIDEnrollment(true);

System Commands:

Open Notification

Open Android notifications (Emulator only)

driver.openNotifications();

Get System Bars:

Retrieve visibility and bounds information of the status and navigation bars

Map<String, String> systemBars = driver.getSystemBars();

Get Display Density:

Retrieve display density(dpi) of the Android device

driver.getDeviceDensity();

Authentication:
Finger Print:

Authenticate users by using their finger print scans on supported emulators.

driver.fingerPrint(1);

For Android emulator. Authenticate users by using their finger print scans.

Touch ID by iOS is here.




































The water jug puzzle

 

'image.png' failed to upload. TransportError: There was an error during the transport or processing of this request. Error code = 103, Path = /_/BloggerUi/data/batchexecute
water pouring puzzle’.

You have an 8 litre jug full of water and two smaller jugs, one that contains 5 litres and the other 3 litres. None of the jugs have markings on them, nor do you have any additional measuring device.You have to divide the 8 litres of water equally between your two best friends, so that each gets 4 litres of water. How can you do this?

Wait! Try and solve this puzzle before reading on.

The water jug puzzle – solution

  • First, water is poured from the 8 litre jug into the 5 litre jug, leaving 3 litres of water in the original 8 litre jug.
  • Next, water is poured from the 5 litre jug into the 3 litre jug, so we now have 3 litres of water in the 8 litre jug, 2 litres of water in the 5 litre jug and 3 litres of water in the 3 litre jug.
  • The 3 litre jug is emptied into the 8 litre jug, so the 8 litre jug now contains 6 litres of water.
  • The 2 litres of water in the 5 litre jug are now poured into the empty 3 litre jug.
  • Water is poured from the 8 litre jug (which at this stage contains 6 litres) into the empty 5 litre jug. We now have 5 litres of water in the 5 litre jug, 2 litres of water in the 3 litre jug and 1 litre of water in the 8 litre jug.
  • Water is poured from the 5 litre jug to fill the 3 litre jug which already contains at this stage 2 litres of water.
  • We are left with 4 litres of water in the 5 litre jug which is given to one friend, and 3 litres of water in the 3 litre jug that is poured back into the 8 litre jug that already contains 1 litre of water. This gives 4 litres of water which are given to the second friend.
The whole scenario can be summarised using numbers in brackets to denote the litres of water at each stage in each of the 8 litre, 5 litre and 3 litre jugs, respectively: