## 如何對活動啟動進行故障排除
### 能力
> ? Activity類是Android應用程序的重要組成部分,活動的啟動和組合方式是平臺應用程序模型的基本組成部分。與通過main()方法啟動應用的編程規范不同,Android通過在Activity不同的生命周期調用特定的回調方法來在Activity實例中初始化代碼。
> © [Android開發人員文檔](https://developer.android.com/guide/components/activities/intro-activities)
? Appium需要知道程序包和活動名稱去正確地初始化被測應用。該信息需要在driver capabilities中提供,并且由以下鍵組成:
- `appActivity`: 主應用程序活動名稱
- `appPackage`: 應用程序包名
- `appWaitActivity`: 要等待或首先啟動的應用程序活動名
- `appWaitPackage`: 要等待或首先啟動的應用程序包名
- `appWaitDuration`: 等待 `appWaitActivity` 啟動的最大時間 (以毫秒為單位,默認為20000)
? 以上這些功能都是可選的。如果你沒有明確地設置他們,Appium會從APK清單中讀取這些值來嘗試自動檢測他們。盡管,如果應該在設備(`noReset=true`)上已經安裝了受測應用程序,則至少需要設置`appActivity`和`appPackage`選項,因為在這種情況下沒有可用的軟件包清單。如果你沒有明確地設置`appWaitPackage` 和`appWaitActivity` 的值,則他們會被自動設為與`appPackage`/`appActivity`的值一致。有關更多的詳細信息,清查閱 [appium-adb](https://github.com/appium/appium-adb/blob/master/lib/tools/android-manifest.js) 包中`packageAndLaunchActivityFromManifest` 方法的實現。
### Appium如何啟動活動
? 活動由 [Call activity manager `am`](https://developer.android.com/studio/command-line/adb#am)啟動. Appium嘗試使用`am start`來啟動`appPackage`/`appActivity` 組合,直到`appWaitPackage`/`appWaitActivity`啟動或者達到`appWaitDuration` 超時時間。當前聚焦的活動名是通過`adb shell dumpsys window windows` 命令輸出 (`mFocusedApp` 或者`mCurrentFocus` 條目)中解析出來的。有關更多詳細信息,請查閱[appium-adb](https://github.com/appium/appium-adb/blob/master/lib/tools/apk-utils.js) 包中`startApp`, 和`getFocusedPackageAndActivity`方法的實現。
### 可能的問題和解決方案
#### java.lang.SecurityException: 拒絕權限: 啟動意圖(Intent)
? 完整的錯誤描述通常看起來像`'java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.mypackage/.myactivity.MainActivity launchParam=MultiScreenLaunchParams { mDisplayId=0 mBaseDisplayId=0 mFlags=0 } } from null (pid=11366, uid=2000) not exported from uid 10191`。該錯誤可能表明,通過`appPackage`/`appActivity`傳遞(或自動隱式檢測到)給Appium的應用程序包名和活動名組合不是正確的,無法啟動被測程序。解決方案是與應用程序開發人員檢查正確的值,并先執行以下命令來手動測試它們:`adb shell am start -W -n com.myfixedpackage/.myfixedactivity.MainActivity -S -a android.intent.action.MAIN -c android.intent.category.LAUNCHER -f 0x10200000`。如果該命令能手動執行成功并在設備上啟動必要的應用程序,那么它也將同樣適用于Appium。
#### com.myactivity或 com.myapp.com.myactivity 從未啟動
? 此異常通常表明第一個應用活動與`appWaitPackage`/`appWaitActivity設置的(或自動檢測)不是同一個包/活動。此類錯誤通常發生在具有多個活動的應用程序中。為了解決此問題,我們應該與應用程序開發人員確認哪個活動/應用包是在應用程序啟動時第一個出現的。當前聚焦的活動名可以用上文提到的命令adb shell dumpsys window windows來進行驗證。此外,Appium允許在設置`appWaitActivity` 的值時使用通配符。這在活動名是動態生成或并非一直保持不變的時候尤其有用。例如 `com.mycomany.*` 會匹配任意的 `com.mycomany.foo`或`com.mycomany.bar`
? 如果你仔細檢查過活動名稱是正確的,但是啟動仍然超時,請嘗試增大`appWaitDuration` 的值。通常,對于大多數應用程序而言,默認的20秒就足夠了。但是,某些比較大的應用可能需要更多的時間才能啟動并展示第一個活動。拜托了,請不要創造這樣的應用。
- 關于TesterHome和MTSC
- 關于Appium
- 簡介
- Appium 客戶端
- 入門指南
- 已支持的平臺
- API 文檔
- Appium驅動
- XCUITest (iOS)
- XCUITest Real Devices (iOS)
- UIAutomation (iOS)
- UIAutomation Safari Launcher (iOS)
- UIAutomator (Android)
- UIAutomator2 (Android)
- Espresso (Android)
- Windows
- Mac
- Appium命令
- Status
- Execute Mobile Command
- Session
- Create
- End
- Get Session Capabilities
- Go Back
- Screenshot
- Source
- Timeouts
- Timeouts
- Implicit Wait
- Async Script
- Orientation
- Get Orientation
- Set Orientation
- Geolocation
- Get Geolocation
- Set Geolocation
- Logs
- Get Log Types
- Get Logs
- Events
- Log event
- Get events
- Settings
- Update Settings
- Get Device Settings
- Settings
- Update Settings
- Get Device Settings
- Execute Driver Script
- Device
- Activity
- Start Activity
- Current Activity
- Current Package
- App
- Install App
- Is App Installed
- Launch App
- Background App
- Close App
- Reset App
- Remove App
- Activate App
- Terminate App
- Get App State
- Get App Strings
- End Test Coverage
- Clipboard
- Get Clipboard
- Set Clipboard
- Emulator
- Power AC
- Power Capacity
- Files
- Push File
- Pull File
- Pull Folder
- Interactions
- Shake
- Lock
- Unlock
- Is Locked
- Rotate
- Keys
- Press keycode
- Long press keycode
- Hide Keyboard
- Is Keyboard Shown
- Network
- Toggle Airplane Mode
- Toggle Data
- Toggle WiFi
- Toggle Location Services
- Send SMS
- GSM Call
- GSM Signal
- GSM Voice
- Network Speed
- Performance Data
- Get Performance Data
- Performance Data Types
- Screen Recording
- Start Screen Recording
- Stop Screen Recording
- Simulator
- Perform Touch ID
- Toggle Touch ID Enrollment
- System
- Open Notifications
- System Bars
- System Time
- Display density
- Authentication
- Finger Print
- Element
- Find Element
- Find Elements
- Actions
- Click
- Send Keys
- Clear
- Attributes
- Text
- Name
- Attribute
- Selected
- Enabled
- Displayed
- Location
- Size
- Rect
- CSS Property
- Location in View
- Other
- Submit
- Active Element
- Equals Element
- Context
- Get Context
- Get All Contexts
- Set Context
- Interactions
- Mouse
- Move To
- Click
- Double Click
- Button Down
- Button Up
- Touch
- Single Tap
- Double Tap
- Move
- Touch Down
- Touch Up
- Long Press
- Scroll
- Flick
- Multi Touch Perform
- Touch Perform
- W3C Actions
- Web
- Window
- Set Window
- Close Window
- Get Handle
- Get Handles
- Get Title
- Get Window Size
- Set Window Size
- Get Window Position
- Set Window Position
- Maximize Window
- Navigation
- Go to URL
- Get URL
- Back
- Forward
- Refresh
- Storage
- Get All Cookies
- Set Cookie
- Delete Cookie
- Delete All Cookies
- Frame
- Switch to Frame
- Switch to Parent Frame
- Execute Async
- Execute
- 編寫 & 運行Appium腳本
- Running Tests
- Desired Capabilities
- The --default-capabilities flag
- Finding Elements
- Touch Actions
- CLI Arguments
- Server Security
- Web/Web Views
- Mobile Web Testing
- Automating Hybrid Apps
- Using ios-webkit-debug-proxy
- Using Chromedriver
- Image Comparison
- iOS
- Low-Level Insights on iOS Input Events
- XCUITest Mobile Gestures
- XCUITest Mobile App Management
- iOS Pasteboard Guide
- iOS Predicate Guide
- iOS Touch ID Guide
- iOS Install Certificate
- tvOS support
- Pushing/Pulling files
- Audio Capture
- Android
- Low-Level Insights on Android Input Events
- UiSelector Guide
- Espresso Datamatcher Guide
- Android Code Coverage Guide
- Activities Startup Troubleshooting Guide
- How To Execute Shell Commands On The Remote Device
- Android Device Screen Streaming
- How To Emulate IME Actions Generation
- How To Test Android App Bundle
- Other
- Reset Strategies
- Network Connection Guide
- Using Unicode with Appium
- Troubleshooting
- Tutorial
- Swipe Tutorial
- Screen
- Element
- Partial screen
- Simple
- Multiple scroll views
- Add scroll layout
- Tricks and Tips
- Screen
- Element
- Element search
- Fast
- Slow
- Guide
- 進階概念
- 定位圖像中的元素
- 使用定位元素的插件
- 遷移到 XCUITest
- 在 Appium 中使用 Selenium Grid
- Appium Logs Filtering
- 跨域 iframes
- 使用自定義 WDA 服務器
- 使用不同版本的 Xcode 運行
- The Event Timings API
- 并行測試的設置
- The Settings API
- Memory Collection
- 向Appium項目做貢獻
- 從源代碼運行 Appium
- 開發者概述
- 標準開發命令
- Appium 風格指南
- 如何編寫文檔
- Appium 包結構
- 鳴謝