Skip to content

Instantly share code, notes, and snippets.

@aont
Created July 2, 2024 07:51
Show Gist options
  • Save aont/1fead4c4d61eb348c49a4e2e853f7072 to your computer and use it in GitHub Desktop.
Save aont/1fead4c4d61eb348c49a4e2e853f7072 to your computer and use it in GitHub Desktop.
import json
import sys
import time
import contextlib
# import os
from appium import webdriver
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.actions import interaction
from selenium.webdriver.common.actions.action_builder import ActionBuilder
from selenium.webdriver.common.actions.pointer_input import PointerInput
from selenium.common.exceptions import NoSuchElementException as SeleniumNoSuchElementException
def get_wifi_status(driver: webdriver.Remote):
dumpsys_wifi_result: str = driver.execute_script("mobile: shell", {"command": "dumpsys", "args": ["wifi"]})
# sys.stderr.write(f"info: {result=}\n")
str_wifi_is = "Wi-Fi is "
assert dumpsys_wifi_result.startswith(str_wifi_is)
dumpsys_wifi_result = dumpsys_wifi_result.replace("\r\n", "\n")
nl_pos = dumpsys_wifi_result.find("\n")
wifi_status = dumpsys_wifi_result[len(str_wifi_is):nl_pos]
# sys.stderr.write(f"debug: {wifi_status=}\n")
if wifi_status == "enabled":
return True
elif wifi_status == "disabled":
return False
else:
raise Exception(dumpsys_wifi_result)
with contextlib.ExitStack() as exit_stack:
options = AppiumOptions()
options.load_capabilities({
"appium:automationName": "UiAutomator2",
"appium:platformName": "Android",
"appium:newCommandTimeout": 3600,
"appium:connectHardwareKeyboard": True
})
sys.stderr.write("debug: connecting to appium\n")
driver = webdriver.Remote("http://127.0.0.1:4723", options=options)
exit_stack.callback(driver.quit)
sys.stderr.write("debug: check wifi status\n")
if not get_wifi_status(driver):
sys.stderr.write("debug: connect wifi\n")
driver.toggle_wifi()
sys.stderr.write("debug: go home\n")
driver.execute_script('mobile: pressKey', {"keycode": 187})
time.sleep(1)
driver.execute_script('mobile: pressKey', {"keycode": 3})
time.sleep(1)
sys.stderr.write("debug: terminate ispeed\n")
driver.terminate_app("com.android.chrome")
sys.stderr.write("debug: sleep 2 sec\n")
time.sleep(2)
sys.stderr.write("debug: launch ispeed\n")
driver.activate_app("jp.co.rakuten_sec.ispeed")
sys.stderr.write("debug: sleep 10 sec\n")
time.sleep(10)
# //android.widget.Button[@resource-id="android:id/button2"]
sys.stderr.write("debug: check if alert exists\n")
alert_title_list = driver.find_elements(by=AppiumBy.XPATH, value="//android.widget.TextView[@resource-id=\"android:id/alertTitle\"]")
if len(alert_title_list)==1:
alert_title = alert_title_list[0]
assert alert_title.get_attribute("text") == "お知らせ"
sys.stderr.write("debug: find and click close button\n")
close_button = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.Button[@resource-id=\"android:id/button2\"]")
assert close_button.get_attribute("text") == "閉じる"
close_button.click()
elif len(alert_title_list)>1:
raise Exception("multiple alertTitle found", alert_title_list)
sys.stderr.write("debug: sleep 5 sec\n")
time.sleep(5)
sys.stderr.write("debug: get awll size\n")
awll = driver.find_element(by=AppiumBy.XPATH, value="/hierarchy/android.widget.FrameLayout/android.widget.LinearLayout")
width, height = json.loads("["+awll.get_attribute("bounds").replace("][","],[")+"]")[1]
sys.stderr.write("debug: find and click menu icon\n")
menu_icon = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.TextView[@resource-id=\"jp.co.rakuten_sec.ispeed:id/menu_icon\"]")
assert menu_icon.get_attribute("text") == "メニュー"
menu_icon.click()
sys.stderr.write("debug: sleep 5 sec\n")
time.sleep(5)
sys.stderr.write("debug: check if information_notification exists\n")
info_notify_button_list = driver.find_elements(by=AppiumBy.XPATH, value="//android.widget.Button[@resource-id=\"jp.co.rakuten_sec.ispeed:id/information_notification\"]")
if len(info_notify_button_list) == 0:
pass
elif len(info_notify_button_list) == 1:
sys.stderr.write("debug: go back to menu page\n")
driver.execute_script('mobile: pressKey', {"keycode": 4})
else:
raise Exception("multiple information_notification", info_notify_button_list)
sys.stderr.write("debug: sleep 3 sec\n")
time.sleep(3)
while True:
sys.stderr.write("debug: scroll\n")
actions = ActionChains(driver)
actions.w3c_actions = ActionBuilder(driver, mouse=PointerInput(interaction.POINTER_TOUCH, "touch"))
actions.w3c_actions.pointer_action.move_to_location(width//2, (height*5)//8)
actions.w3c_actions.pointer_action.pointer_down()
actions.w3c_actions.pointer_action.move_to_location(width//2, (height*3)//8)
actions.w3c_actions.pointer_action.release()
actions.perform()
sys.stderr.write("debug: sleep 2 sec\n")
time.sleep(2)
sys.stderr.write("debug: find and click menu_market_nikkei\n")
try:
menu_market_nikkei = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.ImageView[@resource-id=\"jp.co.rakuten_sec.ispeed:id/menu_market_nikkei\"]")
except SeleniumNoSuchElementException:
continue
else:
menu_market_nikkei.click()
break
sys.stderr.write("debug: check if alert exists\n")
alert_title = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.TextView[@resource-id=\"android:id/alertTitle\"]")
assert alert_title.get_attribute("text") == "WEBコンテンツ表示"
sys.stderr.write("debug: check yes button\n")
yes_button = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.Button[@resource-id=\"android:id/button1\"]")
assert yes_button.get_attribute("text") == "はい"
sys.stderr.write("debug: check wifi status\n")
if get_wifi_status(driver):
sys.stderr.write("debug: disconnect wifi\n")
driver.toggle_wifi()
exit_stack.callback(driver.toggle_wifi)
sys.stderr.write("debug: click yes button\n")
yes_button.click()
sys.stderr.write("debug: sleep 5 sec\n")
time.sleep(5)
sys.stderr.write("debug: click open with chrome\n")
open_with_chrome = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.TextView[@resource-id=\"android:id/title\"]")
assert open_with_chrome.get_attribute("text") == "Chromeで開く"
button_once = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.Button[@resource-id=\"android:id/button_once\"]")
assert button_once.get_attribute("text") == "1回のみ"
button_once.click()
sys.stderr.write("debug: sleep 5 sec\n")
time.sleep(5)
sys.stderr.write("debug: check chrome menu button\n")
chrome_menu_button = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.ImageButton[@resource-id=\"com.android.chrome:id/menu_button\"]")
sys.stderr.write("debug: click chrome menu button\n")
chrome_menu_button.click()
sys.stderr.write("debug: sleep 5 sec\n")
time.sleep(5)
sys.stderr.write("debug: find share button\n")
share_menu_button = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.LinearLayout[@resource-id=\"com.android.chrome:id/share_menu_id\"]")
sys.stderr.write("debug: click share button\n")
share_menu_button.click()
sys.stderr.write("debug: sleep 5 sec\n")
time.sleep(5)
sys.stderr.write("debug: find copy link button\n")
copy_link_button = driver.find_element(by=AppiumBy.XPATH, value="//android.widget.TextView[@resource-id=\"com.android.chrome:id/text\" and @text=\"リンクのコピー\"]")
copy_link_button.click()
text = driver.get_clipboard_text()
sys.stderr.write(f"info: url {text}\n")
sys.stderr.write(f"info: close tab\n")
driver.execute_script('mobile: pressKey', {"keycode": 4})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment