使用 watir-webdriver 获取音频控件的 src 值

Get the src value of an audio control using watir-webdriver

我正在使用 watir-webdriver 和 rspec 为 Web 应用程序编写自动化测试。 我需要检查音频控件是否已正确加载。 对于图像,我使用以下语法成功检查了 src 属性:

$browser.image(:src => "/media/myimage.jpg").present?.should eql(true)

我希望使用类似的方法来检查音频控件。 网页结构如下所示(HTML 大括号已删除):

audio controls="controls" title=""
   source src="/media/R010201/R010201_audio_PhotoTime.ogg" type="audio/ogg "
audio

注意 source 元素是 audio 元素的子元素。使用交互式 Ruby (IRB),我试图使用 watir-webdriver 查找 src 标签。 我尝试了以下方法:

$browser.audio.present?

- returns 正确。

$browser.audio.(:tag_name => 'source').text

-失败

$browser.element(:tag_name => 'source').src.text

-失败 我应该使用什么语法来获取 src 属性中的值?

您正在尝试使用 tagname,在本例中为 audio 我不是 Watir 人,但我想这值得一试。

browser.element(:xpath => "//*[@type='audio/ogg ']").attribute_value('src')

查看 api 文档 here and see another SO thread related here

正确的语法是:

browser.audio.source.src
#=> "/media/R010201/R010201_audio_PhotoTime.ogg"

如果页面上只有一个audio/source,你可以简单地做:

browser.source.src

但是,如果有多个 audio/source,您将需要找到更具体的方法来定位它们。例如,如果音频元素周围有一个唯一的父 div 元素,您将执行:

browser.div(:id => 'unique_id').source.src

你应该可以做到

$browser.source.attribute_value("src")

并获得 "file:///media/R010201/R010201_audio_PhotoTime.ogg" 值。它在我模拟的页面中对我有用。