Showing posts with label Appium. Show all posts
Showing posts with label Appium. Show all posts

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!

Appium - Installation


There are some notice when playing with Appium. Take care of it:
  1. Appium:
    • Newly automation testing open source, we don't have a lot of tutorials about it
  2. Using Virtual Machine:
    • We should get a little unknown bugs and don't know how to fixed them:
      • assert
      • swipe
      • module
  3. Install nopejs:
    1. Option 1:
      • Simply go go: http://nodejs.org/
      • Click install for download pkg package
      • Install the file
    2. Option 2:
  4. Install appium server:
    1. Option 1:
      • Git appium project: https://github.com/appium/appium
      • ./reset.sh
      • Create bin file:
        • chmod +x /path/to/appium/app/bin.js
        • cd /usr/local/bin/
        • ln -s /path/to/appium/app/bin.js appium
        • ln -s /path/to/appium/instruments/client_bin.js instruments_client
    2. Option 2:
      • sudo npm install appium -g
        • This command will download and added modules for nodejs at:
          • /usr/local/lib/node_modules/appium
          • /usr/local/lib/node_modules/instruments
      • Also create bin file at:
        • /usr/local/bin/appium
        • /usr/local/bin/instruments_client
    3. Option 2:
      • npm install appium
        • This command will download and create appium project folder
      • Create bin file:
        • chmod +x /path/to/appium/app/bin.js
        • cd /usr/local/bin/
        • ln -s /path/to/appium/app/bin.js appium
        • ln -s /path/to/appium/instruments/client_bin.js instruments_client
  5. Install mocha & grunt modules:
    1. Option 1:
      • sudo npm install mocha -g
      • sudo npm install grunt-cli -g
      • This command will download and added modules for nodejs at:
        • /usr/local/lib/node_modules/_mocha
        • /usr/local/lib/node_modules/mocha
        • /usr/local/lib/node_modules/grunt-cli
      • Also create bin file at:
        • /usr/local/bin/_mocha
        • /usr/local/bin/mocha
        • /usr/local/bin/grunt
    2. Option 2:
      • npm install mocha
      • npm install grunt-cli
        • This command will download and create mocha & grunt-cli modules folder
      • Create bin file:
        • cd /usr/local/bin/
        • ln -s /path/to/mocha/bin/_mocha _mocha
        • ln -s /path/to/mocha/bin/mocha mocha
        • ln -s /path/to/grunt-cli/bin/grunt grunt
  6. Small test for appium:
  7. Build nodejs modules folder for Appium:
    • Git project folder from GitHub: https://github.com/appium/appium
      • Make sure you get all files and folders in there, because we have another 2 small projects in appium git:
        • submodules:
          • appium.io
          • instruments-without-delay
    • Install nodejs modules:
      • cd appium
      • npm install mocha
      • npm install grunt-cli
      • npm install
        • This command will using package.json file
  8. Create .app file for test:
    • Using Xcode:
      • Go to your iOS project folder
        • xcodebuild -sdk iphonesimulator6.0
    • Using grunt:
      • Stay at root folder of appium source
        • grunt buildApp:<AppName>:<SDK>
        • grunt buildApp:TestApp:iphonesimulator6.0
    • The .app file should in "build/Release-iphonesimulator" folder in iOS project
  9. How to use grunt:
  10. Some popular issues:
    1. Fatal error: Could not find Android SDK, make sure to export ANDROID_HOME
    2. ReferenceError: describe is not defined
      • We must using mocha function all times

Good luck!