Automation QA Testing Course Content

Appium -Web Commands

 Switch to Widow:

Change focus to another window (Web context only)

driver.switchTo().window("handle");

Close the Window

Close the current window (Web context only)

driver.close();

Get Window Handle:

Retrieve the current window handle (Web context only)

String windowHandle = driver.getWindowHandle();

Get Window Handles:

Retrieve the list of all window handles available to the session (Web context only)

Set<String> windowHandles = driver.getWindowHandles();

Get Title:

Get the current page title (Web context only)

String title = driver.getTitle();

Get Window Size:

Get the size of the specified window (Web context only)

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

All Appium Web Commands are Same As Selenium WebDriver Commands.


















Appium- Actions

Mouse Actions: 

Mouse Move To:

Move the mouse by an offset of the specificed element

If no element is specified, the move is relative to the current mouse cursor. If an element is provided but no offset, the mouse will be moved to the center of the element. If the element is not visible, it will be scrolled into view.

Actions action = new Actions(driver);
action.moveTo(element, 10, 10);
action.perform();

Click Action:

Click any mouse button at the current mouse coordinates

Actions action = new Actions(driver);
action.moveTo(element);
action.click();
action.perform();

Double Click Action:

Double-clicks at the current mouse coordinates (set by moveto).

Actions action = new Actions(driver);
action.moveTo(element);
action.doubleClick();
action.perform();

Button Down Action:

Click and hold the left mouse button at the current mouse coordinates.

Note that the next mouse-related command that should follow is buttonup . Any other mouse command (such as click or another call to buttondown) will yield undefined behavior


Actions action = new Actions(driver);
action.moveTo(element);
action.clickAndHold();
action.perform();

Button Up Action

Releases the mouse button previously held

Must be called once for every buttondown command issued. See the note in click and buttondown about implications of out-of-order commands.

Actions action = new Actions(driver);
action.moveTo(element);
action.clickAndHold();
action.moveTo(element, 10, 10);
action.release();
action.perform();

Touch Action

Tap

Single tap on the touch enabled device

TouchActions action = new TouchActions(driver);
action.singleTap(element);
action.perform();

Double Action

Double tap on the touch screen using finger motion events

TouchActions action = new TouchActions(driver);
action.doubleTap(element);
action.perform();

Move To:

Finger move on the screen

As of Appium 1.8.0 all move actions take coordinates that are absolute.

TouchActions action = new TouchActions(driver);
action.down(10, 10);
action.moveTo(50, 50);
action.perform();

Touch Down:

Finger down on the screen

TouchActions action = new TouchActions(driver);
action.down(10, 10);
action.move(50, 50);
action.perform();

Touch Up:

Finger up on the screen

TouchActions action = new TouchActions(driver);
action.down(10, 10);
action.up(20, 20);
action.perform();

Long Press

Long press on the touch screen using finger motion events

TouchActions action = new TouchActions(driver);
action.longPress(element);
action.perform();

Scroll:

Scroll on the touch screen using finger based motion events

TouchActions action = new TouchActions(driver);
action.scroll(element, 10, 100);
action.perform();

OR//scroll for the option
 driver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector()).scrollIntoView(text(\"webview\"));");
		
		

Flick

Flick on the touch screen using finger motion events

TouchActions action = new TouchActions(driver);
action.flick(element, 1, 10, 10);
action.perform();

Perform Multi Touch Action

Perform a multi touch action sequence

TouchActions actionOne = new TouchAction();
actionOne.press(10, 10);
actionOne.moveTo(10, 100);
actionOne.release();
TouchActions actionTwo = new TouchAction();
actionTwo.press(20, 20);
actionTwo.moveTo(20, 200);
actionTwo.release();
MultiTouchAction action = new MultiTouchAction();
action.add(actionOne);
action.add(actionTwo);
action.perform();

Touch Action 

Perform a touch action sequence

TouchAction action = new TouchAction(driver);
action.press(10, 10);
action.moveTo(10, 100);
action.release();
action.perform();

Perform a chain or multiple chains of keyboard and pointer (touch, mouse, stylus) actions

  • input source: Represents an input device (pointer or key) that a series of actions are dispatched to. The input source has a unique ID.
  • action: An action that is dispatched to an input source. For a keyboard source, this can be 'keyDown' or 'keyUp'. For a pointer event this can be 'pointerMove', 'pointerDown', or 'pointerUp'. 'pause' events can also be sent to the device.

The Actions API takes a list of input sources and executes each 'tick'. A 'tick' is a slice of an action chain, so if you have two input sources, the first 'tick' is the 0-indexed action, the second 'tick' is the 1-indexed action, etc.... All of the actions per tick are executed concurrently.


WebElement source = (MobileElement) driver.findElementsByAccessibilityId("SomeAccessibilityID");
WebElement target = (MobileElement) driver.findElementsByAccessibilityId("SomeOtherAccessibilityID");

Point source = dragMe.getCenter();
Point target = driver.findElementByAccessibilityId("dropzone").getCenter();
PointerInput finger = new PointerInput(PointerInput.Kind.TOUCH, "finger");
Sequence dragNDrop = new Sequence(finger, 1);
dragNDrop.addAction(finger.createPointerMove(Duration.ofMillis(0),
                    PointerInput.Origin.viewport(), source.x, source.y));
dragNDrop.addAction(finger.createPointerDown(PointerInput.MouseButton.LEFT.asArg()));
dragNDrop.addAction(finger.createPointerMove(Duration.ofMillis(700),
                    PointerInput.Origin.viewport(),target.x, target.y));
dragNDrop.addAction(finger.createPointerUp(PointerInput.MouseButton.LEFT.asArg()));
driver.perform(Arrays.asList(dragNDrop));






Appium- Mobile Context

 Get Current Context:

Get the current context in which Appium is running

Retrieve the current context. This can be either NATIVE_APP for the native context, or a web view context, which will be:

  • iOS - WEBVIEW_<id>
  • Android - WEBVIEW_<package name>

For information on contexts, see Appium's hybrid automation docs.

String context = driver.getContext();

Get All Contexts:

Get all the contexts available to automate

Retrieve all the contexts available to be automated. This will include, at least, the native context. There can also be zero or more web view contexts. For information on the format of the context names, see the get context documentationmobile command mobile: getContexts is available on iOS (XCUITest) and Android (UIAutomator2 and Espresso) to get more detailed contexts. For information on contexts, see Appium's hybrid automation docs.

Set<String> contextNames = driver.getContextHandles();

Set Current Context:

Set the context being automated

Set the current context to that passed in. If this is moving into a web view context it will involve attempting to connect to that web view:

  • iOS - attempt to connect to the application through the remote debugger
  • Android - start a Chromedriver process and begin a session to connect to the web view

For information on contexts, see Appium's hybrid automation docs.

Set<String> contextNames = driver.getContextHandles();
driver.context(contextNames.toArray()[1]);
// ...
driver.context("NATIVE_APP");





















































































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);