为 karate-core 中的 re-usable 个特征创建函数

Creating functions for re-usable features in karate-core

我在 Karate 之上构建了一个框架,并且有几个可重用的函数(主要是 Javascript 和实用程序)。我们可以重用功能,但我想知道是否有一种方法可以使用 Javascript 函数来包装对这些功能的调用 UI 功能。我认为它使测试更容易抽象一些代码——几乎就像用一些自定义函数扩展 DSL。

我似乎能够通过空手道 DSL 中的函数实现这一点,这些函数不属于 UI 自动化包/karate-core 但不适用于 UI 函数。

例如,我有以下名为 click.feature 的可重用功能(可点击的“divs”的解决方法,其中 .click() 在 Web 中似乎不起作用Driver):

Scenario:
  Then def pos = position(__arg.selector)
  And def x = Math.round(pos.x + pos.width/2)
  And def y = Math.round(pos.y + pos.height/2)
  And mouse().move(x, y).click() 

以及以下JS函数:

Scenario:    
  * def moveAndClick =
  """
    function(selector) {
      karate.call('click.feature', ( { selector : selector } ))
    }
  """

当我尝试使用它时出现以下错误:

click.feature:5 - evaluation (js) failed: position(__arg.selector), javax.script.ScriptException: ReferenceError: "position" is not defined in <eval> at line number 1

这似乎仅限于 UI 函数,因为我尝试了 assertprint 关键字,这些都有效。我是不是遗漏了什么或者这是设计使然?

你能检查一下这个问题,看看是否能解决你的问题:https://github.com/intuit/karate/issues/1280 - would be great if you can build from source and verify: https://github.com/intuit/karate/wiki/Developer-Guide

因此默认情况下,直到您第一次调用 URL 时才会定义 driver 对象。但是如果你想使用 re-usable 函数,他们必须使用 karate.get('driver'),然后你可以使用它来链接函数。