diff --git a/README.md b/README.md index 36bc097..df8da66 100644 --- a/README.md +++ b/README.md @@ -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 elementList = query.findWebElements(); Select selectElement = query.findSelectElement(); + MobileElement mobileElement = query.findMobileElement(); + List mobileElementList = query.findMobileElements(); Ok, that's kind of useful, anything else? diff --git a/pom.xml b/pom.xml index 22122c3..8d49e70 100644 --- a/pom.xml +++ b/pom.xml @@ -57,6 +57,7 @@ 3.7.1 3.8.0 + 5.0.4 4.12 0.7.9 @@ -85,6 +86,11 @@ selenium-java ${selenium.version} + + io.appium + java-client + ${java-client.version} + junit junit diff --git a/src/main/java/com/lazerycode/selenium/util/Query.java b/src/main/java/com/lazerycode/selenium/util/Query.java index 304299b..dff957b 100644 --- a/src/main/java/com/lazerycode/selenium/util/Query.java +++ b/src/main/java/com/lazerycode/selenium/util/Query.java @@ -1,10 +1,12 @@ 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; @@ -12,6 +14,7 @@ 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 @@ -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"); } } @@ -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!"); } /** @@ -74,11 +86,24 @@ public WebElement findWebElement() { * @return List<WebElement>> */ public List 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<MobileElement>> + */ + public List findMobileElements() { + if (isAppium) { + List elementsFound = driver.findElements(locator()); + List 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!"); } /** @@ -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() { diff --git a/src/test/java/com/lazerycode/selenium/util/QueryTest.java b/src/test/java/com/lazerycode/selenium/util/QueryTest.java index 279281d..90f64e2 100644 --- a/src/test/java/com/lazerycode/selenium/util/QueryTest.java +++ b/src/test/java/com/lazerycode/selenium/util/QueryTest.java @@ -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; @@ -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 MOCKED_WEB_ELEMENT_LIST_FOR_DEFAULT = Collections.singletonList(MOCKED_WEB_ELEMENT_FOR_DEFAULT); - private static final List MOCKED_WEB_ELEMENT_LIST_FOR_CHROME = Collections.singletonList(MOCKED_WEB_ELEMENT_FOR_CHROME); + private static final List MOCKED_MOBILE_ELEMENT_LIST_FOR_DEFAULT = Collections.singletonList(MOCKED_MOBILE_ELEMENT_FOR_DEFAULT); @After public void teardown() { @@ -101,7 +105,7 @@ public void returnsDefaultLocatorIfDifferentBrowserIsSet() { } @Test - public void returnsDefaultWebElement() { + public void returnsWebElement() { initQueryObject(); Query query = new Query(DEFAULT_LOCATOR); @@ -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 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 element = query.findWebElements(); + List 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 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 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); }