代码之家  ›  专栏  ›  技术社区  ›  Bytes

在本地appium服务器上运行javascript e2e测试

  •  0
  • Bytes  · 技术社区  · 8 年前

    我想在运行本地android仿真器的Appium服务器实例上运行用javascript和mocha编写的e2e测试。测试中的应用程序是最初用react native编写的apk。

    在Windows上,我通过使用Appium桌面应用程序,使用Android Studio模拟器启动并运行服务器。服务器看起来都很好,有本地应用程序的apk,我想测试工作良好。我还有一个用mocha编写的基本描述/断言测试,我想应用到应用程序中。

    我的问题是,为了让测试真正测试模拟器应用程序,我需要包括什么(可能在测试文件中)?我发现文档非常混乱,示例代码似乎非常特定于不同的用例。

    2 回复  |  直到 8 年前
        1
  •  0
  •   dmle    8 年前

    至少有2个好的js客户端库可用于基于Appium的项目: webdriverio wd . 就个人而言,我正在使用第二个,因此我可以建议您如何使用它和mocha编写测试: 我的测试文件如下所示:

    'use strict'
    
    require(path.resolve('hooks', 'hooks'))
    
    describe('Suite name', function () {
      before('Start new auction', async function () {
        //do before all the tests in this file, e.g. generate test data
      })
    
      after('Cancel auction', async function () {
        //do after all the tests in this file, e.g. remove test data
      })
    
     it('test1', async () => {
      // test steps and checks are here
     })
    
     it('test2', async () => {
      // test steps and checks are here
     })
    
     it('test3', async () => {
      // test steps and checks are here
     })
    })
    

    哪里 挂钩。js公司 包含所有测试的全局前/后:

    const hooks = {}
    
    before(async () => {
      // before all the tests, e.g. start Appium session
    })
    
    after(async () => {
      // after all the tests, e.g. close session
    })
    
    beforeEach(async () => {
      // before each test, e.g. restart app
    })
    
    afterEach(async function () {
      // e.g. take screenshot if test failed
    })
    
    module.exports = hooks
    

    我并不是说这是设计测试的最佳实践,而是多种方法之一。

        2
  •  0
  •   Bytes    8 年前

    很酷,所以我设法让它工作到一定程度。当我试图运行一些东西时,我检查了Appium控制台日志,发现我的请求中缺少会话id。所需的只是使用会话id连接驱动程序。我的代码看起来有点像:

    "use strict";
    
    var wd = require("wd")
    var assert = require("assert")
    
    var serverConfig = {
        host: "localhost",
        port: 4723,
    }
    
    var driver = wd.remote(serverConfig)
    
    driver.attach("0864a299-dd7a-4b2d-b3a0-e66226817761", function() {
        it("should be true", function() {
            const action = new wd.TouchAction()
            action
                .press({x: 210, y: 130})
                .wait(3000)
                .release()
            driver.performTouchAction(action)
            assert.equal(true, true)
        })
    })
    

    equals true断言只是作为占位符健全性检查。目前唯一的问题是,每次重新启动Appium服务器时,我都会将字母数字会话id复制粘贴到attach方法中,因此我需要找到一种方法来实现自动化。