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 3 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
6 changes: 5 additions & 1 deletion 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();
activity.setAppPackage("your.package.name");
Copy link
Member

Choose a reason for hiding this comment

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

@email2vimalraj Can you update here too.

Copy link
Member

Choose a reason for hiding this comment

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

activity.setAppActivity(".ActivityName");
StartsActivity startsActivity.startActivity(activity);
```

_Samples of the searching by AndroidUIAutomator using_ ```io.appium.java_client.AppiumDriver```
Expand Down
199 changes: 199 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,199 @@
package io.appium.java_client.android;

/**
* 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 String appPackage;
private 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) {
Copy link
Contributor

@TikhomirovSergey TikhomirovSergey Feb 25, 2017

Choose a reason for hiding this comment

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

@email2vimalraj Could you provide these checkings:

import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.commons.lang3.StringUtils.isBlank;
...
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");
    ...
}

Copy link
Member

Choose a reason for hiding this comment

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

Good catch 👍

this.appPackage = appPackage;
this.appActivity = appActivity;
this.stopApp = true;
}

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

/**
* Sets the app package value.
*
* @param appPackage The app package value.
*/
public void setAppPackage(String appPackage) {
Copy link
Member

@SrinivasanTarget SrinivasanTarget Feb 22, 2017

Choose a reason for hiding this comment

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

I like the idea behind this but have few things to be discussed here,

Is there a way to remove getters & setters? May be we need to come up with either custom annotation or something like https://github.com/rzwitserloot/lombok. May be not necessarily in this PR but subjective to discussion if there is any scope of improvement.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Read the user review who was using the lombok for more than a year: http://stackoverflow.com/a/12807937/1505987

I would recommend to stay away from lombok, even though I never used it personally. My justification would be obviously a javadoc and everytime we will have to generate the code.

Copy link
Member

Choose a reason for hiding this comment

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

I also don't recommend lombock. But i liked their way of using annotations.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure that it is necessary to keep this method. Maybe it is more senseful to keep only getter for package and make this field final. @SrinivasanTarget What is your opinion?

Copy link
Member

Choose a reason for hiding this comment

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

+1 - Making this final would be a better option 👍

this.appPackage = appPackage;
}

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

/**
* Sets the app activity value.
*
* @param appActivity The app activity value.
*/
public void setAppActivity(String appActivity) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure that it is necessary to keep this method. Maybe it is more senseful to keep only getter for activity and make this field final. @SrinivasanTarget What is your opinion?

this.appActivity = 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:
Copy link
Contributor

Choose a reason for hiding this comment

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

@email2vimalraj I'm against this change as it is. Please mark these methods Deprecated and with the description to use new ones

* </p>
* <pre>
* {@code
* Activity activity = new Activity();
* activity.setAppPackage("com.foo");
* activity.setAppActivity(".bar");
* 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,50 @@
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",
final 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.setAppPackage("com.android.contacts");
activity.setAppActivity(".ContactsListActivity");
activity.setAppWaitPackage("com.android.contacts");
activity.setAppWaitActivity(".ContactsListActivity");
activity.setStopApp(false);
driver.startActivity(activity);
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