Snake's Home

robotium测试读取sd卡

当时为了解决帐号问题,用robotium 测试时,先将帐号写到sd.然后实现读出来,解决帐号冲突。

page.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package com.XXXcircle.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;

import android.os.Environment;

public class Page {

final String FILE_NAME = "/number.txt";
// guidepage
public String guideActivity = "com.XXX.app.activity.GuideActivity";
public String guidepagebtn = "guide_page_btn";

// Login Page
public String LoginNewActivity = "com.XXX.app.activity.LoginNewActivity";
public String username = "login_account_et";
public String password = "login_password_et";
public String loginbtn = "login_btn";
public String HomeActivity = "HomeActivity";

// Logout Page
public String backbtn = "widget_custom_titlebar_right_gamma_btn";
public String popuptext = "popup_text";
public String settingmainactivity = "SettingMainActivity";
public String settingmianlayout = "setting_main_about_XXX_layout";
public String settingmainquit = "setting_main_quit";
public String positivebtn = "positiveButton";

// Home Page
public String homeactivity = "com.XXX.app.activity.HomeActivity";

// 动态
public String XXXtab = "page_home_XXX_tab_btn";
// 联系人
public String contactstab = "page_home_contacts_tab_btn";
// 探究
public String discovertab = "page_home_discover_tab_btn";

// 我
public String profiletab = "page_home_profile_tab_btn";
// 查找
// public String searchbtn = "widget_custom_titlebar_right_alpha_btn";
// 查找(放大镜)
public String searchbtn = "widget_custom_titlebar_right_beta_btn";
// 设置(加号)
public String setbtn = "widget_custom_titlebar_right_gamma_btn";

// 查找
public String SearchActivity = "com.XXX.app.activity.SearchActivity";
public String searchbtnet = "page_search_content_et";
public String searchcancelbtn = "page_search_cancel_btn";
public String searchbackbtn = "page_search_back_ib";

// 添加好友
public String searchfriendactivity = "SearchFriendActivity";
// 查找框
public String editsearch = "edit_searchfriends";
// 清空
public String searchdel = "txt_search_del";
// 搜索按钮
public String searchclick = "txt_search_click";
// 好友推荐
public String recommend = "iv_recommend_icon";
// 扫一扫
public String saoyisao = "iv_er_icon";
// 结果
public String finduserresultactivity = "com.XXX.app.activity.FindUserResultActivity";
public String backupbtn = "widget_custom_titlebar_back_btn";

// 公共群聊天
public String orgroup = "img_organizationgroup";
public String publictalkgroupactivity = "com.XXX.app.activity.PublicTalkGroupListActivity";
public String txtgroup = "txt_group";
public String talkgroupicon = "iv_talkgroup_icon";
public String chatactivity = "com.XXX.app.activity.ChatActivity";
public String chataudiobtn = "page_chat_message_et";
public String chatmessagesendbtn = "page_chat_message_send_btn";

// 探究
// 动态
public String customimgview = "widget_custom_imgview_iv";
public String discoveractivity = "com.XXX.app.activity.DiscoverActivity";
// 笔的标志
public String releasebtn = "release_dynamic_btn";

// 我
public String settingactivity = "com.XXX.app.activity.SettingMainActivity";
public String mysetting = "r_mysetting";

/*
* 通过ID输入文本内容 enterTextById( String id, String s,int t)
* id:Id(hierarchviewer)获取的文本框id,字符串格式id s:需要输入的文本信息 t:timedelay
* 触发按键后,停留多久(ms)
*/


/*
* public int enterTextById( String id, String s, int t) { int ctrl;
* EditText v;
*
* if (s == "") { return -1; } //
* ctrl=solo.getCurrentActivity().getResources
* ().getIdentifier(id,"id",solo.getCurrentActivity().getPackageName());
* View ctr = solo.getView(id); v = (EditText) (ctr);
*
* // 先清空 solo.clearEditText(v); solo.enterText(v, s); solo.sleep(t); return
* 0; }
*
*
*
* 通过ID点击打开VIEW clickCtrlById( String s,int t)
* s:Id(hierarchviewer)触摸按键的id,字符串格式 t:timedelay 触发按键后,停留多久(ms)
* TARGET_PACKAGE_ID,是一个宏变量,字符串格式,定义了目标APK的包名
*
* public int clickCtrlById( String s, int t) { int ctrl; View v; if (s ==
* "") { return -1; } ctrl = solo.getCurrentActivity().getResources()
* .getIdentifier(s, "id", "com.leju.platform"); v = solo.getView(ctrl);
* solo.clickOnView(v); solo.sleep(t); return 0; } /* /*
* /**我的需求是找列表中符合某种条件的Item可以点进去查看思路:滑动一个,判断一个,也就是说,每次点击的都是当前屏幕的第一个
*
* public void findItem() { solo.sleep(_1S); //获取listView 随便怎么获取 ListView
* listView = solo.getView(ListView.class, 0);
*
* if (listView.getCount() == 0) { assertEquals("", "列表为空"); } int clickLine
* = 1; int oneScreenCount = listView.getChildCount(); for (int index = 0,
* size = listView.getCount(); index < size; index++) { log("列表点击第" + (index
* + 1) + "个"); solo.clickInList(clickLine); if ( your conditions) {
* log("列表符合条件的是第" + (index + 1) + "个"); //break; return; } if (index >=
* (size - oneScreenCount) || (size <= oneScreenCount)) { //滑动到了列表的最后一屏
* clickLine = clickLine + 1; } else { clickLine = 1; } if (index == (size -
* 1)) { assertEquals("", "当前没有符合条件的东西"); } solo.goBack();
* solo.scrollListToLine(0, index + 1); }
*
* //如果return换成break 此时点击clickLine就是你想要的了 //solo.clickInList(clickLine); }
*
*
* RelativeLayout parentView =
* (RelativeLayout)solo.getView("pic_lick_num_id").getParent(); LinearLayout
* linearlayout=(LinearLayout)parentView.findViewById(1);
* linearlayout.getChildAt(0);
* solo.clickOnView(solo.getView("gallery_flower_btn"));
*
*
* try { View fastLoginDialog = solo.getView("xxxxxxx:id/has_account",0);
* solo.clickOnView(fastLoginDialog);
*
* View username = solo.getView("xxxxxxx:id/edit_account",0); EditText un =
* (EditText)username; solo.typeText(un, "hh1906"); View pwd =
* solo.getView("xxxxxxx:id/edit_pwd",0); EditText pw = (EditText)pwd;
* solo.typeText(pw, "123456"); solo.clickOnButton("登 录");
* Thread.sleep(5000);
*
*
* } catch (AssertionFailedError e) { //登录操作 View rb =
* solo.getView("xxxxxxx:id/right_bt",0); solo.clickOnView(rb); View avatar
* = solo.getView("xxxxxxx:id/avatar",0); solo.clickOnView(avatar); View
* username = solo.getView("xxxxxxx:id/edit_account",0); EditText un =
* (EditText)username; solo.typeText(un, "hh1906"); View pwd =
* solo.getView("xxxxxxx:id/edit_pwd",0); EditText pw = (EditText)pwd;
* solo.typeText(pw, "123456"); solo.clickOnButton("登 录");
* Thread.sleep(5000);
*
* }
*/

private void write(String content) {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 获取SD卡的目录
File sdCardDir = Environment.getExternalStorageDirectory();
File targetFile = new File(sdCardDir.getCanonicalPath()
+ FILE_NAME);
// 以指定文件创建RandomAccessFile对象
RandomAccessFile raf = new RandomAccessFile(targetFile, "rw");
// 将文件记录指针移动到最后
raf.seek(targetFile.length());
// 输出文件内容
raf.write(content.getBytes());
raf.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

public String read() {
try {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 获得SD卡对应的存储目录
File sdCardDir = Environment.getExternalStorageDirectory();
// 获取指定文件对应的输入流
FileInputStream fis = new FileInputStream(
sdCardDir.getCanonicalPath() + FILE_NAME);
// 将指定输入流包装成BufferReader
BufferedReader br = new BufferedReader(new InputStreamReader(
fis));
StringBuilder sb = new StringBuilder("");
String line = null;
// 循环读取文件内容
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public Object[] splitstring(String number) {

Object[] numberArray=number.split(",");
//Object[] o = new Object[2];
//o[1]=numberArray[1];
//o[2]=numberArray[2];
return numberArray;
}

}

ddfriends.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.XXXcircle.testcases;


import org.testng.annotations.Test;

import com.XXXcircle.action.addfriendsaction;
import com.XXXcircle.action.chataction;
import com.XXXcircle.test.Page;



public class addfriends extends addfriendsaction{

/*
public void testread(){
Page page = new Page();
String a=page.read();
Object[] as= page.splitstring(a);
System.out.print(as[0]);
System.out.print(as[1]);
}
*/


public addfriends() throws ClassNotFoundException {
super();

}

public void testaddfriend() throws ClassNotFoundException{
Page page = new Page();
String a=page.read();
Object[] as= page.splitstring(a);
//System.out.print(as[0]);
//System.out.print(as[1]);
//testread();
testaddfriends(as[0].toString(), as[1].toString(),"尼玛坤爷");

}
}