Thanks Bret -- took a few more lines in 1.4.2 (as I mentioned earlier I haven't had success with 1.5 -- receive error on IE.attach) -- anyway here's the code if anyone wants it -- including a test and the html for the test file.  Basically someone can see what methods/classes were required to add the element.
 
I think that meta programming idea sounds really cool, and I'd think could allow for dynamic extension for new/different tags -- especially since this code is an exact dupe of div (and span and p and others I suspect) -- just replacing li for div (correct case of course).
 
# feature tests for Li's
# revision: $Revision: 1.17 $
 
$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..') if $0 == __FILE__
require 'setup'
 
#EXTEND WATIR FOR LI TAGS -- cloned directly from corresponding DIV code
module Watir       
   module SupportsSubElements
      def li(how, what)
         return Li.new(self, how, what)
      end
       
      def lis
         return Lis.new(self)
      end
   end
   class IE  
      def show_lis
         lis = document.getElementsByTagName("LI")
         puts "Found #{lis.length} li tags"
         index=1
         lis.each do |d|
            puts "#{index}  id=#{d.invoke('id')}      class=#{d.invoke("className")}"
            index+=1
         end
      end
   end
  
   class Li < SpanDivCommon
      TAG = 'LI'
      def tag; TAG; end
      def self.tag; TAG; end
   end
   
   class Lis < ElementCollections
      include CommonCollection
      def element_class; Li; end
   
      def set_show_items
         super
         @show_attributes.delete( "name")
         @show_attributes.add( "className" , 20)
      end
   end
end
#END EXTEND
 

#TEST FOR LI EXTEND -- taken from P test cases
class TC_Lis < Test::Unit::TestCase
    include Watir
       
    def setup()
        $ie.goto($htmlRoot + "li.html")
    end
   
    def test_li
        assert($ie.li(:id, 'number1').exists?)
        assert($ie.li(:index, 3).exists?)
        assert($ie.li(:title, 'test_3').exists?)
       
        assert_false($ie.li(:id, 'missing').exists?)
        assert_false($ie.li(:index, 8).exists?)
        assert_false($ie.li(:title, 'test_55').exists?)
       
        assert_raises( UnknownObjectException) {$ie.li(:id , 'missing').class_name }
        assert_raises( UnknownObjectException) {$ie.li(:id , 'missing').text }
        assert_raises( UnknownObjectException) {$ie.li(:id , 'missing').title }
        assert_raises( UnknownObjectException) {$ie.li(:id , 'missing').to_s }
        assert_raises( UnknownObjectException) {$ie.li(:id , 'missing').disabled }
       
        assert_equal(  'redText' , $ie.li(:index,1).class_name)
        assert_equal(  'LI_tag_1' , $ie.li(:index,1).title)
        assert_equal(  'This text is in a li with an id of number2' , $ie.li(:index,2).text)
    end
   
    def test_li_iterator
        assert_equal( 3, $ie.lis.length)
        assert_equal( 'italicText', $ie.lis[2].class_name)
        assert_equal( 'number3', $ie.lis[3].id)
       
        count=1
        $ie.lis.each do |li|
            assert_equal('number'+count.to_s , li.id)
            count+=1
        end
        assert_equal( count-1 ,  $ie.lis.length)
    end
 
end
#END TEST
 
HTML for test file 'li.html'
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <title>Test page for Div</title>
 
<STYLE TYPE="text/css">
        DIV {font-size: 24pt;}
        .redText {color: red;}
        .blueText {color: blue;}
        .italicText {font-style: italic;}
 
        SPAN {font-size: 18pt;}
        .redText {color: red;}
        .blueText {color: blue;}
        .italicText {font-style: italic;}
 

        P{font-size: 24pt;}
        .redText {color: red;}
        .blueText {color: blue;}
        .italicText {font-style: italic;}
 

</STYLE>
 

</head>
<body>
CVS Revision: "$Revision: 1.5 $"
<br>
<br>
<ul>
<li id = number1 title = "LI_tag_1" class=redText >This text is in a li with an id of number1 and title of LI_tag_1</p>
<br>
<li id = number2 title = "LI_tag_2" class=italicText >This text is in a li with an id of number2</p>
<br>
<li id = number3 title = "test_3" class=blueText >This text is in a li with an id of number3</p>
</ul>
</body>
</html>


From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Bret Pettichord
Sent: Tuesday, July 11, 2006 5:03 PM
To: [email protected]
Subject: Re: [Wtr-general] Lists question

On 7/11/06, Attebery, Bill <[EMAIL PROTECTED]> wrote:

Has anyone written code to handle <UL>/<OL> and <LI> tags -- as a collection with iterators?
I have the following HTML -- and I'd like to be able to handle the LIs as a collection and iterate through them (and access the link within) based on the text -- has anyone done this?  If not I presume I'll have to extend watir and essentially clone the  Spans, Divs or Ps classes and methods?

Any suggestions?

I recommend extending Watir.

I've been steadily reducing the amount of code it takes to support a new tag in Watir. This shouldn't be very hard. It should take about 13 lines of code to add support for each each of these in 1.5 -- if you include "end" as a line of code.

I've recently been reading up about meta programming and suspect that i could rework Watir so that adding support for tags like these would only take three or four lines of code.

Bret

The content contained in this electronic message is not intended to
constitute formation of a contract binding TWTC. TWTC will be
contractually bound only upon execution, by an authorized officer, of
a contract including agreed terms and conditions or by express
application of its tariffs.

This message is intended only for the use of the individual or entity
to which it is addressed. If the reader of this message is not the
intended recipient, or the employee or agent responsible for
delivering the message to the intended recipient, you are hereby
notified that any dissemination, distribution or copying of this
message is strictly prohibited. If you have received this
communication in error, please notify us immediately by replying to
the sender of this E-Mail or by telephone.
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general

Reply via email to