Useful commands in instruments for automatize tests for iOs

After having set the basic concepts for Instruments here, we keep going on learning things about Instruments

DIFFERENCE BETWEEN “PUSHTIMEOUT” AND “DELAY”

Sometimes, some delay is needed before the next command is executed, for instance wait until the connection has been established after a http request.
It is posible to use the “delay” for it, but it maybe takes longer to establish the connection than is set or maybe it’s faster and we are waiting for no reason.

It is better to use “pushTimeout” because it waits up to X seconds or until the condition that you indicate happens.
So if the condition happens before X seconds, the tests continues on immediately.

Example: It would wait up to 60 seconds


target.pushTimeout(60);
target.frontMostApp().mainWindow().tableViews()[0].groups()["Secure Connection Established"];
target.popTimeout();

HANDLING UNEXPECTED AND EXPECTED ALERTS

Handling alert in automated tests has always been difficult.
The problem could appears because in the real devices an unexpected alert is shown and because of that the test is blocked or failed.
To avoid that, this code can be added in the beginning of the script to handle this event, when returning false, alert is automatically dismiss and the scripts could run without any interference.


UIATarget.onAlert = function onAlert(alert){
  var alertName = alert.name();
  UIALogger.logWarning( alertName + "’ pops up!”);
  return false; // use default handler
}

But alerts can be part of the tested workflow. In this case, so we don’t want to dismiss them.
The way to dismiss the alert that are not part of the application and not to dismiss the ones that are part of the application, is to check the title of the alert and return true if it matches.


UIATarget.onAlert = function onAlert(alert) {
  var alertName = alert.name();
  UIALogger.logWarning( alertName + "’ pops up!”);
  if (alertName == “match title”) {
    alert.buttons()["ok"].tap();
    return true; // bypass default handler
  }
  return false; // use default handler
}

Sometimes the fact that the alert appears is the step that indicates if the test is passed or failed.
In this situation, it is not correct to write the command “logPass” in the event onAlert because then you wouldn’t be able to indicate that the test has failed if the alert is not shown ever .
The correct way to do is, close the alert inside the event and return true, and where the flow was you can check if the alert was shown, like this


if (target.frontMostApp().alert().name() == “Title of the alert) {
 UIALogger.logPass(testName);
}else{
 UIALogger.logFail(testName);
}

OTHER INTERESTING FUNCTIONS

  • Set the orientation of the device, setDeviceOrientation
    
    target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_LANDSCAPERIGHT);
    target.setDeviceOrientation(UIA_DEVICE_ORIENTATION_PORTRAIT);
    
  • Scroll to see an element
    
    if(varTableView.cells()[3].isVisible()){
         varTableView.cells()[3].scrollToVisible();
    }
    
  • Click on done on the keyboard
    target.frontMostApp().keyboard().typeString("\n");
    
  • Dismiss popover
    target.frontMostApp().mainWindow().popover().dismiss();
  • Set a value in a field
    
    target.frontMostApp().mainWindow().textFields()[0].setValue("valor");
    

    instead of

    
    target.frontMostApp().mainWindow().tableViews()[0].cells()[2].textFields()[0].tap();
    target.frontMostApp().keyboard().typeString(passwordAccount1);
    

    It is better to use the first one because using the one above, sometimes some letters are missing.

  • Delete field
    
    target.frontMostApp().mainWindow().tableViews()[0].cells()[0].textFields()[0].setValue("");
    
  • Swipe
    Add some delay after and before can be useful.

    
    container.tableViews()[0].cells()[0].dragInsideWithOptions({startOffset:{x:0.87, y:0.2}, endOffset:{x:0.0, y:0.2}, duration:0.25});
    
  • To see the element tree
    
    UIATarget.localTarget().frontMostApp().logElementTree()
    
  • Click on an element
    element.tap()
  • Move the app in background
    
    UIATarget.localTarget().deactivateAppForDuration(10);
    
  • Wait until the process is finished.
    It is useful because some actions took more time unexpectedly because of network problem, for instance. So we can wait the execution of particular condition instead of specifying time.

    waitUntilInvisible();
  • Access to an item of an array
    elements().firstWithName("1")

In next posts, we would talk about execute instruments from command lines and how to integrate automation test on jenkins afterwards.

 

 

 

Leave a Comment

¿Necesitas una estimación?

Calcula ahora

Privacy Preference Center

Own cookies

__unam, gdpr 1P_JAR, DV, NID, _icl_current_language

Analytics Cookies

This cookies help us to understand how users interact with our site

_ga, _gat_UA-42883984-1, _gid, _hjIncludedInSample,

Subscription Cookies

These cookies are used to execute functions of the Web, such as not displaying the advertising banner and / or remembering the user's settings within the session.

tl_3832_3832_2 tl_5886_5886_12 tve_leads_unique

Other