一、用戶體驗測試
用戶體驗是評估一個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() { List deniedPermissions = 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); }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/197067.html