2013-07-23 57 views
1

當我有一個路徑元素時,我有一個問題用watir測試highcharts。 當你將鼠標懸停在圖形上時,路徑元素的座標發生變化。所以我怎麼能通過線路循環? 示例演示= http://www.highcharts.com/demo/line-time-seriesWatir測試Highcharts只有1個路徑元素

+0

@justinko我已經學會了如何遍歷每個路徑元素來測試數據,但是當你只有1個路徑元素時,你知道如何循環嗎?對不起,請再次在高層圖上問你。謝謝 – PeterZDE

+0

根據你要測試的內容,你可以直接從html中提取數據,或者直接使用jQuery從圖表中提取數據。 –

+0

@JustinKo我試圖測試當你徘徊在點上的值。如何直接從html中提取數據? – PeterZDE

回答

0

這樣做的一種方法是使用圖上的xpath。在firefox瀏覽器中使用Firepath插件,當您在圖形中移動鼠標時,每個位置都會有不同的xpath。然後你可以做類似

browser.element_by_xpath(".//*[@id='highcharts-0']/svg/g[6]/g[2]/path[2]']").click 

並選擇你需要的值。這麼做,你可能沒有準確的xpath來表示圖表中的每個位置,但它幾乎可以滿足你的需要。

+0

Ur方法不起作用,element_by_xpath已被廢棄。元素(:xpath) – PeterZDE

0

您可以解析html以提取用於創建圖表的數據。這假設你不是試圖實際測試圖表本身(即你信任第三方庫,只是需要數據用於其他目的)。

require 'watir-webdriver' 
require 'date'  

b = Watir::Browser.new :firefox 
b.goto 'http://www.highcharts.com/demo/line-time-series' 
puts b.html 
#Get the javascript that contains the data used to generate the chart 
data_script = b.scripts.find{ |script| script.html =~ /data/ }.html 

#Get the starting date from the script 
point_start = /pointStart: Date.UTC\((2006), (0), (01)\)/.match(data_script) 
date_start = Date.new(point_start[1].to_i, point_start[2].to_i+1, point_start[3].to_i) 

#Get an array of the data values from the script 
data_values = data_script.scan(/data: \[(.*?)\]/m)[0][0].gsub(/\s/, '').split(',') 

#Create an array of all the dates. 
#Assuming we know it is incremented by day (otherwise check the pointInterval) 
dates = (date_start .. (date_start+data_values.length-1)).map do |day| 
    day.strftime("%b %e, %Y").squeeze(' ') 
end 

#Combine the dates and values 
p dates.zip(data_values) 
#=> [["Jan 1, 2006", "0.8446"], ["Jan 2, 2006", "0.8445"], ... , ["Dec 30, 2008", "0.7049"], ["Dec 31, 2008", "0.7095"]]