-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from CAMOBAP/bugfix/refactor-android-ui-tests
Bugfix/refactor android UI tests
- Loading branch information
Showing
9 changed files
with
321 additions
and
49 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<html> | ||
<head> | ||
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/> | ||
</head> | ||
<body> | ||
<div id="hcaptcha-container"></div> | ||
|
||
<input id="input-text" /> | ||
|
||
<button id="on-error" onclick="onError()">Error</button> | ||
<button id="on-pass" onclick="onPass()">Pass</button> | ||
|
||
<script type="text/javascript"> | ||
var DI = window.JSDI.getDebugInfo(); | ||
var BridgeObject = window.JSInterface; | ||
var bridgeConfig = JSON.parse(BridgeObject.getConfig()); | ||
|
||
function onHcaptchaLoaded() { | ||
try { | ||
BridgeObject.onLoaded(); | ||
} catch (e) { | ||
BridgeObject.onError(29); | ||
} | ||
} | ||
|
||
function onPass() { | ||
const token = document.getElementById("input-text").value; | ||
BridgeObject.onPass(token); | ||
} | ||
|
||
function onError() { | ||
const errorCode = parseInt(document.getElementById("input-text").value); | ||
BridgeObject.onError(errorCode); | ||
} | ||
|
||
onHcaptchaLoaded(); | ||
</script> | ||
</body> | ||
</html> |
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
126 changes: 99 additions & 27 deletions
126
sdk/src/androidTest/java/com/hcaptcha/sdk/HCaptchaDialogFragmentTest.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 |
---|---|---|
@@ -1,69 +1,141 @@ | ||
package com.hcaptcha.sdk; | ||
|
||
import android.os.Bundle; | ||
import android.webkit.WebView; | ||
import android.util.Log; | ||
|
||
import androidx.fragment.app.testing.FragmentScenario; | ||
import androidx.test.espresso.web.webdriver.DriverAtoms; | ||
import androidx.test.espresso.web.webdriver.Locator; | ||
import androidx.test.ext.junit.runners.AndroidJUnit4; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import static androidx.test.espresso.Espresso.onView; | ||
import static androidx.test.espresso.assertion.ViewAssertions.matches; | ||
import static androidx.test.espresso.matcher.ViewMatchers.*; | ||
import static androidx.test.espresso.web.sugar.Web.onWebView; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.clearElement; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.findElement; | ||
import static androidx.test.espresso.web.webdriver.DriverAtoms.webClick; | ||
import static com.hcaptcha.sdk.AssertUtil.waitToBeDisplayed; | ||
import static com.hcaptcha.sdk.AssertUtil.waitToDisappear; | ||
import static com.hcaptcha.sdk.HCaptchaDialogFragment.KEY_CONFIG; | ||
import static com.hcaptcha.sdk.HCaptchaDialogFragment.KEY_LISTENER; | ||
import static org.hamcrest.Matchers.not; | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
|
||
@RunWith(AndroidJUnit4.class) | ||
public class HCaptchaDialogFragmentTest { | ||
public class HCaptchaDialogTestAdapter extends HCaptchaDialogListener { | ||
@Override | ||
void onSuccess(HCaptchaTokenResponse hCaptchaTokenResponse) { | ||
} | ||
|
||
@Override | ||
void onFailure(HCaptchaException hCaptchaException) { | ||
} | ||
}; | ||
|
||
public FragmentScenario<HCaptchaDialogFragment> launchCaptchaFragment() { | ||
return launchCaptchaFragment(true); | ||
} | ||
|
||
public FragmentScenario<HCaptchaDialogFragment> launchCaptchaFragment(boolean showLoader) { | ||
return launchCaptchaFragment(showLoader, new HCaptchaDialogTestAdapter()); | ||
} | ||
|
||
public FragmentScenario<HCaptchaDialogFragment> launchCaptchaFragment(HCaptchaDialogListener listener) { | ||
return launchCaptchaFragment(true, listener); | ||
} | ||
|
||
public FragmentScenario<HCaptchaDialogFragment> getTestScenario() { | ||
public FragmentScenario<HCaptchaDialogFragment> launchCaptchaFragment(boolean showLoader, HCaptchaDialogListener listener) { | ||
final HCaptchaConfig hCaptchaConfig = HCaptchaConfig.builder() | ||
.siteKey("10000000-ffff-ffff-ffff-000000000001") | ||
.endpoint("https://js.hcaptcha.com/1/api.js") | ||
.locale("en") | ||
.loading(showLoader) | ||
.size(HCaptchaSize.INVISIBLE) | ||
.theme(HCaptchaTheme.LIGHT) | ||
.build(); | ||
final Bundle args = new Bundle(); | ||
args.putSerializable(KEY_CONFIG, hCaptchaConfig); | ||
args.putParcelable(KEY_LISTENER, new HCaptchaDialogListener() { | ||
@Override | ||
void onSuccess(HCaptchaTokenResponse hCaptchaTokenResponse) { | ||
} | ||
|
||
@Override | ||
void onFailure(HCaptchaException hCaptchaException) { | ||
} | ||
}); | ||
return FragmentScenario.launch(HCaptchaDialogFragment.class, args); | ||
args.putParcelable(KEY_LISTENER, listener); | ||
return FragmentScenario.launchInContainer(HCaptchaDialogFragment.class, args); | ||
} | ||
|
||
@Test | ||
public void loader_is_visible_while_webview_is_loading() { | ||
getTestScenario(); | ||
public void loaderVisible() { | ||
launchCaptchaFragment(); | ||
onView(withId(R.id.loadingContainer)).check(matches(isDisplayed())); | ||
onView(withId(R.id.webView)).check(matches(not(isDisplayed()))); | ||
onView(withId(R.id.webView)).perform(waitToBeDisplayed(1000)); | ||
onView(withId(R.id.loadingContainer)).perform(waitToDisappear(10000)); | ||
} | ||
|
||
@Test | ||
public void loaderDisabled() { | ||
launchCaptchaFragment(false); | ||
onView(withId(R.id.loadingContainer)).check(matches(not(isDisplayed()))); | ||
onView(withId(R.id.webView)).perform(waitToBeDisplayed(1000)); | ||
} | ||
|
||
@Test | ||
public void webview_is_visible_after_loading() { | ||
final FragmentScenario<HCaptchaDialogFragment> scenario = getTestScenario(); | ||
scenario.onFragment(new FragmentScenario.FragmentAction<HCaptchaDialogFragment>() { | ||
public void webViewReturnToken() throws Exception { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
final HCaptchaDialogListener listener = new HCaptchaDialogTestAdapter() { | ||
@Override | ||
public void perform(@NotNull HCaptchaDialogFragment fragment) { | ||
assertNotNull(fragment.getDialog()); | ||
WebView.setWebContentsDebuggingEnabled(true); | ||
fragment.onLoaded(); | ||
void onSuccess(HCaptchaTokenResponse hCaptchaTokenResponse) { | ||
assertEquals("test-token", hCaptchaTokenResponse.getTokenResult()); | ||
latch.countDown(); | ||
} | ||
}); | ||
onView(withId(R.id.loadingContainer)).check(matches(not(isDisplayed()))); | ||
onView(withId(R.id.webView)).check(matches(isDisplayed())); | ||
onView(isRoot()).perform(waitToBeDisplayed(R.id.webView, 1000)); | ||
}; | ||
|
||
final FragmentScenario<HCaptchaDialogFragment> scenario = launchCaptchaFragment(listener); | ||
onView(withId(R.id.webView)).perform(waitToBeDisplayed(1000)); | ||
|
||
onWebView(withId(R.id.webView)).forceJavascriptEnabled(); | ||
|
||
onWebView().withElement(findElement(Locator.ID, "input-text")) | ||
.perform(clearElement()) | ||
.perform(DriverAtoms.webKeys("test-token")); | ||
|
||
onWebView().withElement(findElement(Locator.ID, "on-pass")) | ||
.perform(webClick()); | ||
|
||
assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); // wait for callback | ||
} | ||
|
||
@Test | ||
public void webViewReturnsError() throws Exception { | ||
CountDownLatch latch = new CountDownLatch(1); | ||
final HCaptchaDialogListener listener = new HCaptchaDialogTestAdapter() { | ||
@Override | ||
void onFailure(HCaptchaException hCaptchaException) { | ||
assertEquals(HCaptchaError.SESSION_TIMEOUT, hCaptchaException.getHCaptchaError()); | ||
latch.countDown(); | ||
} | ||
}; | ||
|
||
final FragmentScenario<HCaptchaDialogFragment> scenario = launchCaptchaFragment(listener); | ||
onView(withId(R.id.webView)).perform(waitToBeDisplayed(1000)); | ||
|
||
onWebView(withId(R.id.webView)).forceJavascriptEnabled(); | ||
|
||
onWebView().withElement(findElement(Locator.ID, "input-text")) | ||
.perform(clearElement()) | ||
.perform(DriverAtoms.webKeys( | ||
String.valueOf(HCaptchaError.SESSION_TIMEOUT.getErrorId()))); | ||
|
||
onWebView().withElement(findElement(Locator.ID, "on-error")) | ||
.perform(webClick()); | ||
|
||
assertTrue(latch.await(1000, TimeUnit.MILLISECONDS)); // wait for callback | ||
} | ||
} |
Oops, something went wrong.