all = viewUtils.getAllViews(true);
802 | for (int i = 0; i < all.size(); i++) {
803 | if ((all.get(i).getId() == id)) {
804 | Log.i(LOG_TAG, "The view which id is: "+ i +":"+ all.get(i).getId()
805 | + " found succeed");
806 | WebView view = (WebView) all.get(i);
807 | //
808 | // view.addJavascriptInterface(JavascriptInterface.getInstance(), "webdriver");
809 | // view.loadUrl("javascript:var result=[];" +
810 | // " var element = document.getElementById('J_Postage');" +
811 | // " if (element != null) {" +
812 | // " result.push(element)};");
813 | // Log.d(LOG_TAG, view.getUrl() + "");
814 | // view.loadUrl("javascript:window.webdriver.excutejs(element)");
815 | // view.loadUrl("javascript:window.webdriver.excutejs(result[0])");
816 | // JavascriptInterface.getInstance().getResult();
817 | return view;
818 | }
819 | }
820 |
821 | try {
822 | Log.i(LOG_TAG, "View which id is " + id
823 | + " does not found, sleep");
824 | Thread.sleep(Constants.RETRY_TIME);
825 |
826 | } catch (InterruptedException e) {
827 | e.printStackTrace();
828 | }
829 | }
830 |
831 | TmtsLog.e(LOG_TAG, "View which id is " + id + " does not found in "
832 | + Constants.FIND_VIEW_TIME_OUT / 1000 + "seconds, time out");
833 | return null;
834 | }
835 |
836 | /**
837 | * Transform {@link String} id into {@link int} id.
838 | *
839 | * @param name
840 | * String name of view id, the string after @+id/ defined in
841 | * layout files.
842 | * @return {@link View}'s id defined in R.java.
843 | * @throws Exception
844 | * Exception.
845 | */
846 | public static int getIdByName(String name) throws Exception {
847 | String className = targetPkg + ".R$id";
848 | Class> id = null;
849 | id = Class.forName(className);
850 | Field idField = null;
851 | idField = id.getField(name);
852 |
853 | if (null != idField) {
854 | return idField.getInt(idField);
855 |
856 | } else {
857 | return -1;
858 | }
859 |
860 | }
861 |
862 | /**
863 | * Click a button on dialog by text,
864 | *
865 | * @param buttonText
866 | * Text of the button,
867 | * @throws InterruptedException
868 | * InterruptedException,
869 | */
870 | void clickDialogButton(String buttonText)
871 | throws InterruptedException {
872 | final TmtsButton button = findTmtsButton(buttonText);
873 |
874 | if (null != button) {
875 | button.doClick();
876 | }
877 | }
878 |
879 | /**
880 | * Click on {@link View} by the given text.
881 | *
882 | * @param text
883 | * Text of the view.
884 | * @throws InterruptedException
885 | */
886 | void clickOnViewByText(String text) throws InterruptedException {
887 | clickUtils.clickOnText(text, false, 1, true, 0);
888 | }
889 |
890 | /**
891 | * Scrolls up.
892 | *
893 | * @return {@code true} if more scrolling can be done.
894 | *
895 | */
896 | boolean scrollUp() {
897 | return scrollUtils.scroll(Constants.UP);
898 | }
899 |
900 | /**
901 | * Scroll down.
902 | * @return {@code true} if more scrolling can be done.
903 | */
904 | boolean scrollDown() {
905 | return scrollUtils.scroll(Constants.DOWN);
906 | }
907 |
908 |
909 | /**
910 | * Perform full scroll to top.
911 | */
912 | void fullScrollUp() {
913 | while (scrollUp()) {
914 | scrollUp();
915 | }
916 | }
917 |
918 | /**
919 | * Perform full scroll to bottom.
920 | */
921 | void fullScrollDown() {
922 | while (scrollDown()) {
923 | scrollDown();
924 | }
925 | }
926 |
927 | boolean scrollListDown() {
928 | try {
929 | Thread.sleep(Constants.ANR_TIME);
930 | } catch (InterruptedException e) {
931 | e.printStackTrace();
932 | }
933 | return scrollUtils.scrollList(ListView.class, 0, Constants.DOWN, null);
934 | }
935 |
936 | boolean scrollListUp() {
937 | try {
938 | Thread.sleep(Constants.ANR_TIME);
939 | } catch (InterruptedException e) {
940 | e.printStackTrace();
941 | }
942 | return scrollUtils.scrollList(ListView.class, 0, Constants.UP, null);
943 | }
944 |
945 | void scrollListBottom() {
946 | while (scrollListDown()) {
947 | scrollListDown();
948 | }
949 | }
950 |
951 | void scrollListTop() {
952 | while (scrollListUp()) {
953 | scrollListUp();
954 | }
955 | }
956 |
957 | protected void finalize() {
958 | try {
959 | activityUtils.finalize();
960 |
961 | } catch (Throwable e) {
962 | e.printStackTrace();
963 | }
964 | }
965 |
966 | /**
967 | * Private method to print log in this class.
968 | *
969 | * @param view
970 | * view
971 | * @param name
972 | * name of view
973 | * @param getMethod
974 | * Name of the method.
975 | */
976 | @SuppressWarnings("deprecation")
977 | private static void printLog(View view, String name, String getMethod) {
978 |
979 | if (null != view) {
980 | Log.i(LOG_TAG, getMethod + ": " + name + " succeed");
981 |
982 | } else {
983 | TmtsLog.e(LOG_TAG, getMethod + ": " + name + " failed");
984 | }
985 | }
986 | }
987 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/TmtsLog.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework;
20 |
21 | import java.io.File;
22 | import java.io.FileWriter;
23 | import java.io.IOException;
24 | import java.util.Calendar;
25 |
26 | import android.os.Environment;
27 | import android.os.StatFs;
28 | import android.util.Log;
29 |
30 | /**
31 | * Class for printing and saving log.
32 | * @author bingyang.djj
33 | *
34 | */
35 | public class TmtsLog {
36 | private static final String LOG_TAG = "TmtsLog";
37 | private static final String LOG_FILE_EXTENSION = ".html";
38 | private static final String NEW_LINE = "
";
39 | private static final String RED_START = "";
40 | private static final String GREEN_START = "
";
41 | private static final String COLOR_END = "
";
42 |
43 | /**
44 | * Mini space to save log file, 1MB
45 | */
46 | private static final long SAFETY_SPACE = 1;
47 | private static File normalFile;
48 |
49 | /**
50 | * Print and save normal log
51 | * @param tag Log tag
52 | * @param msg message
53 | */
54 | public static void i(String tag, String msg) {
55 | Log.i(tag, msg);
56 | final StringBuffer log = new StringBuffer();
57 | log.append(GREEN_START).append(getlogTime()).append(" ")
58 | .append(getPid()).append(" ")
59 | .append(tag).append(" ")
60 | .append(msg).append(NEW_LINE);
61 |
62 | new Runnable() {
63 |
64 | @Override
65 | public void run() {
66 | saveNormalLog(log.append(COLOR_END).toString());
67 | }
68 | }.run();
69 |
70 | }
71 |
72 | /**
73 | * @deprecated Use e(String tag, String msg, Throwable e)
74 | * Print and save error/crash log
75 | * @param tag Log tag
76 | * @param msg Log message
77 | */
78 | public static void e(String tag, String msg) {
79 | StackTraceElement[] traceElements = new Throwable().getStackTrace();
80 | final StringBuffer allLog = new StringBuffer();
81 | Log.e(tag, msg);
82 | allLog.append(RED_START).append(msg).append(NEW_LINE);
83 |
84 | //start from 1, we do not need 0
85 | for (int i = 1; i < traceElements.length; i++) {
86 | Log.e(tag, traceElements[i].toString());
87 | allLog.append(traceElements[i].toString())
88 | .append(NEW_LINE);
89 | }
90 |
91 | new Runnable() {
92 |
93 | @Override
94 | public void run() {
95 | saveNormalLog(allLog.append(COLOR_END).toString());
96 | }
97 | }.run();
98 |
99 | }
100 |
101 | /**
102 | * Print and save error/crash log
103 | * @param tag Log tag
104 | * @param msg Log message
105 | */
106 | public static void e(String tag, String msg, Throwable e) {
107 | StackTraceElement[] traceElements = e.getStackTrace();
108 | final StringBuffer allLog = new StringBuffer();
109 | Log.e(tag, msg);
110 | allLog.append(RED_START).append(msg).append(NEW_LINE);
111 |
112 | for (int i = 0; i < traceElements.length; i++) {
113 | Log.e(tag, traceElements[i].toString());
114 | allLog.append(traceElements[i].toString())
115 | .append(NEW_LINE);
116 | }
117 |
118 | new Runnable() {
119 |
120 | @Override
121 | public void run() {
122 | saveNormalLog(allLog.append(COLOR_END).toString());
123 | }
124 | }.run();
125 |
126 | }
127 |
128 | /**
129 | * Get the Sd Root path.
130 | * @return Sd Root path.
131 | */
132 | private static File getSdRootPath() {
133 | File sdDir = null;
134 |
135 | boolean sdCardExist = Environment.getExternalStorageState()
136 | .equals(android.os.Environment.MEDIA_MOUNTED);
137 |
138 | if (sdCardExist) {
139 | sdDir = Environment.getExternalStorageDirectory();
140 | return sdDir;
141 |
142 | } else {
143 | Log.i(LOG_TAG, "Sd Card not found");
144 | }
145 | return null;
146 | }
147 |
148 | /**
149 | * Get available size of Sd Card.
150 | * @return Available size of Sd Card in MB.
151 | */
152 | private static long getAvailableSize() {
153 | File path = getSdRootPath();
154 | if (null == path) {
155 | return 0;
156 | }
157 |
158 | StatFs stat = new StatFs(path.getPath());
159 |
160 | long blockSize = stat.getBlockSize();
161 | long availableBlocks = stat.getAvailableBlocks();
162 |
163 | return (availableBlocks * blockSize) / 1024 / 1024;
164 | }
165 |
166 | /**
167 | * Determine whether the Sd Card has enough space to hold log file
168 | * @return Whether the Sd Card has enough space to hold log file
169 | */
170 | private static boolean isEnoughSpace() {
171 | return (getAvailableSize() > SAFETY_SPACE) ? true : false;
172 | }
173 |
174 | /**
175 | * Determine where to save log files
176 | * @return Root path to save log files
177 | */
178 | private static File getRootPath() {
179 | return isEnoughSpace() ? getSdRootPath() : Environment.getDataDirectory();
180 | }
181 |
182 | /**
183 | * Save crash/error log information to sdcard in html format
184 | * @param logContent Log content
185 | */
186 | private static void saveLogToFile(String logContent) {
187 |
188 | File directory = new File(getRootPath() + "/TMTS_Log/Error");
189 | if (!directory.exists()) {
190 | directory.mkdirs();
191 | }
192 |
193 | if (!isEnoughSpace()) {
194 | try {
195 | throw new Exception("There is not enough space in SD Card");
196 | } catch (Exception e) {
197 | e.printStackTrace();
198 | }
199 | }
200 |
201 | String fileName = directory.getPath() + "/log_" + getTimeStamp() + LOG_FILE_EXTENSION;
202 | Log.i(LOG_TAG, "file is " + fileName);
203 |
204 | FileWriter fwriter = null;
205 |
206 | try {
207 | fwriter = new FileWriter(fileName);
208 | synchronized (fwriter) {
209 | fwriter.write(logContent);
210 | }
211 |
212 | } catch (IOException ex) {
213 | ex.printStackTrace();
214 |
215 | } finally {
216 | try {
217 | if (null != fwriter) {
218 | fwriter.flush();
219 | fwriter.close();
220 | }
221 |
222 | } catch (IOException ex) {
223 | ex.printStackTrace();
224 | }
225 | }
226 | }
227 |
228 | /**
229 | * Save normal log to Sd Card
230 | * @param logContent Log information
231 | */
232 | private static void saveNormalLog(String logContent) {
233 | File directory = new File(getRootPath() + "/TMTS_Log/Normal");
234 | if (!directory.exists()) {
235 | directory.mkdirs();
236 | }
237 |
238 | if (!isEnoughSpace()) {
239 | try {
240 | throw new Exception("There is not enough space in SD Card");
241 |
242 | } catch (Exception e) {
243 | e.printStackTrace();
244 | }
245 | }
246 | File file = null;
247 | if (null == normalFile) {
248 | String fileName = directory.getPath() + "/log_" + getTimeStamp() + LOG_FILE_EXTENSION;
249 | file = new File(fileName);
250 | normalFile = file;
251 |
252 | } else {
253 | file = normalFile;
254 | }
255 |
256 | FileWriter fwriter = null;
257 |
258 | try {
259 | fwriter = new FileWriter(file, true);
260 | synchronized (fwriter) {
261 | fwriter.write(logContent);
262 | }
263 |
264 | } catch (Exception e) {
265 | e.printStackTrace();
266 |
267 | } finally {
268 | try {
269 | if (null != fwriter) {
270 | fwriter.flush();
271 | fwriter.close();
272 | }
273 |
274 | } catch (IOException ex) {
275 | ex.printStackTrace();
276 | }
277 | }
278 | }
279 |
280 | /**
281 | * Get a timestamp used for log file name
282 | * @return Timestamp
283 | */
284 | private static String getTimeStamp() {
285 | Calendar now = Calendar.getInstance();
286 | String timestamp = now.get(Calendar.YEAR)
287 | + "_" + (now.get(Calendar.MONTH)+1)
288 | + "_" + now.get(Calendar.DAY_OF_MONTH)
289 | + "_" + now.get(Calendar.HOUR_OF_DAY)
290 | + now.get(Calendar.MINUTE)
291 | + now.get(Calendar.SECOND)
292 | + now.get(Calendar.MILLISECOND);
293 |
294 | return timestamp;
295 | }
296 |
297 | /**
298 | * Get current time used for log
299 | * @return current time
300 | */
301 | private static String getlogTime() {
302 | Calendar now = Calendar.getInstance();
303 | String logTime = (now.get(Calendar.MONTH)+1)
304 | + "-" + now.get(Calendar.DAY_OF_MONTH)
305 | + " " + now.get(Calendar.HOUR_OF_DAY)
306 | + ":" + now.get(Calendar.MINUTE)
307 | + ":" + now.get(Calendar.SECOND)
308 | + "." + now.get(Calendar.MILLISECOND);
309 |
310 | return logTime;
311 | }
312 |
313 | /**
314 | * Get current pid.
315 | * @return current pid.
316 | */
317 | private static String getPid() {
318 | return String.valueOf(android.os.Process.myPid());
319 | }
320 |
321 | }
322 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/TmtsTestCase.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TMTS/Android-automation/c3f23d528a6334eac402fe95045624e083f2a31f/Tmts_Java/src/com/taobao/tmts/framework/TmtsTestCase.java
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/gen/LayoutViews.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.gen;
20 |
21 | import com.taobao.tmts.framework.TmtsTestCase;
22 |
23 | /**
24 | * Base Class for Auto Gen SubViews.
25 | * @author bingyang.djj
26 | * Added in 2011-06-21
27 | */
28 | public class LayoutViews {
29 | protected TmtsTestCase context;
30 |
31 | /**
32 | * Initialize this class.
33 | * @param context Instance of TmtsTestCase
34 | */
35 | public void setContext(TmtsTestCase context) {
36 | this.context = context;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/gen/TmtsGenClass.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.gen;
20 |
21 | import com.taobao.tmts.framework.TmtsTestCase;
22 |
23 | /**
24 | * Base Class for Auto Gen Class from layout files
25 | * @author bingyang.djj
26 | *
27 | */
28 | public class TmtsGenClass {
29 | protected TmtsTestCase context;
30 |
31 | /**
32 | * Initialize this class.
33 | * @param context Instance of TmtsTestCase
34 | */
35 | public void setContext(TmtsTestCase context) {
36 | this.context = context;
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/utils/ActivityUtils.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.utils;
20 |
21 | import java.util.ArrayList;
22 |
23 | import junit.framework.Assert;
24 | import android.app.Activity;
25 | import android.app.Instrumentation;
26 | import android.app.Instrumentation.ActivityMonitor;
27 | import android.content.IntentFilter;
28 | import android.util.Log;
29 | import android.view.KeyEvent;
30 |
31 | import com.taobao.tmts.framework.TmtsLog;
32 |
33 | /**
34 | * This class contains activity related methods. Examples are:
35 | * getCurrentActivity(), getActivityList(), getAllOpenedActivities().
36 | *
37 | * @author Renas Reda, renas.reda@jayway.com
38 | */
39 | public class ActivityUtils {
40 | private static final String LOG_TAG = "ActivityUtils";
41 |
42 | private final Instrumentation inst;
43 | private ActivityMonitor activityMonitor;
44 | private Activity activity;
45 | private final Sleeper sleeper;
46 | private ArrayList activityList = new ArrayList();
47 |
48 | /**
49 | * Constructor that takes in the instrumentation and the start activity.
50 | *
51 | * @param inst
52 | * the {@code Instrumentation} instance.
53 | * @param activity
54 | * the start {@code Activity}
55 | * @param sleeper
56 | * the {@code Sleeper} instance
57 | *
58 | */
59 | public ActivityUtils(Instrumentation inst, Activity activity,
60 | Sleeper sleeper) {
61 | this.inst = inst;
62 | this.activity = activity;
63 | this.sleeper = sleeper;
64 | setupActivityMonitor();
65 | }
66 |
67 | /**
68 | * Returns a {@code List} of all the opened/active activities.
69 | *
70 | * @return a {@code List} of all the opened/active activities
71 | *
72 | */
73 | public ArrayList getAllOpenedActivities() {
74 | return activityList;
75 | }
76 |
77 | /**
78 | * This is were the activityMonitor is set up. The monitor will keep check
79 | * for the currently active activity.
80 | *
81 | */
82 | private void setupActivityMonitor() {
83 |
84 | try {
85 | IntentFilter filter = null;
86 | activityMonitor = inst.addMonitor(filter, null, false);
87 | } catch (Exception e) {
88 | e.printStackTrace();
89 | }
90 | }
91 |
92 | /**
93 | * Sets the Orientation (Landscape/Portrait) for the current activity.
94 | *
95 | * @param orientation
96 | * An orientation constant such as
97 | * {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_LANDSCAPE}
98 | * or
99 | * {@link android.content.pm.ActivityInfo#SCREEN_ORIENTATION_PORTRAIT}
100 | * .
101 | *
102 | */
103 | public void setActivityOrientation(int orientation) {
104 | Activity activity = getCurrentActivity();
105 | activity.setRequestedOrientation(orientation);
106 | }
107 |
108 | /**
109 | * Returns the current {@code Activity}, after sleeping a default pause
110 | * length.
111 | *
112 | * @return the current {@code Activity}
113 | *
114 | */
115 | public Activity getCurrentActivity() {
116 | return getCurrentActivity(true);
117 | }
118 |
119 | /**
120 | * Waits for an activity to be started if one is not provided by the
121 | * constructor.
122 | *
123 | */
124 | private final void waitForActivityIfNotAvailable() {
125 | if (activity == null) {
126 | if (activityMonitor != null) {
127 | while (activityMonitor.getLastActivity() == null) {
128 | sleeper.sleepMini();
129 | }
130 | } else {
131 | sleeper.sleepMini();
132 | setupActivityMonitor();
133 | waitForActivityIfNotAvailable();
134 | }
135 | }
136 | }
137 |
138 | /**
139 | * Returns the current {@code Activity}.
140 | *
141 | * @param shouldSleepFirst
142 | * whether to sleep a default pause first
143 | * @return the current {@code Activity}
144 | *
145 | */
146 | public Activity getCurrentActivity(boolean shouldSleepFirst) {
147 | if (shouldSleepFirst) {
148 | sleeper.sleep();
149 | inst.waitForIdleSync();
150 | }
151 |
152 | waitForActivityIfNotAvailable();
153 | Boolean found = false;
154 |
155 | if (activityMonitor != null) {
156 | if (activityMonitor.getLastActivity() != null)
157 | activity = activityMonitor.getLastActivity();
158 | }
159 | Activity storedActivity;
160 | for (int i = 0; i < activityList.size(); i++) {
161 | storedActivity = activityList.get(i);
162 | if (storedActivity.getClass().getName()
163 | .equals(activity.getClass().getName()))
164 | found = true;
165 | }
166 | if (found)
167 | return activity;
168 | else {
169 | activityList.add(activity);
170 | return activity;
171 | }
172 | }
173 |
174 | /**
175 | * Waits for the given {@link Activity}.
176 | *
177 | * @param name
178 | * the name of the {@code Activity} to wait for e.g.
179 | * {@code "MyActivity"}
180 | * @param timeout
181 | * the amount of time in milliseconds to wait
182 | * @return {@code true} if {@code Activity} appears before the timeout and
183 | * {@code false} if it does not
184 | *
185 | */
186 | public boolean waitForActivity(String name, int timeout) {
187 | long now = System.currentTimeMillis();
188 | final long endTime = now + timeout;
189 | while (!getCurrentActivity().getClass().getSimpleName().equals(name)
190 | && now < endTime) {
191 | now = System.currentTimeMillis();
192 | }
193 | if (now < endTime)
194 | return true;
195 |
196 | else
197 | return false;
198 | }
199 |
200 | /**
201 | * Returns to the given {@link Activity}.
202 | *
203 | * @param name
204 | * the name of the {@code Activity} to return to, e.g.
205 | * {@code "MyActivity"}
206 | *
207 | */
208 | public void goBackToActivity(String name) {
209 | boolean found = false;
210 | for (Activity activity : activityList) {
211 | if (activity.getClass().getSimpleName().equals(name))
212 | found = true;
213 | }
214 | if (found) {
215 | while (!getCurrentActivity().getClass().getSimpleName()
216 | .equals(name)) {
217 | try {
218 | inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
219 | } catch (SecurityException e) {
220 | Assert.assertTrue("Activity named " + name
221 | + " can not be returned to", false);
222 | }
223 | }
224 | } else {
225 | for (int i = 0; i < activityList.size(); i++)
226 | Log.d("Robotium", "Activity priorly opened: "
227 | + activityList.get(i).getClass().getSimpleName());
228 | Assert.assertTrue("No Activity named " + name
229 | + " has been priorly opened", false);
230 | }
231 | }
232 |
233 | /**
234 | * Returns a localized string
235 | *
236 | * @param resId
237 | * the resource ID for the string
238 | * @return the localized string
239 | *
240 | */
241 | public String getString(int resId) {
242 | Activity activity = getCurrentActivity(false);
243 | return activity.getString(resId);
244 | }
245 |
246 | /**
247 | *
248 | * All activites that have been active are finished.
249 | *
250 | */
251 | public void finalize() throws Throwable {
252 | // TmtsLog.e(LOG_TAG, "finalize() start", new Throwable());
253 | try {
254 | // Finish all opened activities
255 | for (int i = activityList.size() - 1; i >= 0; i--) {
256 | activityList.get(i).finish();
257 | sleeper.sleep(100);
258 | }
259 |
260 | // Finish the initial activity, pressing Back for good measure
261 | // getCurrentActivity().finish();
262 | // try {
263 | // inst.sendKeyDownUpSync(KeyEvent.KEYCODE_BACK);
264 | // Log.e(LOG_TAG, "send BACK KEY");
265 | // } catch (SecurityException ignored) {
266 | // // Guard against lack of INJECT_EVENT permission
267 | // }
268 | Log.e(LOG_TAG, getCurrentActivity() +"");
269 | activityList.clear();
270 |
271 | // Remove the monitor added during startup
272 | if (activityMonitor != null) {
273 | inst.removeMonitor(activityMonitor);
274 | }
275 | } catch (Exception ignored) {
276 | }
277 | super.finalize();
278 | TmtsLog.e(LOG_TAG, "asdf", new Throwable());
279 | Log.e(LOG_TAG, "finalize() finished");
280 | }
281 |
282 | }
283 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/utils/ClickUtils.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.utils;
20 |
21 | import java.util.ArrayList;
22 | import java.util.regex.Pattern;
23 |
24 | import junit.framework.Assert;
25 | import android.app.Activity;
26 | import android.app.Instrumentation;
27 | import android.os.SystemClock;
28 | import android.util.Log;
29 | import android.view.MotionEvent;
30 | import android.view.View;
31 | import android.view.ViewConfiguration;
32 | import android.widget.TextView;
33 |
34 | /**
35 | * Some methods come from Solo Clicker.java.
36 | *
37 | * @author bingyang.djj, original authors is Renas Reda, renas.reda@jayway.com.
38 | *
39 | */
40 | public class ClickUtils {
41 | private static final String LOG_TAG = "ClickUtils";
42 |
43 | private Instrumentation inst;
44 | private ViewUtils viewUtils;
45 | private ActivityUtils activityUtils;
46 | private static Sleeper sleeper;
47 | private MatchCounter matchCounter;
48 | private ScrollUtils scrollUtils;
49 |
50 | public ClickUtils(Instrumentation inst, Activity activity) {
51 | this.inst = inst;
52 | this.sleeper = new Sleeper();
53 | this.matchCounter = new MatchCounter();
54 | this.activityUtils = new ActivityUtils(inst, activity, sleeper);
55 | this.viewUtils = new ViewUtils(inst, activityUtils, sleeper);
56 | this.scrollUtils = new ScrollUtils(inst, activityUtils, viewUtils, sleeper);
57 | }
58 |
59 | /**
60 | * Click on a given view
61 | * @throws InterruptedException
62 | */
63 | public static void clickOnScreen(Instrumentation instrumentation, View view, boolean longClick) throws InterruptedException {
64 | // if(view == null)
65 | // Assert.assertTrue("View is null and can therefore not be clicked!", false);
66 | int[] xy = new int[2];
67 |
68 | view.getLocationOnScreen(xy);
69 |
70 | final int viewWidth = view.getWidth();
71 | final int viewHeight = view.getHeight();
72 | final float x = xy[0] + (viewWidth / 2.0f);
73 | float y = xy[1] + (viewHeight / 2.0f);
74 | Log.i(LOG_TAG, "click on x: " + x + " y: "+ y);
75 |
76 | if (longClick) {
77 | clickLongOnScreen(instrumentation, x, y);
78 | } else {
79 | clickOnScreen(instrumentation, x, y);
80 | }
81 | }
82 |
83 | /**
84 | * Clicks on a given coordinate on the screen
85 | *
86 | * @param x the x coordinate
87 | * @param y the y coordinate
88 | *
89 | */
90 | public static void clickOnScreen(Instrumentation instrumentation, float x, float y) {
91 | long downTime = SystemClock.uptimeMillis();
92 | long eventTime = SystemClock.uptimeMillis();
93 | MotionEvent event = MotionEvent.obtain(downTime, eventTime,
94 | MotionEvent.ACTION_DOWN, x, y, 0);
95 | MotionEvent event2 = MotionEvent.obtain(downTime, eventTime,
96 | MotionEvent.ACTION_UP, x, y, 0);
97 |
98 | try{
99 | instrumentation.sendPointerSync(event);
100 | instrumentation.sendPointerSync(event2);
101 | Log.i(LOG_TAG, "sent motionevent to click, x is " + x + " y is " + y);
102 |
103 | }catch(SecurityException e){
104 | Assert.assertTrue("Click can not be completed!", false);
105 | }
106 | }
107 |
108 | /**
109 | * Long clicks a given coordinate on the screen
110 | *
111 | * @param x the x coordinate
112 | * @param y the y coordinate
113 | * @throws InterruptedException
114 | *
115 | */
116 |
117 | public static void clickLongOnScreen(Instrumentation instrumentation, float x, float y) throws InterruptedException {
118 | long downTime = SystemClock.uptimeMillis();
119 | long eventTime = SystemClock.uptimeMillis();
120 | MotionEvent event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_DOWN, x, y, 0);
121 | try{
122 | instrumentation.sendPointerSync(event);
123 | }catch(SecurityException e){
124 | Assert.assertTrue("Click can not be completed! Something is in the way e.g. the keyboard.", false);
125 | }
126 | instrumentation.waitForIdleSync();
127 | eventTime = SystemClock.uptimeMillis();
128 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE,
129 | x + ViewConfiguration.getTouchSlop() / 2,
130 | y + ViewConfiguration.getTouchSlop() / 2, 0);
131 | instrumentation.sendPointerSync(event);
132 | instrumentation.waitForIdleSync();
133 | // if(time > 0)
134 | // sleeper.sleep(time);
135 | // else
136 | Thread.sleep((int)(ViewConfiguration.getLongPressTimeout() * 1.5f));
137 |
138 | eventTime = SystemClock.uptimeMillis();
139 | event = MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_UP, x, y, 0);
140 | instrumentation.sendPointerSync(event);
141 | instrumentation.waitForIdleSync();
142 | // sleeper.sleep();
143 |
144 | }
145 |
146 | /**
147 | * Clicks on a specific {@link TextView} displaying a given text.
148 | *
149 | * @param regex the text that should be clicked on. The parameter will be interpreted as a regular expression.
150 | * @param longClick {@code true} if the click should be a long click
151 | * @param match the regex match that should be clicked on
152 | * @param scroll whether to scroll to find the regex
153 | * @param time the amount of time to long click
154 | * @throws InterruptedException
155 | */
156 | public void clickOnText(String regex, boolean longClick, int match, boolean scroll, int time) throws InterruptedException {
157 | final Pattern pattern = Pattern.compile(regex);
158 | // waiter.waitForText(regex, 0, TIMEOUT, scroll, true);
159 | TextView textToClick = null;
160 | ArrayList textViewList = viewUtils.getCurrentViews(TextView.class);
161 | textViewList = ViewUtils.removeInvisibleViews(textViewList);
162 | if (match == 0) {
163 | match = 1;
164 | }
165 | for (TextView textView : textViewList){
166 | if(pattern.matcher(textView.getText().toString()).find()){
167 | matchCounter.addMatchToCount();
168 | }
169 | if (matchCounter.getTotalCount() == match) {
170 | matchCounter.resetCount();
171 | textToClick = textView;
172 | break;
173 | }
174 | }
175 | if (textToClick != null) {
176 | clickOnScreen(this.inst, textToClick, longClick);
177 |
178 | } else if (scroll && scrollUtils.scroll(ScrollUtils.DOWN)) {
179 | clickOnText(regex, longClick, match, scroll, time);
180 | } else {
181 | if (matchCounter.getTotalCount() > 0)
182 | Assert.assertTrue("There are only " + matchCounter.getTotalCount() + " matches of " + regex, false);
183 | else {
184 | for (TextView textView : textViewList) {
185 | Log.d(LOG_TAG, regex + " not found. Have found: " + textView.getText());
186 | }
187 | Assert.assertTrue("The text: " + regex + " is not found!", false);
188 | }
189 | matchCounter.resetCount();
190 | }
191 | }
192 | }
193 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/utils/MatchCounter.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.utils;
20 |
21 | /**
22 | * This class contains methods for counting matches, retrieving the count and
23 | * reseting the counter.
24 | *
25 | * @author Renas Reda, renas.reda@jayway.com
26 | *
27 | */
28 |
29 | class MatchCounter {
30 |
31 | private int totalAmountOfMatches;
32 |
33 | /**
34 | * Constructs this object.
35 | *
36 | */
37 | public MatchCounter() {
38 | totalAmountOfMatches = 0;
39 | }
40 |
41 | /**
42 | * Resets the counter.
43 | *
44 | */
45 | public void resetCount() {
46 | totalAmountOfMatches = 0;
47 | }
48 |
49 | /**
50 | * Adds 1 to the counter.
51 | *
52 | */
53 | public void addMatchToCount() {
54 | totalAmountOfMatches++;
55 | }
56 |
57 | /**
58 | * Adds number to the counter.
59 | *
60 | * @param numberOfMatches
61 | * the number to add to the counter
62 | *
63 | */
64 | public void addMatchesToCount(int numberOfMatches) {
65 |
66 | totalAmountOfMatches += numberOfMatches;
67 |
68 | }
69 |
70 | /**
71 | * Returns the total count.
72 | *
73 | * @return the total count
74 | *
75 | */
76 | public int getTotalCount() {
77 | return totalAmountOfMatches;
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/utils/ScrollUtils.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.utils;
20 |
21 | import java.util.ArrayList;
22 |
23 | import junit.framework.Assert;
24 | import android.app.Instrumentation;
25 | import android.view.View;
26 | import android.widget.AbsListView;
27 | import android.widget.GridView;
28 | import android.widget.ListView;
29 | import android.widget.ScrollView;
30 |
31 | /**
32 | * Some methods come from come from Solo Scroller.java.
33 | * @author bingyang.djj, original authors is Renas Reda, renas.reda@jayway.com.
34 | *
35 | */
36 | public class ScrollUtils {
37 |
38 | private static final String LOG_TAG = "ScrollUtils";
39 | private Instrumentation inst;
40 | private ActivityUtils activityUtils;
41 | private ViewUtils viewUtils;
42 | private Sleeper sleeper;
43 |
44 |
45 | public static final int DOWN = 0;
46 | public static final int UP = 1;
47 |
48 | public ScrollUtils(Instrumentation inst, ActivityUtils activityUtils,
49 | ViewUtils viewUtils, Sleeper sleeper) {
50 | this.inst = inst;
51 | this.activityUtils = activityUtils;
52 | this.viewUtils = viewUtils;
53 | this.sleeper = sleeper;
54 | }
55 |
56 | /**
57 | * Scrolls up and down.
58 | *
59 | * @param direction the direction in which to scroll
60 | * @return {@code true} if more scrolling can be done
61 | *
62 | */
63 | public boolean scroll(int direction) {
64 | final ArrayList viewList = viewUtils.getViews(null, true);
65 | final ArrayList listViews = ViewUtils.filterViews(ListView.class, viewList);
66 |
67 | if (listViews.size() > 0) {
68 | return scrollList(ListView.class, 0, direction, listViews);
69 | }
70 |
71 | final ArrayList gridViews = ViewUtils.filterViews(GridView.class, viewList);
72 |
73 | if (gridViews.size() > 0) {
74 | return scrollList(GridView.class, 0, direction, gridViews);
75 | }
76 |
77 | final ArrayList scrollViews = ViewUtils.filterViews(ScrollView.class, viewList);
78 |
79 | if (scrollViews.size() > 0) {
80 | return scrollScrollView(direction, scrollViews);
81 | }
82 | return false;
83 | }
84 |
85 | /**
86 | * Scrolls a list.
87 | *
88 | * @param listIndex the list to be scrolled
89 | * @param direction the direction to be scrolled
90 | * @return {@code true} if more scrolling can be done
91 | *
92 | */
93 | public boolean scrollList(Class classToFilterBy, int listIndex, int direction, ArrayList listViews) {
94 | final T listView = viewUtils.getView(classToFilterBy, listViews, listIndex);
95 |
96 | if(listView == null)
97 | Assert.assertTrue("No ListView with index " + listIndex + " is found!", false);
98 |
99 | if (direction == DOWN) {
100 | if (listView.getLastVisiblePosition() >= listView.getCount()-1) {
101 | scrollListToLine(listView, listView.getLastVisiblePosition());
102 | return false;
103 | }
104 |
105 | if(listView.getFirstVisiblePosition() != listView.getLastVisiblePosition())
106 | scrollListToLine(listView, listView.getLastVisiblePosition());
107 |
108 | else
109 | scrollListToLine(listView, listView.getFirstVisiblePosition()+1);
110 |
111 | } else if (direction == UP) {
112 | if (listView.getFirstVisiblePosition() < 2) {
113 | scrollListToLine(listView, 0);
114 | return false;
115 | }
116 |
117 | final int lines = listView.getLastVisiblePosition() - listView.getFirstVisiblePosition();
118 | int lineToScrollTo = listView.getFirstVisiblePosition() - lines;
119 |
120 | if(lineToScrollTo == listView.getLastVisiblePosition())
121 | lineToScrollTo--;
122 |
123 | if(lineToScrollTo < 0)
124 | lineToScrollTo = 0;
125 |
126 | scrollListToLine(listView, lineToScrollTo);
127 | }
128 | sleeper.sleep();
129 | return true;
130 | }
131 |
132 | /**
133 | * Scroll the list to a given line
134 | * @param listView the listView to scroll
135 | * @param line the line to scroll to
136 | */
137 | private void scrollListToLine(final T view, final int line){
138 |
139 | final int lineToMoveTo;
140 | if(view instanceof GridView)
141 | lineToMoveTo = line+1;
142 | else
143 | lineToMoveTo = line;
144 |
145 | inst.runOnMainSync(new Runnable(){
146 | public void run(){
147 | view.setSelection(lineToMoveTo);
148 | }
149 | });
150 | }
151 |
152 | /**
153 | * Scrolls a ScrollView.
154 | *
155 | * @param direction the direction to be scrolled
156 | * @return {@code true} if more scrolling can be done
157 | *
158 | */
159 | private boolean scrollScrollView(int direction, ArrayList scrollViews){
160 | final ScrollView scroll = viewUtils.getView(ScrollView.class, scrollViews, 0);
161 | int scrollAmount = 0;
162 |
163 | if(scroll != null){
164 | int height = scroll.getHeight();
165 | height--;
166 | int scrollTo = 0;
167 |
168 | if (direction == DOWN) {
169 | scrollTo = (height);
170 | }
171 |
172 | else if (direction == UP) {
173 | scrollTo = (-height);
174 | }
175 | scrollAmount = scroll.getScrollY();
176 | scrollScrollViewTo(scroll,0, scrollTo);
177 | if (scrollAmount == scroll.getScrollY()) {
178 | return false;
179 | }
180 | else{
181 | return true;
182 | }
183 | }
184 | return false;
185 | }
186 |
187 | /**
188 | * Scroll the list to a given line
189 | * @param listView the listView to scroll
190 | * @param line the line to scroll to
191 | */
192 | private void scrollScrollViewTo(final ScrollView scrollView, final int x, final int y){
193 | inst.runOnMainSync(new Runnable(){
194 | public void run(){
195 | scrollView.scrollBy(x, y);
196 | }
197 | });
198 | }
199 | }
200 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/utils/Sleeper.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.utils;
20 |
21 | public class Sleeper {
22 |
23 | private final int PAUSE = 500;
24 | private final int MINIPAUSE = 300;
25 |
26 | /**
27 | * Sleeps the current thread for a default pause length.
28 | *
29 | */
30 | public void sleep() {
31 | sleep(PAUSE);
32 | }
33 |
34 |
35 | /**
36 | * Sleeps the current thread for a default mini pause length.
37 | *
38 | */
39 | public void sleepMini() {
40 | sleep(MINIPAUSE);
41 | }
42 |
43 |
44 | /**
45 | * Sleeps the current thread for time
milliseconds.
46 | *
47 | * @param time the length of the sleep in milliseconds
48 | *
49 | */
50 | public void sleep(int time) {
51 | try {
52 | Thread.sleep(time);
53 | } catch (InterruptedException ignored) {}
54 | }
55 |
56 | }
57 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/utils/ViewUtils.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.utils;
20 |
21 | import java.lang.reflect.Field;
22 | import java.util.ArrayList;
23 | import java.util.List;
24 |
25 | import junit.framework.Assert;
26 | import android.app.Activity;
27 | import android.app.Instrumentation;
28 | import android.view.View;
29 | import android.view.ViewGroup;
30 | import android.widget.TextView;
31 |
32 | /**
33 | * This class contains view methods. Examples are getViews(),
34 | * getCurrentTextViews(), getCurrentImageViews().
35 | *
36 | * @author Renas Reda, renas.reda@jayway.com
37 | *
38 | */
39 | public class ViewUtils {
40 |
41 | private final Instrumentation inst;
42 | private final ActivityUtils activityUtils;
43 | private final Sleeper sleeper;
44 |
45 | /**
46 | * Constructs this object.
47 | *
48 | * @param inst
49 | * the {@code Instrumentation} instance
50 | * @param activityUtils
51 | * the {@code ActivityUtils} instance
52 | * @param sleeper
53 | * the {@code Sleeper} instance
54 | *
55 | */
56 |
57 | public ViewUtils(Instrumentation inst, ActivityUtils activityUtils,
58 | Sleeper sleeper) {
59 | this.inst = inst;
60 | this.activityUtils = activityUtils;
61 | this.sleeper = sleeper;
62 | }
63 |
64 | /**
65 | * Returns the absolute top parent {@code View} in for a given {@code View}.
66 | *
67 | * @param view
68 | * the {@code View} whose top parent is requested
69 | * @return the top parent {@code View}
70 | *
71 | */
72 |
73 | public View getTopParent(View view) {
74 | if (view.getParent() != null
75 | && !view.getParent().getClass().getName()
76 | .equals("android.view.ViewRoot")) {
77 | return getTopParent((View) view.getParent());
78 | } else {
79 | return view;
80 | }
81 | }
82 |
83 | /**
84 | * Returns the list item parent. It is used by clickInList().
85 | *
86 | * @param view
87 | * the view who's parent is requested
88 | * @return the parent of the view
89 | *
90 | */
91 |
92 | public View getListItemParent(View view) {
93 | if (view.getParent() != null
94 | && !(view.getParent() instanceof android.widget.ListView)) {
95 | return getListItemParent((View) view.getParent());
96 | } else {
97 | return view;
98 | }
99 |
100 | }
101 |
102 | /**
103 | * Returns the scroll or list parent view
104 | *
105 | * @param view
106 | * the view who's parent should be returned
107 | * @return the parent scroll view, list view or null
108 | *
109 | */
110 |
111 | public View getScrollOrListParent(View view) {
112 |
113 | if (!(view instanceof android.widget.AbsListView)
114 | && !(view instanceof android.widget.ScrollView)) {
115 | try {
116 | return getScrollOrListParent((View) view.getParent());
117 | } catch (Exception e) {
118 | return null;
119 | }
120 | } else {
121 | return view;
122 | }
123 | }
124 |
125 | /**
126 | * Returns views from the shown DecorViews.
127 | *
128 | * @param onlySufficientlyVisible
129 | * if only sufficiently visible views should be returned
130 | * @return all the views contained in the DecorViews
131 | *
132 | */
133 |
134 | public ArrayList getAllViews(boolean onlySufficientlyVisible) {
135 | final View[] views = getWindowDecorViews();
136 | final ArrayList allViews = new ArrayList();
137 | final View[] nonDecorViews = getNonDecorViews(views);
138 |
139 | if (views != null && views.length > 0) {
140 | View view;
141 | for (int i = 0; i < nonDecorViews.length; i++) {
142 | view = nonDecorViews[i];
143 | try {
144 | addChildren(allViews, (ViewGroup) view,
145 | onlySufficientlyVisible);
146 | } catch (Exception ignored) {
147 | }
148 | }
149 | view = getRecentDecorView(views);
150 | try {
151 | addChildren(allViews, (ViewGroup) view, onlySufficientlyVisible);
152 | } catch (Exception ignored) {
153 | }
154 | }
155 | return allViews;
156 | }
157 |
158 | /**
159 | * Returns the most recent DecorView
160 | *
161 | * @param views
162 | * the views to check
163 | * @return the most recent DecorView
164 | *
165 | */
166 |
167 | public final View getRecentDecorView(View[] views) {
168 | final View[] decorViews = new View[views.length];
169 | int i = 0;
170 | View view;
171 |
172 | for (int j = 0; j < views.length; j++) {
173 | view = views[j];
174 | if (view.getClass()
175 | .getName()
176 | .equals("com.android.internal.policy.impl.PhoneWindow$DecorView")) {
177 | decorViews[i] = view;
178 | i++;
179 | }
180 | }
181 | return getRecentContainer(decorViews);
182 | }
183 |
184 | /**
185 | * Returns the most recent view container
186 | *
187 | * @param views
188 | * the views to check
189 | * @return the most recent view container
190 | *
191 | */
192 |
193 | private final View getRecentContainer(View[] views) {
194 | View container = null;
195 | long drawingTime = 0;
196 | View view;
197 |
198 | for (int i = 0; i < views.length; i++) {
199 | view = views[i];
200 | if (view != null && view.isShown() && view.hasWindowFocus()
201 | && view.getDrawingTime() > drawingTime) {
202 | container = view;
203 | drawingTime = view.getDrawingTime();
204 | }
205 | }
206 | return container;
207 | }
208 |
209 | /**
210 | * Returns all views that are non DecorViews
211 | *
212 | * @param views
213 | * the views to check
214 | * @return the non DecorViews
215 | */
216 |
217 | public final View[] getNonDecorViews(View[] views) {
218 | final View[] decorViews = new View[views.length];
219 | int i = 0;
220 | View view;
221 |
222 | for (int j = 0; j < views.length; j++) {
223 | view = views[j];
224 | if (!(view.getClass().getName()
225 | .equals("com.android.internal.policy.impl.PhoneWindow$DecorView"))) {
226 | decorViews[i] = view;
227 | i++;
228 | }
229 | }
230 | return decorViews;
231 | }
232 |
233 | /**
234 | * Returns a {@code View} with a given id.
235 | *
236 | * @param id
237 | * the R.id of the {@code View} to be returned
238 | * @return a {@code View} with a given id
239 | */
240 |
241 | public View getView(int id) {
242 | final Activity activity = activityUtils.getCurrentActivity(false);
243 | return activity.findViewById(id);
244 | }
245 |
246 | /**
247 | * Extracts all {@code View}s located in the currently active
248 | * {@code Activity}, recursively.
249 | *
250 | * @param parent
251 | * the {@code View} whose children should be returned, or
252 | * {@code null} for all
253 | * @param onlySufficientlyVisible
254 | * if only sufficiently visible views should be returned
255 | * @return all {@code View}s located in the currently active
256 | * {@code Activity}, never {@code null}
257 | *
258 | */
259 |
260 | public ArrayList getViews(View parent, boolean onlySufficientlyVisible) {
261 | activityUtils.getCurrentActivity(false);
262 | final ArrayList views = new ArrayList();
263 | final View parentToUse;
264 |
265 | if (parent == null) {
266 | inst.waitForIdleSync();
267 | return getAllViews(onlySufficientlyVisible);
268 | } else {
269 | parentToUse = parent;
270 |
271 | views.add(parentToUse);
272 |
273 | if (parentToUse instanceof ViewGroup) {
274 | addChildren(views, (ViewGroup) parentToUse,
275 | onlySufficientlyVisible);
276 | }
277 | }
278 |
279 | return views;
280 | }
281 |
282 | /**
283 | * Adds all children of {@code viewGroup} (recursively) into {@code views}.
284 | *
285 | * @param views
286 | * an {@code ArrayList} of {@code View}s
287 | * @param viewGroup
288 | * the {@code ViewGroup} to extract children from
289 | * @param onlySufficientlyVisible
290 | * if only sufficiently visible views should be returned
291 | *
292 | */
293 |
294 | public void addChildren(ArrayList views, ViewGroup viewGroup,
295 | boolean onlySufficientlyVisible) {
296 | for (int i = 0; i < viewGroup.getChildCount(); i++) {
297 | final View child = viewGroup.getChildAt(i);
298 |
299 | if (onlySufficientlyVisible && isViewSufficientlyShown(child))
300 | views.add(child);
301 |
302 | else if (!onlySufficientlyVisible)
303 | views.add(child);
304 |
305 | if (child instanceof ViewGroup) {
306 | addChildren(views, (ViewGroup) child, onlySufficientlyVisible);
307 | }
308 | }
309 | }
310 |
311 | /**
312 | * Returns true if the view is sufficiently shown
313 | *
314 | * @param view
315 | * the view to check
316 | * @return true if the view is sufficiently shown
317 | *
318 | */
319 |
320 | public final boolean isViewSufficientlyShown(View view) {
321 | final int[] xyView = new int[2];
322 | final int[] xyParent = new int[2];
323 |
324 | if (view == null)
325 | return false;
326 |
327 | final float viewHeight = view.getHeight();
328 | final View parent = getScrollOrListParent(view);
329 | view.getLocationOnScreen(xyView);
330 |
331 | if (parent == null) {
332 | xyParent[1] = 0;
333 | } else {
334 | parent.getLocationOnScreen(xyParent);
335 | }
336 |
337 | if (xyView[1] + (viewHeight / 2.0f) > getScrollListWindowHeight(view))
338 | return false;
339 |
340 | else if (xyView[1] + (viewHeight / 2.0f) < xyParent[1])
341 | return false;
342 |
343 | return true;
344 | }
345 |
346 | /**
347 | * Returns the height of the scroll or list view parent
348 | *
349 | * @param view
350 | * the view who's parents height should be returned
351 | * @return the height of the scroll or list view parent
352 | */
353 |
354 | public float getScrollListWindowHeight(View view) {
355 | final int[] xyParent = new int[2];
356 | final View parent = getScrollOrListParent(view);
357 | final float windowHeight;
358 | if (parent == null) {
359 | windowHeight = activityUtils.getCurrentActivity(false)
360 | .getWindowManager().getDefaultDisplay().getHeight();
361 | } else {
362 | parent.getLocationOnScreen(xyParent);
363 | windowHeight = xyParent[1] + parent.getHeight();
364 | }
365 |
366 | return windowHeight;
367 |
368 | }
369 |
370 | /**
371 | * Returns a {@code View} with a certain index, from the list of current
372 | * {@code View}s of the specified type.
373 | *
374 | * @param classToFilterBy
375 | * which {@code View}s to choose from
376 | * @param index
377 | * choose among all instances of this type, e.g.
378 | * {@code Button.class} or {@code EditText.class}
379 | * @return a {@code View} with a certain index, from the list of current
380 | * {@code View}s of the specified type
381 | */
382 |
383 | public T getView(Class classToFilterBy, int index) {
384 | sleeper.sleep();
385 | inst.waitForIdleSync();
386 | ArrayList views = getCurrentViews(classToFilterBy);
387 | views = removeInvisibleViews(views);
388 | T view = null;
389 | try {
390 | view = views.get(index);
391 | } catch (IndexOutOfBoundsException e) {
392 | Assert.assertTrue("No " + classToFilterBy.getSimpleName()
393 | + " with index " + index + " is found!", false);
394 | }
395 | return view;
396 | }
397 |
398 | /**
399 | * Returns a {@code View} that shows a given text, from the list of current
400 | * {@code View}s of the specified type.
401 | *
402 | * @param classToFilterBy
403 | * which {@code View}s to choose from
404 | * @param text
405 | * the text that the view shows
406 | * @return a {@code View} showing a given text, from the list of current
407 | * {@code View}s of the specified type
408 | */
409 |
410 | public T getView(Class classToFilterBy, String text) {
411 | sleeper.sleep();
412 | inst.waitForIdleSync();
413 | ArrayList views = getCurrentViews(classToFilterBy);
414 | T viewToReturn = null;
415 | for (T view : views) {
416 | if (view.getText().toString().equals(text))
417 | viewToReturn = view;
418 | }
419 | if (viewToReturn == null)
420 | Assert.assertTrue("No " + classToFilterBy.getSimpleName()
421 | + " with text " + text + " is found!", false);
422 |
423 | return viewToReturn;
424 | }
425 |
426 | /**
427 | * Returns a view.
428 | *
429 | * @param classToFilterBy
430 | * the class to filter by
431 | * @param views
432 | * the list with views
433 | * @param index
434 | * the index of the view
435 | * @return the view with a given index
436 | */
437 |
438 | public final T getView(Class classToFilterBy,
439 | ArrayList views, int index) {
440 | T viewToReturn = null;
441 | long drawingTime = 0;
442 | if (views == null) {
443 | views = getCurrentViews(classToFilterBy);
444 | }
445 | if (index < 1) {
446 | for (T view : views) {
447 | if (view.getDrawingTime() > drawingTime) {
448 | drawingTime = view.getDrawingTime();
449 | viewToReturn = view;
450 | }
451 | }
452 | } else {
453 | try {
454 | viewToReturn = views.get(index);
455 | } catch (Exception ignored) {
456 | }
457 | }
458 | return viewToReturn;
459 | }
460 |
461 | /**
462 | * Returns an {@code ArrayList} of {@code View}s of the specified
463 | * {@code Class} located in the current {@code Activity}.
464 | *
465 | * @param classToFilterBy
466 | * return all instances of this class, e.g. {@code Button.class}
467 | * or {@code GridView.class}
468 | * @return an {@code ArrayList} of {@code View}s of the specified
469 | * {@code Class} located in the current {@code Activity}
470 | */
471 |
472 | public ArrayList getCurrentViews(
473 | Class classToFilterBy) {
474 | return getCurrentViews(classToFilterBy, null);
475 | }
476 |
477 | /**
478 | * Returns an {@code ArrayList} of {@code View}s of the specified
479 | * {@code Class} located under the specified {@code parent}.
480 | *
481 | * @param classToFilterBy
482 | * return all instances of this class, e.g. {@code Button.class}
483 | * or {@code GridView.class}
484 | * @param parent
485 | * the parent {@code View} for where to start the traversal
486 | * @return an {@code ArrayList} of {@code View}s of the specified
487 | * {@code Class} located under the specified {@code parent}
488 | */
489 |
490 | public ArrayList getCurrentViews(
491 | Class classToFilterBy, View parent) {
492 | ArrayList filteredViews = new ArrayList();
493 | List allViews = getViews(parent, true);
494 | for (View view : allViews) {
495 | if (view != null
496 | && classToFilterBy.isAssignableFrom(view.getClass())) {
497 | filteredViews.add(classToFilterBy.cast(view));
498 | }
499 | }
500 | return filteredViews;
501 | }
502 |
503 | private static Class> windowManager;
504 | static {
505 | try {
506 | windowManager = Class.forName("android.view.WindowManagerImpl");
507 |
508 | } catch (ClassNotFoundException e) {
509 | throw new RuntimeException(e);
510 | } catch (SecurityException e) {
511 | e.printStackTrace();
512 | }
513 | }
514 |
515 | /**
516 | * Returns the WindorDecorViews shown on the screen
517 | *
518 | * @return the WindorDecorViews shown on the screen
519 | *
520 | */
521 |
522 | public View[] getWindowDecorViews() {
523 |
524 | Field viewsField;
525 | Field instanceField;
526 | try {
527 | viewsField = windowManager.getDeclaredField("mViews");
528 | instanceField = windowManager.getDeclaredField("mWindowManager");
529 | viewsField.setAccessible(true);
530 | instanceField.setAccessible(true);
531 | Object instance = instanceField.get(null);
532 | return (View[]) viewsField.get(instance);
533 | } catch (SecurityException e) {
534 | e.printStackTrace();
535 | } catch (NoSuchFieldException e) {
536 | e.printStackTrace();
537 | } catch (IllegalArgumentException e) {
538 | e.printStackTrace();
539 | } catch (IllegalAccessException e) {
540 | e.printStackTrace();
541 | }
542 | return null;
543 | }
544 |
545 | /**
546 | * Removes invisible {@code View}s
547 | *
548 | * @param viewList
549 | * an {@code ArrayList} with {@code View}s that is being checked
550 | * for invisible {@code View}s.
551 | * @return a filtered {@code ArrayList} with no invisible {@code View}s.
552 | */
553 |
554 | public static ArrayList removeInvisibleViews(
555 | ArrayList viewList) {
556 | ArrayList tmpViewList = new ArrayList(viewList.size());
557 | for (T view : viewList) {
558 | if (view != null && view.getVisibility() != View.GONE
559 | && view.getVisibility() != View.INVISIBLE) {
560 | tmpViewList.add(view);
561 | }
562 | }
563 | return tmpViewList;
564 | }
565 |
566 | /**
567 | * Filters views
568 | *
569 | * @param classToFilterBy
570 | * the class to filter
571 | * @param viewList
572 | * the ArrayList to filter form
573 | * @return an ArrayList with filtered views
574 | */
575 | public static ArrayList filterViews(
576 | Class classToFilterBy, ArrayList viewList) {
577 | ArrayList filteredViews = new ArrayList(viewList.size());
578 | for (View view : viewList) {
579 | if (view != null
580 | && classToFilterBy.isAssignableFrom(view.getClass())) {
581 | filteredViews.add(classToFilterBy.cast(view));
582 | }
583 | }
584 | return filteredViews;
585 | }
586 | }
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/JavascriptInterface.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TMTS/Android-automation/c3f23d528a6334eac402fe95045624e083f2a31f/Tmts_Java/src/com/taobao/tmts/framework/view/JavascriptInterface.java
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsAbsListView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.AbsListView;
23 |
24 | public class TmtsAbsListView extends TmtsAdapterView {
25 | private static final String LOG_TAG = "TmtsAbsListView";
26 | private AbsListView absListView;
27 |
28 | public TmtsAbsListView(Instrumentation inst,
29 | AbsListView absListView) {
30 | super(inst, absListView);
31 | this.absListView = absListView;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsAdapterView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.Adapter;
23 | import android.widget.AdapterView;
24 |
25 | public class TmtsAdapterView extends TmtsViewGroup {
26 | private static final String LOG_TAG = "TmtsAdapterView";
27 | private AdapterView extends Adapter> adapterView;
28 |
29 | public TmtsAdapterView(Instrumentation inst, AdapterView extends Adapter> adapterView) {
30 | super(inst, adapterView);
31 | this.adapterView = adapterView;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsAutoCompleteTextView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.Adapter;
23 | import android.widget.AutoCompleteTextView;
24 |
25 | import com.taobao.tmts.framework.Constants;
26 |
27 | /**
28 | * Taobao Android Test Framework Class for {@link AutoCompleteTextView}.
29 | * @author bingyang.djj
30 | * Added in 2011-05-23
31 | */
32 | public class TmtsAutoCompleteTextView extends TmtsEditText {
33 |
34 | /**
35 | * Log tag.
36 | */
37 | private static final String LOG_TAG = "TmtsAutoCompleteTextView";
38 | private AutoCompleteTextView autoCompleteTextView;
39 |
40 | /**
41 | * Constructor of {@link TmtsAutoCompleteTextView}.
42 | * @param inst {@link Instrumentation}.
43 | * @param autoCompleteTextView {@link AutoCompleteTextView}.
44 | */
45 | public TmtsAutoCompleteTextView(Instrumentation inst, AutoCompleteTextView autoCompleteTextView) {
46 | super(inst, autoCompleteTextView);
47 | this.autoCompleteTextView = autoCompleteTextView;
48 | }
49 |
50 | /**
51 | * Get the text of AutoCompleteTextView by the given index
52 | * @param index index we specified
53 | * @return text of the given index
54 | */
55 | public String getAutoCompleteText(int index) {
56 | Adapter adapter = autoCompleteTextView.getAdapter();
57 | int maxIndex = adapter.getCount();
58 |
59 | if (index < 0 || index > maxIndex - 1) {
60 | return null;
61 |
62 | } else {
63 | try {
64 | Thread.sleep(Constants.ANR_TIME);
65 | } catch (InterruptedException e) {
66 | e.printStackTrace();
67 | }
68 | return adapter.getItem(index).toString();
69 | }
70 | }
71 |
72 | // /**
73 | // * Sets the string value of the AutoCompleteTextView TextView
74 | // * @param Text the string value to set
75 | // */
76 | // public void setText(final String text) {
77 | // inst.runOnMainSync(new Runnable(){
78 | //
79 | // @Override
80 | // public void run(){
81 | // autoCompleteTextView.setText(text);
82 | // }
83 | // });
84 | // }
85 | //
86 | // /**
87 | // * Return the text of the AutoCompleteTextView
88 | // * @return The text of the AutoCompleteTextView
89 | // */
90 | // public String getText() {
91 | // try {
92 | // Thread.sleep(Constants.ANR_TIME);
93 | // } catch (InterruptedException e) {
94 | // e.printStackTrace();
95 | // }
96 | // return autoCompleteTextView.getText().toString();
97 | // }
98 | }
99 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsButton.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.Button;
23 |
24 | /**
25 | * Taobao Android Test Framework Class for {@link Button}.
26 | * @author shidun
27 | * Added in 2011-05-16
28 | */
29 | public class TmtsButton extends TmtsTextView {
30 | private Button button;
31 |
32 | /**
33 | * Constructor of {@link TmtsScrollView}.
34 | * @param inst {@link android.app.Instrumentation}.
35 | * @param button {@link android.widget.Button}.
36 | */
37 | public TmtsButton(Instrumentation inst, Button button){
38 | super(inst, button);
39 | this.button = button;
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsCheckBox.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.util.Log;
23 | import android.widget.CheckBox;
24 |
25 | /**
26 | * Taobao Android Test Framework Class for {@link CheckBox}.
27 | * @author bingyang.djj
28 | * Added in 2011-05-20
29 | */
30 | public class TmtsCheckBox extends TmtsButton {
31 | private static final String LOG_TAG = "TmtsCheckBox";
32 | private CheckBox checkBox;
33 |
34 | /**
35 | * Constructor of {@link TmtsScrollView}.
36 | * @param inst {@link Instrumentation}.
37 | * @param checkBox {@link CheckBox}.
38 | */
39 | public TmtsCheckBox(Instrumentation inst, CheckBox checkBox) {
40 | super(inst, checkBox);
41 | this.checkBox = checkBox;
42 | }
43 |
44 | /**
45 | * Check the checkbox
46 | */
47 | public void check() {
48 | inst.runOnMainSync(new Runnable(){
49 |
50 | @Override
51 | public void run(){
52 | Log.i(LOG_TAG, "Check the checkbox");
53 | checkBox.setChecked(true);
54 | }
55 | });
56 | }
57 |
58 | /**
59 | * Uncheck the checkbox
60 | */
61 | public void unCheck() {
62 | inst.runOnMainSync(new Runnable(){
63 |
64 | @Override
65 | public void run(){
66 | Log.i(LOG_TAG, "unCheck the checkbox");
67 | checkBox.setChecked(false);
68 | }
69 | });
70 | }
71 |
72 | /**
73 | * return the state of checkbox
74 | * @return Whether the checkbox is checked, true for checked
75 | * and false for unchekced
76 | */
77 | public boolean isChecked() {
78 | return checkBox.isChecked();
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsCheckedTextView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.CheckedTextView;
23 |
24 | /**
25 | * Taobao Android Test Framework Class for {@link CheckedTextView}.
26 | *
27 | * @author bingyang.djj Added in 2011-08-08
28 | */
29 | public class TmtsCheckedTextView extends TmtsTextView {
30 | private static final String LOG_TAG = "TmtsCheckedTextView";
31 | private CheckedTextView checkedTextView;
32 |
33 | /**
34 | * Constructor of {@link TmtsCheckedTextView}.
35 | *
36 | * @param inst
37 | * {@link Instrumentation}.
38 | * @param view
39 | * {@link CheckedTextView}.
40 | */
41 | public TmtsCheckedTextView(Instrumentation inst,
42 | CheckedTextView checkedTextView) {
43 | super(inst, checkedTextView);
44 | this.checkedTextView = checkedTextView;
45 | }
46 |
47 | public boolean isChecked() {
48 | return checkedTextView.isChecked();
49 | }
50 |
51 | /**
52 | * Changes the checked state of this text view.
53 | *
54 | * @param checked
55 | * true to check the text, false to uncheck it.
56 | */
57 | public void setChecked(final boolean checked) {
58 | inst.waitForIdleSync();
59 | inst.runOnMainSync(new Runnable() {
60 |
61 | @Override
62 | public void run() {
63 | checkedTextView.setChecked(checked);
64 | }
65 | });
66 | }
67 |
68 | /**
69 | * Change the checked state of the view to the inverse of its current state.
70 | */
71 | public void toggle() {
72 | inst.waitForIdleSync();
73 | inst.runOnMainSync(new Runnable() {
74 |
75 | @Override
76 | public void run() {
77 | checkedTextView.toggle();
78 | }
79 | });
80 | }
81 |
82 | }
83 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsEditText.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.EditText;
23 |
24 | /**
25 | * Taobao Android Test Framework Class for {@link EditText}.
26 | * @author bingyang.djj
27 | * Added in 2011-05-20
28 | */
29 | public class TmtsEditText extends TmtsTextView {
30 | private static final String LOG_TAG = "TmtsEditText";
31 | private EditText editText;
32 |
33 | /**
34 | * Constructor of {@link TmtsEditText}.
35 | * @param inst {@link Instrumentation}.
36 | * @param editText {@link EditText}.
37 | */
38 | public TmtsEditText(Instrumentation inst, EditText editText) {
39 | super(inst, editText);
40 | this.editText = editText;
41 | }
42 |
43 | // /**
44 | // * Return the text the EditText is displaying
45 | // * @return the text the EditText is displaying
46 | // */
47 | // public String getText() {
48 | // try {
49 | // Thread.sleep(Constants.ANR_TIME);
50 | // } catch (InterruptedException e) {
51 | // e.printStackTrace();
52 | // }
53 | // return editText.getText().toString();
54 | // }
55 | //
56 | // /**
57 | // * Sets the string value of the EditText
58 | // * @param text the String to set to EditText
59 | // */
60 | // public void setText(final String text) {
61 | // inst.runOnMainSync(new Runnable() {
62 | //
63 | // @Override
64 | // public void run() {
65 | // editText.setText(text);
66 | // }
67 | // });
68 | // }
69 | }
70 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsFrameLayout.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.FrameLayout;
23 |
24 | public class TmtsFrameLayout extends TmtsViewGroup {
25 | private static final String LOG_TAG = "TmtsFrameLayout";
26 | private FrameLayout frameLayout;
27 |
28 | public TmtsFrameLayout(Instrumentation inst, FrameLayout frameLayout) {
29 | super(inst, frameLayout);
30 | this.frameLayout = frameLayout;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsImageButton.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.ImageButton;
23 |
24 | /**
25 | * Taobao Android Test Framework Class for {@link ImageButton}.
26 | * @author bingyang.djj
27 | * Added in 2011-05-23
28 | */
29 | public class TmtsImageButton extends TmtsImageView{
30 | private static final String LOG_TAG = "TmtsImageButton";
31 | private ImageButton imageButton;
32 |
33 | /**
34 | * Constructor of {@link TmtsImageButton}.
35 | * @param inst {@link Instrumentation}.
36 | * @param imageButton {@link ImageButton}.
37 | */
38 | public TmtsImageButton(Instrumentation inst, ImageButton imageButton) {
39 | super(inst, imageButton);
40 | this.imageButton = imageButton;
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsImageView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.graphics.drawable.Drawable;
23 | import android.widget.ImageView;
24 |
25 | /**
26 | * Taobao Android Test Framework Class for {@link ImageView}.
27 | * @author bingyang.djj
28 | * Added in 2011-05-23
29 | */
30 | public class TmtsImageView extends TmtsView{
31 | private static final String LOG_TAG = "TmtsImageView";
32 | private ImageView imageView;
33 |
34 | /**
35 | * Constructor of {@link TmtsImageView}.
36 | * @param inst {@link Instrumentation}.
37 | * @param imageView {@link ImageView}.
38 | */
39 | public TmtsImageView(Instrumentation inst, ImageView imageView) {
40 | super(inst, imageView);
41 | this.imageView = imageView;
42 | }
43 |
44 | /**
45 | * Gets the background drawable
46 | * @return The drawable used as the background for this view, if any.
47 | */
48 | public Drawable getBackground() {
49 | return imageView.getBackground();
50 | }
51 |
52 | /**
53 | * Return the view's drawable, or null if no drawable has been assigned.
54 | * @return The view's drawable
55 | */
56 | public Drawable getDrawable() {
57 | return imageView.getDrawable();
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsListView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.util.Log;
23 | import android.view.View;
24 | import android.widget.ListView;
25 |
26 | import com.taobao.tmts.framework.Constants;
27 | import com.taobao.tmts.framework.TmtsLog;
28 |
29 | /**
30 | * Taobao Android Test Framework Class for {@link ListView}.
31 | *
32 | * @author bingyang.djj Added in 2011-05-23
33 | */
34 | public class TmtsListView extends TmtsAbsListView {
35 | private static final String LOG_TAG = "TmtsListView";
36 | private ListView listView;
37 |
38 | /**
39 | * Constructor of {@link TmtsListView}.
40 | *
41 | * @param inst
42 | * {@link Instrumentation}.
43 | * @param listView
44 | * {@link ListView}.
45 | */
46 | public TmtsListView(Instrumentation inst, ListView listView) {
47 | super(inst, listView);
48 | this.listView = listView;
49 | }
50 |
51 | /**
52 | * Get listview's child view by index
53 | *
54 | * @param index
55 | * The position of the child view
56 | * @return Child view specified by the index, will be null if the index is
57 | * illegal.
58 | */
59 | @SuppressWarnings("deprecation")
60 | private View getViewByIndex(final int index) {
61 | inst.waitForIdleSync();
62 | final long startTime = System.currentTimeMillis();
63 | while (System.currentTimeMillis() < startTime
64 | + Constants.FIND_VIEW_TIME_OUT) {
65 | if (listView.getChildAt(index) == null) {
66 | Log.i(LOG_TAG, "getViewByIndex(" + index
67 | + ") return null, sleep");
68 |
69 | try {
70 | Thread.sleep(Constants.RETRY_TIME);
71 | } catch (InterruptedException e) {
72 | e.printStackTrace();
73 | }
74 |
75 | } else {
76 | Log.i(LOG_TAG, "getViewByIndex(" + index + ") succeed");
77 | return listView.getChildAt(index);
78 | }
79 | }
80 | TmtsLog.e(LOG_TAG, "getViewByIndex(" + index
81 | + ") failed, time out after " + Constants.FIND_VIEW_TIME_OUT
82 | / 1000 + " seconds, return null");
83 | return null;
84 | }
85 |
86 | /**
87 | * Get listview's child view by index and return as a TmtsView. If the line
88 | * is invisable, scroll to it first and the first line is what you want
89 | *
90 | * @param index
91 | * The position of the child view
92 | * @return Child view in TmtsView format specified by the index, will be
93 | * null if the index is illegal
94 | */
95 | public TmtsView getTmtsViewByIndex(int index) {
96 | return new TmtsView(inst, getViewByIndex(index));
97 | }
98 |
99 | /**
100 | * @deprecated Returns the currently checked item. The result is only valid
101 | * if the choice mode has been set to CHOICE_MODE_SINGLE.
102 | * @return The position of the currently checked item or INVALID_POSITION if
103 | * nothing is selected
104 | */
105 | public int getCheckedItemPosition() {
106 | return listView.getCheckedItemPosition();
107 | }
108 |
109 | /**
110 | * Scroll the list to a given line
111 | *
112 | * @param line
113 | * the line to scroll to
114 | */
115 | public void scrollListToLine(final int line) {
116 | try {
117 | Thread.sleep(Constants.FIND_VIEW_TIME_OUT);
118 | } catch (InterruptedException e) {
119 | e.printStackTrace();
120 | }
121 | final int max = listView.getAdapter().getCount() - 1;
122 |
123 | inst.runOnMainSync(new Runnable() {
124 |
125 | @Override
126 | public void run() {
127 |
128 | if (line > max) {
129 | Log.i(LOG_TAG, "line > max, scrollListToLine(" + max + ")");
130 | listView.setSelection(max);
131 |
132 | } else {
133 | Log.i(LOG_TAG, "scrollListToLine(" + line + ")");
134 | listView.setSelection(line);
135 | }
136 | }
137 | });
138 | }
139 |
140 | /**
141 | * Perform click on list item by the given text.
142 | *
143 | * @param text
144 | * Text of the list item.
145 | * @throws InterruptedException
146 | */
147 | public void clickItemByText(String text) throws InterruptedException {
148 | clickUtils.clickOnText(text, false, 1, true, 0);
149 | }
150 |
151 | /**
152 | * Smoothly scroll to the specified adapter
153 | * position. The view will scroll such that the indicated
154 | * position is displayed.
155 | * @param position
156 | * Scroll to this adapter position
157 | */
158 | public void smoothScrollToPosition(final int position) {
159 | try {
160 | Thread.sleep(Constants.FIND_VIEW_TIME_OUT);
161 | } catch (InterruptedException e) {
162 | e.printStackTrace();
163 | }
164 |
165 | final int max = listView.getAdapter().getCount() - 1;
166 |
167 | inst.runOnMainSync(new Runnable() {
168 |
169 | @Override
170 | public void run() {
171 | if (position > max) {
172 | Log.i(LOG_TAG, "position > max, smoothScrollToPosition(" + max + ")");
173 | listView.smoothScrollToPosition(max);
174 |
175 | } else {
176 | Log.i(LOG_TAG, "smoothScrollToPosition(" + position + ")");
177 | listView.smoothScrollToPosition(position);
178 | }
179 | }
180 | });
181 | }
182 |
183 | public int getSelcetedItemPosition() {
184 | try {
185 | Thread.sleep(Constants.ANR_TIME);
186 | } catch (InterruptedException e) {
187 | e.printStackTrace();
188 | }
189 |
190 | return listView.getSelectedItemPosition();
191 | }
192 | }
193 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsMultiAutoCompleteTextView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.MultiAutoCompleteTextView;
23 |
24 | public class TmtsMultiAutoCompleteTextView extends TmtsAutoCompleteTextView {
25 | private static final String LOG_TAG = "TmtsMultiAutoCompleteTextView";
26 | private MultiAutoCompleteTextView multiAutoCompleteTextView;
27 |
28 | public TmtsMultiAutoCompleteTextView(Instrumentation inst,
29 | MultiAutoCompleteTextView multiAutoCompleteTextView) {
30 | super(inst, multiAutoCompleteTextView);
31 | this.multiAutoCompleteTextView = multiAutoCompleteTextView;
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsProgressBar.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.ProgressBar;
23 |
24 | /**
25 | * Taobao Android Test Framework Class for ProgressBar
26 | * @author bingyang.djj
27 | * Added in 2011-05-25
28 | */
29 | public class TmtsProgressBar extends TmtsView {
30 | private static final String LOG_TAG = "TmtsProgressBar";
31 | private ProgressBar progressBar;
32 |
33 | /**
34 | * Constructor of {@link TmtsScrollView}.
35 | * @param inst {@link Instrumentation}.
36 | * @param progressBar {@link ProgressBar}
37 | */
38 | public TmtsProgressBar(Instrumentation inst, ProgressBar progressBar) {
39 | super(inst, progressBar);
40 | this.progressBar = progressBar;
41 | }
42 |
43 | /**
44 | * Set the current progress to the specified value.
45 | * @param progress the new progress, between 0 and getMax()
46 | */
47 | public void setProgress(final int progress) {
48 | inst.runOnMainSync(new Runnable() {
49 |
50 | @Override
51 | public void run() {
52 | progressBar.setProgress(progress);
53 | }
54 | });
55 | }
56 |
57 | /**
58 | * Get the progress bar's current level of progress. Return 0 when the progress bar is in indeterminate mode.
59 | * @return the current progress, between 0 and getMax()
60 | */
61 | public int getProgress() {
62 | return progressBar.getProgress();
63 | }
64 | }
65 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsRelativeLayout.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.RelativeLayout;
23 |
24 | /**
25 | * Taobao Android Test Framework Class for {@link RelativeLayout}.
26 | * @author bingyang.djj
27 | * Added in 2011-05-30
28 | */
29 | public class TmtsRelativeLayout extends TmtsViewGroup{
30 | private static final String LOG_TAG = "TmtsLinearLayout";
31 | private RelativeLayout relativeLayout;
32 |
33 | /**
34 | * Constructor of {@link TmtsView}.
35 | * @param inst {@link Instrumentation}.
36 | * @param relativeLayout {@link RelativeLayout}.
37 | */
38 | public TmtsRelativeLayout(Instrumentation inst, RelativeLayout relativeLayout) {
39 | super(inst, relativeLayout);
40 | this.relativeLayout = relativeLayout;
41 | }
42 |
43 | // @Override
44 | // public void click() {
45 | // inst.runOnMainSync(new Runnable() {
46 | //
47 | // @Override
48 | // public void run() {
49 | // relativeLayout.performClick();
50 | // }
51 | // });
52 | // }
53 |
54 | // /**
55 | // * Perform click on the specified tab.
56 | // * @param index Tab index, start from 0 to totalIndex - 1.
57 | // * @param totalIndex All tab counts.
58 | // */
59 | // @Override
60 | // public void clickOnTab(int index, int totalIndex) {
61 | // try {
62 | // Thread.sleep(Tmts.FIND_VIEW_TIME_OUT);
63 | // } catch (InterruptedException e) {
64 | // e.printStackTrace();
65 | // }
66 | // int[] xy = new int[2];
67 | // if (null == relativeLayout) {
68 | // Log.e(LOG_TAG, "View is null, clickOnTab() will fail");
69 | // }
70 | // relativeLayout.getLocationOnScreen(xy);
71 | // final int viewWidth = relativeLayout.getWidth();
72 | // final int viewHeight = relativeLayout.getHeight();
73 | //
74 | // final float x = xy[0] + (viewWidth / 2.0f) * ((2 * index + 1) / totalIndex);
75 | // float y = xy[1] + (viewHeight / 2.0f);
76 | // super.clickOnScreen(x, y);
77 | // }
78 | }
79 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsScrollView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.ScrollView;
23 |
24 | /**
25 | * Taobao Android Test Framework Class for {@link ScrollView}.
26 | * @author bingyang.djj
27 | * Added in 2011-05-24
28 | */
29 | public class TmtsScrollView extends TmtsFrameLayout{
30 | private static final String LOG_TAG = "TmtsScrollView";
31 | private ScrollView scrollView;
32 |
33 | /**
34 | * Constructor of {@link TmtsScrollView}.
35 | * @param inst {@link Instrumentation}.
36 | * @param scrollView {@link ScrollView}.
37 | */
38 | public TmtsScrollView(Instrumentation inst, ScrollView scrollView) {
39 | super(inst, scrollView);
40 | this.scrollView = scrollView;
41 | }
42 |
43 | /**
44 | * Set the scrolled position of your view.
45 | * @param x the x position to scroll to
46 | * @param y the y position to scroll to
47 | */
48 | public void scrollTo(final int x, final int y) {
49 | inst.runOnMainSync(new Runnable() {
50 |
51 | @Override
52 | public void run() {
53 | scrollView.scrollTo(x, y);
54 | }
55 | });
56 | }
57 |
58 | /**
59 | * Move the scrolled position of your view.
60 | * @param x the amount of pixels to scroll by horizontally
61 | * @param y the amount of pixels to scroll by horizontally
62 | */
63 | public void scrollBy(final int x, final int y) {
64 | inst.runOnMainSync(new Runnable() {
65 |
66 | @Override
67 | public void run() {
68 | scrollView.scrollBy(x, y);
69 | }
70 | });
71 | }
72 |
73 | /**
74 | * Handles scrolling in response to a "home/end" shortcut press.
75 | * This method will scroll the view to the top or bottom and give the focus to the topmost/bottommost component in the new visible area.
76 | * If no component is a good candidate for focus, this scrollview reclaims the focus.
77 | * @param direction the scroll direction: android.view.View.FOCUS_UP to go the top of the view or
78 | * android.view.View.FOCUS_DOWN to go the bottom
79 | */
80 | public void fullScroll(final int direction) {
81 | inst.runOnMainSync(new Runnable() {
82 |
83 | @Override
84 | public void run() {
85 | scrollView.fullScroll(direction);
86 | }
87 | });
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsTextView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.TextView;
23 |
24 | import com.taobao.tmts.framework.Constants;
25 |
26 | /**
27 | * Taobao Android Test Framework Class for {@link TextView}.
28 | * @author bingyang.djj
29 | * Added in 2011-05-19
30 | */
31 | public class TmtsTextView extends TmtsView {
32 | private static final String LOG_TAG = "TmtsTextView";
33 | private TextView textView;
34 |
35 | /**
36 | * Constructor of {@link TmtsTextView}.
37 | * @param inst {@link Instrumentation}.
38 | * @param textView {@link TextView}.
39 | */
40 | public TmtsTextView(Instrumentation inst, TextView textView) {
41 | super(inst, textView);
42 | this.textView = textView;
43 | }
44 |
45 | /**
46 | * Get text of the current {@link TextView}.
47 | * @return Text of the current TextView.
48 | */
49 | public String getText() {
50 | //wait for app to take action sometimes
51 | try {
52 | Thread.sleep(Constants.ANR_TIME);
53 | } catch (InterruptedException e) {
54 | e.printStackTrace();
55 | }
56 | return textView.getText().toString();
57 | }
58 |
59 | /**
60 | * Set the specified text to {@link TextView}.
61 | * @param text the text ready to set to TextView
62 | */
63 | public void setText(final String text) {
64 | inst.runOnMainSync(new Runnable() {
65 |
66 | @Override
67 | public void run() {
68 | textView.setText(text);
69 | }
70 | });
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsView.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import java.lang.reflect.Constructor;
22 |
23 | import android.app.Instrumentation;
24 | import android.os.Handler;
25 | import android.util.Log;
26 | import android.view.View;
27 |
28 | import com.taobao.tmts.framework.Constants;
29 | import com.taobao.tmts.framework.Tmts;
30 | import com.taobao.tmts.framework.TmtsLog;
31 | import com.taobao.tmts.framework.utils.ClickUtils;
32 |
33 | /**
34 | * Taobao Android Test Framework Class for {@link View}.
35 | *
36 | * @author shidun Added in 2011-05-16, last modified by bingyang in 2011-05-19
37 | */
38 | public class TmtsView {
39 | private static final String LOG_TAG = "TmtsView";
40 | protected Instrumentation inst;
41 | private View view;
42 |
43 | protected ClickUtils clickUtils;
44 |
45 | /**
46 | * Constructor of {@link TmtsView}.
47 | *
48 | * @param inst
49 | * {@link Instrumentation}.
50 | * @param view
51 | * {@link View}.
52 | */
53 | public TmtsView(Instrumentation inst, View view) {
54 | this.inst = inst;
55 | this.view = view;
56 | }
57 |
58 | /**
59 | * @deprecated Users should not call this method.
60 | * @param clickUtils
61 | */
62 | public void init(ClickUtils clickUtils) {
63 | this.clickUtils = clickUtils;
64 | }
65 |
66 | /**
67 | * Get a handler associated with the thread running the View.
68 | *
69 | * @return handler associated with the thread running the View. This handler
70 | * can be used to pump events in the UI events queue.
71 | */
72 | public Handler getHandler() {
73 | return view.getHandler();
74 | }
75 |
76 | /**
77 | * Returns this view's identifier.
78 | *
79 | * @return a positive integer used to identify the view or NO_ID if the view
80 | * has no ID
81 | */
82 | public int getViewId() {
83 | Log.i(LOG_TAG, "getViewId() is " + view.getId());
84 | return view.getId();
85 | }
86 |
87 | /**
88 | * Perform click on this view By MontionEvent, use this method to perform
89 | * click much more often than using click()
90 | * @throws InterruptedException
91 | */
92 | @SuppressWarnings("deprecation")
93 | public void doClick() throws InterruptedException {
94 | Log.i(LOG_TAG, "doClick()");
95 |
96 | // sometimes we need to wait for a view to be shown
97 | try {
98 | Thread.sleep(Constants.ANR_TIME);
99 | } catch (InterruptedException e) {
100 | e.printStackTrace();
101 | }
102 | if (null == view) {
103 | TmtsLog.e(LOG_TAG, "View is null, doClick() failed");
104 | }
105 |
106 | ClickUtils.clickOnScreen(this.inst, this.view, false);
107 | }
108 |
109 | public void doLongClick() throws InterruptedException {
110 | Log.i(LOG_TAG, "doLongClick()");
111 |
112 | // sometimes we need to wait for a view to be shown
113 | try {
114 | Thread.sleep(Constants.ANR_TIME);
115 | } catch (InterruptedException e) {
116 | e.printStackTrace();
117 | }
118 | if (null == view) {
119 | TmtsLog.e(LOG_TAG, "View is null, doLongClick() failed");
120 | }
121 |
122 | ClickUtils.clickOnScreen(this.inst, this.view, true);
123 | }
124 |
125 | /**
126 | * Perform click on this view By api:performClick()
127 | *
128 | * @deprecated Use doClick()
129 | */
130 | public void click() {
131 | try {
132 | Thread.sleep(Constants.ANR_TIME);
133 | } catch (InterruptedException e) {
134 | e.printStackTrace();
135 | }
136 |
137 | inst.runOnMainSync(new Runnable() {
138 |
139 | @Override
140 | public void run() {
141 | view.performClick();
142 | }
143 | });
144 | }
145 |
146 | /**
147 | * Return the width of the your view.
148 | *
149 | * @return The width of your view, in pixels.
150 | */
151 | public int getViewWidth() {
152 | Log.i(LOG_TAG, "getViewWidth() is " + view.getWidth());
153 | return view.getWidth();
154 | }
155 |
156 | /**
157 | * Return the height of your view.
158 | *
159 | * @return The height of your view, in pixels.
160 | */
161 | public int getViewHeight() {
162 | Log.i(LOG_TAG, "getViewWidth() is " + view.getHeight());
163 | return view.getHeight();
164 | }
165 |
166 | /**
167 | * Perform click on the specified tab. Call this method to click muti-tabs.
168 | *
169 | * @param index
170 | * Tab index, start from 0 to totalIndex - 1.
171 | * @param totalIndex
172 | * All tab counts.
173 | */
174 | public void clickOnTab(int index, int totalIndex) {
175 | try {
176 | Thread.sleep(Constants.FIND_VIEW_TIME_OUT);
177 | } catch (InterruptedException e) {
178 | e.printStackTrace();
179 | }
180 | int[] xy = new int[2];
181 | if (null == view) {
182 | Log.e(LOG_TAG, "View is null, clickOnTab() will fail");
183 | }
184 | view.getLocationOnScreen(xy);
185 | final int viewWidth = view.getWidth();
186 | final int viewHeight = view.getHeight();
187 |
188 | final float x = xy[0] + (viewWidth / 2.0f)
189 | * ((2 * index + 1) / totalIndex);
190 | float y = xy[1] + (viewHeight / 2.0f);
191 | ClickUtils.clickOnScreen(this.inst, x, y);
192 | }
193 |
194 | /**
195 | *
196 | * @param
197 | * @param name
198 | * @param caster
199 | * @return
200 | * @throws Exception
201 | */
202 | public T findViewById(String name, Class caster) throws Exception {
203 | return caster.cast(view.findViewById(Tmts.getIdByName(name)));
204 | }
205 |
206 | /**
207 | *
208 | * @param
209 | * @param name
210 | * @param caster
211 | * @return
212 | * @throws Exception
213 | */
214 | public T findTmtsViewById(String name, Class caster) throws Exception {
215 | Constructor>[] constructors = caster.getDeclaredConstructors();
216 | Object obj = constructors[0].newInstance(inst, view.findViewById(Tmts.getIdByName(name)));
217 | return caster.cast(obj);
218 | }
219 |
220 | public void requestFocus() {
221 | inst.waitForIdleSync();
222 | inst.runOnMainSync(new Runnable() {
223 |
224 | @Override
225 | public void run() {
226 | view.requestFocus();
227 | }
228 | });
229 | }
230 | }
231 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsViewAnimator.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.ViewAnimator;
23 |
24 | public class TmtsViewAnimator extends TmtsFrameLayout {
25 | private static final String LOG_TAG = "TmtsViewAnimator";
26 | private ViewAnimator viewAnimator;
27 |
28 | public TmtsViewAnimator(Instrumentation inst, ViewAnimator viewAnimator) {
29 | super(inst, viewAnimator);
30 | this.viewAnimator = viewAnimator;
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsViewFlipper.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.ViewFlipper;
23 |
24 | /**
25 | * Taobao Android Test Framework Class for {@link ViewFlipper}.
26 | * @author bingyang.djj
27 | * Added in 2011-06-03
28 | */
29 | public class TmtsViewFlipper extends TmtsViewAnimator {
30 | private static final String LOG_TAG = "TmtsViewFlipper";
31 | private ViewFlipper viewFlipper;
32 |
33 | /**
34 | * Constructor of {@link TmtsViewFlipper}.
35 | * @param inst {@link Instrumentation}.
36 | * @param viewFlipper {@link ViewFlipper}.
37 | */
38 | public TmtsViewFlipper(Instrumentation inst, ViewFlipper viewFlipper) {
39 | super(inst, viewFlipper);
40 | this.viewFlipper = viewFlipper;
41 | }
42 |
43 | /**
44 | * Sets which child view will be displayed.
45 | * @param whichChild the index of the child view to display
46 | */
47 | public void setChildView(final int whichChild) {
48 | inst.runOnMainSync(new Runnable() {
49 |
50 | @Override
51 | public void run() {
52 | viewFlipper.setDisplayedChild(whichChild);
53 | }
54 | });
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsViewGroup.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.view.ViewGroup;
23 |
24 | public class TmtsViewGroup extends TmtsView {
25 | private static final String LOG_TAG = "TmtsViewGroup";
26 | private ViewGroup viewGroup;
27 |
28 | public TmtsViewGroup(Instrumentation inst, ViewGroup viewGroup) {
29 | super(inst, viewGroup);
30 | this.viewGroup = viewGroup;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsViewSwitcher.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.app.Instrumentation;
22 | import android.widget.ViewSwitcher;
23 |
24 | public class TmtsViewSwitcher extends TmtsViewAnimator {
25 | private static final String LOG_TAG = "TmtsViewSwitcher";
26 | private ViewSwitcher viewSwitcher;
27 |
28 | public TmtsViewSwitcher(Instrumentation inst, ViewSwitcher viewSwitcher) {
29 | super(inst, viewSwitcher);
30 | this.viewSwitcher = viewSwitcher;
31 | }
32 |
33 | }
34 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsWebElement.java:
--------------------------------------------------------------------------------
1 | /* TMTS - Android automation testing Framework.
2 | Copyright (C) 2010-2011 TaoBao UI AutoMan Team
3 |
4 | This program is free software; you can redistribute it and/or
5 | modify it under the terms of the GNU General Public License
6 | as published by the Free Software Foundation; either version 2
7 | of the License, or (at your option) any later version.
8 |
9 | This program is distributed in the hope that it will be useful,
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 | GNU General Public License for more details.
13 |
14 | You should have received a copy of the GNU General Public License
15 | along with this program; if not, write to the Free Software
16 | Foundation, Inc., HuaXing road, Hangzhou,China.
17 | Email:taichan@taobao.com,shidun@taobao.com,bingyang@taobao.com
18 | */
19 | package com.taobao.tmts.framework.view;
20 |
21 | import android.util.Log;
22 |
23 | public class TmtsWebElement {
24 | private final TmtsWebView mTmtsWebView;
25 |
26 | public TmtsWebElement(TmtsWebView tmtsWebView){
27 | mTmtsWebView = tmtsWebView;
28 | }
29 |
30 | public TmtsWebElement nextSibling() {
31 | //TODO
32 | return this;
33 | }
34 |
35 | public String innerHTML(){
36 | return excuteJavaScriptAndReturn("element.innerHTML");
37 | }
38 |
39 | public void click() {
40 | Log.d("TmtsActivity","click in webelement");
41 | loadJavascript("element.dispatchEvent(e)");
42 | }
43 |
44 |
45 | private String excuteJavaScriptAndReturn(String script) {
46 | loadJavascript("window.webdriver.excutejs("+script+")");
47 | return JavascriptInterface.getInstance().getResult();
48 | }
49 |
50 | public void excuteJs(final String script) throws InterruptedException {
51 | loadJavascript(script);
52 | }
53 |
54 | private void loadJavascript(String script) {
55 | // this.currentScript = script;
56 | mTmtsWebView.getInterface().setReady(false);
57 | mTmtsWebView.getWebView().loadUrl("javascript:"+ script);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsWebView.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/TMTS/Android-automation/c3f23d528a6334eac402fe95045624e083f2a31f/Tmts_Java/src/com/taobao/tmts/framework/view/TmtsWebView.java
--------------------------------------------------------------------------------