2012-03-01 2 views
3

페이지 콘텐츠의 스타일을 지정해야하는 경우가 있습니다. CSS가 반드시 필요한 것은 아닙니다.웹 페이지에서 굵은 텍스트의 비율을 테스트하려면 어떻게합니까?

예를 들어, 내가 쓰고 싶은 테스트 (오이)입니다 :

내가 페이지
에 굵은 텍스트의 비율을 말한 싶은
웹 마스터 텍스트 무게
를 표준화하기 위해

문제는 실제로이 결과를 생성하는 방법을 파악하는 데 어려움이 있습니다. 다양한 HTML 테스팅 프레임 워크 (Selenium, Watir, Capybara)를 살펴보면 계산 된 시각적 결과가 아니라 태그 나 CSS 클래스의 존재 여부 만 테스트 할 수있는 것처럼 보입니다.

파이어 버그에서 나는 (강한 > <, B > < 및 font-weight:bold 정의를 작동) 계산 된 CSS 결과를 볼 수 있습니다,하지만 난 CI에서 실행되도록 테스트 프레임 워크에이를 넣을 수 있어야합니다.

+0

지역별 "백분율"? 단어 개수로? –

+0

Word가 작동 할 수 있습니다. –

답변

1

Watir에서는 win32ole 객체에 직접 액세스하여 font-weight 요소에 액세스 할 수 있습니다.

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

이 당신이 다음의 fontWeight를이 무엇인지 확인 페이지의 모든 요소를 ​​반복하기 만하면됩니다 것이라고 생각 무엇

http://www.w3schools.com/cssref/pr_font_weight.asp에 설명 된대로 당신에게 무게를 나타내는 숫자를 줄 것이다 : 예를 들어, 요소에있는 텍스트의 양. 당신이하는 방식은 테스트하고있는 페이지에 달려 있습니다.

해결 방법 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>과 같은 다른 태그를 사용하는 경우 (아래 예제 HTML 참조) 더 복잡한 체크가 필요합니다. 이는 .text가 children 요소를 포함하여 요소의 모든 텍스트를 반환하기 때문입니다. 이 경우

<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 

그것은 매우 Watir과 같은 솔루션이 아니라 희망이 당신의 문제를 해결한다.

관련 문제