Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote mobile driver support #113

Merged
merged 8 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.umutayb</groupId>
<artifactId>Pickleib</artifactId>
<version>1.9.9</version>
<version>1.9.9-1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Pickleib</name>
Expand Down Expand Up @@ -50,11 +50,11 @@
<buildDirectory>${project.basedir}/out/artifacts/POM-Framework_jar</buildDirectory>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.version>3.8.0</maven.compiler.version>
<java.utilities.version>1.6.3</java.utilities.version>
<gpt.utilities.version>0.1.3</gpt.utilities.version>
<java.utilities.version>1.6.5</java.utilities.version>
<gpt.utilities.version>0.1.5</gpt.utilities.version>
<docker-java.version>3.3.0</docker-java.version>
<selenium.version>4.18.1</selenium.version>
<appium.version>8.5.1</appium.version>
<selenium.version>4.13.0</selenium.version>
<appium.version>8.6.0</appium.version>
<retrofit.version>2.9.0</retrofit.version>
<okhttp.version>4.10.0</okhttp.version>
</properties>
Expand Down Expand Up @@ -195,7 +195,6 @@
<version>20231013</version>
</dependency>


<!-- JUnit -->
<dependency>
<groupId>junit</groupId>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/pickleib/driver/DriverFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static DriverType getType(@Nullable String text) {

public static DriverType getParentType(Platform platform) {
return switch (platform) {
case WINDOWS, SONOMA, LINUX, UNIX, VENTURA, MONTEREY, BIG_SUR, CATALINA, MOJAVE, HIGH_SIERRA, SIERRA, EL_CAPITAN, YOSEMITE, MAVERICKS, MOUNTAIN_LION, SNOW_LEOPARD, MAC, WIN11, WIN10, WIN8_1, WIN8, WIN7, VISTA, XP -> Web;
case WINDOWS, LINUX, UNIX, VENTURA, MONTEREY, BIG_SUR, CATALINA, MOJAVE, HIGH_SIERRA, SIERRA, EL_CAPITAN, YOSEMITE, MAVERICKS, MOUNTAIN_LION, SNOW_LEOPARD, MAC, WIN11, WIN10, WIN8_1, WIN8, WIN7, VISTA, XP -> Web;
case ANDROID -> Android;
case IOS -> iOS;
case ANY -> null;
Expand Down
36 changes: 24 additions & 12 deletions src/main/java/pickleib/mobile/driver/AppiumDriverFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,42 @@
import pickleib.driver.DriverFactory;
import utils.Printer;
import java.net.URL;

import static pickleib.mobile.driver.ServiceFactory.service;
import static utils.StringUtilities.Color.*;
import static utils.StringUtilities.*;

public class AppiumDriverFactory implements DriverFactory {

static Printer log = new Printer(AppiumDriverFactory.class);
static String deviceName;

public static AppiumDriver getDriver(String deviceName, JSONObject capabilitiesJSON, boolean remote){
AppiumDriverFactory.deviceName = deviceName;
DesiredCapabilities capabilities = getConfig(capabilitiesJSON);
String urlString;
if (remote) {
String userName = ContextStore.get("remote-mobile-username");
String accessKey = ContextStore.get("remote-mobile-access-key");
String server = ContextStore.get("remote-mobile-server");
urlString = String.format("https://%s:%s@%s/wd/hub", userName , accessKey, server);
}
else {
String address = ContextStore.get("address", "0.0.0.0");
String port = ContextStore.get("port", "4723");
urlString = "http://" + address + ":" + port + "/wd/hub";
capabilities.setCapability("app", contextCheck("UPLOAD-" + capabilitiesJSON.get("app")));
if (service != null) urlString = service.getUrl().toString();
}
return getDriver(capabilities, urlString);
}

public static AppiumDriver getDriver(String deviceName, JSONObject capabilities){
DesiredCapabilities desiredCapabilities = getConfig(capabilities);
desiredCapabilities.setCapability("app", contextCheck("UPLOAD-" + capabilities.get("app")));
public static AppiumDriver getDriver(DesiredCapabilities capabilities, String urlString){
try {
URL url;
if (service == null) {
String address = ContextStore.get("address", "0.0.0.0");
String port = ContextStore.get("port", "4723");
url = new URL("http://" + address + ":" + port + "/wd/hub");
}
else url = service.getUrl();
log.important(deviceName + markup(GRAY, " was selected"));
return new AppiumDriver(url, desiredCapabilities);
return new AppiumDriver(new URL(urlString), capabilities);
}
catch (Exception gamma) {
gamma.printStackTrace();
if(gamma.toString().contains("Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure")){
log.info("Please make sure " + markup(PURPLE, "Appium ") + "is on & verify the port that its running on at 'resources/test.properties'.");
throw new RuntimeException(markup(YELLOW, gamma.getMessage()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public static void initialize() {
String directory = ContextStore.get("config", "src/test/resources/configurations");

JSONObject json = FileUtilities.Json.parseJSONFile(directory+"/"+device+".json");
driver = AppiumDriverFactory.getDriver(StringUtilities.firstLetterCapped(device), json);
driver = AppiumDriverFactory.getDriver(
StringUtilities.firstLetterCapped(device),
json,
Boolean.parseBoolean(ContextStore.get("use-remote-mobile-driver", "false"))
);
}

public static void terminate(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@
import org.openqa.selenium.remote.RemoteWebDriver;
import utils.NumericUtilities;
import utils.Printer;
import utils.StringUtilities;

import java.io.File;

import static utils.StringUtilities.Color.*;
import static utils.StringUtilities.highlighted;

@SuppressWarnings("unused")
public class ScreenCaptureUtility {
static Printer log = new Printer(ScreenCaptureUtility.class);
Expand Down Expand Up @@ -41,4 +36,27 @@ public static File captureScreen(String name, String extension, RemoteWebDriver
return null;
}
}

/**
* Captures screen
* @param name screenshot name
* @param driver session driver
* @return returns the screenshot file
*/
public static File silentCaptureScreen(String name, String extension, RemoteWebDriver driver) {
try {
if (!extension.contains(".")) extension = "." + extension;
name += "#"+ NumericUtilities.randomNumber(1,10000) + extension;
File sourceFile = new File("screenshots");
File fileDestination = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(fileDestination, new File(sourceFile, name));

return fileDestination;
}
catch (Exception exception){
log.error("Could not capture screen", exception);
exception.printStackTrace();
return null;
}
}
}
2 changes: 0 additions & 2 deletions src/test/java/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import pages.ElementsPage;
import pages.FormsPage;
import pickleib.enums.Direction;
import pickleib.enums.ElementState;
import pickleib.utilities.element.acquisition.ElementAcquisition;
import pickleib.web.driver.PickleibWebDriver;
import pickleib.web.driver.WebDriverFactory;
Expand Down