2012-03-01 9 views
3

私は、ページコンテンツがどのようにスタイル付けされているかをテストする必要がある場合があります(必ずしもCSSである必要はありません)。ウェブページの太字の割合をテストするにはどうすればよいですか?

例えば、私が書きたいの試験(キュウリ)は次のとおりです。

私はページ上の太字の割合を告げたことにしたいウェブマスター
としてテキスト重量
を標準化するために

問題は、実際にこの結果を生成する方法を考え出すのが難しいことです。さまざまなHTMLテストフレームワーク(Selenium、Watir、Capybara)を見ると、計算された視覚的結果ではなく、タグの有無やcssクラスの存在のみをテストできるようです。

はFirebugのでは、私は(強い> <、B > <、およびfont-weight:bold定義のために働く)を算出CSSの結果を見ることができますが、私はCIの下で実行するテストフレームワークにこれを置くことができるようにする必要があります。

+0

"パーセンテージ"(面積別)?単語数で? –

+0

Wordが動作する可能性があります。 –

答えて

1

Watirでは、win32oleオブジェクトに直接アクセスしてfont-weight要素にアクセスできます。たとえば:

ie.div(:index, 1).document.currentStyle.fontWeight 

http://www.w3schools.com/cssref/pr_font_weight.asp

で説明したようにこれは私はあなたがして行う必要があるだろうと思う何をあなたに重みを表す数値が得られますが、そのたfontWeightが何であるかをチェックし、ページ上のすべての要素を反復処理され、要素内のテキストの量あなたが行う方法は、テストしているページによって異なります。

ソリューション1 - すべてのテキストがdiv要素である場合はリーフノードである:すべてのテキストは、このようなリーフノードである場合

<body> 
    <div style='font-weight:bold'>Bold</div> 
    <div>Plain</div> 
</body> 

あなたは簡単に行うことができます:

bold_text = 0 
plain_text = 0 
ie.divs.each{ |x| 
    if x.document.currentStyle.fontWeight >= 700 
    bold_text += x.text.length 
    else 
    plain_text += x.text.length 
    end 
} 

解決策2 - スタイルが相互作用する場合、または複数の要素を使用する場合:

テキストがすべてリーフノード内にないか、<b>などの他のタグを使用している場合は、さらに複雑なチェックが必要です。これは.textがその要素の子要素を含む要素内のすべてのテキストを返すためです。この場合

<body> 
    <div style='font-weight:normal'> 
    Start 
    <div style='font-weight:bold'>Bold1</div> 
    <div style='font-weight:bold'>Bold2</div> 
    End 
    </div> 
    <b>Bold Text</b> 
</body> 

、私はほとんどの場合、次の作品を信じている(ただし、改善を必要とする場合があります):

#Counting letters, but you could easily change to words 
bold_count = 0 
plain_count = 0 

#Check all elements, though you can change this to restrict to a particular containing element if desired. 
node_list = ie.document.getElementsByTagName("*") 

0.upto(node_list.length-1) do |i| 
    #Name the node so it is easier to work with. 
    node = node_list["#{i}"] 

    #Determine if the text for the current node is bold or not. 
    #Note that this works in IE. You might need to modify for other browsers. 
    if node.currentStyle.fontWeight >= 700 
     bold = true 
    else 
     bold = false 
    end 

    #Go through the childNodes. If the node is text, count it. Otherwise ignore. 
    node.childNodes.each do |child| 
     unless child.nodeValue.nil? 
      if bold 
       bold_count += child.nodeValue.length 
      else 
       plain_count += child.nodeValue.length 
      end 
     end 
    end 

end 

#Determine number of characters that are bold and not. These can be used to determine your percentage. 
puts bold_count 
puts plain_count 

それは非常にワチールのようなソリューションではありませんが、うまくいけば、あなたの問題を解決します。

関連する問題