Tuesday 7 May 2013

Appium - Simple first test

There are some tutorials how to write simple first test:
  1. Install Nodejs:
    1. http://nodejs.org/download/
  2. Install mocha:
    1. npm install -g mocha
  3. Create test folder:
    1. Open terminal
    2. cd Desktop
    3. mkdir appium-test
  4. Download test app:
    1. cd appium-test
    2. curl -O http://developer.apple.com/library/ios/samplecode/UICatalog/UICatalog.zip
    3. unzip UICatalog.zip
    4. cd UICatalog
    5. xcodebuild -sdk iphonesimulator6.0
  5. Setup appium:
    1. Download appium app:
    2. Run appium server
    3. Choose "App Path" using UICatalog build .app file:
      • UICatalog/build/Release-iphonesimulator/UICatalog.app
    4. Launch
  6. Install node modules:
    1. cd appium-test
    2. npm install path
    3. npm install saucelabs
    4. npm install should
    5. npm install underscore
    6. npm install wd
  7. Create test files:
    1. driverblock.js
      • /*global beforeEach:true, afterEach:true, describe:true */
        "use strict";
         
        var wd = require('wd')
          , _ = require("underscore")
          , sauce = require("saucelabs")
          , sauceRest = null
          , path = require("path")
          , should = require("should")
          , defaultHost = '127.0.0.1'
          , defaultPort = 4723
          , defaultCaps = {
              browserName: ''
              , device: 'iPhone Simulator'
              , platform: 'Mac'
              , version: '6.0'
              //, newCommandTimeout: 60
            };
         
        var sauceRest = {
          username: 'vinhquang09'
          , password: '1acf93f3-ba7d-4776-a5bc-6958be79b20b'
        };
         
         
        var driverBlock = function(tests, host, port, caps, extraCaps) {
          host = (typeof host === "undefined" || host === null) ? _.clone(defaultHost) : host;
          port = (typeof port === "undefined" || port === null) ? _.clone(defaultPort) : port;
          caps = (typeof caps === "undefined" || caps === null) ? _.clone(defaultCaps) : caps;
          caps = _.extend(caps, typeof extraCaps === "undefined" ? {} : extraCaps);
          var driverHolder = {driver: null, sessionId: null};
          var expectConnError = extraCaps && extraCaps.expectConnError;
         
          beforeEach(function(done) {
            driverHolder.driver = wd.remote(host, port);
            driverHolder.driver.init(caps, function(err, sessionId) {
              if (expectConnError && err) {
                driverHolder.connError = err;
                return done();
              }
              should.not.exist(err);
              driverHolder.sessionId = sessionId;
              driverHolder.driver.setImplicitWaitTimeout(5000, function(err) {
                should.not.exist(err);
                done();
              });
            });
          });
         
          afterEach(function(done) {
            driverHolder.driver.quit(function(err) {
              if (err && err.status && err.status.code != 6) {
                throw err;
              }
              if (host.indexOf("saucelabs") !== -1 && sauceRest !== null) {
                sauceRest.updateJob(driverHolder.sessionId, {
                  passed: true
                }, function() {
                  done();
                });
              } else {
                done();
              }
            });
          });
         
          tests(driverHolder);
        };
         
         
        var describeForApp = function(app, device, appPackage, appActivity) {
          if (typeof device === "undefined") {
            device = "ios";
          }
          var browserName, appPath, realDevice;
         
          realDevice = "iPhone Simulator";
          browserName = "iOS";
         
          appPath = path.resolve(__dirname, app + "/build/Release-iphonesimulator/" + app + ".app");
         
          return function(desc, tests, host, port, caps, extraCaps) {
            if (typeof extraCaps === "undefined") {
              extraCaps = {};
            }
            var newExtraCaps = {
              app: appPath,
              browserName: browserName,
              device: realDevice
            };
         
            driverBlock(tests, host, port, caps, newExtraCaps);
          };
        };
         
         
        var describeForSauce = function(appUrl) {
          return function(desc, tests, host, port, extraCaps) {
            host = typeof host === "undefined" ? 'ondemand.saucelabs.com' : host;
            port = typeof port === "undefined" ? 80 : port;
         
            host = sauceRest.username + ':' + sauceRest.password + '@' + host;
            var caps = {
              platform: "Mac 10.8"
              , name: "First test"
              , device: "iPhone Simulator"
              , browserName: ""
              , app: appUrl
              , version: ""
              , tags: ['appium', 'test']
            };
         
            driverBlock(tests, host, port, caps, extraCaps);
          };
        };
         
         
        module.exports.block = driverBlock;
        module.exports.describeForApp = describeForApp;
        module.exports.describeForSauce = describeForSauce;
    2. simple.js:
      • /*global it:true */
        "use strict";
         
        var should = require("should")
          , describeWd = require("./driverblock.js").describeForApp('UICatalog');
          //, describeWd = require("./driverblock.js").describeForSauce('http://developer.apple.com/library/ios/samplecode/UICatalog/UICatalog.zip');
         
         
        describeWd('moveTo and click', function(h) {
          it('should be able to click on arbitrary x-y elements', function(done) {
            h.driver.elementsByTagName('tableCell', function(err, els) {
              should.not.exist(err);
              h.driver.moveTo(els[0], 10, 10, function(err) {
                should.not.exist(err);
                h.driver.click(function(err) {
                  should.not.exist(err);
                  h.driver.elementByXPath("button[@name='Rounded']", function(err, el) {
                    should.not.exist(err);
                    should.exist(el.value);
                    done();
                  });
                });
              });
            });
          });
        });
  8. Run test:
    • mocha -t 60000 -R spec simple.js

Good luck!

1 comment: