1
1
package io .github .lambdatest .utils ;
2
2
3
3
import io .appium .java_client .AppiumDriver ;
4
+ import io .appium .java_client .PerformsTouchActions ;
5
+ import io .appium .java_client .TouchAction ;
6
+ import io .appium .java_client .touch .WaitOptions ;
7
+ import io .appium .java_client .touch .offset .PointOption ;
4
8
import org .openqa .selenium .*;
5
9
import org .openqa .selenium .interactions .PointerInput ;
6
10
import org .openqa .selenium .interactions .Sequence ;
11
+ import org .openqa .selenium .remote .RemoteWebElement ;
7
12
8
13
import java .io .File ;
9
14
import java .io .IOException ;
10
15
import java .nio .file .Files ;
11
16
import java .nio .file .StandardCopyOption ;
12
17
import java .time .Duration ;
13
- import java .util .ArrayList ;
14
- import java .util .Collections ;
15
- import java .util .List ;
18
+ import java .util .*;
16
19
import java .util .logging .Logger ;
17
20
18
21
public class FullPageScreenshotUtil {
@@ -35,12 +38,12 @@ public FullPageScreenshotUtil(WebDriver driver, String saveDirectoryName) {
35
38
private int samePageCounter = 1 ; //Init with value 1 , finalise at 3
36
39
private int maxCount = 10 ;
37
40
public List <File > captureFullPage (int pageCount ) {
38
- if (pageCount <=0 ){
39
- pageCount = maxCount ;
40
- }
41
- if (pageCount < maxCount ) {
42
- maxCount = pageCount ;
43
- }
41
+ if (pageCount <=0 ){
42
+ pageCount = maxCount ;
43
+ }
44
+ if (pageCount < maxCount ) {
45
+ maxCount = pageCount ;
46
+ }
44
47
int chunkCount = 0 ;
45
48
boolean isLastScroll = false ;
46
49
List <File > screenshotDir = new ArrayList <>();
@@ -75,31 +78,45 @@ private File captureAndSaveScreenshot(String ssDir, int index) {
75
78
private void scrollDown () {
76
79
Dimension screenSize = driver .manage ().window ().getSize ();
77
80
int screenHeight = screenSize .getHeight ();
81
+ int screenWidth = screenSize .getWidth ();
82
+
78
83
// Define start and end points for scrolling
79
84
int startX = 4 ; //start from 4 pixels from the left, to avoid click on action items/webview
80
85
int startY = (int ) (screenHeight * 0.70 ); // Start at 70% of the screen height
81
86
int endY = (int ) (screenHeight * 0.45 ); // Scroll up to 25%
87
+ int scrollHeight = startY - endY ;
82
88
83
89
try {
84
- // Create a PointerInput action for touch gestures
85
- PointerInput finger = new PointerInput (PointerInput .Kind .TOUCH , "finger" );
86
- Sequence swipe = new Sequence (finger , 0 );
90
+ // Try iOS style swipe
91
+ JavascriptExecutor javascriptExecutorIos = (JavascriptExecutor ) driver ;
92
+ Map <String , Object > swipeObj = new HashMap <>();
93
+ swipeObj .put ("fromX" , startX );
94
+ swipeObj .put ("fromY" , startY );
95
+ swipeObj .put ("toX" , startX );
96
+ swipeObj .put ("toY" , endY );
97
+ swipeObj .put ("duration" , 0.8 );
98
+ javascriptExecutorIos .executeScript ("mobile: dragFromToForDuration" , swipeObj );
87
99
88
- // Press (touch) at the start position
89
- swipe .addAction (finger .createPointerMove (Duration .ZERO , PointerInput .Origin .viewport (), startX , startY ));
90
- swipe .addAction (finger .createPointerDown (PointerInput .MouseButton .LEFT .asArg ()));
91
- // Move to end position (scrolling)
92
- swipe .addAction (finger .createPointerMove (Duration .ofMillis (500 ), PointerInput .Origin .viewport (), startX , endY ));
93
- swipe .addAction (finger .createPointerUp (PointerInput .MouseButton .LEFT .asArg ()));
94
- if (driver instanceof AppiumDriver ) {
95
- AppiumDriver appiumDriver = (AppiumDriver ) driver ;
96
- appiumDriver .perform (Collections .singleton (swipe ));
97
- } else {
98
- log .warning ("Driver is not an instance of AppiumDriver" );
99
- AppiumDriver appiumDriver = (AppiumDriver ) driver ;
100
- appiumDriver .perform (Collections .singleton (swipe ));
100
+ } catch (Exception iosException ) {
101
+ try {
102
+ // If iOS swipe fails, assume it's Android and do scrollGesture
103
+ JavascriptExecutor jsExecutorAndroid = (JavascriptExecutor ) driver ;
104
+ Map <String , Object > scrollParams = new HashMap <>();
105
+ scrollParams .put ("left" , startX );
106
+ scrollParams .put ("top" , endY );
107
+ scrollParams .put ("width" , screenWidth - startX );
108
+ scrollParams .put ("height" , scrollHeight );
109
+ scrollParams .put ("direction" , "down" );
110
+ scrollParams .put ("percent" , 1.0 );
111
+ scrollParams .put ("speed" , 2500 );
112
+ jsExecutorAndroid .executeScript ("mobile:scrollGesture" , scrollParams );
113
+ } catch (Exception e ) {
114
+ log .warning ("Error during Android scroll operation: " + e .getMessage ());
115
+ e .printStackTrace ();
101
116
}
102
- // Allow time for UI to update
117
+ }
118
+
119
+ try {
103
120
Thread .sleep (1000 );
104
121
} catch (Exception e ) {
105
122
log .warning ("Error during scroll operation: " + e .getMessage ());
0 commit comments