一、用户体验测试
用户体验是评估一个APP是否好用的重要指标,开发过程中需要进行全面测试。
1、界面测试
// 界面测试代码示例: public void testInterface() { onView(withId(R.id.button_login)).check(matches(isDisplayed())); onView(withText("注册")).check(matches(isClickable())); onView(withId(R.id.editText_username)).perform(typeText("test"), closeSoftKeyboard()); onView(withId(R.id.editText_password)).perform(typeText("123456"), closeSoftKeyboard()); onView(withId(R.id.button_login)).perform(click()); onView(withText("登录成功")).check(matches(isDisplayed())); }
2、功能测试
// 功能测试代码示例: public void testFunction() { onView(withId(R.id.editText_search)).perform(typeText("test"), closeSoftKeyboard()); onView(withId(R.id.button_search)).perform(click()); onView(withId(R.id.text_result)).check(matches(withText("搜索结果:test"))); }
3、易用性测试
易用性测试需要模拟用户的操作行为,从而评估APP的易用性。
// 易用性测试代码示例: public void testUsability() { onView(withId(R.id.button_login)).perform(scrollTo(), click()); onView(withId(R.id.editText_username)).perform(replaceText("")); onView(withId(R.id.editText_password)).perform(replaceText("")); onView(withId(R.id.button_login)).perform(click()); onView(withText("请输入用户名和密码")).check(matches(isDisplayed())); }
二、设备兼容性测试
在不同设备上测试,发现并修复因使用不同设备和系统而引起的问题。
1、不同分辨率测试
// 不同分辨率测试代码示例: public void testResolution() { onView(withId(R.id.button)).perform(click()); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { onView(withText("Popup Window")).inRoot(isPlatformPopup()).perform(click()); } else { onView(withText("Popup Window")).perform(click()); } onView(withId(R.id.popup_text)).check(matches(withText("Popup Window"))); }
2、不同操作系统测试
// 不同操作系统测试代码示例: public void testOS() { onView(withId(R.id.button)).perform(click()); onView(withText("Android OS")).perform(click()); onView(withId(R.id.os_text)).check(matches(withText("Android OS"))); }
3、不同设备测试
// 不同设备测试代码示例: public void testDevice() { onView(withId(R.id.button)).perform(click()); if ("Tablet".equals(getDeviceType())) { onView(withText("Tablet")).perform(click()); } else { onView(withText("Phone")).perform(click()); } onView(withId(R.id.device_text)).check(matches(withText(getDeviceType()))); }
三、安全性测试
APP应该保证用户的信息安全,防止数据泄露和其他安全问题。
1、离线数据安全测试
// 离线数据安全测试代码示例: public void testOfflineDataSecurity() { password = "123456"; String hash = encrypt(password); saveHash(hash); String savedHash = loadHash(); assertEquals(hash, savedHash); }
2、网络数据传输安全测试
// 网络数据传输安全测试代码示例: public void testNetworkDataSecurity() { onView(withId(R.id.button_login)).perform(click()); onView(withId(R.id.editText_username)).perform(typeText("test"), closeSoftKeyboard()); onView(withId(R.id.editText_password)).perform(typeText("123456"), closeSoftKeyboard()); onView(withId(R.id.button_login)).perform(click()); // 拦截网络请求,模拟HTTPS中间人攻击 MockWebServer server = new MockWebServer(); server.setProtocol("http/1.0"); server.useHttps(getSslContext().getSocketFactory(), false); server.enqueue(new MockResponse().setBody("登录成功")); server.start(); HttpUrl url = server.url("/api/login"); onView(withId(R.id.editText_url)).perform(replaceText(url.toString())); onView(withId(R.id.button_send)).perform(click()); onView(withId(R.id.text_result)).check(matches(withText("网络请求异常"))); }
3、权限测试
// 权限测试代码示例: public void testPermission() { ListdeniedPermissions = checkPermissions(); if (!deniedPermissions.isEmpty()) { grantPermissions(deniedPermissions); } onView(withId(R.id.button_camera)).perform(click()); onView(withId(R.id.image_preview)).check(matches(isDisplayed())); }
四、性能测试
通过不断的性能测试,优化APP的性能。
1、CPU和内存测试
// CPU和内存测试代码示例: public void testCPUAndMemory() { int count = 1000000; long startTime = System.currentTimeMillis(); for (int i = 0; i < count; i++) { Math.sqrt(i); } long endTime = System.currentTimeMillis(); long time = endTime - startTime; assertTrue("CPU测试失败,执行时间超过1秒。", time < 1000); Activity activity = getActivity(); Debug.MemoryInfo memoryInfo = new Debug.MemoryInfo(); Debug.getMemoryInfo(memoryInfo); long usedMemory = memoryInfo.getTotalPss() * 1024; assertTrue("内存测试失败,内存使用量超过100MB。", usedMemory < 100 * 1024 * 1024); }
2、网络速度测试
// 网络速度测试代码示例: public void testNetworkSpeed() { onView(withId(R.id.button_download)).perform(click()); long startTime = System.currentTimeMillis(); onView(withId(R.id.progress_bar)).perform(waitUntil(progressIs(100)), click()); long endTime = System.currentTimeMillis(); long time = (endTime - startTime) / 1000; assertTrue("网络速度测试失败,下载时间超过10秒。", time < 10); }
3、电量消耗测试
// 电量消耗测试代码示例: public void testBatteryConsumption() { onView(withId(R.id.button_play)).perform(click()); sleep(30000); onView(withId(R.id.button_pause)).perform(click()); IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED); Intent intent = getContext().registerReceiver(null, intentFilter); int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1); assertTrue("电量消耗测试失败,电量下降过快。", level > 70); }