如何在另一个特征文件的 javascript 函数中调用我的特征文件函数?
How can I call my feature file function in javascript Function of another feature file?
所以我创建了一个如下所示的滚动功能
Feature: Dynamic page scrolling
Scenario: Scrolling function
* def ScrollHeight = function(){return script("document.body.scrollHeight")
* def ScrollFunction = function(){ script("window.scrollTo(0,document.body.scrollHeight)")
* def ScrollingFunction =
"""
function(){
var height = ScrollHeight()
while(true)
{ ScrollFunction()
var newHeight = ScrollHeight()
if(height === newHeight )
break;
height = newHeight
}
}
"""
所以我在我有我的测试场景的同一个功能文件中创建了这个,但我想把它作为一个集中功能到另一个功能文件中,我也可以在其他功能文件中使用它。
但我将无法从其他功能文件调用 ScrollHeight 和 ScrollFunction?
这就是我尝试在新功能文件中创建它的方式
Scrolling.feature
Feature: Dynamic page scrolling Function
Scenario: Scrolling function
*def obj = read('classpath:Testing.feature')
* def ScrollingFunction =
"""
function(){
var height = obj.ScrollHeight()
while(true)
{ obj.ScrollFunction()
var newHeight = obj.ScrollHeight()
if(height === newHeight )
break;
height = newHeight
}
}
"""
我的Testing.feature文件看起来像
Feature: Testing
Background:
* def ScrollHeight = function(){return script("document.body.scrollHeight")
* def ScrollFunction = function(){
script("window.scrollTo(0,document.body.scrollHeight)")}
Scenario: Test-1
* def fun = call read('classpath:Scrolling.feature')
* call fun.ScrollingFunction
但是对我不起作用
script()
等所有方法只有在默认实例化driver
后才可用。因此,请确保在 driver <url>
步骤 之后 声明任何 re-usable 函数。
否则你必须为 re-usability 做这样的事情:
* def getScrollHeight =
"""
function() {
var driver = karate.get('driver');
return driver.script("document.body.scrollHeight");
}
"""
如果这不起作用,请创建一个快速入门示例,以便我们研究它:https://github.com/intuit/karate/tree/develop/examples/ui-test
您不需要引用其他函数。
它可以在同一个脚本中完成。
这对我有用:
Feature: Search Posts
Background:
* configure driver = { type: 'chrome', start: false }
# build search endpoint
* def buildSearch =
"""
function() {
var keys = 'fake%20news';
var filter = 'eyJycF9jcmVhdGlvbl90aW1lIjoie1wibmFtZVwiOlwiY3JlYXRpb25fdGltZVwiLFwiYX'+
'Jnc1wiOlwie1xcXCJzdGFydF95ZWFyXFxcIjpcXFwiMjAyMFxcXCIsXFxcInN0YXJ0X21v'+
'bnRoXFxcIjpcXFwiMjAyMC0xXFxcIixcXFwiZW5kX3llYXJcXFwiOlxcXCIyMDIwXFxcIi'+
'xcXFwiZW5kX21vbnRoXFxcIjpcXFwiMjAyMC0xMlxcXCIsXFxcInN0YXJ0X2RheVxcXCI6'+
'XFxcIjIwMjAtOC0xMlxcXCIsXFxcImVuZF9kYXlcXFwiOlxcXCIyMDIwLTgtMTJcXFwifVwifSJ9';
return 'https://m.facebook.com/search/posts/?q='+keys+'&epa=FILTERS&filters='+filter;
}
"""
Scenario: facebook search
# Scroll down until no more pager
# return null to keep looping
* def fecthPage =
"""
function() {
var results = script("window.scrollTo(0, document.body.scrollHeight); document.querySelectorAll('#see_more_pager').length")
return results == 0 ? results : null;
}
"""
# Print Post information
* def permalinks = []
* def getPost =
"""
function(x, y, z) {
var feat = x.script("_.getAttribute('data-ft');");
var store = x.script("_.getAttribute('data-store');");
if (feat && store) {
feat = karate.toJson(feat);
store = karate.toJson(store);
karate.appendTo(permalinks, {'Features': feat, 'Store': store});
}
}
"""
Given driver buildSearch()
* waitUntil(fecthPage)
* def posts = locateAll('//div[@data-store][@data-ft]')
* karate.forEach(posts, getPost)
* print "Posts:", permalinks
为了避免 chrome 配置文件占用我的所有磁盘,我在空手道之前启动它。
这就是为什么 start: false
所以我创建了一个如下所示的滚动功能
Feature: Dynamic page scrolling
Scenario: Scrolling function
* def ScrollHeight = function(){return script("document.body.scrollHeight")
* def ScrollFunction = function(){ script("window.scrollTo(0,document.body.scrollHeight)")
* def ScrollingFunction =
"""
function(){
var height = ScrollHeight()
while(true)
{ ScrollFunction()
var newHeight = ScrollHeight()
if(height === newHeight )
break;
height = newHeight
}
}
"""
所以我在我有我的测试场景的同一个功能文件中创建了这个,但我想把它作为一个集中功能到另一个功能文件中,我也可以在其他功能文件中使用它。 但我将无法从其他功能文件调用 ScrollHeight 和 ScrollFunction?
这就是我尝试在新功能文件中创建它的方式 Scrolling.feature
Feature: Dynamic page scrolling Function
Scenario: Scrolling function
*def obj = read('classpath:Testing.feature')
* def ScrollingFunction =
"""
function(){
var height = obj.ScrollHeight()
while(true)
{ obj.ScrollFunction()
var newHeight = obj.ScrollHeight()
if(height === newHeight )
break;
height = newHeight
}
}
"""
我的Testing.feature文件看起来像
Feature: Testing
Background:
* def ScrollHeight = function(){return script("document.body.scrollHeight")
* def ScrollFunction = function(){
script("window.scrollTo(0,document.body.scrollHeight)")}
Scenario: Test-1
* def fun = call read('classpath:Scrolling.feature')
* call fun.ScrollingFunction
但是对我不起作用
script()
等所有方法只有在默认实例化driver
后才可用。因此,请确保在 driver <url>
步骤 之后 声明任何 re-usable 函数。
否则你必须为 re-usability 做这样的事情:
* def getScrollHeight =
"""
function() {
var driver = karate.get('driver');
return driver.script("document.body.scrollHeight");
}
"""
如果这不起作用,请创建一个快速入门示例,以便我们研究它:https://github.com/intuit/karate/tree/develop/examples/ui-test
您不需要引用其他函数。 它可以在同一个脚本中完成。
这对我有用:
Feature: Search Posts
Background:
* configure driver = { type: 'chrome', start: false }
# build search endpoint
* def buildSearch =
"""
function() {
var keys = 'fake%20news';
var filter = 'eyJycF9jcmVhdGlvbl90aW1lIjoie1wibmFtZVwiOlwiY3JlYXRpb25fdGltZVwiLFwiYX'+
'Jnc1wiOlwie1xcXCJzdGFydF95ZWFyXFxcIjpcXFwiMjAyMFxcXCIsXFxcInN0YXJ0X21v'+
'bnRoXFxcIjpcXFwiMjAyMC0xXFxcIixcXFwiZW5kX3llYXJcXFwiOlxcXCIyMDIwXFxcIi'+
'xcXFwiZW5kX21vbnRoXFxcIjpcXFwiMjAyMC0xMlxcXCIsXFxcInN0YXJ0X2RheVxcXCI6'+
'XFxcIjIwMjAtOC0xMlxcXCIsXFxcImVuZF9kYXlcXFwiOlxcXCIyMDIwLTgtMTJcXFwifVwifSJ9';
return 'https://m.facebook.com/search/posts/?q='+keys+'&epa=FILTERS&filters='+filter;
}
"""
Scenario: facebook search
# Scroll down until no more pager
# return null to keep looping
* def fecthPage =
"""
function() {
var results = script("window.scrollTo(0, document.body.scrollHeight); document.querySelectorAll('#see_more_pager').length")
return results == 0 ? results : null;
}
"""
# Print Post information
* def permalinks = []
* def getPost =
"""
function(x, y, z) {
var feat = x.script("_.getAttribute('data-ft');");
var store = x.script("_.getAttribute('data-store');");
if (feat && store) {
feat = karate.toJson(feat);
store = karate.toJson(store);
karate.appendTo(permalinks, {'Features': feat, 'Store': store});
}
}
"""
Given driver buildSearch()
* waitUntil(fecthPage)
* def posts = locateAll('//div[@data-store][@data-ft]')
* karate.forEach(posts, getPost)
* print "Posts:", permalinks
为了避免 chrome 配置文件占用我的所有磁盘,我在空手道之前启动它。
这就是为什么 start: false