在做Android測試時,很多公司都不給源代碼,僅僅只有apk包,這個給測試帶來了一定的困難。Robotium本身支持對單獨apk包進行測試,可能某些時候會出現一些小問題。下面是我使用robotium測試單獨包的過程。僅供參考。
1、下載AndroidCalculator.apk包,可以到官方網站去下載。
2、啓動eclipse,建立android工程,這步驟就不詳細描述了
3、在工程中編寫代碼:
import android.test.ActivityInstrumentationTestCase2;
import com.jayway.android.robotium.solo.Solo;
public class TestAPK extends ActivityInstrumentationTestCase2{
private static final String TARGET_PACKAGE_ID="com.calculator";
private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME="com.calculator.Main";
private static Class launcherActivityClass;
static{
try{
launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
}catch(ClassNotFoundException e){
new RuntimeException(e);
}
}
public TestAPK() throws ClassNotFoundException{
super(TARGET_PACKAGE_ID, launcherActivityClass);
}
private Solo solo;
@Override
protected void setUp() throws Exception{
solo = new Solo(getInstrumentation(), getActivity());
}
public void testDisplayBlackBox(){
solo.enterText(0, "10");
solo.enterText(1, "30");
solo.clickOnButton("Multiply");
assertTrue(solo.searchText("300"));
}
@Override
public void tearDown() throws Exception{
solo.finishOpenedActivities();
}
}
4、啓動模擬器AVD
5、重簽名AndroidCalculator.apk,一般使用工具,使用工具爲:re-sign.jar,將簽名修改爲debug模式
6、啓動cmd,輸入adb devices,查看設備連接狀態,連接正常,進行第7步
7、安裝重新簽名後的包,adb install AndroidCalculator_debug.apk
8、運行程序,Run as --> Android Junit Test.打開模擬器,驗證是否正常。
我遇到的坑有:
1、測試時沒有模擬用戶輸入,我的方式是:重新啓動後,添加了@SuppressWarnings("rawtypes"),就可以了。
2、重新簽名,re-sign有時候重新簽名不成功。