Search code examples
iosappiumxcuitest

XCUITest does not repeat same element click with Appium and iOS


I am using Eclipse, Appium, and a virtual iPhone device to attempt to automate a user registration on an app. However, clicking the same element 2 times in the same session results in the 2nd or more clicks to be ignored or unsuccessful. As far as I can tell, Appium is not throwing any errors. The test ins to type to a pin code display to input 10 numbers.

I have confirmed that every element works if none of the numbers are repeated. If any of the numbers are repeated, the click submitted to the device.

I have also confirmed that by doing this test manually under the same conditions, I am able to repeat pressing the same number button as many times as I want without issue. All clicks are registered.

Changing the pause doesn't matter. I have changed it to be up to 5 seconds per click. Speed of clicks doesn't seem to be the issue. Neither does the type of element being used. I also tried this with XCode.

This is my simple test to register to a service using a fixed code.

public static void RegisterUser() {
    
    //Click the register button.
    driver.findElement(By.id("registerButton")).click();
    
    //Enter Reg Code.
    ClickButton("**/XCUIElementTypeStaticText[`label == \"9\"`]");
    ClickButton("**/XCUIElementTypeStaticText[`label == \"9\"`]");
    ClickButton("**/XCUIElementTypeStaticText[`label == \"1\"`]");
    ClickButton("**/XCUIElementTypeStaticText[`label == \"9\"`]");
    ClickButton("**/XCUIElementTypeStaticText[`label == \"3\"`]");
    ClickButton("**/XCUIElementTypeStaticText[`label == \"1\"`]");
    ClickButton("**/XCUIElementTypeStaticText[`label == \"6\"`]");
    ClickButton("**/XCUIElementTypeStaticText[`label == \"6\"`]");
    ClickButton("**/XCUIElementTypeStaticText[`label == \"7\"`]");
    ClickButton("**/XCUIElementTypeStaticText[`label == \"0\"`]");
    
}

//Click the target and wait half a second.
public static void ClickButton(String element) {
    
    driver.findElement(MobileBy.iOSClassChain(element)).click();
    Pause(100);
}

//Pause the current thread.
public static void Pause(int pauseTime) {
    
    try {
        Thread.sleep(pauseTime);
    }
    catch(InterruptedException e) {
        
        e.printStackTrace();
        e.getMessage();
        e.getCause();
    }
}

This is the output from Appium for the 2 clicks on the same element. This one results in a successful click.

[HTTP] {"using":"-ios class chain","value":"**/XCUIElementTypeStaticText[`label == \"9\"`]"}
[debug] [W3C (47290369)] Calling AppiumDriver.findElement() with args: ["-ios class chain","**/XCUIElementTypeStaticText[`label == \"9\"`]","47290369-1558-4bae-98f7-ab0a3d0dc031"]
[debug] [XCUITest] Executing command 'findElement'
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, name, class name, -ios predicate string, -ios class chain, accessibility id, css selector
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [XCUITest] Setting custom timeout to 12000 ms for 'findElement' command
[debug] [WD Proxy] Matched '/element' to command name 'findElement'
[debug] [WD Proxy] Proxying [POST /element] to [POST http://127.0.0.1:8100/session/7C394CAB-D8A7-4977-A463-97BE23B2671B/element] with body: {"using":"class chain","value":"**/XCUIElementTypeStaticText[`label == \"9\"`]"}
[debug] [WD Proxy] Got response with status 200: {"value":{"ELEMENT":"6A000000-0000-0000-7205-010000000000","element-6066-11e4-a52e-4f735466cecf":"6A000000-0000-0000-7205-010000000000"},"sessionId":"7C394CAB-D8A7-4977-A463-97BE23B2671B"}
[debug] [W3C (47290369)] Responding to client with driver.findElement() result: {"element-6066-11e4-a52e-4f735466cecf":"6A000000-0000-0000-7205-010000000000","ELEMENT":"6A000000-0000-0000-7205-010000000000"}
[HTTP] <-- POST /wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element 200 92 ms - 137
[HTTP] 
[HTTP] --> POST /wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element/6A000000-0000-0000-7205-010000000000/click
[HTTP] {"id":"6A000000-0000-0000-7205-010000000000"}
[W3C (47290369)] Driver proxy active, passing request on via HTTP proxy
[debug] [XCUITest] Executing command 'proxyReqRes'
[debug] [WD Proxy] Matched '/wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element/6A000000-0000-0000-7205-010000000000/click' to command name 'click'
[debug] [WD Proxy] Proxying [POST /wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element/6A000000-0000-0000-7205-010000000000/click] to [POST http://127.0.0.1:8100/session/7C394CAB-D8A7-4977-A463-97BE23B2671B/element/6A000000-0000-0000-7205-010000000000/click] with body: {"id":"6A000000-0000-0000-7205-010000000000"}
[debug] [WD Proxy] Got response with status 200: {"value":null,"sessionId":"7C394CAB-D8A7-4977-A463-97BE23B2671B"}
[WD Proxy] Replacing sessionId 7C394CAB-D8A7-4977-A463-97BE23B2671B with 47290369-1558-4bae-98f7-ab0a3d0dc031
[HTTP] <-- POST /wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element/6A000000-0000-0000-7205-010000000000/click 200 165 ms - 65

This one does not actually result in a click. But Appium thinks it does.

[HTTP] {"using":"-ios class chain","value":"**/XCUIElementTypeStaticText[`label == \"9\"`]"}
[debug] [W3C (47290369)] Calling AppiumDriver.findElement() with args: ["-ios class chain","**/XCUIElementTypeStaticText[`label == \"9\"`]","47290369-1558-4bae-98f7-ab0a3d0dc031"]
[debug] [XCUITest] Executing command 'findElement'
[debug] [BaseDriver] Valid locator strategies for this request: xpath, id, name, class name, -ios predicate string, -ios class chain, accessibility id, css selector
[debug] [BaseDriver] Waiting up to 0 ms for condition
[debug] [XCUITest] Setting custom timeout to 12000 ms for 'findElement' command
[debug] [WD Proxy] Matched '/element' to command name 'findElement'
[debug] [WD Proxy] Proxying [POST /element] to [POST http://127.0.0.1:8100/session/7C394CAB-D8A7-4977-A463-97BE23B2671B/element] with body: {"using":"class chain","value":"**/XCUIElementTypeStaticText[`label == \"9\"`]"}
[debug] [WD Proxy] Got response with status 200: {"value":{"ELEMENT":"6E000000-0000-0000-7205-010000000000","element-6066-11e4-a52e-4f735466cecf":"6E000000-0000-0000-7205-010000000000"},"sessionId":"7C394CAB-D8A7-4977-A463-97BE23B2671B"}
[debug] [W3C (47290369)] Responding to client with driver.findElement() result: {"element-6066-11e4-a52e-4f735466cecf":"6E000000-0000-0000-7205-010000000000","ELEMENT":"6E000000-0000-0000-7205-010000000000"}
[HTTP] <-- POST /wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element 200 103 ms - 137
[HTTP] 
[HTTP] --> POST /wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element/6E000000-0000-0000-7205-010000000000/click
[HTTP] {"id":"6E000000-0000-0000-7205-010000000000"}
[W3C (47290369)] Driver proxy active, passing request on via HTTP proxy
[debug] [XCUITest] Executing command 'proxyReqRes'
[debug] [WD Proxy] Matched '/wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element/6E000000-0000-0000-7205-010000000000/click' to command name 'click'
[debug] [WD Proxy] Proxying [POST /wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element/6E000000-0000-0000-7205-010000000000/click] to [POST http://127.0.0.1:8100/session/7C394CAB-D8A7-4977-A463-97BE23B2671B/element/6E000000-0000-0000-7205-010000000000/click] with body: {"id":"6E000000-0000-0000-7205-010000000000"}
[debug] [WD Proxy] Got response with status 200: {"value":null,"sessionId":"7C394CAB-D8A7-4977-A463-97BE23B2671B"}
[WD Proxy] Replacing sessionId 7C394CAB-D8A7-4977-A463-97BE23B2671B with 47290369-1558-4bae-98f7-ab0a3d0dc031
[HTTP] <-- POST /wd/hub/session/47290369-1558-4bae-98f7-ab0a3d0dc031/element/6E000000-0000-0000-7205-010000000000/click 200 175 ms - 65

Solution

  • I can imagine the case, when after the first click you have more than one element, located by

    **/XCUIElementTypeStaticText[`label == \"9\"`]
    

    (e.g. because of something new with label 9 appeared..)

    And the next click goes to the first found element, but it's not a button.

    Try always to click the last found element from the list.

    P. S. there are no issues with your appium logs.