本文介紹如何使用無影 AgentBay SDK 建立具有進階配置選項的雲手機會話。雲手機配置允許使用者控制應用程式訪問以及移動環境的螢幕解析度設定。
概述
建立雲手機會話時,可以使用 CreateSessionParams 中的 extra_configs 參數配置進階設定。包括:
通過白名單或黑名單規則控制應用程式訪問。
鎖定或解鎖螢幕解析度。
增強安全性和測試靈活性。
配置選項
解析度設定
lock_resolution(bool): 控制螢幕解析度是否被鎖定。True: 鎖定解析度以防止更改。False: 允許解析度更改以實現靈活的顯示適配。
應用管理規則
app_manager_rule(AppManagerRule):控制應用程式訪問。rule_type:"White"(白名單)或"Black"(黑名單)。app_package_name_list:允許或阻止的包名列表。
白名單配置
在生產環境中使用白名單,確保只有經過獲批准的應用程式程式可以安裝和訪問。通過明確允許信任的應用程式,提供最進階別的安全性。
樣本:建立帶有應用程式白名單的會話
from agentbay import AgentBay
from agentbay.session_params import CreateSessionParams
from agentbay.api.models import ExtraConfigs, MobileExtraConfig, AppManagerRule
# 初始化SDK
agent_bay = AgentBay(api_key="your_api_key")
# 配置帶白名單的行動應用程式管理
app_whitelist_rule = AppManagerRule(
rule_type="White",
app_package_name_list=[
"com.example.allowed.app",
"com.company.trusted.app",
"com.system.essential.service"
]
)
# 配置移動設定
mobile_config = MobileExtraConfig(
lock_resolution=True,
app_manager_rule=app_whitelist_rule
)
# 建立額外配置
extra_configs = ExtraConfigs(mobile=mobile_config)
# 建立帶移動配置的會話
params = CreateSessionParams(
image_id="mobile_latest",
labels={"project": "mobile-testing", "environment": "development"},
extra_configs=extra_configs
)
session_result = agent_bay.create(params)
if session_result.success:
session = session_result.session
print(f"移動會話已建立,ID: {session.session_id}")
print("移動配置已應用:")
print("- 解析度鎖定")
print("- 應用白名單已啟用,包含允許的包")
else:
print(f"建立移動會話失敗: {session_result.error_message}")黑名單配置
在開發與測試中使用黑名單來阻止已知的問題應用程式,同時允許其他應用程式的靈活性。當需要阻止訪問特定應用程式而不限制整個環境時,使用黑名單配置。
樣本:建立帶有應用程式黑名單的會話
from agentbay import AgentBay
from agentbay.session_params import CreateSessionParams
from agentbay.api.models import ExtraConfigs, MobileExtraConfig, AppManagerRule
# 初始化SDK
agent_bay = AgentBay(api_key="your_api_key")
# 配置帶黑名單的行動應用程式管理
app_blacklist_rule = AppManagerRule(
rule_type="Black",
app_package_name_list=[
"com.malware.suspicious.app",
"com.unwanted.adware",
"com.blocked.social.media"
]
)
# 配置帶解析度靈活性的移動設定
mobile_config = MobileExtraConfig(
lock_resolution=False,
app_manager_rule=app_blacklist_rule
)
# 建立額外配置
extra_configs = ExtraConfigs(mobile=mobile_config)
# 建立帶移動黑名單配置的會話
params = CreateSessionParams(
image_id="mobile_latest",
labels={"project": "security-testing"},
extra_configs=extra_configs
)
session_result = agent_bay.create(params)
if session_result.success:
session = session_result.session
print(f"安全移動會話已建立,ID: {session.session_id}")
print("安全配置已應用:")
print("- 解析度已解鎖以提高靈活性")
print("- 應用黑名單已啟用以阻止惡意包")
else:
print(f"建立移動會話失敗:{session_result.error_message}")