Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Karate 如何在JSON(页面对象模型)-空手道DSL中正确创建和使用动态Xpath_Karate - Fatal编程技术网

Karate 如何在JSON(页面对象模型)-空手道DSL中正确创建和使用动态Xpath

Karate 如何在JSON(页面对象模型)-空手道DSL中正确创建和使用动态Xpath,karate,Karate,例如,我在pages文件夹中有一个示例JSON对象,其中包含特定页面的所有XPath { "pageTitle1": "//*[@class='page-title' and text()='text1']", "pageTitle2": "//*[@class='page-title' and text()='text2']", "pageTitle_x" : "

例如,我在pages文件夹中有一个示例JSON对象,其中包含特定页面的所有XPath

{
    "pageTitle1": "//*[@class='page-title' and text()='text1']",
    "pageTitle2": "//*[@class='page-title' and text()='text2']",
    "pageTitle_x" : "//*[@class='page-title' and text()='%s']"
}

 * def pageHome = read('classpath:/pages/pageHome.json')
 * click(pageHome.pageTitle_x) <-- how to properly replace %s in the string?
{
“页面标题1”:“/*[@class='page-title'和text()='text1']”,
“页面标题2”:“/*[@class='page-title'和text()='text2']”,
“pageTitle_x”:“/*[@class='page-title'和text()='%s']”
}
*def pageHome=read('classpath:/pages/pageHome.json')

*首先单击(pageHome.pageTitle_x)一点建议。从长远来看,试图这样“太聪明”会导致可维护性问题。我在这里说了很多,请阅读:

也就是说,您可以编写可重复使用的JS函数来完成所有这些任务:

* def pageTitle = function(x){ return "//*[@class='page-title' and text()='" + x "']" }
现在,使用它,您可以执行以下操作:

* click(pageTitle('foo'))
如果重新设计功能,甚至可能:

* click(pageTitle(pageHome.pageTitle_x, 'foo'))

但是看看事情是如何变得更加复杂和不可读的。选择权在你。请注意,您可以在JS中执行的任何操作(例如String.replace())都是可能的,这取决于您和您的创造力。

我问这个问题是因为这在使用Selenium自动化时是常见的,在页面对象模型中大量使用带有%s的动态xpath。谢谢你的快速回复,彼得,我会试试你提供的。@Don yes。我对硒和“POM”的看法记录在这里的第7点:值得一读。谢谢分享。