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.
Related
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)
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 :)
I want to click a link with Mechanize that I select with xpath (nokogiri).
How is that possible?
next_page = page.search "//div[#class='grid-dataset-pager']/span[#class='currentPage']/following-sibling::a[starts-with(#class, 'page')][1]"
next_page.click
The problem is that nokogiri element doesn't have click function.
I can't read the href (URL) and send get request because the link has onclick function defined (no href attribute).
If that's not possible, what are the alternatives?
Use page.at instead of page.search when you're trying to find only one element.
You can make your selector simpler (shorter) by using CSS selector syntax:
next_page = page.at('div.grid-dataset-pager > span.currentPage + a[class^="page"]')
You can construct your own Link instance if you have the Nokogiri element, page, and mechanize object to feed the constructor:
next_link = Mechanize::Page::Link.new( next_page, mech, page )
next_link.click
However, you might not need that, because Mechanize#click lets you supply a string with the text of the anchor/button to click on.
# Assuming this link text is unique on the page, which I suspect it is
mech.click next_page.text
Edit after re-reading the question completely: However, none of this is going to help you, because Mechanize is not a web browser! It does not have a JavaScript engine, and thus won't (can't) execute your onclick for you. For this you'll need to use Ruby to control a real web browser, e.g. using Watir or Selenium or Celerity or the like.
In general you would do:
page.link_with(:node => next_link).click
However like Phrogz says, this won't really do what you want.
Why don't you use a hpricot element instead? Mechanize can click on a hpricot element as long as the link has a 'src' or 'href' attribute. Try something along these lines:
page = agent.get("http://www.example.com")
next_page = agent.click((page/"//your/xpath/a"))
Edit After reading Phrogz answer I also realized that this won't really do it. Mechanize doesn't support Javascript yet. With this in mind you have 3 options.
Use a library that controls a real web browser. See #Phrogz answer.
Use Capybara which is an integration testing library but can also be used as a stand alone crawler. I've done this successfully with HTMLUnit which is a also an integration testing library in Java. Capybara comes with Selenium support by default though it also supports Webkit via an external gem. Capybara interprets Javascript out of the box. This blog post might help.
Grok the page that you intend to crawl and use something like HTTPFox to monitor what the onclick Javascript function does and replicate this in your Mechanize script.
Good luck.
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
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.