This repository has been archived by the owner on Jun 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81a2400
commit e77fe11
Showing
9 changed files
with
245 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...App/src/androidTest/java/com/paypal/example/paypalandroidsdkexample/test/PaymentTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.paypal.example.paypalandroidsdkexample.test; | ||
|
||
import android.test.suitebuilder.annotation.LargeTest; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import android.support.test.runner.AndroidJUnit4; | ||
|
||
import static com.lukekorth.deviceautomator.AutomatorAction.click; | ||
import static com.lukekorth.deviceautomator.AutomatorAssertion.text; | ||
import static com.lukekorth.deviceautomator.UiObjectMatcher.withText; | ||
import static com.lukekorth.deviceautomator.UiObjectMatcher.withTextStartingWith; | ||
import static com.paypal.example.paypalandroidsdkexample.test.TestHelper.onDeviceWithScreenShot; | ||
import static com.paypal.example.paypalandroidsdkexample.test.TestHelper.onLogin; | ||
import static com.paypal.example.paypalandroidsdkexample.test.TestHelper.onAgree; | ||
import static com.paypal.example.paypalandroidsdkexample.test.TestHelper.takeScreenshot; | ||
import static org.hamcrest.CoreMatchers.containsString; | ||
|
||
|
||
@RunWith(AndroidJUnit4.class) | ||
@LargeTest | ||
public class PaymentTest { | ||
|
||
@Before | ||
public void setup() { | ||
TestHelper.setup(); | ||
} | ||
|
||
/** | ||
* Makes a Single Payment | ||
*/ | ||
@Test(timeout = 60000) | ||
public void buyAThing() { | ||
onDeviceWithScreenShot(withText("Buy a Thing")).perform(click()); | ||
onDeviceWithScreenShot(withText("Pay with")).perform(click()); | ||
onLogin(); | ||
// Payment Confirmation | ||
onDeviceWithScreenShot(withText("Pay")).perform(click()); | ||
// Confirm Result Success | ||
onDeviceWithScreenShot(withTextStartingWith("Result :")).check(text(containsString("PaymentConfirmation"))); | ||
} | ||
|
||
/** | ||
* Gets a Future Payment Consent | ||
*/ | ||
@Test(timeout = 60000) | ||
public void futurePaymentConsent() { | ||
takeScreenshot("futurePaymentConsent"); | ||
onDeviceWithScreenShot(withText("Future Payment Consent")).perform(click()); | ||
onLogin(); | ||
onAgree(); | ||
// Confirm Result Success | ||
onDeviceWithScreenShot(withTextStartingWith("Result :")).check(text(containsString("Future Payment"))); | ||
|
||
} | ||
|
||
/** | ||
* Makes a Future Payment Purchase | ||
*/ | ||
@Test(timeout = 60000) | ||
public void futurePaymentPurchase() { | ||
takeScreenshot("futurePaymentPurchase"); | ||
onDeviceWithScreenShot(withText("Future Payment Purchase")).perform(click()); | ||
// Confirm Result Success | ||
onDeviceWithScreenShot(withTextStartingWith("Result :")).check(text(containsString("Client Metadata Id"))); | ||
} | ||
|
||
/** | ||
* Gets Profile Sharing Consent | ||
*/ | ||
@Test(timeout = 60000) | ||
public void profileSharingConsent() { | ||
takeScreenshot("profileSharingConsent"); | ||
onDeviceWithScreenShot(withText("Profile Sharing Consent")).perform(click()); | ||
onLogin(); | ||
onAgree(); | ||
// Confirm Result Success | ||
onDeviceWithScreenShot(withTextStartingWith("Result :")).check(text(containsString("Profile Sharing code"))); | ||
} | ||
|
||
} |
125 changes: 125 additions & 0 deletions
125
...eApp/src/androidTest/java/com/paypal/example/paypalandroidsdkexample/test/TestHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.paypal.example.paypalandroidsdkexample.test; | ||
|
||
import android.app.Activity; | ||
import android.graphics.Bitmap; | ||
import android.os.Environment; | ||
import android.preference.PreferenceManager; | ||
import android.support.test.runner.lifecycle.ActivityLifecycleMonitorRegistry; | ||
import android.support.test.runner.lifecycle.Stage; | ||
import android.text.format.DateFormat; | ||
import android.view.View; | ||
|
||
import com.lukekorth.deviceautomator.DeviceAutomator; | ||
import com.lukekorth.deviceautomator.UiObjectMatcher; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.io.FileOutputStream; | ||
import java.io.OutputStream; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.Iterator; | ||
|
||
import static android.support.test.InstrumentationRegistry.getInstrumentation; | ||
import static android.support.test.InstrumentationRegistry.getTargetContext; | ||
import static com.lukekorth.deviceautomator.AutomatorAction.click; | ||
import static com.lukekorth.deviceautomator.AutomatorAction.setText; | ||
import static com.lukekorth.deviceautomator.DeviceAutomator.onDevice; | ||
import static com.lukekorth.deviceautomator.UiObjectMatcher.withContentDescription; | ||
import static com.lukekorth.deviceautomator.UiObjectMatcher.withText; | ||
|
||
/** | ||
* This helper class provides with methods to help write better tests. | ||
*/ | ||
public abstract class TestHelper { | ||
|
||
public static final String PAYPAL_SAMPLE_APP_PACKAGE_NAME = "com.paypal.example.paypalandroidsdkexample.debug"; | ||
public static final String PAYPAL_SANDBOX_USERNAME = "[email protected]"; | ||
public static final String PAYPAL_SANDBOX_PASSWORD = "123123123"; | ||
|
||
public static void setup() { | ||
PreferenceManager.getDefaultSharedPreferences(getTargetContext()) | ||
.edit() | ||
.clear() | ||
.commit(); | ||
onDevice().onHomeScreen().launchApp(PAYPAL_SAMPLE_APP_PACKAGE_NAME); | ||
} | ||
|
||
public static DeviceAutomator onDeviceWithScreenShot() { | ||
DeviceAutomator result = onDevice(); | ||
takeScreenshot(null); | ||
return result; | ||
} | ||
|
||
public static DeviceAutomator onDeviceWithScreenShot(UiObjectMatcher matcher) { | ||
DeviceAutomator result = onDevice(matcher); | ||
takeScreenshot(null); | ||
return result; | ||
} | ||
|
||
public static void takeScreenshot(String name) { | ||
if (name == null) { | ||
Date now = new Date(); | ||
DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); | ||
name = String.valueOf(now.getTime()); | ||
} | ||
// In Testdroid Cloud, taken screenshots are always stored | ||
// under /test-screenshots/ folder and this ensures those screenshots | ||
// be shown under Test Results | ||
String path = | ||
Environment.getExternalStorageDirectory().getAbsolutePath() + "/test-screenshots/" + name + ".png"; | ||
OutputStream out = null; | ||
|
||
try { | ||
View scrView = getActivityInstance().getWindow().getDecorView().getRootView(); | ||
scrView.setDrawingCacheEnabled(true); | ||
Bitmap bm = scrView.getDrawingCache(); | ||
Bitmap bitmap = Bitmap.createBitmap(bm); | ||
scrView.setDrawingCacheEnabled(false); | ||
|
||
File imageFile = new File(path); | ||
out = new FileOutputStream(imageFile); | ||
bitmap.compress(Bitmap.CompressFormat.PNG, 90, out); | ||
out.flush(); | ||
} catch (Exception ex) { | ||
// Ignore any exceptions as this is just for debugging purposes | ||
} finally { | ||
try { | ||
if (out != null) { | ||
out.close(); | ||
} | ||
} catch (Exception ex) { | ||
// Nothing to do here | ||
} | ||
} | ||
} | ||
|
||
public static Activity getActivityInstance() { | ||
final Activity[] currentActivity = new Activity[1]; | ||
getInstrumentation().waitForIdleSync(); | ||
getInstrumentation().runOnMainSync(new Runnable() { | ||
public void run() { | ||
Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED); | ||
Iterator<Activity> iterator = resumedActivities.iterator(); | ||
while (iterator.hasNext()) { | ||
currentActivity[0] = (Activity) iterator.next(); | ||
} | ||
} | ||
}); | ||
return currentActivity[0]; | ||
} | ||
|
||
public static void onLogin() { | ||
onDeviceWithScreenShot(withContentDescription("Email")).perform(click(), setText(PAYPAL_SANDBOX_USERNAME)); | ||
onDeviceWithScreenShot().pressDPadDown().typeText(PAYPAL_SANDBOX_PASSWORD); | ||
onDeviceWithScreenShot(withText("Log In")).perform(click()); | ||
} | ||
|
||
public static void onAgree() { | ||
try { | ||
onDeviceWithScreenShot(withText("Agree")).perform(click()); | ||
} catch (RuntimeException ex) { | ||
// This may mean, he has already agreed to it before. | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters