calabash / calabash-ios

Calabash for iOS
Other
1.81k stars 369 forks source link

cant tap alert button when using loops #1372

Closed UGhari closed 6 years ago

UGhari commented 6 years ago

Running Simulator with IPhone 7, Version 11.2 and Calabash version 0.21.4

My application has a normal IOS Alert view, within this alert view, I have a UITextField that allows me to enter some information. When I tap submit on the alert one of two things could happen:

1) I get redirected to a new view with more information of the item 2) I get shown another alert view with an error message with an error and an 'OK' button to dismiss the alert.

There are two problems:

1) Because we are retrieving data from a backend sometimes it could take a few seconds for the error alert to appear so I cant put a hardcoded sleep to wait for the error alert to appear. 2) I can't put an ID on the error alert view to see if the error alert is showing, because to my understanding you cant add IDs to error alert views from swift.

What I am doing instead is querying the alert view, for data I know only appears on the error alert view here is the code:

   errorMessageFound = false
   sleep 2

   alertLabels = query("UILabel",:text)

   if alertLabels.include? "No Product Found"
       errorMessageFound = true
   end

   if errorMessageFound == false
       fail("Error alert for invalid barcode not shown")
   else

   touch("UILabel marked:'OK'");
   sleep 1
   touch("UINavigationBar descendant label")[0]

   end

The above code works but if the backend has not returned within 2 seconds the wrong alert view is queried and the test fails. So instead I wrote this code below:


errorMessageFound = false

time = 20
    time_start = Time.now
    begin      
        time_running = Time.now - time_start

        alertLabels = query("UILabel",:text)

        if alertLabels.include? "No Product Found"
            errorMessageFound = true
            break;
        end

    end until (time_running.to_i >= time)

    if errorMessageFound == false
        fail("Error alert for invalid barcode not shown")
    else
        touch("UILabel marked:'OK'");
        sleep 1
        touch("UINavigationBar descendant label")[0]

    end

Everything works, Calabash passes touch("UILabel marked:'OK'"); but the button is not tapped in the simulator and as a result touch("UINavigationBar descendant label")[0] cant be found so I get the error:

Could not find any views with query:
UINavigationBar descendant label

I tried different variations to the same above code, it seems anytime I try use the loop the alert OK button is not pressed.

Im wondering if this is a bug with Calabash

jmoody commented 6 years ago

I think this is a timing problem.

In your loop, add wait_for_none_animating and a sleep of at least 0.5 seconds between every iteration.

Alerts are animated on. Calabash can detect the alert before it is ready to be touched - while it is animating. If a gesture occurs while the alert is animating, it will be ignored.

UGhari commented 6 years ago

It was a timing error, your suggestions worked thanks