1
1
package com .saucelabs .saucebindings ;
2
2
3
+ import io .appium .java_client .AppiumDriver ;
4
+ import io .appium .java_client .android .AndroidDriver ;
5
+ import io .appium .java_client .ios .IOSDriver ;
3
6
import lombok .Getter ;
4
7
import lombok .Setter ;
5
8
import org .openqa .selenium .InvalidArgumentException ;
11
14
12
15
public class SauceSession {
13
16
@ Getter @ Setter protected DataCenter dataCenter = DataCenter .US_LEGACY ;
14
- @ Getter private final SauceOptions sauceOptions ;
17
+ @ Getter private SauceOptions sauceOptions ;
15
18
@ Setter private URL sauceUrl ;
16
-
17
- @ Getter private RemoteWebDriver driver ;
19
+ @ Getter private RemoteWebDriver webDriver ;
20
+ @ Getter private AppiumDriver appDriver ;
18
21
19
22
public SauceSession () {
20
23
this (new SauceOptions ());
21
24
}
22
25
23
26
public SauceSession (SauceOptions options ) {
24
- sauceOptions = options ;
27
+ this . sauceOptions = options ;
25
28
}
26
29
27
30
public RemoteWebDriver start () {
28
- driver = createRemoteWebDriver (getSauceUrl (), sauceOptions .toCapabilities ());
29
- return driver ;
31
+ String environment = sauceOptions .toCapabilities ().getCapability ("platformName" ).toString ();
32
+ String browserName = sauceOptions .toCapabilities ().getBrowserName ();
33
+
34
+ if (browserName .equals ("" )){
35
+ if (environment .toLowerCase ().equals ("android" )){
36
+ return createAndroidDriver (getSauceUrl (), sauceOptions .toCapabilities ());
37
+ }
38
+ else if (environment .toLowerCase ().equals ("ios" )) {
39
+ return createIOSDriver (getSauceUrl (), sauceOptions .toCapabilities ());
40
+ }
41
+ else {
42
+ throw new InvalidArgumentException ("Invalid Sauce Labs capabilities. Please set a browser name or set platformName as \" Android\" or \" IOS\" ." );
43
+ }
44
+ }
45
+ else {
46
+ return createRemoteWebDriver (getSauceUrl (), sauceOptions .toCapabilities ());
47
+ }
30
48
}
31
49
32
50
public URL getSauceUrl () {
@@ -45,6 +63,14 @@ protected RemoteWebDriver createRemoteWebDriver(URL url, MutableCapabilities cap
45
63
return new RemoteWebDriver (url , capabilities );
46
64
}
47
65
66
+ protected AppiumDriver createIOSDriver (URL url , MutableCapabilities capabilities ) {
67
+ return new IOSDriver <>(url , capabilities );
68
+ }
69
+
70
+ protected AppiumDriver createAndroidDriver (URL url , MutableCapabilities capabilities ) {
71
+ return new AndroidDriver <>(url , capabilities );
72
+ }
73
+
48
74
public void stop (Boolean passed ) {
49
75
String result = passed ? "passed" : "failed" ;
50
76
stop (result );
@@ -56,12 +82,20 @@ public void stop(String result) {
56
82
}
57
83
58
84
private void updateResult (String result ) {
59
- getDriver ().executeScript ("sauce:job-result=" + result );
85
+ if (webDriver != null )
86
+ getWebDriver ().executeScript ("sauce:job-result=" + result );
87
+ else {
88
+ System .out .println ("use API for mobile case" );
89
+ }
90
+
60
91
}
61
92
62
93
private void stop () {
63
- if (getDriver () !=null ) {
64
- getDriver ().quit ();
94
+ if (getWebDriver () !=null ) {
95
+ getWebDriver ().quit ();
96
+ }
97
+ if (getAppDriver () != null ){
98
+ getAppDriver ().quit ();
65
99
}
66
100
}
67
101
}
0 commit comments