Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #381 from sbtqa/appium2
Browse files Browse the repository at this point in the history
Appium2
  • Loading branch information
kosteman authored Sep 30, 2022
2 parents 0b0949e + 64d68cd commit 423c8f1
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ru.sbtqa.tag.pagefactory.environment.Environment;
import ru.sbtqa.tag.pagefactory.properties.Configuration;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -104,7 +105,7 @@ public static void visibility(WebElement element, String message) {
* @param timeout condition timeout in seconds
*/
public static void visibility(WebElement element, String message, int timeout) {
new WebDriverWait(Environment.getDriverService().getDriver(), timeout)
new WebDriverWait(Environment.getDriverService().getDriver(), Duration.ofSeconds(timeout))
.ignoring(StaleElementReferenceException.class)
.withMessage(message).until(ExpectedConditions.visibilityOf(element));
}
Expand Down Expand Up @@ -642,7 +643,7 @@ public static void absence(WebElement element, String message, int timeout) {
* @param message message in case verification failed
*/
public static void wait(ExpectedCondition condition, String message) {
new WebDriverWait(Environment.getDriverService().getDriver(), PROPERTIES.getTimeout())
new WebDriverWait(Environment.getDriverService().getDriver(), Duration.ofSeconds(PROPERTIES.getTimeout()))
.withMessage(message).until(condition);
}

Expand All @@ -655,7 +656,7 @@ public static void wait(ExpectedCondition condition, String message) {
* @param timeout condition timeout in seconds
*/
public static void wait(ExpectedCondition condition, String message, int timeout) {
new WebDriverWait(Environment.getDriverService().getDriver(), timeout)
new WebDriverWait(Environment.getDriverService().getDriver(), Duration.ofSeconds(timeout))
.withMessage(message).until(condition);
}
}
10 changes: 2 additions & 8 deletions plugins/html-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,10 @@
<type>jar</type>
</dependency>
<dependency>
<groupId>ru.yandex.qatools.htmlelements</groupId>
<groupId>ru.sbtqa.htmlelements</groupId>
<artifactId>htmlelements-java</artifactId>
<version>1.20.0</version>
<version>1.21.4</version>
<type>jar</type>
<exclusions>
<exclusion>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</exclusion>
</exclusions>
</dependency>

<!--test-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ page.package=ru.sbtqa.tag.pagefactory.pages
screenshot.strategy=driver

webdriver.browser.name=Chrome
webdriver.version = 104.0.5112.29
webdriver.version = 106.0.5249.21
webdriver.starting.url=http://localhost:8181

# run chrome in headless mode
Expand Down
8 changes: 1 addition & 7 deletions plugins/mobile-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,7 @@
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.0.0</version>
<exclusions>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
</exclusion>
</exclusions>
<version>8.2.0</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package ru.sbtqa.tag.pagefactory.mobile;

import io.appium.java_client.MobileDriver;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.support.PageFactory;
import ru.sbtqa.tag.pagefactory.Page;
Expand All @@ -18,7 +18,7 @@ public abstract class MobilePage implements Page {
public MobilePage() {
MobileSetupSteps.initMobile();

MobileDriver driver = Environment.getDriverService().getDriver();
AppiumDriver driver = Environment.getDriverService().getDriver();
PageFactory.initElements(new AppiumFieldDecorator(driver), this);

Environment.setPageActions(new MobilePageActions());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
package ru.sbtqa.tag.pagefactory.mobile.actions;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import ru.sbtqa.tag.pagefactory.actions.PageActions;
import ru.sbtqa.tag.pagefactory.environment.Environment;
import ru.sbtqa.tag.pagefactory.mobile.properties.MobileConfiguration;
import ru.sbtqa.tag.pagefactory.utils.Wait;

import static ru.sbtqa.tag.pagefactory.mobile.utils.PlatformName.IOS;

public class MobilePageActions implements PageActions {

private static final MobileConfiguration PROPERTIES = MobileConfiguration.create();

@Override
public void fill(Object element, String text) {
WebElement webElement = (WebElement) element;
webElement.click();

if (PROPERTIES.getAppiumPlatformName() == IOS) {
webElement.sendKeys(text);
} else {
AppiumDriver driver = Environment.getDriverService().getDriver();
driver.getKeyboard().sendKeys(text);
}
webElement.sendKeys(text);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.remote.IOSMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.remote.MobilePlatform;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,9 +22,6 @@
import java.net.MalformedURLException;
import java.net.URL;

import static ru.sbtqa.tag.pagefactory.mobile.utils.PlatformName.ANDROID;
import static ru.sbtqa.tag.pagefactory.mobile.utils.PlatformName.IOS;

public class MobileDriverService implements DriverService {

private static final Logger LOG = LoggerFactory.getLogger(MobileDriverService.class);
Expand All @@ -35,39 +33,38 @@ public class MobileDriverService implements DriverService {
@Override
public void mountDriver() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, PROPERTIES.getAppiumPlatformName());
capabilities.setCapability(MobileCapabilityType.APP, PROPERTIES.getAppiumApp());
capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, PROPERTIES.getAppiumVersion());
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, PROPERTIES.getAppiumDeviceName());
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, PROPERTIES.getAppiumPlatformVersion());
capabilities.setCapability(MobileCapabilityType.BROWSER_NAME, PROPERTIES.getAppiumBrowserName());
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, PROPERTIES.getAppiumAutomationName());
capabilities.setCapability(MobileCapabilityType.UDID, PROPERTIES.getAppiumUdid());
capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, PROPERTIES.getNewCommandTimeout());
capabilities.setCapability(MobileCapabilityType.FULL_RESET, PROPERTIES.getAppiumFullReset());
capabilities.setCapability(MobileCapabilityType.NO_RESET, PROPERTIES.getAppiumNoReset());

capabilities.setCapability(IOSMobileCapabilityType.AUTO_ACCEPT_ALERTS, PROPERTIES.getAppiumAlertsAutoAccept());

capabilities.setCapability("bundleId", PROPERTIES.getAppiumBundleId());
capabilities.setCapability("appPackage", PROPERTIES.getAppiumAppPackage());
capabilities.setCapability("appActivity", PROPERTIES.getAppiumAppActivity());
capabilities.setCapability("permissions", PROPERTIES.getAppiumPermissions());
capabilities.setCapability("autoGrantPermissions", PROPERTIES.getAppiumAutoGrantPermissions());
capabilities.setCapability("unicodeKeyboard", PROPERTIES.getAppiumKeyboardUnicode());
capabilities.setCapability("resetKeyboard", PROPERTIES.getAppiumKeyboardReset());
capabilities.setCapability("connectHardwareKeyboard", false);
capabilities.setCapability("xcodeOrgId", PROPERTIES.getAppiumXcodeOrgId());
capabilities.setCapability("xcodeSigningId", PROPERTIES.getAppiumXcodeSigningId());
capabilities.setCapability("showIOSLog", PROPERTIES.getAppiumShowIOSLog());
capabilities.setCapability("appium:disableWindowAnimation", PROPERTIES.getDisableWindowAnimation());
capabilities.setCapability("appium:useJSONSource", PROPERTIES.getAppiumUseJSONSource());
capabilities.setCapability("appium:simpleIsVisibleCheck", PROPERTIES.getAppiumSimpleIsVisibleCheck());
capabilities.setCapability("appium:useNewWDA", PROPERTIES.getAppiumUseNewWDA());
capabilities.setCapability("appium:usePrebuiltWDA", PROPERTIES.getAppiumUsePrebuiltWDA());
capabilities.setCapability("appium:derivedDataPath", PROPERTIES.getAppiumDerivedDataPath());

if (PROPERTIES.getAppiumPlatformName() == ANDROID) {
setCapability(capabilities, MobileCapabilityType.PLATFORM_NAME, PROPERTIES.getAppiumPlatformName());
setCapability(capabilities, MobileCapabilityType.APP, PROPERTIES.getAppiumApp());
setCapability(capabilities, MobileCapabilityType.DEVICE_NAME, PROPERTIES.getAppiumDeviceName());
setCapability(capabilities, MobileCapabilityType.PLATFORM_VERSION, PROPERTIES.getAppiumPlatformVersion());
setCapability(capabilities, MobileCapabilityType.BROWSER_NAME, PROPERTIES.getAppiumBrowserName());
setCapability(capabilities, MobileCapabilityType.AUTOMATION_NAME, PROPERTIES.getAppiumAutomationName());
setCapability(capabilities, MobileCapabilityType.UDID, PROPERTIES.getAppiumUdid());
setCapability(capabilities, MobileCapabilityType.NEW_COMMAND_TIMEOUT, PROPERTIES.getNewCommandTimeout());
setCapability(capabilities, MobileCapabilityType.FULL_RESET, PROPERTIES.getAppiumFullReset());
setCapability(capabilities, MobileCapabilityType.NO_RESET, PROPERTIES.getAppiumNoReset());

setCapability(capabilities, IOSMobileCapabilityType.AUTO_ACCEPT_ALERTS, PROPERTIES.getAppiumAlertsAutoAccept());

setCapability(capabilities, "bundleId", PROPERTIES.getAppiumBundleId());
setCapability(capabilities, "appPackage", PROPERTIES.getAppiumAppPackage());
setCapability(capabilities, "appActivity", PROPERTIES.getAppiumAppActivity());
setCapability(capabilities, "permissions", PROPERTIES.getAppiumPermissions());
setCapability(capabilities, "autoGrantPermissions", PROPERTIES.getAppiumAutoGrantPermissions());
setCapability(capabilities, "unicodeKeyboard", PROPERTIES.getAppiumKeyboardUnicode());
setCapability(capabilities, "resetKeyboard", PROPERTIES.getAppiumKeyboardReset());
capabilities.setCapability( "connectHardwareKeyboard", false);
setCapability(capabilities, "xcodeOrgId", PROPERTIES.getAppiumXcodeOrgId());
setCapability(capabilities, "xcodeSigningId", PROPERTIES.getAppiumXcodeSigningId());
setCapability(capabilities, "showIOSLog", PROPERTIES.getAppiumShowIOSLog());
capabilities.setCapability( "appium:disableWindowAnimation", PROPERTIES.getDisableWindowAnimation());
setCapability(capabilities, "appium:useJSONSource", PROPERTIES.getAppiumUseJSONSource());
setCapability(capabilities, "appium:simpleIsVisibleCheck", PROPERTIES.getAppiumSimpleIsVisibleCheck());
setCapability(capabilities, "appium:useNewWDA", PROPERTIES.getAppiumUseNewWDA());
setCapability(capabilities, "appium:usePrebuiltWDA", PROPERTIES.getAppiumUsePrebuiltWDA());
setCapability(capabilities, "appium:derivedDataPath", PROPERTIES.getAppiumDerivedDataPath());

if (PROPERTIES.getAppiumPlatformName().equals("ANDROID")) {
capabilities.merge(new SelenoidCapabilitiesParser().parse());
}

Expand All @@ -80,9 +77,9 @@ public void mountDriver() {
throw new FactoryRuntimeException("Could not parse appium url. Check 'appium.url' property", e);
}

mobileDriver = PROPERTIES.getAppiumPlatformName() == IOS ? new IOSDriver(url, capabilities) : new AndroidDriver(url, capabilities);
mobileDriver = PROPERTIES.getAppiumPlatformName().equals("IOS") ? new IOSDriver(url, capabilities) : new AndroidDriver(url, capabilities);

if (PROPERTIES.getAppiumPlatformName() == ANDROID) {
if (PROPERTIES.getAppiumPlatformName().equals("ANDROID")) {
((AndroidDriver) mobileDriver).setSetting(Setting.WAIT_FOR_IDLE_TIMEOUT, PROPERTIES.getWaitForIdleTimeout());
}

Expand All @@ -92,6 +89,12 @@ public void mountDriver() {
}
}

private void setCapability(DesiredCapabilities capabilities, String name, String value) {
if (value != null && !value.equals("")) {
capabilities.setCapability(name, value);
}
}

@Override
public void demountDriver() {
if (isDriverEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ru.sbtqa.tag.pagefactory.mobile.junit;

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.ios.IOSTouchAction;
import io.appium.java_client.touch.offset.PointOption;
Expand Down Expand Up @@ -73,7 +72,7 @@ public T press(String elementTitle) throws PageException {
* @param elementTitle title of the element
*/
public T tap(String elementTitle) throws PageException {
MobileElement element = Environment.getFindUtils().getElementByTitle(PageContext.getCurrentPage(), elementTitle);
WebElement element = Environment.getFindUtils().getElementByTitle(PageContext.getCurrentPage(), elementTitle);
tap(element);
return (T) this;
}
Expand All @@ -83,7 +82,7 @@ public T tap(String elementTitle) throws PageException {
*
* @param element element
*/
public T tap(MobileElement element) {
public T tap(WebElement element) {
new IOSTouchAction(Environment.getDriverService().getDriver())
.press(iosPressOptions()
.withElement(element(element))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public interface MobileConfiguration extends Configuration {

@Key("appium.device.platform.name")
@DefaultValue("IOS")
PlatformName getAppiumPlatformName();
String getAppiumPlatformName();

@Key("appium.fullReset")
@DefaultValue("")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void startRecord() {
return;
}

if (PROPERTIES.getAppiumPlatformName() == PlatformName.IOS) {
if (PROPERTIES.getAppiumPlatformName().equals("IOS")) {
IOSStartScreenRecordingOptions startOptions = new IOSStartScreenRecordingOptions()
.withVideoType(PROPERTIES.getAppiumVideoType())
.withVideoScale(PROPERTIES.getAppiumVideoScale())
Expand Down Expand Up @@ -71,7 +71,7 @@ public byte[] stopRecord() {
}

// get Base64 encoded video content
String encodedString = PROPERTIES.getAppiumPlatformName() == PlatformName.IOS
String encodedString = PROPERTIES.getAppiumPlatformName().equals("IOS")
? ((IOSDriver) Environment.getDriverService().getDriver()).stopRecordingScreen()
: ((AndroidDriver) Environment.getDriverService().getDriver()).stopRecordingScreen();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.offset.PointOption;
import java.util.List;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;

import org.openqa.selenium.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ru.sbtqa.tag.pagefactory.environment.Environment;
Expand Down Expand Up @@ -121,7 +119,7 @@ private static void swipe(Point location, Dimension size, DirectionStrategy dire
int x = location.getX();
int y = location.getY();
LOG.debug("Swipe parameters: location {}, dimension {}, direction {}, time {}", location, size, direction, time);
new TouchAction(appiumDriver)
new TouchAction(Environment.getDriverService().getDriver())
.press(PointOption.point(x + startx, y + starty))
.waitAction()
.moveTo(PointOption.point(x + endx, y + endy))
Expand Down Expand Up @@ -165,11 +163,11 @@ public static void swipeToText(DirectionStrategy direction, String text, MatchSt
for (int depthCounter = 0; depthCounter < depth; depthCounter++) {
String oldPageSource = appiumDriver.getPageSource();
if (strategy == MatchStrategy.EXACT) {
if (!appiumDriver.findElementsByXPath("//*[@text='" + text + "']").isEmpty()) {
if (!appiumDriver.findElements(By.xpath("//*[@text='" + text + "']")).isEmpty()) {
return;
}
} else {
List<WebElement> textViews = appiumDriver.findElementsByClassName("android.widget.TextView");
List<WebElement> textViews = appiumDriver.findElements(By.className("android.widget.TextView"));
if (!textViews.isEmpty()) {
for (WebElement textView : textViews) {
if (textView.getText().contains(text)) {
Expand Down
5 changes: 5 additions & 0 deletions plugins/web-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@
<artifactId>webdrivermanager</artifactId>
<version>${tag.version.webdrivermanager}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class WebDriverCapabilitiesParser implements CapabilitiesParser {

Expand Down Expand Up @@ -133,4 +134,11 @@ private void setCapability(String capabilityName, String capabilityValue) {
private boolean isBoolean(String string) {
return "true".equalsIgnoreCase(string) || "false".equalsIgnoreCase(string);
}

public List<String> getOptions(DesiredCapabilities capabilities) {
return Arrays.stream(capabilities.getCapability("options.args").toString()
.split(","))
.map(arg -> "--" + arg)
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
import io.github.bonigarcia.wdm.ChromeDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import ru.sbtqa.tag.pagefactory.web.capabilities.WebDriverCapabilitiesParser;
import ru.sbtqa.tag.pagefactory.web.configure.WebDriverManagerConfigurator;
import ru.sbtqa.tag.pagefactory.web.support.BrowserName;

import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class CreatedChromeDriver implements Supplier<WebDriver> {

Expand All @@ -20,6 +25,9 @@ public CreatedChromeDriver(final DesiredCapabilities capabilities) {
@Override
public WebDriver get() {
WebDriverManagerConfigurator.configureDriver(ChromeDriverManager.getInstance(), BrowserName.CHROME.getName());
return new ChromeDriver(capabilities);
ChromeOptions options = new ChromeOptions().merge(capabilities);
options.addArguments(new WebDriverCapabilitiesParser().getOptions(capabilities));

return new ChromeDriver(options);
}
}
Loading

0 comments on commit 423c8f1

Please sign in to comment.