如何在Webdriver Ruby测试中存储动态值和重用 [英] How to store dynamic value and reuse in Webdriver Ruby test

查看:66
本文介绍了如何在Webdriver Ruby测试中存储动态值和重用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个ruby webdriver测试,该测试需要存储动态值(例如订单ID),以便稍后在文本中使用.我想我需要从字符串中提取值,然后将其存储为变量以供将来使用.

I'm writing a ruby webdriver test that needs to store a dynamic value such as an order ID to use later on in the text. I think I need to extract the value from the string and then store it as a variable to call for future use.

字符串看起来像这样,我只需要提取/存储数字值即可.

The string looks like this and I just need to extract/store the numeric value.

<span class="receiptNum hidden-xs">Receipt #: 12303430</span>

任何有关如何提取该值并创建变量以供将来使用的提示或示例都将很棒!

Any tips or examples on how to extract that value and create a variable for future use would be great!

推荐答案

要从此元素中提取文本(仅数字),请尝试使用以下代码:

To extract the text (only numbers) out of the this element, try using the following code:

@numbers = @driver.find_element(:css=>'receiptNum').text.scan(\d+)

当前,我将该数字保存在一个实例变量中,该变量可以在同一测试中再次使用,因为它将随测试对象一起循环直到测试完成.

Currently I am saving this number in an instance variable which can be used again in the same test as it will flow around with the test object till the test finishes.

其他选项包括将其保存在临时txt文件中,并在需要时从中读取.

Other option include saving it in a temp txt file and reading from it when required.

注意:现在获取数据并稍后使用它不是一个好习惯,请尽量不要经常使用它.

Note: Fetching data now and using it later is not a good practice, try not to use this very frequently.

希望有帮助!

这篇关于如何在Webdriver Ruby测试中存储动态值和重用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆