Nokogiri Mechanize Parsing Issue [closed] - ruby

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am using the Mechanize gem to parse through the webpage
http://www.opentable.com/rest_profile_menu.aspx?rid=116320&scpref=96&tab=1
To get the contents inside .menu class but it fetches only the name of the restaurant
When I type the commands:
require 'mechanize'
agent = Mechanize.new
agent.get("http://www.opentable.com/rest_profile_menu.aspx?rid=116320&scpref=96&tab=1").link_with(:text =>"Menu").click.search(".menu")
I get the output as:
"Sorry, we have no menus available for this restaurant at this time. Please check back later" ;where as I can see a lot of output in browser.
Please help me find a solution to get the contents.

The web page you're using uses an AJAX request to load the menu details.
Nokogiri and Mechanize do not support AJAX request pages.
I had the same situation in my project. I used phantomjs with Nodejs.

If you are trying to get the HTML contents of all elements with class "menu", then you might want to adjust your code a bit.
If you want to access the HTML response from the page in question, I find it easier to assign a variable to your get request:
page = agent.get("http://www.opentable.com/rest_profile_menu.aspx?rid=116320&scpref=96&tab=1")
.link_with(:text =>"Menu").click.search(".menu") may not be exactly what you want to use for "filtering through the HTML elements". Instead you want to use a method on your page.body string called parser.
object_mapped_html = page.parser
parser object maps all the HTML elements for you so that you can search through them with ease. I prefer using the css command, however I would recommend looking at the Nokogiri tutorial on how to parse through a HTML/XML webpage. An example of what you are looking to accomplish is this:
require 'mechanize'
agent = Mechanize.new
page = agent.get("http://www.opentable.com/rest_profile_menu.aspx?rid=116320&scpref=96&tab=1")
html_data_you_are_looking_for = page.parser.css("div.menu")
That div with the class you specified does have the HTML data as follows:
1.9.3-p429 :046 > page.parser.css("div.menu").text
=> "\r\n\t\t\t\t\t\t\t!AY CHIWOWA Menu\r\n\t\t\t\t\t\t\t\r\n\r\n\tSee the current menu for !AY CHIWOWA in Chicago.\r\n\tRestaurantProfileMenu.OnLoad({FailedText: \"Sorry, we have no menus available for this restaurant at this time. Please check back later.\"});\r\n\r\n\t\t\t\t\t\t"
I'm not 100% sure what you are trying to accomplish but I looked at the webpage with a web inspector and the class for a majority of the items on the menu are locu-menu-item or locu-menus. Perhaps if you searched through .locu-menus or .locu-menu-items you might find what you are looking for.

Related

How to use getoproperty in UFT [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
In my application when I click on save a reference no will be generated.it will keep on changing on every time application runs.I want to get this reference no in qtp report
Reference no is shown on a webelement.I stored the webelement in object repository with name 'reference'
My code is
Dim refernceno,test
test="001"
refernceno=browser("abc").page("abc"). Webelement ("reference"). Getroproperty ("reference")
Reporter.ReportEvent micpass,"Test passed"&test,"Reference"&refernceno
But the result shown is only Test passed 001
No reference no is showing
Kindly suggest how to use getroproperty
GetROProperty is used to fetch the set of properties defined by UFT from the object in the application. You can see these properties by using the object spy.
UFT allows you to access the Web DOM attributes by using the attribute/ syntax in GetROProperty (and other places).
Try:
refernceno = Browser("abc").Page("abc").Webelement("Reference").GetROProperty("attribute/reference")

Watir: Retrieve all dynamic HTML elements that match an attribute?

I am trying to scrape dynamic content with Watir and I am stuck.
Basically, I know that I can use
browser.element(css: ".some_class").wait_until_present
in order to scrape only when "some_class" is loaded.
The problem is that it is only giving me the first element having this class name and I want all of them.
I also know I can use
browser.spans(css: ".some_class")
in order to collect ALL the classes having this name, the problem is that I can't combine it with "wait_until_present" (it gives me an error). And spans on his own is not working because the content is not loaded yet, the page is using javascript
Is there a way to combine both? That means waiting for the class_name to be loaded AND select all the elements matching this class name, not just the first one?
I've been stuck for ages...
Thanks a lot for your help
There currently isn't anything in Watir for waiting for a collection of elements (though I had been recently thinking about adding something). For now, you just have to manually wait for an element to appears and then get the collection.
The simplest one is to call both of your lines:
browser.element(css: ".some_class").wait_until_present
browser.spans(css: ".some_class")
If you wanted to one-liner it, you could use #tap:
browser.spans(css: ".some_class").tap { |c| c[0].wait_until_present }
#=> Watir::SpanCollection
Note that if you are just checking the class name, you might want to avoid writing the CSS-selector. Not only is it easier to read without it, it won't be as performant.
browser.spans(class: "some_class").tap { |c| c[0].wait_until_present }

Following Test Automation best practise of "Methods return other PageObjects" in Ruby

I am a big advocate of the Page Object Pattern (POP) as defined by the experts at Selenium:
https://code.google.com/p/selenium/wiki/PageObjects
A key view of theirs that I have always followed when using Appium with Java is:
"Methods return other PageObjects"
e.g. LoginPage loginPage = homePage.gotoLoginPage();
I am now trying to following POP using Calabash with Ruby and so have been writing code like this:
e.g. #login_page = #home_page.goto_login_page
However, since Ruby doesn't know what type of object #login_page is or #home_page is, you dont get any of the benefits of intellisense showing what methods are available for a given page.
Anyone know a good way around this?
As much as I appreciate and apply PO design pattern, as much I disagree with returning page object by page object. Page object should be independent and don't need to know about other page objects. Look at two examples:
You test form validation. Click on submit button returns page object which is subsequent in the workflow, but in this case you remain on page with validation errors. Your page object won't know about it and will return the other page.
Page which you get to after clicking a button may differ depending on the context (e.g. from what other page you got to current page). It can lead to having multiple versions of actually same method, which will return different page objects depending on context. This is not good and overcomplicates simple thing.
If you want to return current page object, you can benefit from it e.g. in Java, when you return this at the end of the method. Then you can chain all methods you execute as long as you are on the same page. But when it comes to the question 'how to implement returning different page objects' - answer is simple - 'just don't'. Please note wiki entry you quoted has not been updated for a good while and best practices has evolved since it was originally published.
It seems like you already have your solution. However for others and perhaps also for you the x-platform approach to calabash uses page objects so you could check out that implementation https://github.com/calabash/x-platform-example
An alternative method would be as follows. Not as neat as I would like (given the need to manually create new instances of subsequent pages), but available as an alternative option:
When(/^I buy a movie from the movie page$/) do
movie_page = MoviePage.new
movie_page.buyMovie("Test Movie")
purchase_page = PurchasePage.new
purchase_page.confirmPurchase
end
Found a way of getting this to work after much research and applying well known Java/C#/Obj-c principles to Ruby:
Given(/^I am on the launch page$/) do
#launch_page ||= LaunchPage.new
end
When(/^I open the set alarm time page$/) do
#set_alarm_page = #launch_page.goto_set_alarm_page
end
When(/^I open our apps from the home page$/) do
#launch_page.navigation_toolbar.open_our_apps
end
Then(/^I should see the homepage alarm time is (\d+)$/) do |alarm_time|
alarm_time_actual = #launch_page.get_alarm_time
assert_equal(alarm_time, alarm_time_actual)
end
As long as somewhere on the step definition class you explicitly create a new page object (in the above example: LaunchPage.new), then all subsequent pages will provide intellisense method/property values, since the resulting page types returned will be known by RubyMine.

How can I find the page object of the page watir is currently on?

Context:
I'm trying to make reusable step definitions that click on page objects on the current page,
e.g. (cucumber step def follows):
When(/^the user clicks the "([^"]*)" button$/) do |button|
click_button = button.downcase.gsub(" ","_")
#current_page #somehow get current page object on this line
#current_page.click_button
end
Problem statement:
I can't find anything that returns the current page object.
An explanation for why the obvious solution didn't work:
I thought #current_page was already there as something I could use. I looked in the source code for page object, and the variable #current_page does exist. Not sure how to use it if I can...
BTW, in this case, I have a bunch of testers that can write Gherkin but not necessarily step definitions. We are trying to rapidly finish a bunch of regression tests for an in house app with an unchanging interface.
This is somewhat at odds with what page-object is trying to provide.
Page object attempts to provide well named actions for interacting with a specific page. If you are wanting to make something that works in general against any page, it will be much easier to write it with watir-webdriver directly.
That said, I agree that a specification based heavily on implementation like that is likely to change. I also would add that it doesn't add much value. I would only continue down this path if you understand and accept that you are using cucumber as a test templating tool instead of a requirements communication tool.
As Justin Ko mentioned, #current_page gets set when you call the on or visit methods. Its not a good idea to lump something that changes the page object in a step that performs a specific action (in this case clicking a button). You might want a different step that indicates the behavior of the application, such as
the application lands on the <your page> page
Then you're can use the name of the page object class to load #current_page via the on method in that step definition. This also gives the benifit (or curse of having your step having more lower level details) of indicating expected page navigation behavior.

Ruby DataMapper: How can I do pagination?

I am going to retrieve a list of objects.
get "/todoitems/?" do
debugger
todo = Todolist.all
todo.to_json
end
Is there example that can retrieve page by page?
Many thanks.
Here's a gem dm-pagination which provides pagination support for Datamapper
I also found dm-paginator
This should point you in the right direction:
http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec-pagination
Essentially, there's a gem called will-paginate that takes care of sorting things in a page by page structure. There's an example included in the link

Resources