CB-11296: Appium: Better element clicking and session error handling
diff --git a/appium-tests/common/common.spec.js b/appium-tests/common/common.spec.js
index ec19739..e2dbe51 100644
--- a/appium-tests/common/common.spec.js
+++ b/appium-tests/common/common.spec.js
@@ -41,6 +41,8 @@
     var driver;
     var webviewContext;
     var promiseCount = 0;
+    // going to set this to false if session is created successfully
+    var failedToStart = true;
 
     function getNextPromiseId() {
         return 'appium_promise_' + promiseCount++;
@@ -109,11 +111,16 @@
             .then(function () {
                 switch (PLATFORM) {
                     case 'ios':
-                        return wdHelper.tapElementByXPath(UNORM.nfd('//UIAStaticText[@label="' + name + '"]'), driver);
+                        return driver
+                            .waitForElementByXPath(UNORM.nfd('//UIAStaticText[@label="' + name + '"]'), 20000)
+                            .elementByXPath(UNORM.nfd('//UIAStaticText[@label="' + name + '"]'))
+                            .elementByXPath(UNORM.nfd('//UIAStaticText[@label="' + name + '"]'));
                     case 'android':
-                        return driver.waitForElementByXPath('//android.widget.TextView[@text="' + name + '"]', MINUTE).click();
+                        return driver
+                            .waitForElementByXPath('//android.widget.TextView[@text="' + name + '"]', MINUTE);
                 }
             })
+            .click()
             .context(webviewContext)
             .executeAsync(function (pID, cb) {
                 navigator._appiumPromises[pID].promise
@@ -218,19 +225,30 @@
             });
     }
 
+    function checkSession(done) {
+        if (failedToStart) {
+            fail('Failed to start a session');
+            done();
+        }
+    }
+
     it('contacts.ui.util configuring driver and starting a session', function (done) {
         getDriver()
-            .fail(fail)
+            .then(function () {
+                failedToStart = false;
+            }, fail)
             .done(done);
     }, 5 * MINUTE);
 
     describe('Picking contacts', function () {
         afterEach(function (done) {
+            checkSession(done);
             removeTestContacts()
                 .finally(done);
         }, MINUTE);
 
         it('contacts.ui.spec.1 Pick a contact', function (done) {
+            checkSession(done);
             var bday = new Date(1991, 1, 1);
             driver
                 .then(function () {
@@ -249,6 +267,7 @@
         }, 5 * MINUTE);
 
         it('contacts.ui.spec.2 Update an existing contact', function (done) {
+            checkSession(done);
             driver
                 .then(function () {
                     return addContact('Dooney', 'Evans');
@@ -272,6 +291,7 @@
         }, 10 * MINUTE);
 
         it('contacts.ui.spec.3 Create a contact with no name', function (done) {
+            checkSession(done);
             driver
                 .then(function () {
                     return addContact();
@@ -299,6 +319,7 @@
         }, 5 * MINUTE);
 
         it('contacts.ui.spec.4 Create a contact with Unicode characters in name', function (done) {
+            checkSession(done);
             driver
                 .then(function () {
                     return addContact('Н€йромонах', 'ФеофаЊ');
@@ -316,6 +337,7 @@
     });
 
     it('contacts.ui.util Destroy the session', function (done) {
+        checkSession(done);
         driver
             .quit()
             .done(done);