Pages

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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.