File tree Expand file tree Collapse file tree 3 files changed +52
-4
lines changed
Expand file tree Collapse file tree 3 files changed +52
-4
lines changed Original file line number Diff line number Diff line change @@ -504,9 +504,18 @@ func (d *IOSDevice) StartAgent(config StartAgentConfig) error {
504504 return fmt .Errorf ("failed to wait for WebDriverAgent: %w" , err )
505505 }
506506
507- // wait 1 second after pressing home, so we make sure wda is in the background
508- _ = d .wdaClient .PressButton ("HOME" )
509- time .Sleep (1 * time .Second )
507+ // check if WebDriverAgent is the active app and press HOME to background it
508+ activeApp , err := d .wdaClient .GetActiveAppInfo ()
509+ if err == nil {
510+ utils .Verbose ("Active app: %s (%s)" , activeApp .Name , activeApp .BundleID )
511+
512+ // if WDA is in foreground, press HOME to background it
513+ if strings .Contains (activeApp .Name , "WebDriverAgent" ) {
514+ utils .Verbose ("WebDriverAgent is active, pressing HOME to background it" )
515+ _ = d .wdaClient .PressButton ("HOME" )
516+ time .Sleep (1 * time .Second )
517+ }
518+ }
510519 }
511520 }
512521
Original file line number Diff line number Diff line change 1+ package wda
2+
3+ import "fmt"
4+
5+ // ActiveAppInfo represents information about the currently active application
6+ type ActiveAppInfo struct {
7+ BundleID string `json:"bundleId"`
8+ Name string `json:"name"`
9+ ProcessID int `json:"pid"`
10+ }
11+
12+ // GetActiveAppInfo returns information about the currently active/foreground application
13+ // This uses the /wda/activeAppInfo endpoint which doesn't require a session
14+ func (c * WdaClient ) GetActiveAppInfo () (* ActiveAppInfo , error ) {
15+ response , err := c .GetEndpoint ("wda/activeAppInfo" )
16+ if err != nil {
17+ return nil , fmt .Errorf ("failed to get active app info: %w" , err )
18+ }
19+
20+ // extract value from response
21+ value , ok := response ["value" ].(map [string ]interface {})
22+ if ! ok {
23+ return nil , fmt .Errorf ("unexpected response format: missing or invalid 'value' field" )
24+ }
25+
26+ bundleID , _ := value ["bundleId" ].(string )
27+ name , _ := value ["name" ].(string )
28+ pid := 0
29+ if pidFloat , ok := value ["pid" ].(float64 ); ok {
30+ pid = int (pidFloat )
31+ }
32+
33+ return & ActiveAppInfo {
34+ BundleID : bundleID ,
35+ Name : name ,
36+ ProcessID : pid ,
37+ }, nil
38+ }
Original file line number Diff line number Diff line change @@ -33,7 +33,8 @@ func main() {
3333 hook .Shutdown ()
3434 os .Exit (0 )
3535 case err := <- done :
36- // normal exit: let WDA and other resources persist
36+ // cleanup resources on normal exit
37+ hook .Shutdown ()
3738 if err != nil {
3839 fmt .Fprintln (os .Stderr , err )
3940 os .Exit (1 )
You can’t perform that action at this time.
0 commit comments