eunice
eunice

Reputation: 85

How can I call a div class under div in watir?

I am creating automated test scripts with Ruby and Watir.

I am trying to call a div class from these lines of codes:

<div class="slimScrollDiv" style="position: relative; overflow: hidden; width: auto; height: 500px;">

  <div class="modal-body" style="overflow: hidden; width: auto; height: 500px;">
    <form id="user-form" class="form-inline" enctype="multipart/form-data" method="POST" novalidate="novalidate">
        <fieldset></fieldset>
    </form>
  </div>

My aim is to use this one browser.div(:class => '').send_keys :space because I want to scroll the div, for me to be able to send_key "" in some other textfield hidden. I even try to use browser.scroll.to :bottom, but still it doesn't work.

Upvotes: 0

Views: 2169

Answers (2)

Chuck van der Linden
Chuck van der Linden

Reputation: 6660

So I am making some guesses as to what you are really trying to accomplish here. I'll first address your question as originally stated, then address how to do what I think you are trying to accomplish.

Selecting elements inside other elements The high level answer is that there is nothing special to doing this, in many (most) cases you don't even need to worry about how deeply nested an element is, as long as you have a unique way to identify it, such as a class, name, etc that is unique.. For example in the code you provided, to get the div with class modal-body you would just do

browser.div(:class => 'modal-body)

If there were a lot of divs on the page with that class, then you might want to locate the one you want by looking inside some other container element that is unique so you find the right one.. in the code above that would be something like

browser.div(:class => 'slimScrollDiv').div(:class => 'modal-body)

Usually you only do that sort of thing when it is difficult to find an easy way to identify an element because it has few or no attributes, or non-unique attributes.

Scrolling a custom control This I think is your real issue. You appear to have a control with a non-standard scrollbox. That is to say that instead of being simple HTML that is defined so that the browser creates and controls a scroll box (which would normally work with the methods you have already tried, or even get scrolled into view auto_magically) you instead have a custom control where the scroll box is driven by javascript code such as jQuery or Bootstrap. Such code usually is event driven, looking for things like mousewheel movement when the mouse is over the control, or manually grabbing and dragging the element that is being rendered to look like a scrollbar. Often such controls do NOT respond to keypresses like space or arrows, EVEN if they have been selected, clicked on, have focus etc.

Still I would first suggest try using .location_once_scrolled_into_view on the sub element you want that is inside the scrollbox. (See example in other answer) If that does not work then try manipulating the controls of the sçroll area.

Based on class values in your code fragment, I think you may be dealing with a jQuery powered 'slimScroll' control similar to those shown on this page. If that is indeed the case, then you can scroll the control by using drag methods on the scrollbar element. In slimScroll the element you want will be inside the div with class 'slimScrollDiv' and is another div with the class 'slimScrollBar'.

As long as the scrollbar is visible, you should be able to do a .drag_and_drop_by on it to scroll the content in the scrollbox. If it is not visible (as can happen in the first example on the linked page) then it may be harder, you might have to try firing mouseover events, or invoking some JS code to change it's style to not be hidden. You will need to experiment to see how many pixels to drag it at a time to scroll the window a 'right' amount based on how much content is in there. (potentially you might be able to figure that out based on the .style 'height' values for the elements, but that would take experimentation with the actual page and only needed if the amount of content in the box is variable)

For the second example on the linked page, I was able to scroll things using the following code

scrollbar = browser.div(:class => 'slimScrollBar', :index => 1)
scrollbar.drag_and_drop_by 0,10

I'm not sure what is inside your scrollbox, but for psuedo-code purposes of we just call it 'thing_I_want' then you might be able to scroll it into view with code something like this

scrollbar = browser.div(:class => 'slimScrollBar')
until thing_I_want.present?
  scrollbar.drag_and_drop_by 0,5
end

as I said you might have to play with the drag distance there to get a 'right' amount, but 5 (pixels) seemed a reasonable starting place.

Upvotes: 2

SysTest RJR
SysTest RJR

Reputation: 259

hidden text_field or just not visible in page?

you could try focus:

browser.text_field(:id => 'myid').focus

or if you need to scrolldown until it is visible

unless browser.text_field(:id => 'myid').visible?   
  browser.div(:class => 'slimScrollDiv').send_keys :arrow_down
end

or if you need to scrolldown to the footer (end of page)

 browser.div(:id => 'footer').wd.location_once_scrolled_into_view

Upvotes: 1

Related Questions