#代码前准备
#python -m uiautomator2 init
#python -m weditor
获取当前activity名:adb shell dumpsys window | findstr mCurrentFocus
import uiautomator2 as u2
import time
#熟悉基本功能
d = u2.connect('28084106100000116')
print(d.info)
print(d.serial)
d.screen_off()
time.sleep(3)
d.screen_on()
time.sleep(3)
#d.app_start('com.android.tv.settings')
d(resourceId="com.google.android.tvlauncher:id/settings") #进入设置
time.sleep(1)
d.xpath('//*[@resource-id="com.google.android.tvlauncher:id/settings"]').click() #进入WiFi设置
time.sleep(1)
d.xpath('//*[@resource-id="com.android.tv.settings:id/list"]/android.widget.LinearLayout[3]/android.widget.LinearLayout[1]').click()
time.sleep(1)
d(resourceId="android:id/switch_widget").click() #关闭WiFi开关
image = d.screenshot() # default format="pillow"
image.save("close.jpg")
d(resourceId="android:id/switch_widget").click() #打开WiFi开关
image = d.screenshot() # default format="pillow"
image.save("open.jpg")
time.sleep(1)
d.press("home")
time.sleep(1)
d(resourceId="com.google.android.tvlauncher:id/inputs").click()
time.sleep(1)
d.xpath('//*[@resource-id="com.google.android.tvlauncher:id/list"]/android.widget.FrameLayout[1]').click()
time.sleep(3)
d.keyevent("adb shell input keyevent 23")
d.keyevent("adb shell input keyevent 22")
d.keyevent("adb shell input keyevent 23")
d.keyevent("adb shell input keyevent 23")
time.sleep(10)
d.press("home")
time.sleep(2)
if d.xpath('//*[@text="xm11"]').exists:
print('Google account add successful')
#增删账号函数
def addGoogleaccount():
d(resourceId="com.google.android.tvlauncher:id/settings").click() #进入设置
time.sleep(2)
d.xpath('//*[@text="Accounts & Sign-In"]').click()#进入Accounts&Sign-In
time.sleep(2)
#d.keyevent("adb shell input keyevent 20")
#time.sleep(2)
if (d.xpath('//*[@text="Add account"]').exists):
d.xpath('//*[@text="Add account"]').click()#点击Add account
time.sleep(2)
d.xpath('//*[@text="Google"]').click()#点击Google
time.sleep(2)
#d.xpath('//*[@text="Sign In"]').click()
d.keyevent("adb shell input keyevent 23") #点击sign in不成功,改成keyevent点击确定进入
time.sleep(2)
d.keyevent("adb shell input keyevent 23")
time.sleep(2)
d.keyevent("adb shell input keyevent 20")
time.sleep(2)
d.keyevent("adb shell input keyevent 23")
time.sleep(10)
d.xpath('//*[@resource-id="identifierId"]').wait(timeout=5.0)
d.xpath('//*[@resource-id="identifierId"]').click()
#d.xpath('//*[@resource-id="identifierId"]').clear_text()
d.xpath('//*[@resource-id="identifierId"]').set_text('xxx')
d.xpath('//*[@text="Next"]').click()
time.sleep(5)
d.xpath('//android.widget.EditText').click()
d.xpath('//android.widget.EditText').wait(timeout=5.0)
d.xpath('//android.widget.EditText').set_text('xxx')
d.xpath('//*[@text="Next"]').click()
time.sleep(10)
if d.xpath('//*[@text="xxx@gmail.com"]').exists:
print('Google account add successful')
d.xpath('//*[@text="xxx@gmail.com"]').click()
time.sleep(2)
d.xpath('//*[@text="Remove account"]').click()
time.sleep(2)
d.xpath('//*[@resource-id="com.android.tv.settings:id/guidedactions_list"]/android.widget.LinearLayout[2]/android.widget.LinearLayout[1]').click()
time.sleep()
if __name__=='__main__':
addGoogleaccount()