Assertions with Selenium::Webdriver::Element object in Ruby - 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 :)

Related

Can't click on href download link inside dynamically changing table using Selenium web-driver

I can access all of the rows I need to, and their cells. I can even access the text of the href hyperlink cell to print it. But when I try xpath or other element finding strategies to '.click', none are working. This table is in an iframe which I switch to and am able to get all the table's info (although sometimes it does complain saying no connection to page or something.)
I have tried direct xpath, // searching whole html tree xpath and others. I even tried converting all my code to watir. Anyone know without showing all my code why this nested loop gives me the row and cell I need. But won't let me click that cell? What other methods can I use than '.text' to access values of that cell to click the anchor/href that maybe isn't xpath? Thanks!
Same as above.
# Iterate through all cells of the table
table.find_elements(:tag_name, "tr").each do |r|
if r.text.include? "#{Num}"
puts "*******************************************"
puts "cell found in table."
puts "*******************************************"
puts "Row text:"
puts r.text
r.find_elements(:tag_name, "td").each do |c|
puts "c.text:"
puts c.text
if c.text.include? "Excel"
c = c.find_element(:xpath, "/html/body/div/table/tbody/tr[36]/td[7]/a")
c.click
end
end
end
end
Just want to be able to access rows that have the text for the number entered and if that text exists anywhere in the row, go to the cell where the href is and click it.
This XPath should get you all the way to the anchor tag (without all the extra work)
"//table//tr/td[contains(text(),'#{num}')]/../td/a[contains(text(),'Excel')]"
What it does:
Looks for any cell in a row the contains the text of your num variable (//table//tr/td[contains(text(),'#{num}')])
Then selects that row (/..) [parent accessor]
finds the cell with an anchor tag where the text contains the word "Excel" (/td/a[contains(text(),'Excel')])
Now I don't use selenium but I am assuming that in theory the following should work:
table.find_element(:xpath,
"//tr/td[contains(text(),'#{num}')]/../td/a[contains(text(),'Excel')]").click

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.

Capybara/Selenium - Is there a way to click a link that was isolated but has no unique identifier or content?

I'm trying to click a link with Capyabara/Selenium.
It's a pin link on a Bing map, example can be found here.
I can iterate through the 161 links with the all(…).each:
within(".map") do
all("a.dealer-pin").each do |link|
click_link(link)
end
end
But the click_link(link) throws an error:
Capybara::ElementNotFound:
Unable to find link
#<Capybara::Node::Element tag="a"
path="/html/body/form/article/div/div/section/div[2]/div/div[2]/div/div[2]/div[2]/div[3]/div/a[1]">
Those links have neither a unique identifier nor a content (image tag) that is unique or has a unique identifier.
Like the error is hinting, each DOM element can be referenced by its XPath which is similar, but more specific, than standard CSS selectors. You can the Path within the Chrome inspector by right clicking on the specific node.
Be careful testing something that's too specific. It's likely going to be fragile and break often making a test that's hard to maintain.
Relevant capybara doc: http://www.rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Finders
Since you already have the link element you just have to call click on it - click_link finds links by their id, title, or text contents (also alt attribute of a nested img) - but you have already found them with a normal css query
within(".map") do
all("a.dealer-pin").each do |link|
link.click
end
end

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.

Adding Hyperlinks to created Bookmarks in a Word documnet using Ruby

How do you add a Hyperlink to a word document using an existing bookmark. I have been testing using IRB but continue to get Command Failed.
I have attached to a running word application have text selected that I want to tie to the hyperlink. For testing I have been trying to just add a google hyperlnk. I figure once I get that then I would be able to figure out the bookmark. This is the command I am using
doc.Hyperlink.add(word.selection, 'http://www.google.com', '','','text to display')
The two blank parms are for SubAddress and ScreenTip respectivly.
Luke-
You're very close...
Change this...
doc.Hyperlink.add(word.selection, 'http://www.google.com', '','','text to display')
...to this...
doc.Hyperlinks.add(word.selection.Range, 'http://www.google.com', '','','text to display')
There were two changes necessary:
(1) You call the Add method on the Hyperlinks (plural) collection, and (2) the first argument needs to be a Range object.
With these changes, your code works for me.

Resources