Alternate way to switch to a window handle using Selenium Webdriver - ruby

I am testing an webview app containing multiple webviews/pages. To get the webviews I do:
$driver.window_handles
which returns me an array of window indexes of numbers something like ['1', '3', '5', '6', '7', '8']
Now if I want to switch to a specific window which contains text "My TITLE"; I have to iterate through each of the indexes in the array using loop and switch the window to that index and then check if the text is present in that window.
Eg:
$driver.switch_to.window('1') if 'My Title' not found; $driver.switch_to.window('3') and so on until I find the window.
I even tried to look for the title of the window/driver; but for some windows I am getting title as nil so wasn't a best way for me to find out the window.
Is there any other way I can try to get the required window?

You can take the below approach:
#create a array with window_id and its corresponding window page title
wnd_titl = driver.window_handles.map do |w|
driver.switch_to.window(w)
[w,driver.title]
end
#required window
win_id = wnd_titl.find { |e1,e2| e2 == 'My TITLE' }.first
driver.switch_to.window(win_id) #switched to the required window

Related

Ruby Shoes: how to replace button text with an image/icon

I am trying to write a small Shoes app which contains a button. For example
Shoes.app :title => "Buttons" do
button_next = button "Next"
button_prev = button "Previous"
end
Now, instead of the texts "Next" or "Previous" there should be icons on the buttons: a green arrow, showing to the right for the button_next and a green arrow showing to the left for the button_prev.
Of couse I already have the icons as .jpg-files but I can't figure out how to replace the text with the icons on the buttons.
For now I found out this way:
Shoes.app do
next_image = image "next.jpg", :width => 50, :height => 35
next_image.click do
alert "Hey!"
end
end
It works in the way that I can click the image and a new window, saying "Hey!" appears. But it is not ok, because I miss the typical button-appearance like changing of the color when hovering over it, or the press-down-effect when clicking on it.
So my question is: how can I create a real button in Ruby Shoes and replace the name of the button with an icon? Any ideas?

How to retrieve hidden elements when visibility is hidden

I want to retrieve hidden text when the visibiility attribute is hidden:
<div id = "tt52433002" class="yui-module yui-overlay yui-tt yui-overlay-hidden" style="z-index: 2; visibility: hidden;">
<div class="bd">Associated with the domain : testci20160503105556.com</div>
</div>
I tried:
browser.hidden(:class, 'bd').text
and
browser.hidden(:class, 'bd').value
But I get this error:
"unable to locate element, using {:class=>"bd", :tag_name=>"input", :type=>"hidden"}"
Watir is designed to act like a user. So if a user can not see the text in an element, then Watir will not return the text of the element.
Also, the element you are looking for is a div not a hidden.
If you need the text you can do:
browser.div(class: 'bd').inner_html
which makes a JavaScript call to provide the result.
This works:
browser.div.attribute_value('id') => tt52433002
as does this:
browser.div(class: 'bd').inner_html[/testci\d{14}/] => testci20160503105556
First things first. The error says that Watir cannot find an element using the criteria you specified. That means either that no such thing exists anywhere in the DOM, or that it might be inside a frame.
Since the element you want is a div, then you should be using the .div method to access it
browser.div(:class => 'bd') #finds first matching div
A potential second problem could occur if that classname is not very unique. Unless you specify an additional parameter, such as index, or perhaps a portion of the text contained by the div, you may not find the div you are looking for. A fast debugging trick (I like to do it from IRB) is to get a collection of matching divs and check the size
div_count = browser.divs(:class => 'bd').size
puts "there are #{divcount} divs of class bd in the dom"
If the count is anything more than 1, then you likely need to change how you are selecting it to ensure you get the right one. for example
browser.div(:class => 'bd', :text => /Associated with the domain/)
If the count is zero, then check for frames
frame_count = browser.frames.size
iframe_count = browser.iframes.size
If there are frames you will have to tell watir to look inside the frame for the div, if more than one frame then be sure you specify the right one
browser.frame.div(:class => 'bd') #looks for div inside first frame
Once you are sure you have the right div, then you ought to be able to use a method like .text or as in another answer .inner_html to get the contents of the div.

how to click on text area to send long text using ruby with selenium web driver

I tried xpath and id
getting `until': timed out after 10 seconds (no such element (Selenium::WebDriver::Error::TimeOutError)
I am using rubymine editor.
wait = Selenium::WebDriver::Wait.new(:timeout => 10)
wait.until { #driver.find_element(:xpath => "//*[#id='j_id0:pb:j_id35']") }
#driver.find_element(:xpath => "//*[#id='j_id0:pb:j_id35']").send_keys "test send sms"
Text area element is placed on bottom of the page.Do I need to scroll down the page and click and sendkeys in the text area.
in the below code I am trying to find nearest element to the text box and do scroll down later click on text area and sendkeys.But even it's not working..
#wait = Selenium::WebDriver::Wait.new(:timeout => 10)
#wait.until { #driver.find_element(:name => "j_id0:pb:j_id33") }
#scroll = #driver.find_element(:name => "j_id0:pb:j_id33")
#scroll.location_once_scrolled_into_view
Please help on this..
Thanks!!
I don't have idea on rudy but can help you logically..
1) first scroll the page from your code to the text area.
2) select the text area by id, xpath or etc. driver.findelement(by.id(...)).sendkey(..........);
Thanks Shukla!#sourabh shukla
Mine inside has frame so I need to switch to frame that is why I am not able ciclk that element!!
#driver.switch_to.frame("06618000000Crrp"#this is id);
Then my element found and entered text!!

scrollable window ncurses ruby

I created a class that I am trying to make to simulate richtextbox, sort of, on windows forms. this means when you add new data to the form/richtextbox it is added to the bottom of the box/window and the rest is scrolled up one line.
ive tried to enable scrollok(), but it does not seem to want to scroll. i am not sure if it's bugged or my way of implementing it is wrong.
class Textpad
attr_accessor :data, :name, :window
def initialize(name, height, width, startx, starty)
#data = []
#name = name
#height = height
#width = width
#startx = startx
#starty = starty
Ncurses.refresh
#window = Ncurses.newwin(height, width, starty, startx)
#window.scrollok true
#window.wrefresh
end
def add(packetid, username, message)
#data.push [Time.new.strftime('[%T]'), packetid, username, message]
#data.shift if #data.length > 500
end
def draw
Ncurses.init_pair(1, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK)
Ncurses.init_pair(2, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK)
Ncurses.init_pair(3, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK)
Ncurses.init_pair(4, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK)
#window.wclear
position = 0
#data.each do |timestamp, packetid, username, message|
case packetid
when '1005'
#window.mvwprintw(1*position, 1, "#{timestamp} «#{username}» #{message}")
#window.mvchgat(1*position, timestamp.length+2, 1, Ncurses::A_NORMAL, 3, NIL)
#window.mvchgat(1*position, timestamp.length+3+username.length, 1, Ncurses::A_NORMAL, 3, NIL) #colorize the symboles around the username
end
position += 1
end
#window.wrefresh
end
end
the problem would be inside my draw method of the Textpad class. i can fill the data array for the Textpad class with hundreds of entries but only the very top of the array gets written (until it reaches the bottom of the window) with no scrolling. Do i manually have to scroll the screen or something? from the documentation it says it should automatically scroll when the cursor reaches the bottom and another line is added.
To quote the man page:
The scrollok option controls what happens when the cursor of a window is moved off the edge of the window or scrolling region, either as a result of a newline action on the bottom line, or typing the last character of the last line. If disabled, (bf is FALSE), the cursor is left on the bottom line. If enabled, (bf is TRUE), the window is scrolled up one line...
What is happening in your code is that you are attempting to print outside the window which is an error, but an error that curses handles by not printing anything.
You can either print an new line when you get to the bottom of the window or once your reach the bottom of the window you can call #window.scroll.
Either way you will need to keep printing on the last line if you are explicitly setting the position.

Group text and images as a block with Prawn?

I'm trying to build a PDF from user-generated content and I have a chunk of information that should be grouped together. I know of the group method to make sure text all gets rendered together, but this doesn't seem to work with a mix of text and images. Is there something that can do this with Prawn, or do I need to try to calculate cursor position and manually linebreak?
Edit: For illustration of what I'm looking to do:
pdf = PDF::Document.new
20.times do
pdf.group do
pdf.text "Something"
pdf.image "path/to/image.jpg"
pdf.text Time.now.to_s
end
end
And I would expect to not ever have "Something" on one page and the image on the next, but that is what I see. Is there some way I can achieve what I want?
Okay, I've figured it out. Prawn does not seem to take the image height into account when grouping, but you can hack your way around it:
pdf.group do
pdf.text "Best regards, (...)"
pdf.image "#{Rails.root}/vendor/signature.jpg", {
:height => 30,
:at => [0, pdf.y.to_i - #bottom_margin]
}
pdf.move_down(35)
pdf.text " "
end
The trick is to use absolute positioning for the image, and move the text cursor down manually.

Resources