|
| 1 | +package com.carlos.grabredenvelope |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.content.Intent |
| 5 | +import android.view.KeyEvent.* |
| 6 | +import androidx.test.core.app.ApplicationProvider.getApplicationContext |
| 7 | +import androidx.test.ext.junit.runners.AndroidJUnit4 |
| 8 | +import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation |
| 9 | +import androidx.test.uiautomator.By |
| 10 | +import androidx.test.uiautomator.UiDevice |
| 11 | +import androidx.test.uiautomator.UiObject2 |
| 12 | +import androidx.test.uiautomator.Until |
| 13 | +import com.carlos.cutils.util.LogUtils |
| 14 | +import org.junit.After |
| 15 | +import org.junit.Before |
| 16 | +import org.junit.Test |
| 17 | +import org.junit.runner.RunWith |
| 18 | +import java.util.* |
| 19 | + |
| 20 | +/** |
| 21 | + * _ooOoo_ |
| 22 | + * o8888888o |
| 23 | + * 88" . "88 |
| 24 | + * (| -_- |) |
| 25 | + * O\ = /O |
| 26 | + * ____/`---'\____ |
| 27 | + * .' \\| |// `. |
| 28 | + * / \\||| : |||// \ |
| 29 | + * / _||||| -:- |||||- \ |
| 30 | + * | | \\\ - /// | | |
| 31 | + * | \_| ''\---/'' | | |
| 32 | + * \ .-\__ `-` ___/-. / |
| 33 | + * ___`. .' /--.--\ `. . __ |
| 34 | + * ."" '< `.___\_<|>_/___.' >'"". |
| 35 | + * | | : `- \`.;`\ _ /`;.`/ - ` : | | |
| 36 | + * \ \ `-. \_ __\ /__ _/ .-` / / |
| 37 | + * ======`-.____`-.___\_____/___.-`____.-'====== |
| 38 | + * `=---=' |
| 39 | + * ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 40 | + * 佛祖保佑 永无BUG |
| 41 | + * 佛曰: |
| 42 | + * 写字楼里写字间,写字间里程序员; |
| 43 | + * 程序人员写程序,又拿程序换酒钱。 |
| 44 | + * 酒醒只在网上坐,酒醉还来网下眠; |
| 45 | + * 酒醉酒醒日复日,网上网下年复年。 |
| 46 | + * 但愿老死电脑间,不愿鞠躬老板前; |
| 47 | + * 奔驰宝马贵者趣,公交自行程序员。 |
| 48 | + * 别人笑我忒疯癫,我笑自己命太贱; |
| 49 | + * 不见满街漂亮妹,哪个归得程序员? |
| 50 | + */ |
| 51 | + |
| 52 | +/** |
| 53 | + * Github: https://github.com/xbdcc/. |
| 54 | + * Created by Carlos on 2019-10-31. |
| 55 | + * 自动发红包脚本 |
| 56 | + */ |
| 57 | +@RunWith(AndroidJUnit4::class) |
| 58 | +class WechatTest { |
| 59 | + |
| 60 | + private lateinit var mUiDevice: UiDevice |
| 61 | + private val PACKAGE_NAME = "com.tencent.mm" |
| 62 | + private val FRIEND_NAME = "昵称" |
| 63 | + private val GROUP_NAME = "小不点" |
| 64 | + private val LAUNCH_TIMEOUT = 10*1000L |
| 65 | + private val ELEMENT_TIMEOUT = 7*1000L |
| 66 | + |
| 67 | + @Before |
| 68 | + fun start() { |
| 69 | + LogUtils.d("start:" +Date()) |
| 70 | + LogUtils.isShowLog = true |
| 71 | + |
| 72 | + mUiDevice = UiDevice.getInstance(getInstrumentation()) |
| 73 | + |
| 74 | + val context = getApplicationContext<Context>() |
| 75 | + val intent = context.packageManager.getLaunchIntentForPackage(PACKAGE_NAME) |
| 76 | + if (intent == null) { |
| 77 | + LogUtils.d("can not find this app.") |
| 78 | + return |
| 79 | + } |
| 80 | + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK) |
| 81 | + context.startActivity(intent) |
| 82 | + |
| 83 | + LogUtils.d("" +Date()) |
| 84 | + // Wait for the app to appear |
| 85 | + mUiDevice.wait(Until.hasObject(By.pkg(PACKAGE_NAME).depth(0)), LAUNCH_TIMEOUT) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun sendPacket() { |
| 90 | + sendPacketToGroup() |
| 91 | +// sendPacketToFriend() |
| 92 | + } |
| 93 | + |
| 94 | + fun sendPacketToGroup() { |
| 95 | + // 找到对应的列表并点击 |
| 96 | + val lists = mUiDevice.findObjects(By.res("com.tencent.mm:id/b4r")) |
| 97 | + for (list in lists) { |
| 98 | + mUiDevice.wait(Until.findObject(By.text(GROUP_NAME)), ELEMENT_TIMEOUT).click() |
| 99 | + break |
| 100 | + } |
| 101 | + |
| 102 | + //点击加号显示底部更多菜单 |
| 103 | + mUiDevice.wait(Until.findObject(By.res("com.tencent.mm:id/aks")), ELEMENT_TIMEOUT).click() |
| 104 | + //如果不加此等待更新可能会出现获取到的子UiObject为空 |
| 105 | + mUiDevice.waitForWindowUpdate(PACKAGE_NAME, LAUNCH_TIMEOUT) |
| 106 | + //点击红包按钮 |
| 107 | + mUiDevice.wait(Until.findObject(By.res("com.tencent.mm:id/pw")), ELEMENT_TIMEOUT).children[4].click() |
| 108 | + mUiDevice.waitForWindowUpdate(PACKAGE_NAME, LAUNCH_TIMEOUT) |
| 109 | + //红包EditText框,输入红包金额等 |
| 110 | + |
| 111 | + val editText = mUiDevice.findObjects(By.clazz("android.widget.EditText")) |
| 112 | + for (uiObject2 in editText) { |
| 113 | + uiObject2.print() |
| 114 | + } |
| 115 | + editText[0].text = "0.03" |
| 116 | + editText[1].text = "3" |
| 117 | + //点击塞钱进红包按钮 |
| 118 | + mUiDevice.findObject(By.res("com.tencent.mm:id/ddo")).click() |
| 119 | + //出现输入密码框 |
| 120 | + mUiDevice.wait(Until.findObject(By.res("com.tencent.mm:id/gwo")), ELEMENT_TIMEOUT) |
| 121 | + //输入支付密码 |
| 122 | + mUiDevice.pressKeyCode(KEYCODE_1) |
| 123 | + mUiDevice.pressKeyCode(KEYCODE_3) |
| 124 | + mUiDevice.pressKeyCode(KEYCODE_4) |
| 125 | + mUiDevice.pressKeyCode(KEYCODE_6) |
| 126 | + mUiDevice.pressKeyCode(KEYCODE_7) |
| 127 | + mUiDevice.pressKeyCode(KEYCODE_9) |
| 128 | + |
| 129 | + } |
| 130 | + |
| 131 | + fun sendPacketToFriend() { |
| 132 | + // 找到对应的列表并点击 |
| 133 | + val lists = mUiDevice.findObjects(By.res("com.tencent.mm:id/bah")) |
| 134 | + for (list in lists) { |
| 135 | + mUiDevice.wait(Until.findObject(By.text(FRIEND_NAME)), ELEMENT_TIMEOUT).click() |
| 136 | + } |
| 137 | + |
| 138 | + //点击加号显示底部更多菜单 |
| 139 | + mUiDevice.wait(Until.findObject(By.res("com.tencent.mm:id/aqk")), ELEMENT_TIMEOUT).click() |
| 140 | + //如果不加此等待更新可能会出现获取到的子UiObject为空 |
| 141 | + mUiDevice.waitForWindowUpdate(PACKAGE_NAME, LAUNCH_TIMEOUT) |
| 142 | + //点击红包按钮 |
| 143 | + mUiDevice.wait(Until.findObject(By.res("com.tencent.mm:id/zo")), ELEMENT_TIMEOUT).children[4].click() |
| 144 | + //输入金额 |
| 145 | + mUiDevice.wait(Until.findObject(By.res("com.tencent.mm:id/d8f")), ELEMENT_TIMEOUT).text = "0.03" |
| 146 | + //点击塞钱进红包按钮 |
| 147 | + mUiDevice.wait(Until.findObject(By.res("com.tencent.mm:id/da3")), ELEMENT_TIMEOUT).click() |
| 148 | + //出现输入密码框 |
| 149 | + mUiDevice.wait(Until.findObject(By.res("com.tencent.mm:id/aos")), ELEMENT_TIMEOUT) |
| 150 | + //输入支付密码 |
| 151 | + mUiDevice.pressKeyCode(KEYCODE_1) |
| 152 | + mUiDevice.pressKeyCode(KEYCODE_3) |
| 153 | + mUiDevice.pressKeyCode(KEYCODE_4) |
| 154 | + mUiDevice.pressKeyCode(KEYCODE_6) |
| 155 | + mUiDevice.pressKeyCode(KEYCODE_7) |
| 156 | + mUiDevice.pressKeyCode(KEYCODE_9) |
| 157 | + } |
| 158 | + |
| 159 | + @After |
| 160 | + fun after() { |
| 161 | + LogUtils.d("after:" +Date()) |
| 162 | +// mUiDevice.abc() |
| 163 | + } |
| 164 | + |
| 165 | + |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +fun UiObject2.print() { |
| 171 | + println("text") |
| 172 | + LogUtils.d("id:" + this.resourceName + ";text:" + this.text ) |
| 173 | +} |
| 174 | + |
0 commit comments