Skip to content

Commit

Permalink
Fixed capability to associate drivers correctly with multiuser execut…
Browse files Browse the repository at this point in the history
…ion and other fixes (#20)

* removed the terminateApp() method for android in closeAppOnDevice() step to fix the timeout issue in the afterhooks

* removed the uploadFile method

* updated the log

* commented out uploadFile method. Not required

* added capability for retrieving the device name when running multiuser tests

* updated properties in RunnerTest

* use maven-publish plugin instead of maven (as required by gradle 7.x). Also removed unnecessary repositories

* added task required to publish artifact locally

* use specific versions of dependencies
also added an install wrapper task to maintain compatibility with gradle 6x

* removed unused import and removed the commmented code

* removed commented code

* remove commented lines

* bump version to v 0.0.62

Co-authored-by: shamaugale <shama.ugale@gmail.com>
  • Loading branch information
anandbagmar and ShamaUgale authored Jun 29, 2021
1 parent de7d1ae commit 9f40878
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 31 deletions.
19 changes: 15 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ plugins {
id "java"
id "idea"
id 'com.github.johnrengelman.shadow' version '7.0.0'
id 'maven-publish'
}

group = 'com.github.znsio'
version '0.0.61'
version '0.0.62'
project.ext.logDir = "${rootDir}/target"

repositories {
mavenLocal()
flatDir {
dirs 'libs'
}
maven { url "https://dl.bintray.com/epam/reportportal" }
mavenCentral()
maven { url 'https://jitpack.io' }
jcenter()
}

configurations {
Expand Down Expand Up @@ -79,4 +79,15 @@ artifacts {

wrapper {
gradleVersion = project.gradleVersion // version from gradle.properties
}
}

publishing {
publications {
testing(MavenPublication) {
from components.java
artifact sourcesJar
}
}
}

task install(dependsOn: publishToMavenLocal)
14 changes: 7 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
# The one and only place to declare versions
gradleVersion=7.1
org.gradle.daemon=false
assertJVersion=+
assertJVersion=3.20.2
atdVersion=1eba540877
applitoolsVersion=3.195.0
commonsLang3Version=+
unirestVersion=3+
jadbVersion=+
webDriverManagerVersion=4+
googleCodeJsonSimpleVersion=+
junitVersion=5+
commonsLang3Version=3.12.0
unirestVersion=3.11.11
jadbVersion=1.2.1
webDriverManagerVersion=4.4.3
googleCodeJsonSimpleVersion=1.1.1
junitVersion=5.7.2
slf4jVersion=1.7.30
5 changes: 5 additions & 0 deletions src/main/java/com/znsio/e2e/runner/Runner.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ public static Driver fetchDriver (long threadId) {
return allDrivers.getDriverForUser(userPersona);
}

public static String fetchDeviceName (long threadId, String forUserPersona) {
Drivers allDrivers = (Drivers) getTestExecutionContext(threadId).getTestState(TEST_CONTEXT.ALL_DRIVERS);
return allDrivers.getDeviceNameForUser(forUserPersona);
}

public static TestExecutionContext getTestExecutionContext (long threadId) {
return SessionContext.getTestExecutionContext(threadId);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/znsio/e2e/steps/RunCukes.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public RunCukes () {
@Override
@DataProvider(parallel = true)
public Object[][] scenarios () {
System.out.printf("ThreadID: %d: in overridden scenarios%n", Thread.currentThread().getId());
LOGGER.info(String.format("ThreadID: %d: in overridden scenarios%n", Thread.currentThread().getId()));
Object[][] scenarios = super.scenarios();
LOGGER.info(scenarios);
return scenarios;
Expand Down
31 changes: 26 additions & 5 deletions src/main/java/com/znsio/e2e/tools/Driver.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.collect.ImmutableMap;
import com.znsio.e2e.entities.Platform;
import com.znsio.e2e.exceptions.InvalidTestDataException;
import com.znsio.e2e.runner.Runner;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void scrollDownByScreenSize () {
int width = windowSize.width / 2;
int fromHeight = (int) (windowSize.height * 0.9);
int toHeight = (int) (windowSize.height * 0.5);
System.out.printf("width: %s, from height: %s, to height: %s", width, fromHeight, toHeight);
LOGGER.info(String.format("width: %s, from height: %s, to height: %s", width, fromHeight, toHeight));

TouchAction touchAction = new TouchAction(appiumDriver);
touchAction.press(PointOption.point(new Point(width, fromHeight)))
Expand All @@ -138,7 +139,7 @@ public void scrollVertically (int fromPercentScreenHeight, int toPercentScreenHe
int fromHeight = windowSize.height * (fromPercentScreenHeight / 100);
int toHeight = windowSize.height * (toPercentScreenHeight / 100);
LOGGER.info(String.format("width: %s, from height: %s, to height: %s", width, fromHeight, toHeight));
System.out.printf("width: %s, from height: %s, to height: %s", width, fromHeight, toHeight);
LOGGER.info(String.format("width: %s, from height: %s, to height: %s", width, fromHeight, toHeight));

TouchAction touchAction = new TouchAction(appiumDriver);
touchAction.press(PointOption.point(new Point(width, fromHeight)))
Expand All @@ -161,7 +162,7 @@ private void tapOnMiddleOfScreenOnDevice () {
Dimension screenSize = appiumDriver.manage().window().getSize();
int midHeight = screenSize.height / 2;
int midWidth = screenSize.width / 2;
System.out.printf("tapOnMiddleOfScreen: Screen dimensions: '%s'. Tapping on coordinates: %d:%d%n", screenSize.toString(), midWidth, midHeight);
LOGGER.info(String.format("tapOnMiddleOfScreen: Screen dimensions: '%s'. Tapping on coordinates: %d:%d%n", screenSize.toString(), midWidth, midHeight));
TouchAction touchAction = new TouchAction(appiumDriver);
touchAction.tap(PointOption.point(midWidth, midHeight)).perform();
waitFor(1);
Expand Down Expand Up @@ -220,8 +221,7 @@ public void swipeLeft () {
int height = getWindowHeight() / 2;
int fromWidth = (int) (getWindowWidth() * 0.9);
int toWidth = (int) (getWindowWidth() * 0.5);
System.out.printf("height: %s, from width: %s, to width: %s", height, fromWidth, toWidth);

LOGGER.info(String.format("height: %s, from width: %s, to width: %s", height, fromWidth, toWidth));
swipe(height, fromWidth, toWidth);
}

Expand Down Expand Up @@ -281,4 +281,25 @@ public void longPress (By elementId) {
.release()
.perform();
}

public void pushFileToDevice(String filePathToPush, String devicePath){
LOGGER.info("Pushing the file: '" + filePathToPush + "' to '" + Runner.platform.name() + "' device on path: '" + devicePath +"'");
try {
if(Runner.platform.equals(Platform.android)) {
((AndroidDriver) driver).pushFile(devicePath , new File(filePathToPush));
}else if(Runner.platform.equals(Platform.iOS)){
((IOSDriver) driver).pushFile(devicePath , new File(filePathToPush));
}
} catch (IOException e) {
throw new InvalidTestDataException("Error in pushing the file: '" + filePathToPush + "' to '" + Runner.platform.name() + "' device on path: '" + devicePath +"'", e);
}
}

public void allowPermission(By element){
waitForVisibilityOf(element);
if(Runner.platform.equals(Platform.android)) {
((AndroidDriver) driver).findElement(element).click();
}
}

}
52 changes: 39 additions & 13 deletions src/main/java/com/znsio/e2e/tools/Drivers.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,13 @@
import java.util.Map;
import java.util.logging.Level;

import static io.appium.java_client.remote.MobileCapabilityType.DEVICE_NAME;

public class Drivers {
private static final String USER_DIR = "user.dir";
private static final Logger LOGGER = Logger.getLogger(Drivers.class.getName());
private final Map<String, Driver> userPersonaDrivers = new HashMap<>();
private final Map<String, Capabilities> userPersonaDriverCapabilities = new HashMap<String, Capabilities>();
private final Map<String, Platform> userPersonaPlatforms = new HashMap<>();
private final Map<String, String> userPersonaBrowserLogs = new HashMap<>();
private final int MAX_NUMBER_OF_APPIUM_DRIVERS = 5;
Expand Down Expand Up @@ -101,19 +104,19 @@ public Driver createDriverFor (String userPersona, Platform forPlatform, TestExe
context.addTestState(TEST_CONTEXT.CURRENT_USER_PERSONA, userPersona);
userPersonaDrivers.put(userPersona, currentDriver);
userPersonaPlatforms.put(userPersona, forPlatform);
System.out.printf("allocateDriverFor: done: userPersona: '%s', Platform: '%s'%n",
LOGGER.info(String.format("allocateDriverFor: done: userPersona: '%s', Platform: '%s'%n",
userPersona,
forPlatform.name());
forPlatform.name()));

return currentDriver;
}

@NotNull
private Driver createAndroidDriverForUser (String userPersona, Platform forPlatform, TestExecutionContext context) {
System.out.printf("createAndroidDriverForUser: begin: userPersona: '%s', Platform: '%s', Number of appiumDrivers: '%d'%n",
LOGGER.info(String.format("createAndroidDriverForUser: begin: userPersona: '%s', Platform: '%s', Number of appiumDrivers: '%d'%n",
userPersona,
forPlatform.name(),
numberOfAndroidDriversUsed);
numberOfAndroidDriversUsed));
Driver currentDriver;
if (Platform.android.equals(forPlatform) && numberOfAndroidDriversUsed == MAX_NUMBER_OF_APPIUM_DRIVERS) {
throw new InvalidTestDataException(
Expand All @@ -127,13 +130,18 @@ private Driver createAndroidDriverForUser (String userPersona, Platform forPlatf
if (numberOfAndroidDriversUsed == 0) {
AppiumDriver<WebElement> appiumDriver = (AppiumDriver<WebElement>) context.getTestState(TEST_CONTEXT.APPIUM_DRIVER);
Capabilities appiumDriverCapabilities = appiumDriver.getCapabilities();
System.out.println("CAPABILITIES: " + appiumDriverCapabilities);
LOGGER.info("CAPABILITIES: " + appiumDriverCapabilities);
userPersonaDriverCapabilities.put(userPersona, appiumDriverCapabilities);
currentDriver = new Driver(
context.getTestName() + "-" + userPersona,
appiumDriver);
} else {
try {
currentDriver = new Driver(context.getTestName() + "-" + userPersona, allocateNewDeviceAndStartAppiumDriver());
AppiumDriver appiumDriver = allocateNewDeviceAndStartAppiumDriver();
currentDriver = new Driver(context.getTestName() + "-" + userPersona, appiumDriver);
Capabilities appiumDriverCapabilities = appiumDriver.getCapabilities();
LOGGER.info("CAPABILITIES: " + appiumDriverCapabilities);
userPersonaDriverCapabilities.put(userPersona, appiumDriverCapabilities);
} catch (Exception e) {
throw new EnvironmentSetupException(
String.format("Unable to create Android driver '#%d' for user persona: '%s'",
Expand All @@ -143,10 +151,10 @@ private Driver createAndroidDriverForUser (String userPersona, Platform forPlatf
}
}
numberOfAndroidDriversUsed++;
System.out.printf("createAndroidDriverForUser: done: userPersona: '%s', Platform: '%s', Number of appiumDrivers: '%d'%n",
LOGGER.info(String.format("createAndroidDriverForUser: done: userPersona: '%s', Platform: '%s', Number of appiumDrivers: '%d'%n",
userPersona,
forPlatform.name(),
numberOfAndroidDriversUsed);
numberOfAndroidDriversUsed));
disableNotificationsAndToastsOnDevice(currentDriver);
return currentDriver;
}
Expand Down Expand Up @@ -200,10 +208,10 @@ private String getCapabilityFor (org.openqa.selenium.Capabilities capabilities,

@NotNull
private Driver createWebDriverForUser (String userPersona, Platform forPlatform, TestExecutionContext context) {
System.out.printf("createWebDriverForUser: begin: userPersona: '%s', Platform: '%s', Number of webdrivers: '%d'%n",
LOGGER.info(String.format("createWebDriverForUser: begin: userPersona: '%s', Platform: '%s', Number of webdrivers: '%d'%n",
userPersona,
forPlatform.name(),
numberOfWebDriversUsed);
numberOfWebDriversUsed));

Driver currentDriver;
if (Platform.web.equals(forPlatform) && numberOfWebDriversUsed == MAX_NUMBER_OF_WEB_DRIVERS) {
Expand Down Expand Up @@ -235,10 +243,10 @@ private Driver createWebDriverForUser (String userPersona, Platform forPlatform,

@NotNull
private Driver createWindowsDriverForUser (String userPersona, Platform forPlatform, TestExecutionContext context) {
System.out.printf("createWindowsDriverForUser: begin: userPersona: '%s', Platform: '%s', Number of webdrivers: '%d'%n",
LOGGER.info(String.format("createWindowsDriverForUser: begin: userPersona: '%s', Platform: '%s', Number of webdrivers: '%d'%n",
userPersona,
forPlatform.name(),
numberOfWindowsDriversUsed);
numberOfWindowsDriversUsed));

Driver currentDriver;
if (Platform.windows.equals(forPlatform) && numberOfWindowsDriversUsed == MAX_NUMBER_OF_APPIUM_DRIVERS) {
Expand All @@ -250,9 +258,13 @@ private Driver createWindowsDriverForUser (String userPersona, Platform forPlatf
);
}
if (numberOfWindowsDriversUsed < MAX_NUMBER_OF_APPIUM_DRIVERS) {
AppiumDriver<WebElement> windowsDriver = (AppiumDriver<WebElement>) context.getTestState(TEST_CONTEXT.APPIUM_DRIVER);
currentDriver = new Driver(
context.getTestName() + "-" + userPersona,
(AppiumDriver<WebElement>) context.getTestState(TEST_CONTEXT.APPIUM_DRIVER));
windowsDriver);
Capabilities windowsDriverCapabilities = windowsDriver.getCapabilities();
LOGGER.info("CAPABILITIES: " + windowsDriverCapabilities);
userPersonaDriverCapabilities.put(userPersona, windowsDriverCapabilities);
} else {
throw new InvalidTestDataException(
String.format("Current number of WindowsDriver instances used: '%d'. " +
Expand Down Expand Up @@ -368,6 +380,8 @@ private WebDriver createFirefoxDriver (String forUserPersona,
LOGGER.info("FirefoxOptions: " + firefoxOptions.asMap());

WebDriver driver = Runner.isRunningInCI() ? createRemoteWebDriver(firefoxOptions) : new FirefoxDriver(firefoxOptions);
Capabilities capabilities = Runner.isRunningInCI() ? ((RemoteWebDriver) driver).getCapabilities() : ((FirefoxDriver) driver).getCapabilities();
userPersonaDriverCapabilities.put(forUserPersona, capabilities);
return driver;
}

Expand Down Expand Up @@ -430,6 +444,8 @@ private WebDriver createChromeDriver (String forUserPersona,
LOGGER.info("ChromeOptions: " + chromeOptions.asMap());

WebDriver driver = Runner.isRunningInCI() ? createRemoteWebDriver(chromeOptions) : new ChromeDriver(chromeOptions);
Capabilities capabilities = Runner.isRunningInCI() ? ((RemoteWebDriver) driver).getCapabilities() : ((ChromeDriver) driver).getCapabilities();
userPersonaDriverCapabilities.put(forUserPersona, capabilities);
return driver;
}

Expand Down Expand Up @@ -462,6 +478,16 @@ public Driver getDriverForUser (String userPersona) {
return userPersonaDrivers.get(userPersona);
}

public String getDeviceNameForUser (String userPersona) {
Capabilities userPersonaCapabilities = userPersonaDriverCapabilities.get(userPersona);
String deviceName = (String) userPersonaCapabilities.getCapability(DEVICE_NAME);
if (null == deviceName) {
LOGGER.info("getDeviceNameForUser: Capabilities available for userPersona: '" + userPersona + "': " + userPersonaCapabilities.asMap().keySet());
throw new InvalidTestDataException(String.format(DEVICE_NAME + " capability NOT found for user persona: '%s'\n%s", userPersona, userPersonaCapabilities.asMap().keySet()));
}
return deviceName;
}

public Platform getPlatformForUser (String userPersona) {
if (!userPersonaDrivers.containsKey(userPersona)) {
LOGGER.info("getPlatformForUser: Platforms available for userPersonas: ");
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/com/znsio/e2e/RunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RunnerTest {
@Test
void mainLocalDefault () {
String featuresDir = "./src/test/resources";
System.setProperty("RUN_IN_CI", "false");
Runner runner = new Runner("./src/test/resources/com/znsio/e2e/features/configs/config.properties", stepDefDir, featuresDir);
String baseUrl = Runner.getFromEnvironmentConfiguration("BASE_URL");
assertThat(baseUrl)
Expand Down Expand Up @@ -57,7 +58,7 @@ void multiUserTest () {
@Test
void multiUserAndroidTest () {
String featuresDir = "./src/test/resources/com/znsio/e2e/features";
System.setProperty("TAG", "@multiuser-android");
System.setProperty("TAG", "@multiuser-android and @login");
Runner runner = new Runner("./src/test/resources/com/znsio/e2e/features/configs/config.properties", stepDefDir, featuresDir);
}
}
1 change: 1 addition & 0 deletions src/test/resources/com/znsio/e2e/features/login.feature
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@login
Feature: Test valid and invalid login

@android @web
Expand Down

0 comments on commit 9f40878

Please sign in to comment.