2016年3月11日,星期五

浓咖啡资源闲置示例


浓咖啡 的优势在于其能够无缝同步所有测试操作与被测应用程序的能力。默认情况下,Espresso等待当前消息队列中的UI事件得到处理,并等待AsyncTask的默认实例完成,然后再进行下一个测试操作。

但是,在某些情况下,应用程序会使用非标准方式(例如直接创建和管理线程)执行后台操作,例如与Web服务通信。

在这种情况下,您必须使用空闲资源来通知Espresso应用程序’长期运行的操作。

1.修改  java class MainActivity.java 应用程式 >src->Main.
2.添加方法 getIdlingResource () 如下例所示。

package com.example.android.testing.espresso.IdlingResourceSample;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.VisibleForTesting;
import android.support.test.espresso.IdlingResource;
import android.support.test.espresso.idling.CountingIdlingResource;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.android.testing.espresso.IdlingResourceSample.IdlingResource.SimpleIdlingResource;

/**
 * Gets a text String from the user and displays it back after a while.
 */
public class MainActivity extends Activity implements View.OnClickListener,
        MessageDelayer.DelayerCallback {

    // The TextView used to display the message  内  the Activity.
    private TextView mTextView;

    // The EditText where the user types the message.
    private EditText mEditText;

    // The Idling Resource which will be null in production.
    @Nullable private SimpleIdlingResource mIdlingResource;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set the listeners for the buttons.
        findViewById(R.id.changeTextBt).setOnClickListener(this);

        mTextView = (TextView) findViewById(R.id.textToBeChanged);
        mEditText = (EditText) findViewById(R.id.editTextUserInput);
    }

    @Override
    public void onClick(View view) {
        // Get the text from the EditText view.
        final String text = mEditText.getText () .toString () ;

        if (view.getId ()  == R.id.changeTextBt) {
            // Set a temporary text.
            mTextView.setText(R.string.waiting_msg);
            // Submit the message to the delayer.
            MessageDelayer.processMessage(text, this, mIdlingResource);
        }
    }

    @Override
    public void onDone(String text) {
        // The delayer notifies the activity via a callback.
        mTextView.setText(text);
    }

    /**
     * Only called from test, creates and returns a new {@link SimpleIdlingResource}.
     */
    @VisibleForTesting
    @NonNull
    public IdlingResource   getIdlingResource   ()  {
        if (mIdlingResource == null) {
            mIdlingResource = new SimpleIdlingResource () ;
        }
        return mIdlingResource;
    }
}

3.向其中注册一个或多个空闲资源 Espresso by calling 浓咖啡 .registerIdlingResource() 在测试设置中,然后在拆卸中,我们应该使用代码注销
  浓咖啡 .unregisterIdlingResources(mIdlingResource);



/*
 *  版权 2016, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.testing.espresso.IdlingResourceSample;

import static android.support.test.espresso. 浓咖啡 .onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;

import android.support.test.espresso.Espresso;
import android.support.test.espresso.IdlingResource;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;


/**
 * Same as  浓咖啡 's BasicSample, but with an Idling Resource to help with synchronization.
 */
@RunWith(AndroidJUnit4.class)
@LargeTest
public class ChangeTextBehaviorTest {

    private static final String STRING_TO_BE_TYPED = " 浓咖啡 ";

    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
            MainActivity.class);

    private IdlingResource mIdlingResource;

    @Before
    public void registerIdlingResource ()  {
        mIdlingResource = mActivityRule.getActivity () .  getIdlingResource   () ;
        // To prove that the test fails, omit this call:
         浓咖啡 .registerIdlingResources(mIdlingResource);
    }

    @Test
    public void changeText_sameActivity ()  {
        // Type text and then press the button.
        onView(withId(R.id.editTextUserInput))
                .perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard () );
        onView(withId(R.id.changeTextBt)).perform(click () );

        // Check that the text was changed.
        onView(withId(R.id.textToBeChanged)).check(matches(withText(STRING_TO_BE_TYPED)));
    }

    @After
    public void unregisterIdlingResource ()  {
        if (mIdlingResource != null) {
             浓咖啡 .unregisterIdlingResources(mIdlingResource);
        }
    }
}

我希望这篇文章可以帮助您找到测试服的代码范围:)
请在下面的评论部分中分享您的反馈,并按照质量检查自动化以获取最新的帖子更新。HappyTesting :-)
 

4条评论:

  1. 在isIdleNow方法中调用时,您忽略了必须创建RepeatService的事实。您还必须解释为什么需要此类。

    回复 删除
  2. 我是Espresso UI Testing的新手。我正在测试一个混合应用程序,该应用程序将调用Web浏览器(而非Web View),并在其中导航到登录网页。登录后,它将返回到该应用程序。在Espresso中,它没有显示我在Web浏览器中执行的操作。我想知道如何将登录活动包含在Web浏览器中,作为espresso UI测试的一部分。
    提前致谢

    回复 删除
  3. 如以下示例所示,此方法getIdlingResource()必须添加到应用程序的每个活动中。

    回复 删除