如何对每次重复中更改变量的命令字符串进行分组和重复
我想在 Watir 中使用 (word) 作为不同的变量重复相同的过程,而无需在我的 .rb 文件中再次写出整个代码。所以无需写下这个:
website = somewebsite.com
word = someword
browser.goto(website)
if browser.text.include?(word)
puts(website)
end
word = someotherword
browser.goto(website)
if browser.text.include?(word)
puts(website)
end
word = anotherword
browser.goto(website)
if browser.text.include?(word)
puts(website)
end
我该怎么做?
谢谢。
I want to repeat the same process in Watir with (word) as a different variable without writing out the whole code again in my .rb file. So without having to write this:
website = somewebsite.com
word = someword
browser.goto(website)
if browser.text.include?(word)
puts(website)
end
word = someotherword
browser.goto(website)
if browser.text.include?(word)
puts(website)
end
word = anotherword
browser.goto(website)
if browser.text.include?(word)
puts(website)
end
how can I do this?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
网站是一样的吗?将其留在循环之外。
如果您想要更好的性能,请完全省略循环:
The website is the same? Leave it outside the loop.
If you want better performance, omit the loop altogether:
虽然您可以对测试代码中的数据进行硬编码,但更好的想法可能是通过将更改的数据放入电子表格、CSV 或 XML 文件等文件中来进行更多数据驱动的测试,然后在循环时读取该文件通过(对于某些文件格式(例如 excel 或 xml),您可能需要 ruby gem)
watir 示例页面 在 wiki
=-=-=-=-=-=-=
我即将超出你的问题所问的范围,但因为看起来你只是首先,我想向您介绍我认为“正确方向”的示例。
另一个选择是使用 Cucumber 这样的工具作为测试框架。这允许您通过可执行规范来驱动测试,您可以在其中使用简单语言格式指定测试应执行的操作。对于本次讨论来说更重要的是,它使得使用不同的数据多次重复相同的场景变得非常容易。用 Cucumber 编写的程序“功能”的“场景”看起来像这样
每个文本步骤(以 Give 和 Then 开头的行)都映射到您用 ruby/watir 编写的编码步骤,并且该工具将循环遍历表中每一行数据的步骤集(在本例中为三次)将表中的值传递到步骤。
步骤的代码最终看起来像这样
如果您使用了单元测试框架,.should 方法类似于断言,它基本上告诉系统“寻找这是真的”。如果 .should 方法失败,则该工具会报告该步骤失败,因此该场景在该行数据上失败。
它实际上在很多方面都是一个非常优雅的系统,对整个组织都有好处(不仅仅是在测试中),并且恰好是我最喜欢的驱动测试的方式,特别是因为它很好地处理了使用不同数据的重复步骤或场景,这是最重要的我们最终做了很多事情。
这篇博客文章做得很好详细说明该过程,从使用 PO 开始定义步骤,到创建页面对象(如果开发人员更改 ID 或名称等,抽象层可以更轻松地更新测试),再到编写实际的代码使用 ruby/Watir 的黄瓜步骤。
就我个人而言,这就是我的发展方向,如果没有其他原因,它可以轻松地进行分组测试,并提供现成的结果报告。
这是精彩的会议视频,其中更深入地解释了 Cucumber 。如果您在该网站上搜索 Cucumber,您会发现很多好东西,展示了如何最好地使用该工具。
While you could hardcode the data in the test code, a better idea might be to make a more data driven test by putting the data that changes into a file like a spreadsheet, CSV, or XML file, then read the file in as you loop through (you may need to require a ruby gem for some file formats like excel or xml)
There's examples of this sort of thing on the watir examples page in the wiki
=-=-=-=-=-=-=
I'm about to go way beyond what your question asks, but since it would seem you are just getting started, I'd like to direct you in what I view is an example of a 'right direction'
Another option is to use a tool like Cucumber as a testing framework. This allows for you to drive your tests via an executable specification where you specify what the test should do using a plain language format. More importantly to this discussion, it makes it VERY easy to repeat the same scenario multiple times with different data. A 'scenario' for a program 'feature' written in Cucumber looks something like this
Each of the text steps (the lines starting with Given and Then) maps to coded steps which you write in ruby/watir, and the tool will loop through the set of steps for every row of data in the table (three times in this case) passing in the values from the table to the steps.
The code for a step ends up looking something like this
The .should method is similar to assert if you've used a unit test framework, and it basically tells the system to 'look for this to be true'. If the .should method fails then the tool reports that step as failing, and hence the scenario as failing on that row of data.
It's actually a very elegant system in many ways, with benefits throughout the organization (not just in test) and happens to be my favorite way of driving my tests, especially since it deals so well with repeating steps or scenarios with different data, something most of us end up doing a lot.
This blog posting does a GREAT job of detailing the process, from the start of working with the PO to define the steps, to creating a page object (abstraction layer making it WAY easier to update your tests if the developers alter ID's or Names etc) to coding up the actual cucumber steps using ruby/Watir.
Personally that's the direction I would head, if for no other reason than that it makes it easy to run tests in groups, and provides ready reporting of results.
Here is a great video of a session where Cucumber is explained in more depth. If you search that site for cucumber, you will find a bunch of good stuff showing how to best use the tool.
上面有更好的答案,但这就是我最终使用的:
Array1 = [word1, word2, word3,]
for x in array1.each do
if browser.text.include?(x)
put(x 在网站上找到)
否则显示“未找到”
结束
结束
better answers above but this is what i used in the end:
Array1 = [word1, word2, word3,]
for x in array1.each do
if browser.text.include?(x)
puts(x found on website)
else puts "not found"
end
end