Skip to content

Commit

Permalink
Add support for the Appium MobileElement object
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardesco committed Nov 21, 2017
1 parent 769daa6 commit 6e18ae6
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 47 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ It's designed to return certain element types that you can use in your page obje
WebElement element = query.findWebElement();
List<WebElement> elementList = query.findWebElements();
Select selectElement = query.findSelectElement();
MobileElement mobileElement = query.findMobileElement();
List<MobileElement> mobileElementList = query.findMobileElements();

Ok, that's kind of useful, anything else?

Expand Down
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<!--Dependency Versions-->
<selenium.version>3.7.1</selenium.version>
<assertj-core.version>3.8.0</assertj-core.version>
<java-client.version>5.0.4</java-client.version>
<junit.version>4.12</junit.version>
<!--Plugin Versions-->
<jacoco-maven-plugin.version>0.7.9</jacoco-maven-plugin.version>
Expand Down Expand Up @@ -85,6 +86,11 @@
<artifactId>selenium-java</artifactId>
<version>${selenium.version}</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>${java-client.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
48 changes: 35 additions & 13 deletions src/main/java/com/lazerycode/selenium/util/Query.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.lazerycode.selenium.util;

import io.appium.java_client.MobileElement;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.Select;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class Query {

private static RemoteWebDriver driver;
private static String currentBrowserName;
private static boolean isAppium;

/**
* Set a static driver object that wil be used for all instances of Query
Expand All @@ -22,6 +25,8 @@ public static void initQueryObjects(RemoteWebDriver driverObject) {
driver = driverObject;
if (null != driver) {
currentBrowserName = driver.getCapabilities().getBrowserName();
Object automationName = driver.getCapabilities().getCapability("automationName");
isAppium = (null != automationName) && automationName.toString().toLowerCase().equals("appium");
}
}

Expand Down Expand Up @@ -60,12 +65,19 @@ public void addAlternateLocator(String browser, By locator) {
* @return WebElement
*/
public WebElement findWebElement() {
checkDriverIsSet();
if (customLocators.containsKey(currentBrowserName)) {
return driver.findElement(customLocators.get(currentBrowserName));
}
return driver.findElement(locator());
}

return driver.findElement(defaultLocator);
/**
* This will return a MobileElement object if the supplied locator could find a valid MobileElement.
*
* @return MobileElement
*/
public MobileElement findMobileElement() {
if (isAppium) {
return (MobileElement) driver.findElement(locator());
}
throw new UnsupportedOperationException("You don't seem to be using Appium!");
}

/**
Expand All @@ -74,11 +86,24 @@ public WebElement findWebElement() {
* @return List&lt;WebElement>&gt;
*/
public List<WebElement> findWebElements() {
checkDriverIsSet();
if (customLocators.containsKey(currentBrowserName)) {
return driver.findElements(customLocators.get(currentBrowserName));
return driver.findElements(locator());
}

/**
* This will return a list of MobileElement objects, it may be empty if the supplied locator does not match any elements on screen
*
* @return List&lt;MobileElement>&gt;
*/
public List<MobileElement> findMobileElements() {
if (isAppium) {
List<WebElement> elementsFound = driver.findElements(locator());
List<MobileElement> mobileElementsToReturn = new ArrayList<>();
for (WebElement element : elementsFound) {
mobileElementsToReturn.add((MobileElement) element);
}
return mobileElementsToReturn;
}
return driver.findElements(defaultLocator);
throw new UnsupportedOperationException("You don't seem to be using Appium!");
}

/**
Expand All @@ -98,10 +123,7 @@ public Select findSelectElement() {
*/
public By locator() {
checkDriverIsSet();
if (customLocators.containsKey(currentBrowserName)) {
return customLocators.get(currentBrowserName);
}
return defaultLocator;
return customLocators.getOrDefault(currentBrowserName, defaultLocator);
}

private void checkDriverIsSet() {
Expand Down
73 changes: 39 additions & 34 deletions src/test/java/com/lazerycode/selenium/util/QueryTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.lazerycode.selenium.util;

import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobilePlatform;
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.BrowserType;
import org.openqa.selenium.remote.RemoteWebDriver;
Expand All @@ -22,9 +26,9 @@ public class QueryTest {
private static final By CHROME_LOCATOR = By.id("bar");
private static final By FIREFOX_LOCATOR = By.id("fire");
private static final WebElement MOCKED_WEB_ELEMENT_FOR_DEFAULT = mock(WebElement.class);
private static final WebElement MOCKED_WEB_ELEMENT_FOR_CHROME = mock(WebElement.class);
private static final MobileElement MOCKED_MOBILE_ELEMENT_FOR_DEFAULT = mock(MobileElement.class);
private static final List<WebElement> MOCKED_WEB_ELEMENT_LIST_FOR_DEFAULT = Collections.singletonList(MOCKED_WEB_ELEMENT_FOR_DEFAULT);
private static final List<WebElement> MOCKED_WEB_ELEMENT_LIST_FOR_CHROME = Collections.singletonList(MOCKED_WEB_ELEMENT_FOR_CHROME);
private static final List<WebElement> MOCKED_MOBILE_ELEMENT_LIST_FOR_DEFAULT = Collections.singletonList(MOCKED_MOBILE_ELEMENT_FOR_DEFAULT);

@After
public void teardown() {
Expand Down Expand Up @@ -101,7 +105,7 @@ public void returnsDefaultLocatorIfDifferentBrowserIsSet() {
}

@Test
public void returnsDefaultWebElement() {
public void returnsWebElement() {
initQueryObject();

Query query = new Query(DEFAULT_LOCATOR);
Expand All @@ -111,73 +115,74 @@ public void returnsDefaultWebElement() {
}

@Test
public void returnsChromeWebElementIfChromeLocatorSet() {
public void returnsWebElementList() {
initQueryObject();

Query query = new Query(DEFAULT_LOCATOR);
query.addAlternateLocator(BrowserType.GOOGLECHROME, CHROME_LOCATOR);
WebElement element = query.findWebElement();
List<WebElement> element = query.findWebElements();

assertThat(element).isNotEqualTo(MOCKED_WEB_ELEMENT_FOR_DEFAULT);
assertThat(element).isEqualTo(MOCKED_WEB_ELEMENT_FOR_CHROME);
assertThat(element).isEqualTo(MOCKED_WEB_ELEMENT_LIST_FOR_DEFAULT);
}

@Test
public void returnsDefaultWebElementIfDifferentBrowserIsSet() {
initQueryObject();
public void returnsMobileElement() {
initQueryObjectWithAppiumAndroid();

Query query = new Query(DEFAULT_LOCATOR);
query.addAlternateLocator(BrowserType.FIREFOX, FIREFOX_LOCATOR);
WebElement element = query.findWebElement();
MobileElement element = query.findMobileElement();

assertThat(element).isNotEqualTo(MOCKED_WEB_ELEMENT_FOR_CHROME);
assertThat(element).isEqualTo(MOCKED_WEB_ELEMENT_FOR_DEFAULT);
assertThat(element).isEqualTo(MOCKED_MOBILE_ELEMENT_FOR_DEFAULT);
}

@Test
public void returnsDefaultWebElementList() {
initQueryObject();
public void returnsMobileElementList() {
initQueryObjectWithAppiumAndroid();

Query query = new Query(DEFAULT_LOCATOR);
List<WebElement> element = query.findWebElements();
List<MobileElement> element = query.findMobileElements();

assertThat(element).isEqualTo(MOCKED_WEB_ELEMENT_LIST_FOR_DEFAULT);
assertThat(element).isEqualTo(MOCKED_MOBILE_ELEMENT_LIST_FOR_DEFAULT);
}

@Test
public void returnsChromeWebElementListIfChromeLocatorSet() {
@Test(expected = UnsupportedOperationException.class)
public void findMobileElementThrowsUnsupportedOperationExceptionIfPlatformIsNotAMobileType() {
initQueryObject();

Query query = new Query(DEFAULT_LOCATOR);
query.addAlternateLocator(BrowserType.GOOGLECHROME, CHROME_LOCATOR);
List<WebElement> element = query.findWebElements();

assertThat(element).isNotEqualTo(MOCKED_WEB_ELEMENT_LIST_FOR_DEFAULT);
assertThat(element).isEqualTo(MOCKED_WEB_ELEMENT_LIST_FOR_CHROME);
query.findMobileElement();
}

@Test
public void returnsDefaultWebElementListIfDifferentBrowserIsSet() {
@Test(expected = UnsupportedOperationException.class)
public void findMobileElementsThrowsUnsupportedOperationExceptionIfPlatformIsNotAMobileType() {
initQueryObject();

Query query = new Query(DEFAULT_LOCATOR);
query.addAlternateLocator(BrowserType.FIREFOX, FIREFOX_LOCATOR);
List<WebElement> element = query.findWebElements();

assertThat(element).isNotEqualTo(MOCKED_WEB_ELEMENT_LIST_FOR_CHROME);
assertThat(element).isEqualTo(MOCKED_WEB_ELEMENT_LIST_FOR_DEFAULT);
query.findMobileElements();
}

private void initQueryObject() {
Capabilities mockedCapabilities = mock(Capabilities.class);
when(mockedCapabilities.getBrowserName()).thenReturn(BrowserType.GOOGLECHROME);
when(mockedCapabilities.getPlatform()).thenReturn(Platform.YOSEMITE);

RemoteWebDriver mockedWebDriver = mock(RemoteWebDriver.class);
when(mockedWebDriver.getCapabilities()).thenReturn(mockedCapabilities);
when(mockedWebDriver.findElement(DEFAULT_LOCATOR)).thenReturn(MOCKED_WEB_ELEMENT_FOR_DEFAULT);
when(mockedWebDriver.findElement(CHROME_LOCATOR)).thenReturn(MOCKED_WEB_ELEMENT_FOR_CHROME);
when(mockedWebDriver.findElements(DEFAULT_LOCATOR)).thenReturn(MOCKED_WEB_ELEMENT_LIST_FOR_DEFAULT);
when(mockedWebDriver.findElements(CHROME_LOCATOR)).thenReturn(MOCKED_WEB_ELEMENT_LIST_FOR_CHROME);

Query.initQueryObjects(mockedWebDriver);
}

private void initQueryObjectWithAppiumAndroid() {
Capabilities mockedCapabilities = mock(Capabilities.class);
when(mockedCapabilities.getBrowserName()).thenReturn("");
when(mockedCapabilities.getPlatform()).thenReturn(Platform.fromString(MobilePlatform.ANDROID));
when(mockedCapabilities.getCapability("automationName")).thenReturn("Appium");

RemoteWebDriver mockedWebDriver = mock(AndroidDriver.class);
when(mockedWebDriver.getCapabilities()).thenReturn(mockedCapabilities);
when(mockedWebDriver.findElement(DEFAULT_LOCATOR)).thenReturn(MOCKED_MOBILE_ELEMENT_FOR_DEFAULT);
when(mockedWebDriver.findElements(DEFAULT_LOCATOR)).thenReturn(MOCKED_MOBILE_ELEMENT_LIST_FOR_DEFAULT);

Query.initQueryObjects(mockedWebDriver);
}
Expand Down

0 comments on commit 6e18ae6

Please sign in to comment.