Sunteți pe pagina 1din 22

Read Watirways | Leanpub

https://leanpub.com/watirways/read

1 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

2 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub <div class="city" id="Kitchener"> <div class="street" id="MainStreet"> <div class="house" data-name="SmithFamily" data-numb er="1"> <span class="resident">Ronald</span> <span class="resident">Alice</span> </div> <div class="house" data-name="WilsonFamily" data-num ber="2"> <span class="resident">Henry</span> <span class="resident">Mary</span> </div> </div> <div class="street" id="CedarStreet"> <div class="house" data-name="ChanFamily" data-numbe r="1"> <span class="resident">Blake</span> <span class="resident">Terra</span> </div> <div class="house" data-name="SmithFamily" data-numb er="2"> <span class="resident">George</span> <span class="resident">Coraline</span> </div> </div> </div>

https://leanpub.com/watirways/read

3 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

browser.div

:property1 => 'value1', :property2 => 'value2'

browser.div(:id => 'my_id')

browser.div(:class => 'enabled', :text => 'Navigate', :n ame => /navigate/)

4 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

browser.div(:id => 'my_id')

browser.div(:id => /my_id/)

browser.div

5 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub browser.divs

https://leanpub.com/watirways/read

<ul id="my_id"> <li>Item 1</li> <li>Item 2</li> </ul>

browser.ul

6 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

<li class="lastMove"> <div id="81ae2" class="folder"> <small onclick="someFunction1()"> </small> </div> </li>

7 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub browser.element(:tag_name => 'small')

https://leanpub.com/watirways/read

<div id="divs_id" name="divs_name" class="divs_class">te xt</div>

browser.div(:attribute_name => "attribute_value")

<div id="phone_number"> <span id="area_code">123</span> <span id="exchange">456</span> <span id="subscriber">7890</span> </div>

browser.span(:id => "exchange").text #=> "456"

browser.span(:id => /area/).text #=> "123"

8 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

<span class="number">123</span> <span class="bold number">456</span> <span class="bold number">7890</span>

browser.span(:class => 'number').text #=> "123"

browser.span(:class => /bo/).text #=> "456"

<div data-height="30cm" data-weight="9kg">Owen</div> <div data-height="20cm" data-weight="7kg">Izzy</div>

9 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub browser.div(:data_height => '20cm').text #=> "Izzy"

https://leanpub.com/watirways/read

browser.div(:data_weight => /9/).text #=> "Owen"

<div> <span customattribute="custom1">Custom Attribute 1</ span> <span customattribute="custom2">Custom Attribute 2</ span> <span customattribute="custom3">Custom Attribute 3</ span> </div>

browser.span(:css, 'span[customattribute]').text #=> "Custom Attribute 1"

browser.span(:css, 'span[customattribute="custom2"]').te xt #=> "Custom Attribute 2"

browser.span(:xpath, '//span[@customattribute="custom2"] ').text #=> "Custom Attribute 2"

<div>This is the text of the element.</div>

10 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub browser.div(:text => 'This is the text of the element.')

https://leanpub.com/watirways/read

browser.div(:text => /text of the element/)

<span id="container">This line has <span id="inner">inne r</span> text</span>

browser.span(:text => 'This line has inner text').exists ? #=> true

browser.span(:text => /inner/).id #=> "container"

<label for="first_name">First Name:</label> <input type="text" id="first_name" name="first_name" />

browser.text_field(:label => 'First Name:')

browser.text_field(:label => /Name/)

11 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

<div> <a href="http://www.watir.com">Watir</a> <a href="http://watirwebdriver.com">Watir-Webdriver</a > </div>

browser.link.text #=> "Watir"

browser.link(:index => 0).text #=> "Watir"

browser.link(:index => 1).text #=> "Watir-Webdriver"

browser.divs(:text => /Watir/, :index => 1).length #=> Watir::Exception::MissinWayOfFindingObjectExcepti on

browser.div(:css => 'div#my_id')

browser.div(:xpath => '//div[@id="my_id"]')

12 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

<span>Project Managers:</span> <ul id="project_managers"> <li>Alicia Mcbride</li> <li>Jake Woods</li> </ul> <span>Developers:</span> <ul id="developers"> <li>Jack Greer</li> <li>Cora Williamson</li> <li>Albert Gross</li> </ul> <span>Testers:</span> <ul id="testers"> <li>Owen Francis</li> <li>Luis Leonard</li> </ul>

browser.ul(:id => 'testers').li.text #=> "Owen Francis"

13 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

<ul> <li id="ctl00_EnabledComponentListItem"> <a id="ctl00_Component" data-componentcode="ItemA" h ref="#">Item A</a> <a href="#">X</a> </li> <li id="ctl01_EnabledComponentListItem"> <a id="ctl01_Component" data-componentcode="ItemB" h ref="X">Item B</a> <a href="#">X</a> </li> <li id="ctl02_EnabledComponentListItem"> <a id="ctl02_Component" data-componentcode="ItemC" h ref="#">Item C</a> <a href="#">X</a> </li> </ul>

browser.lis(:id, /EnabledComponentListItem/).find do |li | li.a(:data_componentcode, 'ItemC').exists? end

browser.a(:data_componentcode, 'ItemC').parent

browser.li(:xpath, "//li[.//a[@data-componentcode='ItemC '']]")

14 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

<table> <tbody> <tr> <th>Creature</th> <th>Colour</th> </tr> <tr> <td>Elf</td> <td>Blue</td> </tr> <tr> <td>Dragon</td> <td>Green</td> </tr> <tr> <td>Imp</td> <td>Black</td> </tr> </tbody> </table>

#Get the unique element unique_element = browser.table.td(:text => 'Dragon') #Get the parent element parent_element = unique_element.parent #Get the actual element parent_element.td(:index => 1).text #=> "Green"

15 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

parent_row = browser.table.trs.find do |tr| tr.td(:text => 'Dragon').present? end parent_row.td(:index => 1).text #=> "Green"

<div class="parent"> <span class="child">Target</span> </div>

child = browser.span(:class => 'child') parent = child.parent parent.tag_name #=> "div"

<html> <body> <h1>Heading 1</h1> <ul> <li>Item 1a</li> </ul> <h1>Heading 2</h1> <ul> <li>Item 2a</li> <li>Item 2b</li> </ul> </body> </html> 16 of 22 4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

h1 = browser.h1(:text => 'Heading 2') ul = h1.element(:xpath => './following-sibling::*') puts ul.lis.length #=> 2

h1 = browser.h1(:text => 'Heading 2') ul = h1.element(:xpath => './preceding-sibling::*[1]') puts ul.lis.length #=> 1

start_h1 = browser.h1(:text => 'Heading 2') end_h1 = start_h1.h1(:xpath => './preceding-sibling::h1[ 1]') puts end_h1.text #=> "Heading 1"

h1 = browser.h1(:text => 'Heading 2') ul = browser.ul(:ole_object => h1.document.nextSibling) puts ul.lis.length #=> 2

h1 = browser.h1(:text => 'Heading 2') ul = browser.ul(:ole_object => h1.document.previousSibli ng) puts ul.lis.length #=> 1

17 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

<div>This is the text of the element.</div>

browser.div.text #=> "This is the text of the element."

<span><a href="watir.com">Watir</a> is a Ruby gem.</span >

browser.span.text #=> "Watir is a Ruby gem."

<div> <span>visible text</span> <span style="display:none;">hidden text</span> </div>

browser.div.text #=> "visible text"

<div style="color:red;">Error</div>

18 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

<div class="error_message">Error</div>

browser.div.style('color') #=> "rgba(255, 0, 0, 1)"

browser.div.document.currentStyle.color #=> "red"

browser.div.exists? browser.div.visible? browser.div.present?

<body> <div id="1" style="block:display;">This text is visibl e</div> <div id="2" style="block:none;">This text is not visib le</div> </body> 19 of 22 4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

20 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub Watir::Exception::UnknownObjectException: Unable to locate element, using {:tag_name=>["div"]} from ../lib/watir-classic/element.rb:66:in `assert_exi sts' from ../lib/watir-classic/element.rb:125:in `text' from (irb):5 from C:/Ruby193/bin/irb:12:in `<main>'

https://leanpub.com/watirways/read

browser.frame.div.text

browser.div.when_present.text

browser.div(:id, 'static_rand5').text

browser.div(:id, /static/).text

b.window(:title => /window title/) do b.div(:id => 'div_in_pop p').text end

21 of 22

4/28/2014 5:55 AM

Read Watirways | Leanpub

https://leanpub.com/watirways/read

browser.b tton.clic!

browser.lin!.clic!

22 of 22

4/28/2014 5:55 AM

S-ar putea să vă placă și