如何使用appium编写用于显示应用程序页面的测试用例?

时间:2016-05-19 12:12:43

标签: android node.js testing jquery-mobile appium

我想基于jquery mobile自动化Android应用程序。现在我正在使用appium框架进行测试。我已成功设置,可以使用appium在移动设备上打开应用程序。现在我想编写测试用例来检查当前页面。但我无法找到选择器元素。我尝试了各种解决方案但失败了。这是我的剧本。

    "use strict";

    require("./helpers/setup");

    var wd = require("wd"),
        _ = require('underscore'),
        serverConfigs = require('./helpers/appium-servers');

    describe("android simple", function () {
      this.timeout(300000);
      var driver;
      var allPassed = true;

      before(function () {
        var serverConfig = process.env.npm_package_config_sauce ?
          serverConfigs.sauce : serverConfigs.local;
        driver = wd.promiseChainRemote(serverConfig);
        require("./helpers/logging").configure(driver);

        var desired = process.env.npm_package_config_sauce ?
          _.clone(require("./helpers/caps").android18) :
          _.clone(require("./helpers/caps").android19);
        desired.app = require("./helpers/apps").androidApiCsm;
        if (process.env.npm_package_config_sauce) {
          desired.name = 'android - simple';
          desired.tags = ['sample'];
        }
        return driver
          .init(desired)
          .setImplicitWaitTimeout(5000)
          .contexts().then(function (contexts) { // get list of available views. Returns array: ["NATIVE_APP","WEBVIEW_1"]
              driver.context(contexts[1]); // choose the webview context
          });
      });

      after(function () {
        return driver
          .quit()
          .finally(function () {
            if (process.env.npm_package_config_sauce) {
              return driver.sauceJobStatus(allPassed);
            }
          });
      });

      afterEach(function () {
        allPassed = allPassed && this.currentTest.state === 'passed';
      });

      it("should find an elment and click on the button ",function(){

             return driver.getCurrentActivity(
                   function(err,element){
                         console.log("using xpath err: ", err);
                         console.log("using xpath element: ", element);
            });

           return driver.elementByAccessibilityId('LoginForm',
               function(err,element){
                     console.log("using xpath err: ", err);
                     console.log("using xpath element: ", element);
           });
      });

    });

结果如下。         npm运行android-csm

      android simple
     > CALL init({"browserName":"","appium-version":"1.3","platformName":"Android","platformVersion":"4.4.2","deviceName":"Android Emulator","app":"http://localhost/mobileandroid-debug.apk"}) 
     > POST /session {"desiredCapabilities":{"browserName":"","appium-version":"1.3","platformName":"Android","platformVersion":"4.4.2","deviceName":"Android Emulator","app":"http://localhost/mobileandroid-debug.apk"}}

    Driving the web on session: 9bc4f487-6c3c-4896-b530-e61583e972bc

     > RESPONSE setImplicitWaitTimeout(5000) 
     > CALL contexts() 
     > GET /session/:sessionID/contexts 
     > RESPONSE contexts() ["NATIVE_APP","WEBVIEW_com....."]
     > CALL context("WEBVIEW_com.....") 
     > POST /session/:sessionID/context {"name":"WEBVIEW_com....."}
        ✓ should able to context switch
     > CALL elementByAccessibilityId("LoginForm") 
     > POST /session/:sessionID/element {"using":"accessibility id","value":"LoginForm"}
     > RESPONSE context("WEBVIEW_com....") 
    using xpath err:  {"message":"[elementByAccessibilityId(\"LoginForm\")] Error response status: 7, , NoSuchElement - An element could not be located on the page using the given search parameters. Selenium error: An element could not be located on the page using the given search parameters.","status":7,"cause":{"status":7,"value":{"message":"An element could not be located on the page using the given search parameters."},"sessionId":"9bc4f487-6c3c-4896-b530-e61583e972bc"},"jsonwire-error":{"status":7,"summary":"NoS...
    using xpath element:  undefined

        1) should find an elment and click on the button 


      1 failing

      1) android simple should find an elment and click on the button :
         Error: [elementByAccessibilityId("LoginForm")] Error response status: 7, , NoSuchElement - An element could not be located on the page using the given search parameters. Selenium error: An element could not be located on the page using the given search parameters.
          at exports.newError (node_modules/wd/lib/utils.js:139:13)
          at node_modules/wd/lib/callbacks.js:75:19
          at node_modules/wd/lib/webdriver.js:174:5
          at Request._callback (node_modules/wd/lib/http-utils.js:87:7)
          at Request.self.callback (node_modules/request/request.js:368:22)
          at Request.<anonymous> (node_modules/request/request.js:1219:14)
          at IncomingMessage.<anonymous> (node_modules/request/request.js:1167:12)
          at endReadableNT (_stream_readable.js:926:12)
          at _combinedTickCallback (internal/process/next_tick.js:74:11)
          at process._tickCallback (internal/process/next_tick.js:98:9)

0 个答案:

没有答案