From bd04d328bff484e89f3b2128c29842e70cef20e8 Mon Sep 17 00:00:00 2001 From: ninadbstack <60422475+ninadbstack@users.noreply.github.com> Date: Tue, 22 Oct 2024 15:31:49 +0530 Subject: [PATCH] Fixed driver sharing due to static keyword (#240) * Fixed driver sharing due to static keyword * Fixed duplication --- .../appium/metadata/AndroidMetadata.java | 34 +++----------- .../io/percy/appium/metadata/IosMetadata.java | 32 ++----------- .../io/percy/appium/metadata/Metadata.java | 45 ++++++++++++++----- src/test/java/io/percy/appium/checkstyles.xml | 2 +- 4 files changed, 45 insertions(+), 68 deletions(-) diff --git a/src/main/java/io/percy/appium/metadata/AndroidMetadata.java b/src/main/java/io/percy/appium/metadata/AndroidMetadata.java index 1f7fae6..5219280 100644 --- a/src/main/java/io/percy/appium/metadata/AndroidMetadata.java +++ b/src/main/java/io/percy/appium/metadata/AndroidMetadata.java @@ -3,6 +3,7 @@ import java.util.Map; import org.json.JSONObject; +import org.openqa.selenium.ScreenOrientation; import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver; @@ -11,15 +12,11 @@ public class AndroidMetadata extends Metadata { private AndroidDriver driver; - private String sessionId; - private String orientation; public AndroidMetadata(AppiumDriver driver, String deviceName, Integer statusBar, Integer navBar, String orientation, String platformVersion) { super(driver, deviceName, statusBar, navBar, orientation, platformVersion); this.driver = (AndroidDriver) driver; - this.orientation = orientation; - this.sessionId = driver.getSessionId().toString(); } public String deviceName() { @@ -99,7 +96,7 @@ public Integer scaleFactor() { return 1; } - public String getDisplaySysDump() { + private String getDisplaySysDump() { JSONObject arguments = new JSONObject(); arguments.put("action", "adbShell"); JSONObject command = new JSONObject(); @@ -110,28 +107,7 @@ public String getDisplaySysDump() { return resultString; } - - public String orientation() { - if (orientation != null) { - if (orientation.toLowerCase().equals("portrait") || orientation.toLowerCase().equals("landscape")) { - return orientation.toLowerCase(); - } else if (orientation.toLowerCase().equals("auto")) { - try { - return driver.getOrientation().toString().toLowerCase(); - } catch (java.lang.NoSuchMethodError e) { - return "portrait"; - } - } else { - return "portrait"; - } - } else { - Object orientationCapability = driver.getCapabilities().getCapability("orientation"); - if (orientationCapability != null) { - return orientationCapability.toString().toLowerCase(); - } else { - return "portrait"; - } - } - } - + protected ScreenOrientation driverGetOrientation() { + return this.driver.getOrientation(); + } } diff --git a/src/main/java/io/percy/appium/metadata/IosMetadata.java b/src/main/java/io/percy/appium/metadata/IosMetadata.java index dfda5a2..52a3778 100644 --- a/src/main/java/io/percy/appium/metadata/IosMetadata.java +++ b/src/main/java/io/percy/appium/metadata/IosMetadata.java @@ -3,6 +3,7 @@ import java.util.Map; import org.openqa.selenium.Dimension; +import org.openqa.selenium.ScreenOrientation; import io.appium.java_client.AppiumDriver; import io.appium.java_client.ios.IOSDriver; @@ -10,15 +11,11 @@ public class IosMetadata extends Metadata { private IOSDriver driver; - private String sessionId; - private String orientation; public IosMetadata(AppiumDriver driver, String deviceName, Integer statusBar, Integer navBar, String orientation, String platformVersion) { super(driver, deviceName, statusBar, navBar, orientation, platformVersion); this.driver = (IOSDriver) driver; - this.orientation = orientation; - this.sessionId = driver.getSessionId().toString(); } public String deviceName() { @@ -89,28 +86,7 @@ public Integer scaleFactor() { return actualWidth / screenWidth; } - - public String orientation() { - if (orientation != null) { - if (orientation.toLowerCase().equals("portrait") || orientation.toLowerCase().equals("landscape")) { - return orientation.toLowerCase(); - } else if (orientation.toLowerCase().equals("auto")) { - try { - return driver.getOrientation().toString().toLowerCase(); - } catch (java.lang.NoSuchMethodError e) { - return "portrait"; - } - } else { - return "portrait"; - } - } else { - Object orientationCapability = driver.getCapabilities().getCapability("orientation"); - if (orientationCapability != null) { - return orientationCapability.toString().toLowerCase(); - } else { - return "portrait"; - } - } - } - + protected ScreenOrientation driverGetOrientation() { + return this.driver.getOrientation(); + } } diff --git a/src/main/java/io/percy/appium/metadata/Metadata.java b/src/main/java/io/percy/appium/metadata/Metadata.java index 0f3443e..702121e 100644 --- a/src/main/java/io/percy/appium/metadata/Metadata.java +++ b/src/main/java/io/percy/appium/metadata/Metadata.java @@ -1,19 +1,21 @@ package io.percy.appium.metadata; +import org.openqa.selenium.ScreenOrientation; + import io.appium.java_client.AppiumDriver; public abstract class Metadata { - private static AppiumDriver driver; - private String orientation; - private String platformVersion; - private Integer statusBar; - private Integer navBar; - private String deviceName; - private String sessionId; + private AppiumDriver driver; + protected String orientation; + protected String platformVersion; + protected Integer statusBar; + protected Integer navBar; + protected String deviceName; + protected String sessionId; public Metadata(AppiumDriver driver, String deviceName, Integer statusBar, Integer navBar, String orientation, String platformVersion) { - Metadata.driver = driver; + this.driver = driver; this.platformVersion = platformVersion; this.orientation = orientation; this.statusBar = statusBar; @@ -53,9 +55,30 @@ public Integer getStatusBar() { return statusBar; } - public abstract Integer deviceScreenWidth(); + public String orientation() { + if (orientation != null) { + if (orientation.toLowerCase().equals("portrait") || orientation.toLowerCase().equals("landscape")) { + return orientation.toLowerCase(); + } else if (orientation.toLowerCase().equals("auto")) { + try { + return this.driverGetOrientation().toString().toLowerCase(); + } catch (java.lang.NoSuchMethodError e) { + return "portrait"; + } + } else { + return "portrait"; + } + } else { + Object orientationCapability = driver.getCapabilities().getCapability("orientation"); + if (orientationCapability != null) { + return orientationCapability.toString().toLowerCase(); + } else { + return "portrait"; + } + } + } - public abstract String orientation(); + public abstract Integer deviceScreenWidth(); public abstract String deviceName(); @@ -67,4 +90,6 @@ public Integer getStatusBar() { public abstract Integer scaleFactor(); + protected abstract ScreenOrientation driverGetOrientation(); + } diff --git a/src/test/java/io/percy/appium/checkstyles.xml b/src/test/java/io/percy/appium/checkstyles.xml index f8fa71e..08775c8 100644 --- a/src/test/java/io/percy/appium/checkstyles.xml +++ b/src/test/java/io/percy/appium/checkstyles.xml @@ -165,7 +165,7 @@ - +