How can I find and print all existed window titles using Radrails? - window

I use watir in radrails IDE.
How can I find and print all existing window titles?

I have only been able to get all windows using win32ole. Note that this only works for IE (which I assume you are using since the question's tag is watir not watir-webdriver).
The following is an example of outputting all titles:
require 'win32ole'
WIN32OLE.new('Shell.Application').Windows.each do |window|
if window.path =~ /Internet Explorer/
puts window.Document.Title
end
end

Related

How to mark a certain part of the text in watir?

Hello is there something that can only mark a certain part of the text?
I can not find the right solution anywhere.
I tried: double_click, flash, select_text didn't work for me.
This works, but this mark everything : browser.send_keys [:control, 'a']
I added picture of example, what i want to do.
Thank you for your answers.
The red rectangle shows the markings
You can use the Element#select_text method. Note that prior to Watir 6.8, you will need to manually include the extension (method).
Here is a working example using the Wikipedia page:
require 'watir'
require 'watir/extensions/select_text' # only include this if using Watir 6.7 or prior
browser = Watir::Browser.new
browser.goto('https://en.wikipedia.org/wiki/XPath')
browser.body.select_text('XPath may be used')
sleep(5) # so that you can see the selection
Note that this will highlight the first match. You may want to restrict searching a specific element rather than the entire body.
Here is another example using ckeditor.com:
require 'watir'
require 'watir/extensions/select_text' # only include this if using Watir 6.7 or prior
browser = Watir::Browser.new
browser.goto('ckeditor.com/')
frame = browser.iframe(class: 'cke_wysiwyg_frame')
frame.p.select_text('Bake the brownies')
browser.link(href: /Bold/).click
sleep(10)

xpath selector working firefox not in ruby script

I am trying to test a simple scenario, but the xpath doesn't seem to be available, for once I was successful but again kept failing after that for the same xpath.
Please lemme know what is wrong here. Both the xpaths work in firepath, and the first one even works in the script always.
require 'selenium-webdriver'
browser = Selenium::WebDriver.for :firefox
Given(/^I am on the TRU home page$/) do
browser.get("https://www.toysrus.com")
end
When(/^I search for a product$/) do
browser.find_element(:name,"keyword").send_key("toys")
browser.find_element(:xpath,"/html/body/div[4]/div[1]/div/div/div/nav/div/div[2]/div[6]/div").click
end
And(/^Click on first product$/) do
browser.find_element(:xpath,"//div[2]/div/div[2]/div[2]/div/div[2]/div[4]/div/div[2]/div[2]/div[1]/a/div[2]/div[1]/div[2]").click
end
Then(/^Take me to PDP$/) do
pending
end
Here is the Answer to your Question:
Instead of absolute xpath you can consider constructing logical xpath. Here are the xpath which you can use:
Passing the text toys within Search:
browser.find_element(:xpath,"//input[#name='keyword']").send_key("toys")
Clicking on Search button:
browser.find_element(:xpath,"//div[#class='search-icon-tru']").click
Click on first product:
browser.find_element(:xpath,"//div[#class='product-item__product-title'] [contains(.,'The Peanut Shell Bella Elephant Plush')]").click
Let me know if this Answers your Question.

Assertions with Selenium::Webdriver::Element object in Ruby

I'm very new to using Selenium Webdriver (and Cucumber), but they're skills I'll need for a position that I'm very interested in at the moment, so I'm really trying to get a handle on how they work.
Here's the section of the feature I'm currently trying to test (using Selenium in conjunction with Cucumber):
Background:
Given I am on the Shoe Store's home page
Scenario: Display New Releases for January
When I click on the link "January"
Then I should see a small blurb for each shoe
And I should see an image for each shoe
And I should see a suggested price for each shoe
And here are the relevant steps:
When(/^I click on the link "(.*?)"$/) do |month|
step %[I click on link having text "#{month}"]
end
Then(/^I should see a small blurb for each shoe$/) do
blurbs = $driver.find_elements(:class_name, 'shoe_description')
if blurbs
blurbs.each do |blurb|
# Need to assert that blurb elements exist / have text
end
end
end
The second step is where I can't seem to find a clear answer. If I throw in a binding.pry I can see that I have all the objects I need to iterate through (blurb is a single webdriver object, and when I call blurb.text it shows the exact text that I'd like to assert exists).
It seems like this should be simple.
One simple solution would be to just fail if the blurb does not have text. That would make your blurb loop into the following:
blurbs.each do |blurb|
fail 'blurb contains no text' if blurb.text == ''
end
This will fail out the step and the scenario if the text is empty. You could also expand this to checking that the text matches expected text by comparing the text to the correct value.
One simple assert will do the trick, you can verify that blurb.text is not blank like:
Then(/^I should see a small blurb for each shoe$/) do
blurbs = $driver.find_elements(:class_name, 'shoe_description')
if blurbs
blurbs.each do |blurb|
# Need to assert that blurb elements exist / have text
blurb.text.should_not eq ""
end
end
end.
Hope this helps :)

How to get the position of text using 'PDF-Reader' gem in Ruby

I am new to Ruby and we are using Ruby Selenium framework for automating the PDF verification testing.
I want to verify the content of PDF, like text and also get the position of the text. Along with that I also need to get the text at a given position.
Something like this maybe
require 'pdf-reader'
require 'open-uri'
reader = PDF::Reader.new(open("SAMPLE_URL")) # my resume pdf
page = reader.pages.first
lines = page.split("\n")
text_match_line_numbers = [0...lines.length].select do |i|
lines[i] .include? "text"
end
Look at their docs here, there are more advanced options for navigating the PDF page.

How to implement Watir classes (e.g. PageContainer)?

I'm writing a sample test with Watir where I navigate around a site with the IE class, issue queries, etc..
That works perfectly.
I want to continue by using PageContainer's methods on the last page I landed on.
For instance, using its HTML method on that page.
Now I'm new to Ruby and just started learning it for Watir.
I tried asking this question on OpenQA, but for some reason the Watir section is restricted to normal members.
Thanks for looking at my question.
edit: here is a simple example
require "rubygems"
require "watir"
test_site = "http://wiki.openqa.org/"
browser = Watir::IE.new
browser.goto(test_site)
# now if I want to get the HTML source of this page, I can't use the IE class
# because it doesn't have a method which supports that
# the PageContainer class, does have a method that supports that
# I'll continue what I want to do in pseudo code
Store HTML source in text file
# I know how to write to a file, so that's not a problem;
# retrieving the HTML is the problem.
# more specifically, using another Watir class is the problem.
Close browser
# end
Currently, the best place to get answers to your Watir questions is the Watir-General email list.
For this question, it would be nice to see more code. Is the application under test (AUT) opening a new window/tab that you were having trouble getting to and therefore wanted to try the PageContainer, or is it just navigating to a second page?
If it is the first one, you want to look at #attach, if it is the second, then I would recommend reading the quick start tutorial.
Edit after code added above:
What I think you missed is that Watir::IE includes the Watir::PageContainer module. So you can call browser.html to get the html displayed on the page to which you've navigated.
I agree. It seems to me that browser.html is what you want.

Resources