Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplified the StartsActivity by reducing the number of parameters through POJO class #579

Merged
merged 5 commits into from
Feb 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions docs/The-starting-of-an-Android-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ use Android-specific API eventually.

```java
import io.appium.java_client.android.StartsActivity;
import io.appium.java_client.android.Activity;

...

Expand All @@ -118,7 +119,10 @@ StartsActivity startsActivity = new StartsActivity() {
}
};

StartsActivity startsActivity.startActivity("your.package.name", ".ActivityName");
Activity activity = new Activity("app package goes here", "app activity goes here");
activity.setWaitAppPackage("app wait package goes here");
activity.setWaitAppActivity("app wait activity goes here");
StartsActivity startsActivity.startActivity(activity);
```

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

All that ```AndroidDriver``` can do by design.
All that ```AndroidDriver``` can do by design.
188 changes: 188 additions & 0 deletions src/main/java/io/appium/java_client/android/Activity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package io.appium.java_client.android;

import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.commons.lang3.StringUtils.isBlank;

/**
* This is a simple POJO class to support the {@link StartsActivity}.
*/
public class Activity {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I this this POJO should have 2 mandatory parameteres: package and activity. Please add the constructor with these parameters.

private final String appPackage;
private final String appActivity;
private String appWaitPackage;
private String appWaitActivity;
private String intentAction;
private String intentCategory;
private String intentFlags;
private String optionalIntentArguments;
private boolean stopApp;

/**
* A constructor with two mandatory parameters.
*
* @param appPackage The value for the app package.
* @param appActivity The value for the app activity.
*/
public Activity(String appPackage, String appActivity) {
checkArgument(!isBlank(appPackage),
"App package should be defined as not empty or null string");
checkArgument(!isBlank(appActivity),
"App activity should be defined as not empty or null string");
this.appPackage = appPackage;
this.appActivity = appActivity;
this.stopApp = true;
}

/**
* Gets the app package value.
*
* @return The app package value.
*/
public String getAppPackage() {
return appPackage;
}

/**
* Gets the app activity value.
*
* @return The app activity value.
*/
public String getAppActivity() {
return appActivity;
}

/**
* Gets the app wait package value.
*
* @return The app wait package value.
*/
public String getAppWaitPackage() {
return appWaitPackage;
}

/**
* Sets the app wait package value.
*
* @param appWaitPackage The app wait package value.
*/
public void setAppWaitPackage(String appWaitPackage) {
this.appWaitPackage = appWaitPackage;
}

/**
* Gets the app wait activity value.
*
* @return The app wait activity value.
*/
public String getAppWaitActivity() {
return appWaitActivity;
}

/**
* Sets the app wait activity value.
*
* @param appWaitActivity The app wait activity value.
*/
public void setAppWaitActivity(String appWaitActivity) {
this.appWaitActivity = appWaitActivity;
}

/**
* Gets the intent action value.
*
* @return The intent action value.
*/
public String getIntentAction() {
return intentAction;
}

/**
* Sets the intent action value.
*
* @param intentAction The intent action value.
*/
public void setIntentAction(String intentAction) {
this.intentAction = intentAction;
}

/**
* Gets the intent category value.
*
* @return The intent category value.
*/
public String getIntentCategory() {
return intentCategory;
}

/**
* Sets the intent category value.
*
* @param intentCategory The intent category value.
*/
public void setIntentCategory(String intentCategory) {
this.intentCategory = intentCategory;
}

/**
* Gets the intent flags value.
*
* @return The intent flags value.
*/
public String getIntentFlags() {
return intentFlags;
}

/**
* Sets the intent flags value.
*
* @param intentFlags The intent flags value.
*/
public void setIntentFlags(String intentFlags) {
this.intentFlags = intentFlags;
}

/**
* Gets the optional intent arguments value.
*
* @return The optional intent arguments value.
*/
public String getOptionalIntentArguments() {
return optionalIntentArguments;
}

/**
* Sets the optional intent arguments value.
*
* @param optionalIntentArguments The optional intent arguments value.
*/
public void setOptionalIntentArguments(String optionalIntentArguments) {
this.optionalIntentArguments = optionalIntentArguments;
}

/**
* Gets the stop app value.
*
* @return The stop app value.
*/
public boolean isStopApp() {
return stopApp;
}

/**
* Sets the stop app value.
*
* @param stopApp The stop app value.
*/
public void setStopApp(boolean stopApp) {
this.stopApp = stopApp;
}

@Override public String toString() {
return "Activity{" + "appPackage='" + appPackage + '\'' + ", appActivity='" + appActivity
+ '\'' + ", appWaitPackage='" + appWaitPackage + '\'' + ", appWaitActivity='"
+ appWaitActivity + '\'' + ", intentAction='" + intentAction + '\''
+ ", intentCategory='" + intentCategory + '\'' + ", intentFlags='" + intentFlags + '\''
+ ", optionalIntentArguments='" + optionalIntentArguments + '\'' + ", stopApp="
+ stopApp + '}';
}
}
35 changes: 35 additions & 0 deletions src/main/java/io/appium/java_client/android/StartsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@
import io.appium.java_client.ExecutesMethod;

public interface StartsActivity extends ExecutesMethod {
/**
* This method should start arbitrary activity during a test. If the activity belongs to
* another application, that application is started and the activity is opened.
* <p>
* Usage:
* </p>
* <pre>
* {@code
* Activity activity = new Activity("app package goes here", "app activity goes here");
* activity.setWaitAppPackage("app wait package goes here");
* activity.setWaitAppActivity("app wait activity goes here");
* driver.startActivity(activity);
* }
* </pre>
*
* @param activity The {@link Activity} object
*/
default void startActivity(Activity activity) {
CommandExecutionHelper.execute(this,
startActivityCommand(activity.getAppPackage(), activity.getAppActivity(),
activity.getAppWaitPackage(), activity.getAppWaitActivity(),
activity.getIntentAction(), activity.getIntentCategory(), activity.getIntentFlags(),
activity.getOptionalIntentArguments(), activity.isStopApp()));
}

/**
* This method should start arbitrary activity during a test. If the activity belongs to
* another application, that application is started and the activity is opened.
Expand All @@ -32,7 +57,9 @@ public interface StartsActivity extends ExecutesMethod {
* @param appWaitPackage Automation will begin after this package starts. [Optional]
* @param appWaitActivity Automation will begin after this activity starts. [Optional]
* @param stopApp If true, target app will be stopped. [Optional]
* @deprecated Instead use {@link #startActivity(Activity)}
*/
@Deprecated
default void startActivity(String appPackage, String appActivity, String appWaitPackage,
String appWaitActivity, boolean stopApp) throws IllegalArgumentException {
this.startActivity(appPackage,appActivity,appWaitPackage,
Expand All @@ -47,7 +74,9 @@ default void startActivity(String appPackage, String appActivity, String appWait
* @param appActivity The activity to start. [Required]
* @param appWaitPackage Automation will begin after this package starts. [Optional]
* @param appWaitActivity Automation will begin after this activity starts. [Optional]
* @deprecated Instead use {@link #startActivity(Activity)}
*/
@Deprecated
default void startActivity(String appPackage, String appActivity, String appWaitPackage,
String appWaitActivity) throws IllegalArgumentException {
this.startActivity(appPackage, appActivity,
Expand All @@ -60,7 +89,9 @@ default void startActivity(String appPackage, String appActivity, String appWait
*
* @param appPackage The package containing the activity. [Required]
* @param appActivity The activity to start. [Required]
* @deprecated Instead use {@link #startActivity(Activity)}
*/
@Deprecated
default void startActivity(String appPackage, String appActivity) throws IllegalArgumentException {
this.startActivity(appPackage, appActivity, null, null,
null,null,null,null,true);
Expand All @@ -79,7 +110,9 @@ default void startActivity(String appPackage, String appActivity) throws Illegal
* @param intentFlags Flags that will be used to start activity [Optional]
* @param intentOptionalArgs Additional intent arguments that will be used to
* start activity [Optional]
* @deprecated Instead use {@link #startActivity(Activity)}
*/
@Deprecated
default void startActivity(String appPackage, String appActivity,
String appWaitPackage, String appWaitActivity,
String intentAction, String intentCategory,
Expand All @@ -104,7 +137,9 @@ default void startActivity(String appPackage, String appActivity,
* @param optionalIntentArguments Additional intent arguments that will be used to
* start activity [Optional]
* @param stopApp If true, target app will be stopped. [Optional]
* @deprecated Instead use {@link #startActivity(Activity)}
*/
@Deprecated
default void startActivity(String appPackage, String appActivity, String appWaitPackage,
String appWaitActivity, String intentAction,
String intentCategory, String intentFlags,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public class AndroidAbilityToUseSupplierTest extends BaseAndroidTest {
.waitAction(2000).moveTo(driver.findElementByAccessibilityId("Auto Complete")).release();

@Test public void horizontalSwipingWithSupplier() throws Exception {
driver.startActivity("io.appium.android.apis", ".view.Gallery1");
Activity activity = new Activity("io.appium.android.apis", ".view.Gallery1");
driver.startActivity(activity);
AndroidElement gallery = driver.findElementById("io.appium.android.apis:id/gallery");
List<MobileElement> images = gallery
.findElementsByClassName("android.widget.ImageView");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,49 @@
public class AndroidActivityTest extends BaseAndroidTest {

@Before public void setUp() throws Exception {
driver.startActivity("io.appium.android.apis", ".ApiDemos");
Activity activity = new Activity("io.appium.android.apis", ".ApiDemos");
driver.startActivity(activity);
}

@Test public void startActivityInThisAppTestCase() {
driver.startActivity("io.appium.android.apis",
Activity activity = new Activity("io.appium.android.apis",
".accessibility.AccessibilityNodeProviderActivity");
driver.startActivity(activity);
assertEquals(driver.currentActivity(),
".accessibility.AccessibilityNodeProviderActivity");
}

@Test public void startActivityWithWaitingAppTestCase() {
driver.startActivity("io.appium.android.apis",
".accessibility.AccessibilityNodeProviderActivity",
"io.appium.android.apis", ".accessibility.AccessibilityNodeProviderActivity");
final Activity activity = new Activity("io.appium.android.apis",
".accessibility.AccessibilityNodeProviderActivity");
activity.setAppWaitPackage("io.appium.android.apis");
activity.setAppWaitActivity(".accessibility.AccessibilityNodeProviderActivity");
driver.startActivity(activity);
assertEquals(driver.currentActivity(),
".accessibility.AccessibilityNodeProviderActivity");
}

@Test public void startActivityInNewAppTestCase() {
driver.startActivity("com.android.contacts", ".ContactsListActivity");
Activity activity = new Activity("com.android.contacts", ".ContactsListActivity");
driver.startActivity(activity);
assertEquals(driver.currentActivity(), ".ContactsListActivity");
driver.pressKeyCode(AndroidKeyCode.BACK);
assertEquals(driver.currentActivity(), ".ContactsListActivity");
}

@Test public void startActivityInNewAppTestCaseWithoutClosingApp() {
driver.startActivity("io.appium.android.apis",
Activity activity = new Activity("io.appium.android.apis",
".accessibility.AccessibilityNodeProviderActivity");
driver.startActivity(activity);
assertEquals(driver.currentActivity(), ".accessibility.AccessibilityNodeProviderActivity");
driver.startActivity("com.android.contacts", ".ContactsListActivity",
"com.android.contacts", ".ContactsListActivity", false);

Activity newActivity = new Activity("com.android.contacts", ".ContactsListActivity");
newActivity.setAppWaitPackage("com.android.contacts");
newActivity.setAppWaitActivity(".ContactsListActivity");
newActivity.setStopApp(false);
driver.startActivity(newActivity);
assertEquals(driver.currentActivity(), ".ContactsListActivity");
driver.pressKeyCode(AndroidKeyCode.BACK);
assertEquals(driver.currentActivity(),
".accessibility.AccessibilityNodeProviderActivity");
assertEquals(driver.currentActivity(), ".accessibility.AccessibilityNodeProviderActivity");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
public class AndroidContextTest extends BaseAndroidTest {

@BeforeClass public static void beforeClass2() throws Exception {
driver.startActivity("io.appium.android.apis", ".view.WebView1");
Activity activity = new Activity("io.appium.android.apis", ".view.WebView1");
driver.startActivity(activity);
Thread.sleep(20000);
}

Expand Down
Loading