Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>With WATIR, minus the extraneous libraries, here's all it takes to accomplish what you've described (using the 'name' test case only). I've pulled it out of the function format since you already know how to do that, and this will be a clearer test case path.</p> <pre><code>require 'watir' @browser = Watir::Browser.new :firefox #open a browser called @browser @browser.goto "http://(your search page here)" #go to the search page @browser.text_field(:name =&gt; 'name').value = "Awesome" #fill in the 'name' field @browser.button(:name =&gt; 'submit').click #submit the form </code></pre> <p>If all goes well, we should now be looking at the search results. WATIR already knows it's on a new page - we don't have to specify a URL. In the case that the results are in a frame, we do need to access that frame before we can view its content. Let's pretend they're in a DIV element with an ID of "search_results":</p> <pre><code>results = @browser.div(:id =&gt; "search_results").text resultsFrame = @browser.frame(:index =&gt; 1) #in the case of a frame results = resultsFrame.div(id =&gt; "search_results).text </code></pre> <p>As you can see, you do not need to save the entire page to parse the results. They could be in table cells, they could be in a different div per line, or a new frame. All are easily accessible with WATIR to be stored in a variable, array, or immediately written to the console or log file.</p> <pre><code>@results = Array.new #create an Array to store our results @browser.divs.each do |div| #for each div element on the page if div.id == "search_results" #if the div ID equals "search_results" @results &lt;&lt; div.text #add it to our array named @results end end </code></pre> <p>Now, if you just wanted the top 5 <a href="http://www.ruby-doc.org/core-1.9.3/Array.html" rel="nofollow">there are many ways to access them</a>.</p> <pre><code>@results[0] #first element @results[0..4] #first 5 elements </code></pre> <p>I'd also suggest you look into a few programming principles like DRY (Don't Repeat Yourself). In your function definitions where you see that they share code, like opening the browser and visiting the same URL - you can consolidate those:</p> <pre><code>def search(how, what) @browser = Watir::Browser.new :firefox @browser.goto "(that search url again)" @browser.text_field(:name =&gt; how).value = what etc... end search("name", "Hilton") search("number", "555555") </code></pre> <p>Since we know that the two available text_field names are "name" and "number", and those make good logical sense as a 'how', we can parameterize them and use a single function for both the Search by Name and Search by Number test cases. This is more efficient, as long as the test cases remain similar enough to be shared.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload