Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detox: iOS Simulator how to confirm alert message

I am using Alert from react-native.

How do I get detox to press the "Log out" button on the alert message?

enter image description here

I tried using await element(by.text('Log out')).tap();

But I get "Multiple elements were matched" error. Presumably it finds 3 elements with same label. The original button with label "Log out" used to trigger the alert message, the alert message title, and the alert message button I want detox to press.

Error Trace: [
  {
    "Description" : "Multiple elements were matched: (
    "<UILabel:0x7fe7964db910; AX=Y; AX.label='Log out'; AX.frame={{41, 234}, {238, 20.5}}; AX.activationPoint={160, 244.25}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{16, 20}, {238, 20.5}}; opaque; alpha=1; UIE=N; text='Log out'>",
    "<UILabel:0x7fe7964dda90; AX=Y; AX.label='Log out'; AX.frame={{198.5, 322.5}, {58, 20.5}}; AX.activationPoint={227.5, 332.75}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{0, 12}, {58, 20.5}}; opaque; alpha=1; UIE=N; text='Log out'>",
    "<RCTText:0x7fe79652f300; AX=Y; AX.label='Log out'; AX.frame={{16, 338.5}, {288, 17}}; AX.activationPoint={160, 347}; AX.traits='UIAccessibilityTraitStaticText'; AX.focused='N'; frame={{0, 0}, {288, 17}}; alpha=1>"
). Please use selection matchers to narrow the selection down to single element.",
    "Error Domain" : "com.google.earlgrey.ElementInteractionErrorDomain",
    "Error Code" : "5",
    "File Name" : "GREYElementInteraction.m",
    "Function Name" : "-[GREYElementInteraction grey_errorForMultipleMatchingElements:withMatchedElementsIndexOutOfBounds:]",
    "Line" : "956"
  }
]

I guess one way is to use .atIndex(), but that means I need to play with indexes every time something changes to determine the correct element.

Is there no better way to address this issue?

Thanks.

like image 699
Antoni4 Avatar asked Nov 28 '17 00:11

Antoni4


Video Answer


2 Answers

After some tinkering I ended up using this:

await element(by.label('Log out').and(by.type('_UIAlertControllerActionView'))).tap();

Not sure if this will work for every iOS version, but seem to work on 10.3 and 11.1

Use View Hierarchy Debugger provided by Xcode to see if the type has changed for a different version of iOS.

like image 130
Antoni4 Avatar answered Sep 19 '22 14:09

Antoni4


It should work with finding element by text

await element(by.text('Log out')).tap();

Demo repo: https://github.com/FDiskas/demonas/blob/c703840a991b2f3d96a18ac8c5120ee1d5f901f8/e2e/firstTest.spec.ts#L11

like image 20
FDisk Avatar answered Sep 18 '22 14:09

FDisk