折腾:
【已解决】iOS自动化操作设置出错:启动设置后找不到无线局域网
期间,最终感觉是,虽然逻辑上不够顺,但最稳妥做法是:
对于启动设置后,不论进入设置主页还是WiFi列表页,都支持
期间需要判断,如何算是处于
无线局域网 列表页

研究了页面发现
顶部导航栏是 无线局域网
xml
<XCUIElementTypeNavigationBar type="XCUIElementTypeNavigationBar" name="无线局域网" enabled="true" visible="true" x="0" y="20" width="414" height="44"> <XCUIElementTypeButton type="XCUIElementTypeButton" name="设置" label="设置" enabled="true" visible="true" x="0" y="20" width="66" height="44"/> <XCUIElementTypeOther type="XCUIElementTypeOther" name="无线局域网" label="无线局域网" enabled="true" visible="true" x="163" y="31" width="88" height="21"/> </XCUIElementTypeNavigationBar> <XCUIElementTypeCell type="XCUIElementTypeCell" value="1" enabled="true" visible="true" x="0" y="99" width="414" height="45"> <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" visible="true" x="0" y="99" width="414" height="1"/> <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="无线局域网" name="无线局域网" label="无线局域网" enabled="true" visible="true" x="40" y="99" width="303" height="45"/> <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" visible="true" x="40" y="143" width="374" height="1"/> <XCUIElementTypeSwitch type="XCUIElementTypeSwitch" value="1" name="无线局域网" label="无线局域网" enabled="true" visible="true" x="343" y="106" width="51" height="31"/> </XCUIElementTypeCell>
以及附带的 无线局域网 开启或关闭 有个switch
供参考:

<XCUIElementTypeNavigationBar type="XCUIElementTypeNavigationBar" name="无线局域网" enabled="true" visible="true" x="0" y="20" width="414" height="44"> <XCUIElementTypeButton type="XCUIElementTypeButton" name="设置" label="设置" enabled="true" visible="true" x="0" y="20" width="66" height="44"/> <XCUIElementTypeOther type="XCUIElementTypeOther" name="无线局域网" label="无线局域网" enabled="true" visible="true" x="163" y="31" width="88" height="21"/> </XCUIElementTypeNavigationBar> <XCUIElementTypeCell type="XCUIElementTypeCell" value="0" enabled="true" visible="true" x="0" y="99" width="414" height="45"> <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" visible="true" x="0" y="99" width="414" height="1"/> <XCUIElementTypeOther type="XCUIElementTypeOther" enabled="true" visible="true" x="0" y="143" width="414" height="1"/> <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="无线局域网" name="无线局域网" label="无线局域网" enabled="true" visible="true" x="40" y="99" width="303" height="45"/> <XCUIElementTypeSwitch type="XCUIElementTypeSwitch" value="0" name="无线局域网" label="无线局域网" enabled="true" visible="true" x="343" y="106" width="51" height="31"/> </XCUIElementTypeCell>
不过目前看来,无需额外判断,只需要判断顶部导航栏,即可。
去写代码:
def iOSLaunchSettings(self, isTerminateFirst=True):
"""iOS terminal and re-launch Settings=Preferences app
Args:
isTerminateFirst (bool): force terminal Settings before launch
Returns:
bool
Raises:
"""
iOS_AppId_Settings = "com.apple.Preferences"
if isTerminateFirst:
isGetOk, curState = self.iOSGetAppState(iOS_AppId_Settings)
logging.debug("before terminal: curState=%s", curState)
# stop before start to avoid current page is not homepage of 设置
isTerminalOk, respInfo = self.iOSTerminateApp(iOS_AppId_Settings)
logging.info("isTerminalOk=%s, respInfo=%s", isTerminalOk, respInfo)
isGetOk, curState = self.iOSGetAppState(iOS_AppId_Settings)
logging.debug("after terminal: curState=%s", curState)
# settingsSession = self.wdaClient.session(iOS_AppId_Settings)
# logging.debug("settingsSession=%s" % settingsSession)
# launchResult = self.wdaClient.app_launch(iOS_AppId_Settings)
# logging.debug("launchResult=%s", launchResult)
isLaunchOk, respInfo = self.iOSLaunchApp(iOS_AppId_Settings)
logging.debug("isLaunchOk=%s, respInfo=%s", isLaunchOk, respInfo)
# logging.info("launchResult: value=%s, status=%s, sessionId=%s", launchResult.value, launchResult.status, launchResult.sessionId)
# launchResult: value=None, status=0, sessionId=79A39B72-F5F9-4A01-8E58-DD380452350A
# logging.info("launchResult=%s", str(launchResult))
# launchResult=GenericDict(value=None, sessionId='79A39B72-F5F9-4A01-8E58-DD380452350A', status=0)
return isLaunchOk, respInfo
def iOSIsInWiFiList(self):
"""Check whether is in WiFi list page or not
Args:
Returns:
bool
Raises:
"""
isFoundWifi = False
"""
设置 WiFi列表页:
<XCUIElementTypeNavigationBar type="XCUIElementTypeNavigationBar" name="无线局域网" enabled="true" visible="true" x="0" y="20" width="414" height="44">
<XCUIElementTypeButton type="XCUIElementTypeButton" name="设置" label="设置" enabled="true" visible="true" x="0" y="20" width="66" height="44"/>
<XCUIElementTypeOther type="XCUIElementTypeOther" name="无线局域网" label="无线局域网" enabled="true" visible="true" x="163" y="31" width="88" height="21"/>
</XCUIElementTypeNavigationBar>
"""
wifiName = "无线局域网"
parentNaviBarClassChain = "/XCUIElementTypeNavigationBar[`name = '%s' AND rect.x = 0 AND rect.width = %d`]" % (wifiName, self.X)
wifiQuery = {"type":"XCUIElementTypeOther", "name": wifiName, "enabled": "true"}
wifiQuery["parent_class_chains"] = [ parentNaviBarClassChain ]
isFoundWifi, respInfo = self.findElement(query=wifiQuery, timeout=0.1)
return isFoundWifi
def iOSLaunchSettingsAndIntoWiFiList(self):
"""
iOS launch Settings, then into WiFi list page
Args:
Returns:
bool, None/str
True, None
False, error message str
Raises:
"""
isIntoWiFiListOk = False
respInfo = None
isLaunchOk, respInfo = self.iOSLaunchSettings()
if not isLaunchOk:
respInfo = "Fail to launch 设置"
return isIntoWiFiListOk, respInfo
# Special: sometime alreay being in WiFi list page, so need first check it
isInWifiList = self.iOSIsInWiFiList()
if isInWifiList:
isIntoWiFiListOk = True
respInfo = None
return isIntoWiFiListOk, respInfo
else:
# foundAndClickedWifi = self.iOSFromSettingsIntoWifiList()
foundAndClickedWifi = CommonUtils.multipleRetry({"functionCallback": self.iOSFromSettingsIntoWifiList})
if not foundAndClickedWifi:
respInfo = "Not find 无线局域网 in 设置"
return isIntoWiFiListOk, respInfo
isInWifiList = self.iOSIsInWiFiList()
if isInWifiList:
isIntoWiFiListOk = True
respInfo = None
return isIntoWiFiListOk, respInfo
else:
isIntoWiFiListOk = False
respInfo = "Unknown Error"
return isIntoWiFiListOk, respInfo调用:
isIntoWiFiListOk, respInfo = self.iOSLaunchSettingsAndIntoWiFiList() if not isIntoWiFiListOk: errMsg = respInfo respInfo = "Fail into WiFi list page for %s" % errMsg return isGetTypeOk, respInfo
调试:
def iOSLaunchSettings(self, isTerminateFirst=True):
"""iOS terminal and re-launch Settings=Preferences app
Args:
isTerminateFirst (bool): force terminal Settings before launch
Returns:
bool
Raises:
"""
isEnableDebugState = True
iOS_AppId_Settings = "com.apple.Preferences"
if isTerminateFirst:
if isEnableDebugState:
isGetOk, curState = self.iOSGetAppState(iOS_AppId_Settings)
logging.info("before terminal: curState=%s", curState)
# stop before start to avoid current page is not homepage of 设置
isTerminalOk, respInfo = self.iOSTerminateApp(iOS_AppId_Settings)
logging.info("isTerminalOk=%s, respInfo=%s", isTerminalOk, respInfo)
if isEnableDebugState:
isGetOk, curState = self.iOSGetAppState(iOS_AppId_Settings)
logging.info("after terminal: curState=%s", curState)
# settingsSession = self.wdaClient.session(iOS_AppId_Settings)
# logging.debug("settingsSession=%s" % settingsSession)
# launchResult = self.wdaClient.app_launch(iOS_AppId_Settings)
# logging.debug("launchResult=%s", launchResult)
isLaunchOk, respInfo = self.iOSLaunchApp(iOS_AppId_Settings)
logging.debug("isLaunchOk=%s, respInfo=%s", isLaunchOk, respInfo)
# logging.info("launchResult: value=%s, status=%s, sessionId=%s", launchResult.value, launchResult.status, launchResult.sessionId)
# launchResult: value=None, status=0, sessionId=79A39B72-F5F9-4A01-8E58-DD380452350A
# logging.info("launchResult=%s", str(launchResult))
# launchResult=GenericDict(value=None, sessionId='79A39B72-F5F9-4A01-8E58-DD380452350A', status=0)
if isEnableDebugState:
isGetOk, curState = self.iOSGetAppState(iOS_AppId_Settings)
logging.info("after launch: curState=%s", curState)
return isLaunchOk, respInfo添加设置,是否调试 app的状态
def iOSLaunchSettings(self, isTerminateFirst=True, isDebugState=True): """iOS terminal and re-launch Settings=Preferences app Args: isTerminateFirst (bool): force terminal Settings before launch isDebugState (bool): enable debug for app state Returns: bool Raises: """
调试
即可找到

处于当前WiFi列表页了。
【总结】
最终此处代码是:
def iOSIsInWiFiList(self):
"""Check whether is in WiFi list page or not
Args:
Returns:
bool
Raises:
"""
isFoundWifi = False
"""
设置 WiFi列表页:
<XCUIElementTypeNavigationBar type="XCUIElementTypeNavigationBar" name="无线局域网" enabled="true" visible="true" x="0" y="20" width="414" height="44">
<XCUIElementTypeButton type="XCUIElementTypeButton" name="设置" label="设置" enabled="true" visible="true" x="0" y="20" width="66" height="44"/>
<XCUIElementTypeOther type="XCUIElementTypeOther" name="无线局域网" label="无线局域网" enabled="true" visible="true" x="163" y="31" width="88" height="21"/>
</XCUIElementTypeNavigationBar>
"""
wifiName = "无线局域网"
parentNaviBarClassChain = "/XCUIElementTypeNavigationBar[`name = '%s' AND rect.x = 0 AND rect.width = %d`]" % (wifiName, self.X)
wifiQuery = {"type":"XCUIElementTypeOther", "name": wifiName, "enabled": "true"}
wifiQuery["parent_class_chains"] = [ parentNaviBarClassChain ]
isFoundWifi, respInfo = self.findElement(query=wifiQuery, timeout=0.1)
return isFoundWifi即可判断是否处于 WiFi列表页
而启动 设置,再去判断,完整代码是:
def iOSLaunchSettingsAndIntoWiFiList(self):
"""
iOS launch Settings, then into WiFi list page
Args:
Returns:
bool, None/str
True, None
False, error message str
Raises:
"""
isIntoWiFiListOk = False
respInfo = None
isLaunchOk, respInfo = self.iOSLaunchSettings()
if not isLaunchOk:
respInfo = "Fail to launch 设置"
return isIntoWiFiListOk, respInfo
# Special: sometime alreay being in WiFi list page, so need first check it
isInWifiList = self.iOSIsInWiFiList()
if isInWifiList:
isIntoWiFiListOk = True
respInfo = None
return isIntoWiFiListOk, respInfo
else:
# foundAndClickedWifi = self.iOSFromSettingsIntoWifiList()
foundAndClickedWifi = CommonUtils.multipleRetry({"functionCallback": self.iOSFromSettingsIntoWifiList})
if not foundAndClickedWifi:
respInfo = "Not find 无线局域网 in 设置"
return isIntoWiFiListOk, respInfo
isInWifiList = self.iOSIsInWiFiList()
if isInWifiList:
isIntoWiFiListOk = True
respInfo = None
return isIntoWiFiListOk, respInfo
else:
isIntoWiFiListOk = False
respInfo = "Unknown Error"
return isIntoWiFiListOk, respInfo
def iOSLaunchSettings(self, isTerminateFirst=True, isDebugState=True):
"""iOS terminal and re-launch Settings=Preferences app
Args:
isTerminateFirst (bool): force terminal Settings before launch
isDebugState (bool): enable debug for app state
Returns:
bool
Raises:
"""
iOS_AppId_Settings = "com.apple.Preferences"
if isTerminateFirst:
if isDebugState:
isGetOk, curState = self.iOSGetAppState(iOS_AppId_Settings)
logging.info("before terminal: curState=%s", curState)
# stop before start to avoid current page is not homepage of 设置
isTerminalOk, respInfo = self.iOSTerminateApp(iOS_AppId_Settings)
logging.info("%s: isTerminalOk=%s, respInfo=%s", iOS_AppId_Settings, isTerminalOk, respInfo)
if isDebugState:
isGetOk, curState = self.iOSGetAppState(iOS_AppId_Settings)
logging.info("after terminal: curState=%s", curState)
# settingsSession = self.wdaClient.session(iOS_AppId_Settings)
# logging.debug("settingsSession=%s" % settingsSession)
# launchResult = self.wdaClient.app_launch(iOS_AppId_Settings)
# logging.debug("launchResult=%s", launchResult)
isLaunchOk, respInfo = self.iOSLaunchApp(iOS_AppId_Settings)
logging.info("isLaunchOk=%s, respInfo=%s", isLaunchOk, respInfo)
# logging.info("launchResult: value=%s, status=%s, sessionId=%s", launchResult.value, launchResult.status, launchResult.sessionId)
# launchResult: value=None, status=0, sessionId=79A39B72-F5F9-4A01-8E58-DD380452350A
# logging.info("launchResult=%s", str(launchResult))
# launchResult=GenericDict(value=None, sessionId='79A39B72-F5F9-4A01-8E58-DD380452350A', status=0)
if isDebugState:
isGetOk, curState = self.iOSGetAppState(iOS_AppId_Settings)
logging.info("after launch: curState=%s", curState)
return isLaunchOk, respInfo即可。