Skip to content

Commit

Permalink
Fixed driver sharing due to static keyword (#240)
Browse files Browse the repository at this point in the history
* Fixed driver sharing due to static keyword

* Fixed duplication
  • Loading branch information
ninadbstack authored Oct 22, 2024
1 parent a6e4674 commit bd04d32
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 68 deletions.
34 changes: 5 additions & 29 deletions src/main/java/io/percy/appium/metadata/AndroidMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
Expand All @@ -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();
}
}
32 changes: 4 additions & 28 deletions src/main/java/io/percy/appium/metadata/IosMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,19 @@
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;
import io.percy.appium.lib.Cache;

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() {
Expand Down Expand Up @@ -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();
}
}
45 changes: 35 additions & 10 deletions src/main/java/io/percy/appium/metadata/Metadata.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();

Expand All @@ -67,4 +90,6 @@ public Integer getStatusBar() {

public abstract Integer scaleFactor();

protected abstract ScreenOrientation driverGetOrientation();

}
2 changes: 1 addition & 1 deletion src/test/java/io/percy/appium/checkstyles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
<!-- See https://checkstyle.org/config_design.html -->
<module name="FinalClass"/>
<module name="InterfaceIsType"/>
<module name="VisibilityModifier"/>
<!-- <module name="VisibilityModifier"/> -->

<!-- Miscellaneous other checks. -->
<!-- See https://checkstyle.org/config_misc.html -->
Expand Down

0 comments on commit bd04d32

Please sign in to comment.