diff --git a/pkg/driver/wda/alert.go b/pkg/driver/wda/alert.go index 7f466d86..de30b571 100644 --- a/pkg/driver/wda/alert.go +++ b/pkg/driver/wda/alert.go @@ -175,19 +175,22 @@ func promptButtonsNamed(steps []flow.Step) (names []string, any bool) { } // answerUnnamedPrompt answers the open permission prompt as WDA would have, -// when the flow answers some prompts itself and this one is not among them. -// WDA's alert monitor is all or nothing for a session, so a flow that taps one -// prompt's button (the tracking prompt's "Ask App Not to Track", say) turned it -// off for every prompt, and a later one the flow never meant to answer (the -// notification prompt) stayed up over the app. A lookup that finds nothing -// calls this: it leaves the prompt to the flow when one of its buttons is one -// the flow taps, or when the flow has an acceptAlert or dismissAlert; else it -// taps the button WDA's own answer would have (autoClickAlertSelector's) and -// reports whether it did, so the lookup looks again. +// when the flow answers some prompts itself. WDA's alert monitor is all or +// nothing for a session, so a flow that taps one prompt's button (the tracking +// prompt's "Ask App Not to Track", say) turned it off for every prompt, and +// one the flow does not answer then (the notification prompt) stayed up over +// the app. It leaves the prompt when the step running is aimed at it (a tap +// on one of its buttons, a check of its text), as DeviceLab's driver does, or +// when the flow has an acceptAlert or dismissAlert; else it taps the button +// WDA's own answer would have (autoClickAlertSelector's) and reports whether +// it did. Called before a step in the watch after a launch +// (answerPromptsBeforeStep) and by a lookup that finds nothing +// (answerPromptOnMiss). func (d *Driver) answerUnnamedPrompt() bool { - if !d.flowAnswersAlerts || d.flowAnswersAnyPrompt || d.promptAction == "" { + if !d.answersPrompts() { return false } + d.lastPromptCheck = time.Now() text, err := d.client.AlertText() if err != nil { return false @@ -196,10 +199,8 @@ func (d *Driver) answerUnnamedPrompt() bool { if err != nil { return false } - for _, name := range d.flowPromptButtons { - if _, ok := pickAlertButton(name, buttons); ok { - return false - } + if stepAimsAtAlert(d.step, text, buttons) { + return false } label := "" for _, b := range buttons { @@ -217,10 +218,102 @@ func (d *Driver) answerUnnamedPrompt() bool { logger.Warn("[wda] a permission prompt the flow does not answer itself stayed up after %q: %v", label, err) return false } - logger.Info("[wda] answered a permission prompt the flow does not answer itself with %q: %q", label, firstLine(text)) + logger.Info("[wda] answered a permission prompt the step is not aimed at with %q: %q", label, firstLine(text)) + // Another prompt often follows (notifications, then tracking). + d.watchForPrompts(promptAnswerWindow) return true } +// How long a tap asks for a prompt first: after launchApp (the app's own +// screen before a prompt is often tapped first, and the prompt comes a second +// or two later) and after a prompt was answered (another often follows). +// DeviceLab's driver watches 3 s after either; a launch here gets longer, +// since a tap under a prompt is lost (its element is found in the app +// beneath the prompt) where DeviceLab's lookup sees the prompt. +const ( + promptLaunchWindow = 15 * time.Second + promptAnswerWindow = 3 * time.Second + // promptCheckEvery spaces the asks before taps, promptMissEvery a + // lookup's: one costs 70 ms on a light page and 270 ms on a heavy one. + promptCheckEvery = 500 * time.Millisecond + promptMissEvery = time.Second +) + +// answersPrompts reports whether the driver answers prompts itself: the flow +// answers some (WDA's monitor is off) and launchApp's permissions say how. +func (d *Driver) answersPrompts() bool { + return d.flowAnswersAlerts && !d.flowAnswersAnyPrompt && d.promptAction != "" +} + +// watchForPrompts opens the watch for window from now. +func (d *Driver) watchForPrompts(window time.Duration) { + if until := time.Now().Add(window); until.After(d.promptWatchUntil) { + d.promptWatchUntil = until + } +} + +// answerPromptsBeforeTap answers the prompts over the app before a tap on an +// element found in it, while the watch is open, and reports whether it +// answered any (the element is then looked up again). +func (d *Driver) answerPromptsBeforeTap() bool { + if !d.answersPrompts() || !time.Now().Before(d.promptWatchUntil) || + time.Since(d.lastPromptCheck) < promptCheckEvery { + return false + } + answered := false + for i := 0; i < 3 && d.answerUnnamedPrompt(); i++ { + answered = true + if !d.settleOff { + d.settleScreen() + } + } + return answered +} + +// answerPromptOnMiss answers a prompt for a lookup that found nothing, at +// most once per promptMissEvery. It reports whether it did, so the lookup +// looks again. +func (d *Driver) answerPromptOnMiss() bool { + if !d.answersPrompts() || time.Since(d.lastPromptCheck) < promptMissEvery { + return false + } + return d.answerUnnamedPrompt() +} + +// stepAimsAtAlert reports whether step deals with the open prompt itself: a +// selector that matches one of its buttons or its text, such as `tapOn: +// "Ask App Not to Track"` or a check of the prompt's question. DeviceLab's +// driver leaves a prompt to such a step (devicelab_ios aimsAtAlert). +func stepAimsAtAlert(step flow.Step, text string, buttons []string) bool { + var sel *flow.Selector + switch s := step.(type) { + case *flow.TapOnStep: + sel = &s.Selector + case *flow.DoubleTapOnStep: + sel = &s.Selector + case *flow.LongPressOnStep: + sel = &s.Selector + case *flow.AssertVisibleStep: + sel = &s.Selector + case *flow.AssertNotVisibleStep: + sel = &s.Selector + case *flow.CopyTextFromStep: + sel = &s.Selector + case *flow.WaitUntilStep: + sel = s.Visible + if sel == nil { + sel = s.NotVisible + } + } + if sel == nil || sel.Text == "" { + return false + } + if _, ok := pickAlertButton(sel.Text, buttons); ok { + return true + } + return matchesText(straightQuotes(sel.Text), straightQuotes(text)) +} + func firstLine(s string) string { if i := strings.IndexByte(s, '\n'); i >= 0 { return s[:i] diff --git a/pkg/driver/wda/alert_test.go b/pkg/driver/wda/alert_test.go index 233c0266..fbc87268 100644 --- a/pkg/driver/wda/alert_test.go +++ b/pkg/driver/wda/alert_test.go @@ -290,14 +290,19 @@ func TestLookupAnswersAPromptTheFlowDoesNotName(t *testing.T) { } } -// The prompt the flow answers itself is left to it, as is every prompt for a -// flow with an acceptAlert or dismissAlert. -func TestLookupLeavesTheFlowsOwnPrompts(t *testing.T) { +// A prompt is left to the step aimed at it (a tap on one of its buttons, a +// check of its text), as DeviceLab's driver leaves it; every prompt is left to +// a flow with an acceptAlert or dismissAlert. +func TestLookupLeavesThePromptToTheStepAimedAtIt(t *testing.T) { d, accepted := alertServer(t, true, "Ask App Not to Track", "Allow") d.PrepareForFlow([]flow.Step{&flow.LaunchAppStep{}, &flow.TapOnStep{Selector: textSelector("Ask app not to track")}}) - lookFor(d) + check := &flow.AssertVisibleStep{Selector: textSelector("Ask App Not to Track")} + check.TimeoutMs = 300 + if res := d.Execute(check); !res.Success { + t.Errorf("the check of the prompt's button failed: %s", res.Message) + } if len(*accepted) != 0 { - t.Errorf("the flow's own prompt was answered: %v", *accepted) + t.Errorf("the prompt the step checks was answered: %v", *accepted) } d, accepted = alertServer(t, true, "Don’t Allow", "Allow") @@ -308,6 +313,18 @@ func TestLookupLeavesTheFlowsOwnPrompts(t *testing.T) { } } +// A prompt whose button the flow taps elsewhere is still answered for a step +// aimed at something else, as DeviceLab's driver answers it: the tracking +// prompt over the onboarding held a 10 s wait for the screen beneath it. +func TestLookupAnswersAPromptTheFlowAnswersElsewhere(t *testing.T) { + d, accepted := alertServer(t, true, "Ask App Not to Track", "Allow") + d.PrepareForFlow([]flow.Step{&flow.LaunchAppStep{}, &flow.TapOnStep{Selector: textSelector("Ask app not to track")}}) + lookFor(d) + if len(*accepted) != 1 || (*accepted)[0] != "Allow" { + t.Errorf("accepted = %v, want [Allow]", *accepted) + } +} + // launchApp's permissions decide the answer, as they decide WDA's. func TestLookupAnswersAnUnnamedPromptAsThePermissionsSay(t *testing.T) { d, accepted := alertServer(t, true, "Don’t Allow", "Allow") @@ -334,3 +351,127 @@ func TestLookupAnswersNothingWhenWDAAnswers(t *testing.T) { t.Errorf("accepted = %v, want none", *accepted) } } + +// promptOverAppServer is a WDA whose app has the element "next" under an open +// notification prompt. It records each WDA call in order, so a test can tell +// whether the prompt was answered before the tap. +func promptOverAppServer(t *testing.T) (*Driver, *[]string) { + t.Helper() + open := true + var calls []string + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + p := r.URL.Path + noAlert := map[string]interface{}{"value": map[string]interface{}{"error": "no such alert", "message": "no alert"}} + switch { + case strings.HasSuffix(p, "/alert/text"): + calls = append(calls, "alert/text") + if !open { + w.WriteHeader(http.StatusNotFound) + jsonResponse(w, noAlert) + return + } + jsonResponse(w, map[string]interface{}{"value": "“Lab” Would Like to Send You Notifications"}) + case strings.HasSuffix(p, "/wda/alert/buttons"): + jsonResponse(w, map[string]interface{}{"value": []string{"Don’t Allow", "Allow"}}) + case strings.HasSuffix(p, "/alert/accept"): + calls = append(calls, "accept") + open = false + jsonResponse(w, map[string]interface{}{"value": nil}) + case strings.HasSuffix(p, "/wda/tap"): + if open { + calls = append(calls, "tap under the prompt") + } else { + calls = append(calls, "tap") + } + jsonResponse(w, map[string]interface{}{"value": nil}) + case strings.HasSuffix(p, "/screenshot"): + jsonResponse(w, map[string]interface{}{"value": "c2FtZQ=="}) + case strings.HasSuffix(p, "/elements"): + jsonResponse(w, map[string]interface{}{"value": []map[string]interface{}{{ + "ELEMENT": "next", "name": "next", "text": "Next", "displayed": true, + "rect": map[string]interface{}{"x": 100, "y": 700, "width": 150, "height": 40}, + }}}) + // The element's fields, for a driver that reads them after the find. + case strings.HasSuffix(p, "/element/next/rect"): + jsonResponse(w, map[string]interface{}{"value": map[string]interface{}{"x": 100, "y": 700, "width": 150, "height": 40}}) + case strings.HasSuffix(p, "/element/next/text"): + jsonResponse(w, map[string]interface{}{"value": "Next"}) + case strings.HasSuffix(p, "/element/next/displayed"): + jsonResponse(w, map[string]interface{}{"value": true}) + case strings.HasSuffix(p, "/element/next/name"): + jsonResponse(w, map[string]interface{}{"value": "next"}) + default: + jsonResponse(w, map[string]interface{}{"value": nil}) + } + })) + t.Cleanup(server.Close) + return createTestDriver(server), &calls +} + +func tapCalls(calls []string) []string { + var out []string + for _, c := range calls { + if c != "alert/text" { + out = append(out, c) + } + } + return out +} + +// A flow that answers the tracking prompt itself gets the notification prompt +// answered before its next tap, not after a lookup under the prompt misses: +// the tap went to the prompt and was lost (G15's late answer). +func TestTapAnswersAnUnnamedPromptFirst(t *testing.T) { + d, calls := promptOverAppServer(t) + tracking := &flow.TapOnStep{Selector: textSelector("Ask App Not to Track")} + tracking.Optional = true + d.PrepareForFlow([]flow.Step{&flow.LaunchAppStep{}, tracking}) + d.watchForPrompts(promptLaunchWindow) // as launchApp does + res := d.Execute(&flow.TapOnStep{Selector: flow.Selector{ID: "next"}}) + if !res.Success { + t.Fatalf("tap failed: %s", res.Message) + } + if got := strings.Join(tapCalls(*calls), ", "); got != "accept, tap" { + t.Errorf("calls = %s, want the prompt answered, then the tap", got) + } +} + +// A flow that leaves prompts to WDA's monitor asks for no alert before a tap. +func TestTapAsksForNoAlertWhenWDAAnswers(t *testing.T) { + d, calls := promptOverAppServer(t) + d.PrepareForFlow([]flow.Step{&flow.LaunchAppStep{}}) + d.Execute(&flow.TapOnStep{Selector: flow.Selector{ID: "next"}}) + for _, c := range *calls { + if c == "alert/text" || c == "accept" { + t.Fatalf("calls = %v: a flow WDA answers prompts for asked for the alert", *calls) + } + } +} + +// Outside the watch after a launch or an answered prompt, a step asks for no +// alert before it acts; inside it, at most once per promptCheckEvery. +func TestTapAsksForPromptsOnlyInTheWatch(t *testing.T) { + d, calls := promptOverAppServer(t) + tracking := &flow.TapOnStep{Selector: textSelector("Ask App Not to Track")} + tracking.Optional = true + d.PrepareForFlow([]flow.Step{&flow.LaunchAppStep{}, tracking}) + next := &flow.TapOnStep{Selector: flow.Selector{ID: "next"}} + d.Execute(next) + if got := strings.Join(*calls, ", "); got != "tap under the prompt" { + t.Errorf("before any launch: calls = %s, want only the tap", got) + } + d.watchForPrompts(promptLaunchWindow) + *calls = nil + d.Execute(next) + if got := strings.Join(tapCalls(*calls), ", "); got != "accept, tap" { + t.Errorf("in the watch: calls = %s, want the prompt answered, then the tap", got) + } + *calls = nil + d.Execute(next) // within promptCheckEvery of the last ask + for _, c := range *calls { + if c == "alert/text" { + t.Errorf("asked again within %v: %v", promptCheckEvery, *calls) + } + } +} diff --git a/pkg/driver/wda/commands.go b/pkg/driver/wda/commands.go index 9dfc8bb3..6a6ead76 100644 --- a/pkg/driver/wda/commands.go +++ b/pkg/driver/wda/commands.go @@ -55,6 +55,20 @@ func (d *Driver) tapOn(step *flow.TapOnStep) *core.CommandResult { return errorResult(err, fmt.Sprintf("Element not found: %s", selectorDesc(step.Selector))) } + // A prompt over the app takes the tap, and the element was found in the + // app beneath it: answer the prompt first and look again. Asked here, + // after the find, since a find answers once the app does, and an alert + // check sent while the app is still launching held WDA for 19 s. + if info != nil && info.Class != systemAlertButtonClass && d.answerPromptsBeforeTap() { + if fresh, findErr := d.findElementForTap(step.Selector, step.Optional, step.TimeoutMs); findErr == nil && fresh != nil { + info = fresh + } else if step.Optional { + return successResult("Optional element not found after a permission prompt was answered, skipping tap", nil) + } else { + return errorResult(findErr, fmt.Sprintf("Element not found after a permission prompt was answered: %s", selectorDesc(step.Selector))) + } + } + if info != nil && info.Class == systemAlertButtonClass { label := info.Text if err := d.answerAlert(func() error { return d.client.TapAlertButton(label) }); err != nil { diff --git a/pkg/driver/wda/driver.go b/pkg/driver/wda/driver.go index 667827d3..86f02571 100644 --- a/pkg/driver/wda/driver.go +++ b/pkg/driver/wda/driver.go @@ -48,6 +48,14 @@ type Driver struct { flowPromptButtons []string flowAnswersAnyPrompt bool promptAction string + // step: the step Execute runs, which a prompt is left to when it is + // aimed at it (stepAimsAtAlert). promptWatchUntil: until when a step that + // looks at the screen asks for a prompt first (after launchApp and after + // an answered prompt); lastPromptCheck spaces those asks and a lookup's + // (an alert check costs a quarter second on a heavy page). + step flow.Step + promptWatchUntil time.Time + lastPromptCheck time.Time // alertLookup: what of a system alert the current step's selector may match. alertLookup alertLookup @@ -300,6 +308,12 @@ func (d *Driver) Execute(step flow.Step) *core.CommandResult { if d.screenMayMove && !d.settleOff && actsOnScreen(step) && !flow.RepeatTapAfterFirst(step) { d.settleScreen() } + // A permission prompt the flow does not answer itself is answered before + // the step acts, not after a lookup under it misses: a tap on the app + // under a prompt is lost, and the flow then waited out a lookup for what + // the lost tap should have opened (about 20 s on onboarding). Only a + // flow that answers some prompts itself pays the alert check (G15). + d.step = step // A step that neither acts nor moves the screen (an assert, a script, a // flow's condition) leaves the flag as it was, so the next action still // settles: a tap, a check that answered while the sheet the tap opened @@ -433,6 +447,9 @@ func (d *Driver) Execute(step flow.Step) *core.CommandResult { if core.MayLeaveIOSKeyboardUp(step, result) { d.keyboardMayBeUp = true } + if _, ok := step.(*flow.LaunchAppStep); ok { + d.watchForPrompts(promptLaunchWindow) + } result.Duration = time.Since(start) d.trackCrashLoop(result) return result @@ -1428,8 +1445,8 @@ func (d *Driver) findElementByPageSourceOnce(sel flow.Selector) (*core.ElementIn if info := d.findOnSystemAlert(sel); info != nil { return info, nil } - if d.answerUnnamedPrompt() { - return nil, notFound("a permission prompt the flow does not answer was over the app: answered, looking again") + if d.answerPromptOnMiss() { + return nil, notFound("a permission prompt the step is not aimed at was over the app: answered, looking again") } if sel.Text != "" { if closest := ClosestTexts(allElements, sel.Text, 3); len(closest) > 0 {