Watir Webdriver <script>元素存在?</script>

时间:2013-01-28 18:41:13

标签: webdriver watir watir-webdriver locate

我试图在页面加载后确定是否可以使用Watir WebDriver定位脚本中的文本。我正在使用Watir-WebDriver来自动化我们的测试工作。我无法弄清楚如何定位元素并验证值。有什么帮助吗?

<script><!--
...
     s.events="event9"
...
//--></script>

所以,我想我想知道是否可以使用watir webdriver在HTML脚本中搜索文本?

提前谢谢你。

更新:以下是脚本。

require "rubygems"
require "watir-webdriver"
require "watir-webdriver-performance"
require "rspec"
require "headless"

include Watir
require 'logger'

#path store file: script, data file, logs
path = File.dirname(__FILE__)

#create log file
name_log = 'TEST_0001_bsro_validation_suite'
file = File.open(path + '/logs/' + name_log + '_logFile.log', File::WRONLY | File::APPEND | File::CREAT)
logger = Logger.new(file)
logger.info("=> TEST: 0004_bsro_validation")

#open internet browser
browser = Watir::Browser.new :ff

#go to rebrand website with login info.  this may need to be removed.
test_site = 'http://*****:*****@fcac-rebrand.laughlin.com/'
browser.goto(test_site)
load_secs = browser.performance.summary[:response_time]
logger.info("=> Page Load Time: #{load_secs}")

zipcode_input = browser.text_field(:id => 'universal-selectorZip')


# select year; progressive selection
year_select = browser.select_list(:id => 'universal-year')
browser.select_list(:id => 'universal-year', :disabled => 'disabled').wait_while_present

if year_select.exists?
  year_select.select '2010'
else
  logger.info("=> ERROR: Year Select Not Available")
end

# select make; progressive selection
make_select = browser.select_list(:id => 'universal-make')
browser.select_list(:id => 'universal-make', :disabled => 'disabled').wait_while_present

if make_select.exists?
  make_select.select 'Volkswagen'
else
  logger.info("=> ERROR: Make Select Not Available")
end

# select model; progressive selection
model_select = browser.select_list(:id => 'universal-model')
browser.select_list(:id => 'universal-model', :disabled => 'disabled').wait_while_present

if model_select.exists?
  model_select.select 'Jetta'
else
  logger.info("=> ERROR: Model Select Not Available")
end

# select submodel; progressive selection
submodel_select = browser.select_list(:id => 'universal-submodel')
browser.select_list(:id => 'universal-submodel', :disabled => 'disabled').wait_while_present

if submodel_select.exists?
  submodel_select.select '2.0T TDI Sedan'
else
  logger.info("=> ERROR: Submodel Select Not Available")
end

# input zip code; progressive selection

if zipcode_input.exists?
  zipcode_input.set '53202'
else
  logger.info("=> ERROR: ZIP Code Select Not Available")
end

browser.button(:id => 'universal-submit-tires-quote').click

browser.script.html.include? "event49"

browser.close

我应该补充说,在这种情况下,事件是event49而不是9。谢谢!

2 个答案:

答案 0 :(得分:3)

Zeljko有正确的方法。但是,如果/当页面上有多个脚本且您想要的那个不是第一个脚本时,它将失败。

如果有多个脚本元素,则必须对它们进行迭代,以查看其中一个脚本是否具有该值。

browser.scripts.any?{ |s| s.html.include? "event9" }
#=> true

答案 1 :(得分:1)

这应该这样做:

browser.script.html.include? "event9"
#=> true