Skip to content

Commit 72b9e6a

Browse files
email2vimalrajSrinivasanTarget
authored andcommitted
Simplified the StartsActivity by reducing the number of parameters through POJO class (#579)
* Simplified the StartsActivity by reducing the number of parameters through the POJO class. * Addressed review comments * Added javadoc for the constructor. * Fixed usage instructions * Added a check to prevent Activity parameters not empty and not null. Also made the mandatory parameters final and removed setters.
1 parent df7168b commit 72b9e6a

16 files changed

+314
-42
lines changed

docs/The-starting-of-an-Android-app.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ use Android-specific API eventually.
103103

104104
```java
105105
import io.appium.java_client.android.StartsActivity;
106+
import io.appium.java_client.android.Activity;
106107

107108
...
108109

@@ -118,7 +119,10 @@ StartsActivity startsActivity = new StartsActivity() {
118119
}
119120
};
120121

121-
StartsActivity startsActivity.startActivity("your.package.name", ".ActivityName");
122+
Activity activity = new Activity("app package goes here", "app activity goes here");
123+
activity.setWaitAppPackage("app wait package goes here");
124+
activity.setWaitAppActivity("app wait activity goes here");
125+
StartsActivity startsActivity.startActivity(activity);
122126
```
123127

124128
_Samples of the searching by AndroidUIAutomator using_ ```io.appium.java_client.AppiumDriver```
@@ -149,4 +153,4 @@ findsByAndroidUIAutomator.findElementByAndroidUIAutomator("automatorString");
149153
driver.findElement(MobileBy.AndroidUIAutomator("automatorString"));
150154
```
151155

152-
All that ```AndroidDriver``` can do by design.
156+
All that ```AndroidDriver``` can do by design.
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
package io.appium.java_client.android;
2+
3+
import static com.google.common.base.Preconditions.checkArgument;
4+
import static org.apache.commons.lang3.StringUtils.isBlank;
5+
6+
/**
7+
* This is a simple POJO class to support the {@link StartsActivity}.
8+
*/
9+
public class Activity {
10+
private final String appPackage;
11+
private final String appActivity;
12+
private String appWaitPackage;
13+
private String appWaitActivity;
14+
private String intentAction;
15+
private String intentCategory;
16+
private String intentFlags;
17+
private String optionalIntentArguments;
18+
private boolean stopApp;
19+
20+
/**
21+
* A constructor with two mandatory parameters.
22+
*
23+
* @param appPackage The value for the app package.
24+
* @param appActivity The value for the app activity.
25+
*/
26+
public Activity(String appPackage, String appActivity) {
27+
checkArgument(!isBlank(appPackage),
28+
"App package should be defined as not empty or null string");
29+
checkArgument(!isBlank(appActivity),
30+
"App activity should be defined as not empty or null string");
31+
this.appPackage = appPackage;
32+
this.appActivity = appActivity;
33+
this.stopApp = true;
34+
}
35+
36+
/**
37+
* Gets the app package value.
38+
*
39+
* @return The app package value.
40+
*/
41+
public String getAppPackage() {
42+
return appPackage;
43+
}
44+
45+
/**
46+
* Gets the app activity value.
47+
*
48+
* @return The app activity value.
49+
*/
50+
public String getAppActivity() {
51+
return appActivity;
52+
}
53+
54+
/**
55+
* Gets the app wait package value.
56+
*
57+
* @return The app wait package value.
58+
*/
59+
public String getAppWaitPackage() {
60+
return appWaitPackage;
61+
}
62+
63+
/**
64+
* Sets the app wait package value.
65+
*
66+
* @param appWaitPackage The app wait package value.
67+
*/
68+
public void setAppWaitPackage(String appWaitPackage) {
69+
this.appWaitPackage = appWaitPackage;
70+
}
71+
72+
/**
73+
* Gets the app wait activity value.
74+
*
75+
* @return The app wait activity value.
76+
*/
77+
public String getAppWaitActivity() {
78+
return appWaitActivity;
79+
}
80+
81+
/**
82+
* Sets the app wait activity value.
83+
*
84+
* @param appWaitActivity The app wait activity value.
85+
*/
86+
public void setAppWaitActivity(String appWaitActivity) {
87+
this.appWaitActivity = appWaitActivity;
88+
}
89+
90+
/**
91+
* Gets the intent action value.
92+
*
93+
* @return The intent action value.
94+
*/
95+
public String getIntentAction() {
96+
return intentAction;
97+
}
98+
99+
/**
100+
* Sets the intent action value.
101+
*
102+
* @param intentAction The intent action value.
103+
*/
104+
public void setIntentAction(String intentAction) {
105+
this.intentAction = intentAction;
106+
}
107+
108+
/**
109+
* Gets the intent category value.
110+
*
111+
* @return The intent category value.
112+
*/
113+
public String getIntentCategory() {
114+
return intentCategory;
115+
}
116+
117+
/**
118+
* Sets the intent category value.
119+
*
120+
* @param intentCategory The intent category value.
121+
*/
122+
public void setIntentCategory(String intentCategory) {
123+
this.intentCategory = intentCategory;
124+
}
125+
126+
/**
127+
* Gets the intent flags value.
128+
*
129+
* @return The intent flags value.
130+
*/
131+
public String getIntentFlags() {
132+
return intentFlags;
133+
}
134+
135+
/**
136+
* Sets the intent flags value.
137+
*
138+
* @param intentFlags The intent flags value.
139+
*/
140+
public void setIntentFlags(String intentFlags) {
141+
this.intentFlags = intentFlags;
142+
}
143+
144+
/**
145+
* Gets the optional intent arguments value.
146+
*
147+
* @return The optional intent arguments value.
148+
*/
149+
public String getOptionalIntentArguments() {
150+
return optionalIntentArguments;
151+
}
152+
153+
/**
154+
* Sets the optional intent arguments value.
155+
*
156+
* @param optionalIntentArguments The optional intent arguments value.
157+
*/
158+
public void setOptionalIntentArguments(String optionalIntentArguments) {
159+
this.optionalIntentArguments = optionalIntentArguments;
160+
}
161+
162+
/**
163+
* Gets the stop app value.
164+
*
165+
* @return The stop app value.
166+
*/
167+
public boolean isStopApp() {
168+
return stopApp;
169+
}
170+
171+
/**
172+
* Sets the stop app value.
173+
*
174+
* @param stopApp The stop app value.
175+
*/
176+
public void setStopApp(boolean stopApp) {
177+
this.stopApp = stopApp;
178+
}
179+
180+
@Override public String toString() {
181+
return "Activity{" + "appPackage='" + appPackage + '\'' + ", appActivity='" + appActivity
182+
+ '\'' + ", appWaitPackage='" + appWaitPackage + '\'' + ", appWaitActivity='"
183+
+ appWaitActivity + '\'' + ", intentAction='" + intentAction + '\''
184+
+ ", intentCategory='" + intentCategory + '\'' + ", intentFlags='" + intentFlags + '\''
185+
+ ", optionalIntentArguments='" + optionalIntentArguments + '\'' + ", stopApp="
186+
+ stopApp + '}';
187+
}
188+
}

src/main/java/io/appium/java_client/android/StartsActivity.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,31 @@
2323
import io.appium.java_client.ExecutesMethod;
2424

2525
public interface StartsActivity extends ExecutesMethod {
26+
/**
27+
* This method should start arbitrary activity during a test. If the activity belongs to
28+
* another application, that application is started and the activity is opened.
29+
* <p>
30+
* Usage:
31+
* </p>
32+
* <pre>
33+
* {@code
34+
* Activity activity = new Activity("app package goes here", "app activity goes here");
35+
* activity.setWaitAppPackage("app wait package goes here");
36+
* activity.setWaitAppActivity("app wait activity goes here");
37+
* driver.startActivity(activity);
38+
* }
39+
* </pre>
40+
*
41+
* @param activity The {@link Activity} object
42+
*/
43+
default void startActivity(Activity activity) {
44+
CommandExecutionHelper.execute(this,
45+
startActivityCommand(activity.getAppPackage(), activity.getAppActivity(),
46+
activity.getAppWaitPackage(), activity.getAppWaitActivity(),
47+
activity.getIntentAction(), activity.getIntentCategory(), activity.getIntentFlags(),
48+
activity.getOptionalIntentArguments(), activity.isStopApp()));
49+
}
50+
2651
/**
2752
* This method should start arbitrary activity during a test. If the activity belongs to
2853
* another application, that application is started and the activity is opened.
@@ -32,7 +57,9 @@ public interface StartsActivity extends ExecutesMethod {
3257
* @param appWaitPackage Automation will begin after this package starts. [Optional]
3358
* @param appWaitActivity Automation will begin after this activity starts. [Optional]
3459
* @param stopApp If true, target app will be stopped. [Optional]
60+
* @deprecated Instead use {@link #startActivity(Activity)}
3561
*/
62+
@Deprecated
3663
default void startActivity(String appPackage, String appActivity, String appWaitPackage,
3764
String appWaitActivity, boolean stopApp) throws IllegalArgumentException {
3865
this.startActivity(appPackage,appActivity,appWaitPackage,
@@ -47,7 +74,9 @@ default void startActivity(String appPackage, String appActivity, String appWait
4774
* @param appActivity The activity to start. [Required]
4875
* @param appWaitPackage Automation will begin after this package starts. [Optional]
4976
* @param appWaitActivity Automation will begin after this activity starts. [Optional]
77+
* @deprecated Instead use {@link #startActivity(Activity)}
5078
*/
79+
@Deprecated
5180
default void startActivity(String appPackage, String appActivity, String appWaitPackage,
5281
String appWaitActivity) throws IllegalArgumentException {
5382
this.startActivity(appPackage, appActivity,
@@ -60,7 +89,9 @@ default void startActivity(String appPackage, String appActivity, String appWait
6089
*
6190
* @param appPackage The package containing the activity. [Required]
6291
* @param appActivity The activity to start. [Required]
92+
* @deprecated Instead use {@link #startActivity(Activity)}
6393
*/
94+
@Deprecated
6495
default void startActivity(String appPackage, String appActivity) throws IllegalArgumentException {
6596
this.startActivity(appPackage, appActivity, null, null,
6697
null,null,null,null,true);
@@ -79,7 +110,9 @@ default void startActivity(String appPackage, String appActivity) throws Illegal
79110
* @param intentFlags Flags that will be used to start activity [Optional]
80111
* @param intentOptionalArgs Additional intent arguments that will be used to
81112
* start activity [Optional]
113+
* @deprecated Instead use {@link #startActivity(Activity)}
82114
*/
115+
@Deprecated
83116
default void startActivity(String appPackage, String appActivity,
84117
String appWaitPackage, String appWaitActivity,
85118
String intentAction, String intentCategory,
@@ -104,7 +137,9 @@ default void startActivity(String appPackage, String appActivity,
104137
* @param optionalIntentArguments Additional intent arguments that will be used to
105138
* start activity [Optional]
106139
* @param stopApp If true, target app will be stopped. [Optional]
140+
* @deprecated Instead use {@link #startActivity(Activity)}
107141
*/
142+
@Deprecated
108143
default void startActivity(String appPackage, String appActivity, String appWaitPackage,
109144
String appWaitActivity, String intentAction,
110145
String intentCategory, String intentFlags,

src/test/java/io/appium/java_client/android/AndroidAbilityToUseSupplierTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ public class AndroidAbilityToUseSupplierTest extends BaseAndroidTest {
3030
.waitAction(2000).moveTo(driver.findElementByAccessibilityId("Auto Complete")).release();
3131

3232
@Test public void horizontalSwipingWithSupplier() throws Exception {
33-
driver.startActivity("io.appium.android.apis", ".view.Gallery1");
33+
Activity activity = new Activity("io.appium.android.apis", ".view.Gallery1");
34+
driver.startActivity(activity);
3435
AndroidElement gallery = driver.findElementById("io.appium.android.apis:id/gallery");
3536
List<MobileElement> images = gallery
3637
.findElementsByClassName("android.widget.ImageView");

src/test/java/io/appium/java_client/android/AndroidActivityTest.java

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,40 +24,49 @@
2424
public class AndroidActivityTest extends BaseAndroidTest {
2525

2626
@Before public void setUp() throws Exception {
27-
driver.startActivity("io.appium.android.apis", ".ApiDemos");
27+
Activity activity = new Activity("io.appium.android.apis", ".ApiDemos");
28+
driver.startActivity(activity);
2829
}
2930

3031
@Test public void startActivityInThisAppTestCase() {
31-
driver.startActivity("io.appium.android.apis",
32+
Activity activity = new Activity("io.appium.android.apis",
3233
".accessibility.AccessibilityNodeProviderActivity");
34+
driver.startActivity(activity);
3335
assertEquals(driver.currentActivity(),
3436
".accessibility.AccessibilityNodeProviderActivity");
3537
}
3638

3739
@Test public void startActivityWithWaitingAppTestCase() {
38-
driver.startActivity("io.appium.android.apis",
39-
".accessibility.AccessibilityNodeProviderActivity",
40-
"io.appium.android.apis", ".accessibility.AccessibilityNodeProviderActivity");
40+
final Activity activity = new Activity("io.appium.android.apis",
41+
".accessibility.AccessibilityNodeProviderActivity");
42+
activity.setAppWaitPackage("io.appium.android.apis");
43+
activity.setAppWaitActivity(".accessibility.AccessibilityNodeProviderActivity");
44+
driver.startActivity(activity);
4145
assertEquals(driver.currentActivity(),
4246
".accessibility.AccessibilityNodeProviderActivity");
4347
}
4448

4549
@Test public void startActivityInNewAppTestCase() {
46-
driver.startActivity("com.android.contacts", ".ContactsListActivity");
50+
Activity activity = new Activity("com.android.contacts", ".ContactsListActivity");
51+
driver.startActivity(activity);
4752
assertEquals(driver.currentActivity(), ".ContactsListActivity");
4853
driver.pressKeyCode(AndroidKeyCode.BACK);
4954
assertEquals(driver.currentActivity(), ".ContactsListActivity");
5055
}
5156

5257
@Test public void startActivityInNewAppTestCaseWithoutClosingApp() {
53-
driver.startActivity("io.appium.android.apis",
58+
Activity activity = new Activity("io.appium.android.apis",
5459
".accessibility.AccessibilityNodeProviderActivity");
60+
driver.startActivity(activity);
5561
assertEquals(driver.currentActivity(), ".accessibility.AccessibilityNodeProviderActivity");
56-
driver.startActivity("com.android.contacts", ".ContactsListActivity",
57-
"com.android.contacts", ".ContactsListActivity", false);
62+
63+
Activity newActivity = new Activity("com.android.contacts", ".ContactsListActivity");
64+
newActivity.setAppWaitPackage("com.android.contacts");
65+
newActivity.setAppWaitActivity(".ContactsListActivity");
66+
newActivity.setStopApp(false);
67+
driver.startActivity(newActivity);
5868
assertEquals(driver.currentActivity(), ".ContactsListActivity");
5969
driver.pressKeyCode(AndroidKeyCode.BACK);
60-
assertEquals(driver.currentActivity(),
61-
".accessibility.AccessibilityNodeProviderActivity");
70+
assertEquals(driver.currentActivity(), ".accessibility.AccessibilityNodeProviderActivity");
6271
}
6372
}

src/test/java/io/appium/java_client/android/AndroidContextTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
public class AndroidContextTest extends BaseAndroidTest {
2727

2828
@BeforeClass public static void beforeClass2() throws Exception {
29-
driver.startActivity("io.appium.android.apis", ".view.WebView1");
29+
Activity activity = new Activity("io.appium.android.apis", ".view.WebView1");
30+
driver.startActivity(activity);
3031
Thread.sleep(20000);
3132
}
3233

0 commit comments

Comments
 (0)