Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions pkg/driver/wda/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ func (d *Driver) launchApp(step *flow.LaunchAppStep) *core.CommandResult {
// a flow checking what survives a restart tested nothing.
resuming := false
if hasArgs || step.StopApp == nil || *step.StopApp {
_ = d.client.TerminateApp(bundleID)
_ = d.terminateApp(bundleID)
} else {
resuming = d.inBackground(bundleID)
}
Expand Down Expand Up @@ -1584,13 +1584,21 @@ func (d *Driver) killApp(step *flow.KillAppStep) *core.CommandResult {
// (e.g. "- stopApp" as the first step in a flow).
func (d *Driver) terminateApp(bundleID string) error {
if d.client.HasSession() {
// On a simulator simctl stops the app in 0.2 s, where WebDriverAgent's
// XCTest terminate took 1.1-2.1 s; WebDriverAgent's terminate after it
// returns at once and keeps the session's bookkeeping (an app of the
// session stopped behind its back reads as crashed at the next request).
if d.info != nil && d.info.IsSimulator && d.udid != "" {
if output, err := execCommand("xcrun", "simctl", "terminate", d.udid, bundleID).CombinedOutput(); err != nil {
logger.Debug("simctl terminate %s: %v: %s", bundleID, err, string(output))
}
}
return d.client.TerminateApp(bundleID)
}

// No WDA session — fall back to device-level terminate
if d.info != nil && d.info.IsSimulator {
cmd := exec.Command("xcrun", "simctl", "terminate", d.udid, bundleID)
if output, err := cmd.CombinedOutput(); err != nil {
if output, err := execCommand("xcrun", "simctl", "terminate", d.udid, bundleID).CombinedOutput(); err != nil {
// Ignore errors — app might not be running
logger.Debug("simctl terminate %s: %v: %s", bundleID, err, string(output))
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/driver/wda/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5447,3 +5447,41 @@ func TestEraseTextEmptyFieldShowingPlaceholder(t *testing.T) {
t.Errorf("typed into an empty field: %q", typed)
}
}

// On a simulator an app is stopped with simctl first (0.2 s; WebDriverAgent's
// XCTest terminate took 1.1-2.1 s), then through WebDriverAgent, which keeps
// its session's bookkeeping; a real device stops it through WebDriverAgent only.
func TestTerminateAppOnASimulatorUsesSimctlFirst(t *testing.T) {
var calls []string
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasSuffix(r.URL.Path, "/wda/apps/terminate") {
calls = append(calls, "wda terminate")
}
jsonResponse(w, map[string]interface{}{"value": true})
}))
defer server.Close()

var got [][]string
withFakeExec(t, &got, false)
d := createTestDriver(server)
d.info.IsSimulator, d.udid = true, "SIM-UDID"
if err := d.terminateApp("com.example.app"); err != nil {
t.Fatalf("terminateApp: %v", err)
}
want := []string{"xcrun", "simctl", "terminate", "SIM-UDID", "com.example.app"}
if len(got) != 1 || strings.Join(got[0], " ") != strings.Join(want, " ") {
t.Errorf("exec calls = %v, want one %v", got, want)
}
if len(calls) != 1 {
t.Errorf("WDA terminate calls = %d, want 1 (its bookkeeping)", len(calls))
}

got, calls = nil, nil
d.info.IsSimulator, d.udid = false, "DEVICE-UDID"
if err := d.terminateApp("com.example.app"); err != nil {
t.Fatalf("terminateApp on a device: %v", err)
}
if len(got) != 0 || len(calls) != 1 {
t.Errorf("a device: exec calls %v, WDA terminate calls %d; want none and 1", got, len(calls))
}
}
Loading