Fal*_*Bot 22 android ui-testing robotium android-espresso
我已经决定使用Google的Espresso进行应用程序测试的测试标准之一是:
测试应在屏幕方向旋转后保持活动状态
使用Espresso时如何旋转屏幕?
我尝试了以下Robotium代码(是的,我在我的Espresso测试中放置了Robotium代码,所以起诉我)
solo.setActivityOrientation(solo.LANDSCAPE);
solo.setActivityOrientation(solo.PORTRAIT);
Run Code Online (Sandbox Code Playgroud)
但是当我在Espresso测试中运行它时,它会崩溃应用程序.
有没有办法做到这一点?
在此先感谢您的帮助
Sla*_*ava 18
如果您的测试用例中只有Activity,则可以执行以下操作:
Rule
.@Rule
public ActivityTestRule<TestActivity> mActivityTestRule = new ActivityTestRule<>(TestActivity.class);
Run Code Online (Sandbox Code Playgroud)
Activity
并应用屏幕旋转.mActivityTestRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mActivityTestRule.getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
Run Code Online (Sandbox Code Playgroud)
那是一块馅饼!
lel*_*man 10
你可以用uiautomator库来做
dependencies {
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
}
Run Code Online (Sandbox Code Playgroud)
ui automator需要min sdk版本18,所以如果你的应用程序有一个较低的min sdk你需要AndroidManifest.xml
在androidTest
文件夹中创建一个新的
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:tools="http://schemas.android.com/tools"
package="your.package.name">
<uses-sdk tools:overrideLibrary="android.support.test.uiautomator.v18"/>
</manifest>
Run Code Online (Sandbox Code Playgroud)
然后在你的测试中
UiDevice device = UiDevice.getInstance(getInstrumentation());
device.setOrientationLeft();
device.setOrientationNatural();
device.setOrientationRight();
Run Code Online (Sandbox Code Playgroud)
如何旋转屏幕:
public static void rotateScreen(Activity activity) {
final CountDownLatch countDownLatch = new CountDownLatch(1);
final int orientation = InstrumentationRegistry.getTargetContext()
.getResources()
.getConfiguration()
.orientation;
final int newOrientation = (orientation == Configuration.ORIENTATION_PORTRAIT) ?
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE :
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
activity.setRequestedOrientation(newOrientation);
getInstrumentation().waitForIdle(new Runnable() {
@Override
public void run() {
countDownLatch.countDown();
}
});
try {
countDownLatch.await();
} catch (InterruptedException e) {
throw new RuntimeException("Screen rotation failed", e);
}
}
Run Code Online (Sandbox Code Playgroud)
可以从ActivityRuleActivity
获取。
这个更完整的解决方案创建了一个定制的 EspressoViewAction
并且运行良好。它显示了如何在调用其方法之前获取Activity
(即使它是AppCompatActivity
)setRequestedOrientation()
。它还有一个干净的调用者界面:
onView(isRoot()).perform(orientationLandscape());
onView(isRoot()).perform(orientationPortrait());
Run Code Online (Sandbox Code Playgroud)
我以 100 毫秒的延迟跟踪每个方向变化,尽管您可能不需要它。
您不能混合 Robotium 和 Espresso 测试。有时解决任何问题的最佳方法是检查所需但不兼容方法的源代码。
我很确定您已经有了setUp()
方法,其中的代码如下:
myActivity = this.getActivity();
使用它来更改屏幕方向:
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
或者
myActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
您可能还需要使用 myActivity.getInstrumentation().waitForIdleSync();
orThread.sleep(milliseconds);
来等待轮换结束,因为它是以异步方式执行的。第二种方法取决于模拟器/设备,因此请明智地选择。
希望有帮助。
归档时间: |
|
查看次数: |
7423 次 |
最近记录: |