我一直在执行的Android ES preSSO测试了一个星期。什么是真实的东西 - 正在实施服务器呼叫,它等待与ES preSSO。这就是所谓的空闲能源,矿产资源调用,我们必须遵循的规则,至极是pretty的直forvard。其实我发现了解决方案,但结果是shoking - 我succed只有当我做注释行
居presso.onView(ViewMatchers.withId(R.id.email))执行(ViewActions.typeText(有些事情));
居presso.onView(ViewMatchers.withId(R.id.password))执行(ViewActions.typeText(123))。
和哈克代替它们:
最后的EditText电子邮件=(EditText上)act.findViewById(R.id.email);
最后的EditText密码=(EditText上)act.findViewById(R.id.password);
getInstrumentation()。runOnMainSync(新的Runnable(){
公共无效的run(){
email.setText(工程师);
password.setText(2342);
}
});
...点击按钮启动新的活动后,模拟电话到服务器之前。 这是我的整个文件: build.gradle:
应用插件:com.android.application
安卓{
compileSdkVersion 21
buildToolsVersion21.1.2
defaultConfig {
的applicationIDshoppinglist.kizema.anton.testappes preSSO
的minSdkVersion 14
targetSdkVersion 21
版本code 1
VERSIONNAME1.0
testInstrumentationRunnerandroid.support.test.runner.AndroidJUnitRunner
}
buildTypes {
推出 {
minifyEnabled假
proguardFiles getDefaultProguardFile('ProGuard的-android.txt'),'proguard-rules.pro
}
}
packagingOptions {
排除的LICENSE.txt
不包括META-INF / notice.txt
不包括META-INF / LICENSE.TXT
不包括META-INF / ASL2.0
不包括META-INF / LICENSE.TXT
不包括META-INF /许可证
不包括META-INF /注意事项
不包括META-INF / NOTICE.txt
不包括META-INF / DEPENDENCIES
}
}
依赖{
编译文件树(导演:库,包括:['的* .jar'])
编译com.android.support:appcompat-v7:21.0.3
//应用程序的依赖关系,包括测试
编译com.android.support:support-annotations:21.0.3
//测试只依赖
androidTestCompilecom.android.support.test:测试支持-lib目录下:0.1
androidTestCompilecom.android.support.test.es preSSO:ES preSSO核心:2.0
}
LoginActivity(一活动):
//设置初始监听器
私人无效initLoginHelper(){
loginHelper =新服务器(){
@覆盖
公共无效登录(字符串email,字符串code,字符串电话,字符串password,布尔loginByPhoneNumber){
//问服务器
尝试 {
视频下载(3000);
}赶上(InterruptedException异常E){
e.printStackTrace();
}
//完成
意向意图=新的意图(LoginActivity.this,SecondActivity.class);
startActivityForResult(意向,0);
}
};
}
// onButtonClick处理器
公共无效btnLogInSuka(视图v){
performLogin();
}
无效performLogin(){
新主题(新的Runnable(){
@覆盖
公共无效的run(){
loginHelper.login(emailParam,codeParam,phoneParam,passwordParam,假);
}
})。开始();
}
和AplicationTest.java(ES preSSO测试): @LargeTest 公共类ApplicationTest扩展ActivityInstrumentationTestCase2 {
公共ApplicationTest(){
超(LoginActivity.class);
}
CountingIdlingResource idleRes;
@覆盖
公共无效设置()抛出异常{
super.setUp();
getActivity();
idleRes =新CountingIdlingResource(服务器);
居presso.registerIdlingResources(idleRes);
}
公共无效testSample(){
最后LoginActivity行为=(LoginActivity)getCurrentActivity();
服务器aHelper = act.getUserHelper();
MyUserHelperExternalIdleRes helper2 =新MyUserHelperExternalIdleRes(idleRes,aHelper);
act.setUserHelper(helper2);
//如果注释这并取消接下来的两行,我们收到PerformException
最后的EditText电子邮件=(EditText上)act.findViewById(R.id.email);
最后的EditText密码=(EditText上)act.findViewById(R.id.password);
getInstrumentation()。runOnMainSync(新的Runnable(){
公共无效的run(){
email.setText(工程师);
password.setText(2342);
}
});
//居presso.onView(ViewMatchers.withId(R.id.email))执行(ViewActions.typeText(有些事情))。
//居presso.onView(ViewMatchers.withId(R.id.password))执行(ViewActions.typeText(123))。
居presso.closeSoftKeyboard();
Es$p$psso.onView(ViewMatchers.withId(R.id.btnLogIn)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
居presso.onView(ViewMatchers.withId(R.id.btnLogIn))执行(ViewActions.click())。
Es$p$psso.onView(ViewMatchers.withId(R.id.secondActivityOpened)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
居preSSO pressBack()。
居presso.closeSoftKeyboard();
居presso.onView(ViewMatchers.withId(R.id.btnLogIn))执行(ViewActions.click())。
}
活性getCurrentActivity(){
。getInstrumentation()waitForIdleSync();
最后活动【活动=新活动[1];
尝试 {
runTestOnUiThread(新的Runnable(){
@覆盖
公共无效的run(){
java.util.Collection的<活动> activites = ActivityLifecycleMonitorRegistry.getInstance()getActivitiesInStage(Stage.RESUMED)。
活性[0] = Iterables.getOnlyElement(活动费);
}});
}赶上(的Throwable抛出){
throwable.printStackTrace();
}
返回活性[0];
}
类MyUserHelperExternalIdleRes实现服务器{
私人服务器aHelper;
私人CountingIdlingResource udleRes;
公共MyUserHelperExternalIdleRes(CountingIdlingResource udleRes,服务器aHelper){
this.aHelper = aHelper;
this.udleRes = udleRes;
}
@覆盖
公共无效登录(字符串email,字符串code,字符串电话,字符串password,布尔loginByPhoneNumber){
udleRes.increment();
尝试 {
aHelper.login(电子邮件,code,手机,密码,loginByPhoneNumber);
} 最后 {
udleRes.decrement();
}
}
}
}
所以,如果我们这样做实际上是基本的ES preSSO操作: 居presso.onView(ViewMatchers.withId(R.id.password))。执行(ViewActions.typeText (123));
我们收到PerformException:找不到按钮按ID。 如果我们做一个哈克(与设定的Runnable UI线程)我们succed这个简单的演示。 在我的主要的应用程序还有其他的bug ES preSSO(下称哈克上面写的没有工作,我们收到了同样的mistke)。为了确保我有一些WERY棘手的错误,我想一些与工程(ES preSSO)的设置是错误的 - 我是太糟糕了有摇篮。 请帮我这个,矿石提供一个链接到Android工作室的示例应用程序与ES preSSO测试(我发现没有,所有的应用程序都是很重要配置(无摇篮),并将其导入到Android的工作室后,我无法启动它们与
gradlew connectedAndroidTest
解决方案
我有同样的问题,我认为主要的问题是,按钮,会打开一个新的缩进,但目前看来,这本身并不造成这个错误,这是很奇怪的,它只有当你调用点击()之前的TypeText上一个EditText发生。
PS:我设法工作说出来,好像我有一对夫妇的一倍依赖问题,一旦我解决了,在测试中表现出没有任何问题,而不需要到各地做任何工作
我build.gradle的ES preSSO部分结束是这样的:
相关性{
库{
mavenCentral()
}
//居preSSO
编译org.apache.commons:公地lang3:3.1
androidTestCompile('com.android.support.test.es preSSO:ES preSSO核心:2.0'){
排除组:com.google.guava
排除模块:ES preSSO怠速资源
}
androidTestCompile('com.android.support.test:测试支持-lib目录下:0.1'){
排除组:com.google.guava
排除模块:ES preSSO怠速资源
}
}
我还添加了这种依赖关系之前:
配置{
androidTestCompile.exclude组:com.android.support
androidTestCompile.exclude模块:支持-V4
androidTestCompile.exclude模块:appcompat-V7
}
I have been implementing android espresso test for a week. What is the real thing - is implementing server call and wait for it with espresso. This is called Idle Resourse call, and we have to follow rules, wich are pretty straight forvard. Actually I found solution, but the result is shoking - I am succed only if I do comment lines
Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("some shit"));
Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("123"));
and replacing them with "huck":
final EditText email = (EditText) act.findViewById(R.id.email);
final EditText password = (EditText) act.findViewById(R.id.password);
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
email.setText("Engineer");
password.setText("2342");
}
});
..before clicking on button that launch new activity after emulated call to the server. This are my whole files: build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "shoppinglist.kizema.anton.testappespresso"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'LICENSE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/ASL2.0'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/DEPENDENCIES'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
// App's dependencies, including test
compile 'com.android.support:support-annotations:21.0.3'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
}
LoginActivity (First activity):
//set up initial listener
private void initLoginHelper(){
loginHelper = new Server() {
@Override
public void login(String email, String code, String phone, String password, boolean loginByPhoneNumber) {
//ask server
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//done
Intent intent = new Intent(LoginActivity.this, SecondActivity.class);
startActivityForResult(intent, 0);
}
};
}
//onButtonClick handler
public void btnLogInSuka(View v) {
performLogin();
}
void performLogin() {
new Thread(new Runnable() {
@Override
public void run() {
loginHelper.login(emailParam,codeParam,phoneParam,passwordParam,false);
}
}).start();
}
And AplicationTest.java (espresso tests): @LargeTest public class ApplicationTest extends ActivityInstrumentationTestCase2 {
public ApplicationTest() {
super(LoginActivity.class);
}
CountingIdlingResource idleRes;
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
idleRes = new CountingIdlingResource("server");
Espresso.registerIdlingResources(idleRes);
}
public void testSample(){
final LoginActivity act = (LoginActivity) getCurrentActivity();
Server aHelper = act.getUserHelper();
MyUserHelperExternalIdleRes helper2 = new MyUserHelperExternalIdleRes(idleRes, aHelper);
act.setUserHelper(helper2);
//if comment this and uncomment next two lines we receive PerformException
final EditText email = (EditText) act.findViewById(R.id.email);
final EditText password = (EditText) act.findViewById(R.id.password);
getInstrumentation().runOnMainSync(new Runnable() {
public void run() {
email.setText("Engineer");
password.setText("2342");
}
});
// Espresso.onView(ViewMatchers.withId(R.id.email)).perform(ViewActions.typeText("some shit"));
// Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText("123"));
Espresso.closeSoftKeyboard();
Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click());
Espresso.onView(ViewMatchers.withId(R.id.secondActivityOpened)).check(ViewAssertions.matches(ViewMatchers.isDisplayed()));
Espresso.pressBack();
Espresso.closeSoftKeyboard();
Espresso.onView(ViewMatchers.withId(R.id.btnLogIn)).perform(ViewActions.click());
}
Activity getCurrentActivity() {
getInstrumentation().waitForIdleSync();
final Activity[] activity = new Activity[1];
try {
runTestOnUiThread(new Runnable() {
@Override
public void run() {
java.util.Collection<Activity> activites = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(Stage.RESUMED);
activity[0] = Iterables.getOnlyElement(activites);
}});
} catch (Throwable throwable) {
throwable.printStackTrace();
}
return activity[0];
}
class MyUserHelperExternalIdleRes implements Server {
private Server aHelper;
private CountingIdlingResource udleRes;
public MyUserHelperExternalIdleRes(CountingIdlingResource udleRes, Server aHelper) {
this.aHelper = aHelper;
this.udleRes = udleRes;
}
@Override
public void login(String email, String code, String phone, String password, boolean loginByPhoneNumber) {
udleRes.increment();
try {
aHelper.login(email,code, phone,password,loginByPhoneNumber);
} finally {
udleRes.decrement();
}
}
}
}
So, if we do actually basic espresso operation: Espresso.onView(ViewMatchers.withId(R.id.password)).perform(ViewActions.typeText ("123"));
We receive PerformException : can not find button by id. If we do a huck, (with setting Runnable on UI thread) we are succed with this simple demo. In my main app there are other bugs with espresso (the "huck" written above did not work, we receive the same mistke). For sure I am having some wery tricky mistake, I suppose something with project(espresso ) settings is wrong - I am too bad with gradle. Please help me with this, ore provide a link to android studio sample app with espresso tests (I found none, all apps are badly configured (no gradle), and after import them to android studio, I can not launch them with
gradlew connectedAndroidTest
解决方案
I'm having the same issue, and I think the main problem is that, the button, opens a new Indent, but seems that this alone don't cause the bug, which is very strange, it only happens when you typeText on an EditText before calling the click().
P.S: I managed to work that out, seems like I had a couple of doubled dependency problems, once I solved that, the tests worked out without any problem and without the need to do any work around.
My build.gradle part of the espresso ended like this:
dependencies {
repositories {
mavenCentral()
}
// Espresso
compile 'org.apache.commons:commons-lang3:3.1'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
exclude group: 'com.google.guava'
exclude module: 'espresso-idling-resource'
}
androidTestCompile('com.android.support.test:testing-support-lib:0.1') {
exclude group: 'com.google.guava'
exclude module: 'espresso-idling-resource'
}
}
I also added this before dependencies:
configurations {
androidTestCompile.exclude group: 'com.android.support'
androidTestCompile.exclude module: 'support-v4'
androidTestCompile.exclude module: 'appcompat-v7'
}