android studio单元测试和自动化
android {compileSdkVersion 28defaultConfig {testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}// Required -- JUnit 4 frameworktestImplementation '...
·
android {
compileSdkVersion 28
defaultConfig {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
// Required -- JUnit 4 framework
testImplementation 'junit:junit:4.12'
// Optional -- Mockito framework(可选,用于模拟一些依赖对象,以达到隔离依赖的效果)
testImplementation 'org.mockito:mockito-core:2.19.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
package com.immortals.tw;
import android.Manifest;
import android.util.Log;
import com.global.sdk.permission.request.CheckPermission;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.List;
import androidx.test.espresso.Espresso;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.RootMatchers.isDialog;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
/**
* Created by lhy on 2020/2/22.
* click() 点击view
* clearText() 清除文本内容
* swipeLeft() 从右往左滑
* swipeRight() 从左往右滑
* swipeDown() 从上往下滑
* swipeUp() 从下往上滑
* click() 点击view
* closeSoftKeyboard() 关闭软键盘
* pressBack() 按下物理返回键
* doubleClick() 双击
* longClick() 长按
* scrollTo() 滚动
* replaceText() 替换文本
* openLinkWithText() 打开指定超链
* <p>
* View 没有显示:onView(...).check(matcher(not(isDisplayed())));
* View 不在视图结构中:onView(...).check(doesNotExist());
* <p>
* Toast并没有在我们的常规视图中,Android支持多窗口,如果我们使用常规的方式是无法检测到Toast的,所以这里需要使用inRoot()来进行对Toast的匹配:
* onView(withText("South China Sea"))
* .inRoot(withDecorView(not(is(getActivity().getWindow().getDecorView()))))
* .perform(click());
* <p>
* https://blog.csdn.net/jiashuai94/article/details/78792261?utm_source=distribute.pc_relevant.none-task
* https://blog.csdn.net/qiang_xi/article/details/52933593?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
*/
@RunWith(AndroidJUnit4.class)
@LargeTest
public class MainActivityTest {
final String TAG = this.getClass().getSimpleName();
@Rule
public ActivityTestRule<MainActivity> activityTestRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void permissionsNoticeTest()throws Exception {
// ViewInteraction appCompatButton = onView(allOf(withId(R.id.tv_close_game), isDisplayed()));
// //执行按钮的点击操作
// appCompatButton.perform(click());
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
List<String> list = CheckPermission.checkPermissionDenied(activityTestRule.getActivity(), permissions);
if (list.size() == 0) {
Log.i(TAG, "权限-已授权");
} else {
Log.i(TAG, "权限-申请开始");
Espresso.onView(withId(R.id.tv_close_game)).check(matches(isDisplayed()));
Log.i(TAG, "权限-权限弹窗展示成功");
onView(withId(R.id.tv_premissions_notices_kf)).perform(click());
try {
onView(withText("Allow")).inRoot(isDialog()).perform(click());
}catch (Exception e){
e.printStackTrace();
}
// onView(withText("Allow")).perform(click());
Log.i(TAG, "权限-申请结束");
}
// onView(withText("显示绑定页面123")).perform(click());
//通过id找到textview,并判断是否与文本匹配
//Espresso.onView(withId(R.id.textView)).check(matches(withText("计算结果:6")));
//通过文本"Item 0"找到view,并检查是否显示,然后执行点击事件,此时会弹出对话框
// Espresso.onView(withText("Item 0")).check(matches(isDisplayed())).perform(click());
//通过文本"Item 2"找到view,并检查是否显示,然后执行点击事件,此时会弹出对话框
// Espresso.onView(withText("Item 2")).check(matches(isDisplayed())).perform(click());
// 执行点击返回按钮事件,关闭对话框
// Espresso.pressBack();
//通过id找到recycleview,然后执行滑动事件,滑动到21项
// Espresso.onView(ViewMatchers.withId(R.id.recycleview)).perform(RecyclerViewActions.scrollToPosition(21));
//通过文本"Item 20"找到view,并检查是否显示,然后执行点击事件,此时会弹出对话框
// Espresso.onView(withText("Item 20")).check(matches(isDisplayed())).perform(click());
//通过name为"word"找到搜索输入框
/* onWebView().withElement(findElement(Locator.NAME, "word"))
//往输入框中输入字符串"android"
.perform(DriverAtoms.webKeys("android"))
//通过id为"index-bn"找到"百度一下"按钮
.withElement(DriverAtoms.findElement(Locator.ID, "index-bn"))
//执行点击事件
.perform(webClick())
//通过id为"results"找到结果div
.withElement(DriverAtoms.findElement(Locator.ID, "results"))
//检查div中是否包含字符串"android"
.check(WebViewAssertions.webMatches(DriverAtoms.getText(), Matchers.containsString("android")));*/
}
}
https://blog.csdn.net/weixin_40953487/article/details/87697404
更多推荐
已为社区贡献1条内容
所有评论(0)