When using site_prism, how do I properly set_url so that I can use the rspec .to be_displayed matcher? - site-prism

My page model looks like this:
class ViewPage < SitePrism::Page
set_url "events{/event_id}/whosecoming"
element :flash_messages, 'div#flash_messages'
section :event_info, EventInfoSection, 'div#event'
iframe :map_iframe, MapIframe, '#map'
section :hasher_listing, HasherListing, 'div#hashers'
end
When I use
expect(ViewPage.new).to be_displayed
It does not match with an observed url of 'events/1/whosecoming'
Everything on the page loads fine, I believe I just don't understand how set_url should be used with paramerization.

Answer was in a nearby section of the Site Prism documentation.
The set url was correct, what I needed was to pass the parameter to the expectation.
expect(ViewPage.new).to be_displayed(event_id: 3)

Related

How can I change name of dynamically generated URL in Codeigniter

I'm having dynamic url for my each search result in Codeigniter, but I want to know how can I change my url. For example right now I'm having the url something like this:
www.xyz.com/vendor/vendor_details?iuL80rpoEMxCi89uK6rIyTgqCGuagQ+BUoUnvyBdx09EawMiFfnaB+q3Q8YyBSFwbOVw8+32ZInJrjE2I42teA==
but I want it like this:
www.xyz.com/Delhi/Balaji-Courier-And-Cargo-Bharat-Singh-Market-Opposite-B-7-Petrol-Pump-Vasant-Kunj/011P1238505881A9D9W7_BZDET?xid=RGVsaGkgSW50ZXJuYXRpb25hbCBDb3VyaWVyIFNlcnZpY2VzIEhhbWlsdG9uIFJvYWQ=
here the example
$route['vendor/(:any)/(:any)/(:any)'] = 'vendor/vendor_details/$3';
See this Documentation here: routing
You want to add
www.example.com/city/address/related_id
This Example can help you to get that.
Here are a few routing examples:
$route['journals'] = 'blogs';
A URL containing the word “journals” in the first segment will be remapped to the “blogs” class.
$route[blog/Joe'] = 'blogs/users/34';
A URL containing the segments blog/Joe will be remapped to the “blogs” class and the “users” method. The ID will be set to “34”.
$route['product/(:any)'] = 'catalog/product_lookup';
A URL with “product” as the first segment and anything in the second will be remapped to the “catalog” class and the “product_lookup” method.
$route['product/(:num)'] = 'catalog/product_lookup_by_id/$1';
A URL with “product” as the first segment and a number in the second will be remapped to the “catalog” class and the “product_lookup_by_id” method passing in the match as a variable to the method.

Posting data on website using Mechanize Nokogiri Selenium

I need to post data on a website through a program.
To achieve this I am using Mechanize Nokogiri and Selenium.
Here's my code :
def aeiexport
# first Mechanize is submitting the form to identify yourself on the website
agent = Mechanize.new
agent.get("https://www.glou.com")
form_login_AEI = agent.page.forms.first
form_login_AEI.util_vlogin = "42"
form_login_AEI.util_vpassword = "666"
# this is suppose to submit the form I think
page_compet_list = agent.submit(form_login_AEI, form_login_AEI.buttons.first)
#to be able to scrap the page you end up on after submitting form
body = page_compet_list.body
html_body = Nokogiri::HTML(body)
#tds give back an array of td
tds = html_body.css('.L1').xpath("//table/tbody/tr[position()>1]/td")
# Checking my array of td with some condition
tds.each do |td|
link = td.children.first # Select the first children
if link.html = "2015 32 92 0076 012"
# Only consider the html part of the link, if matched follow the previous link
previous_td = td.previous
previous_url = previous_td.children.first.href
#following the link contained in previous_url
page_selected_compet = agent.get(previous_url)
# to be able to scrap the page I end up on
body = page_selected_compet.body
html_body = Nokogiri::HTML(body)
joueur_access = html_body.search('#tabs0head2 a')
# clicking on the link
joueur_access.click
rechercher_par_numéro_de_licence = html_body.css('.L1').xpath("//table/tbody/tr/td[1]/a[1]")
pure_link_rechercher_par_numéro_de_licence = rechercher_par_numéro_de_licence['href']
#following pure_link_rechercher_par_numéro_de_licence
page_submit_licence = agent.get(pure_link_rechercher_par_numéro_de_licence)
body_submit_licence = page_submit_licence.body
html_body = Nokogiri::HTML(body_submit_licence)
#posting my data in the right field
form.field_with(:name => 'lic_cno[0]') == "9511681"
1) So far what do you think about this code, Do you think there is an error in there
2) This part is the one I am really not sure about : I have posted my data in the right field but now I need to submit it. The problem is that the button I need to click is like this:
<input type="button" class="button" onclick="dispatchAndSubmit(document.JoueurRechercheForm, 'rechercher');" value="Rechercher">
it triggers a javascript function onclick. I am triying Selenium to trigger the click event. Then I end up on another page, where I need to click a few more times.. I tried this:
driver.find_element(:value=> 'Rechercher').click
driver.find_element(:name=> 'sel').click
driver.find_element(:value=> 'Sélectionner').click
driver.find_element(:value=> 'Inscrire').click
But so far I have not succeeded in posting the data.
Could you please tell me if selenium will enable me to do what I need to do. If can I do it ?
At a glance your code can use less indentation and more white space/empty lines to separate the internal logic of AEIexport (which should be changed to aei_export since Ruby uses snake case for method names. You can find more recommendations on how to style ruby code here).
Besides the style of your code, an error I found at the beginning of your method is using an undefined variable page when defining form_login_AEI.
For your second question, I'm not familiar with Selenium; however since it does use a real web browser it can handle JavaScript. Watir is another possible solution.
An alternative would be to view the page source (i.e. in Firebug) and understand what the JavaScript on the page does. Then use Mechanize to follow the link manually.

Add default view for Rails Controller

I currently have no index views (app/views/mycontroller/index.html.erb), but would like to specify a default view if the user just types in: localhost:3000/mycontroller
Very new to RoR. My current config/routes.rb:
MyTestApp::Application.routes.draw do
get "team/thatguy"
get "home/about"
root :to => "home#about"
end
In my controller I've been playing around with redirects, but the desired solution is to only show the smaller uri localhost:3000/mycontroller.
app/controllers/home_controller.rb:
class HomeController < ApplicationController
def index
render "home/about"
end
def about
#title = "My Title"
end
end
Ok, assume you want to render a contact page when you access localhost:3000/mycontroller
What you want to do is:
Create a controller (C of MVC) with a contact action.
$ rails generate controller StaticPages contact
Excuting this code in your console creates some directories and files:
static_pages_contoller.rb with a contact action.
static_pages directory under the app/views directory with a view template called contact.html.erb
and you may have get 'static_pages/contact' in your config/routes.rb
Modify your routes:
Now you have access to the contact template with: localhost:3000/static_pages/contact
So to change the path, add a line to the routes.rb: match '/mycontroller', 'static_pages#contact', via: 'get'
Then you'll get your desired results.
I might be wrong, the whole process and details are here: http://ruby.railstutorial.org/ruby-on-rails-tutorial-book

How to set separate (or multiple) template paths for pages and partials in Mustache with Sinatra?

I set the template path:
class Mustache
self.template_path = 'templates/pages'
end
This works fine.
But, I want to store the partials in a separate folder: templates/partials.
A mustache template looks like:
Hello {{ name }}
Your info: {{> info }
info.mustache is a partial.
The info partial has no class, so I can't set self.template_path.
So, how to set a template path for a partial, or have multiple template paths?
self.template_path = 'templates'
self.template_path = 'templates/'
self.template_path = 'templates/*'
self.template_path = 'templates/**'
self.template_path = [ 'templates/pages', 'templates/partials' ]
did not work.
You can't. If you look at this file you see that only the template path can be set. Partials end up using the same path during rendering.
https://github.com/defunkt/mustache/blob/master/lib/mustache/settings.rb
However, if you name your partial {{>partials/info}}, it will look for info.mustache inside your template path(templates/partials/info.mustache).
You can also override the partial method from mustache.rb like this:
class Fubar < Mustache
def partial(name)
partial_name = "partials/#{name}"
super(partial_name)
end
end
In the end a partial is no different than any other template. The pathing that you want is just for organizational purposes.
Another user brought up a good point that mustache is now maintained. There was a point in time where the original contributor had stopped supporting it. I'm glad someone else had picked up the project.

Intermediate Ramaze Routing Help Please

Part 1:
I have a call to layout(:default){|path,wish| wish !~ /rss|atom|json/} but requests to /foo/bar.json seem to think wish is html and uses the layout anyway. How can I fix this?
Part 2:
I want to route /path/to/file.ext so that it calls the method to on the controller mapped to /path and uses ext when formulating the return. Is there a better (more elegant) way to do this than passing the 'file.ext' to the to method, parsing it, and doing cases? This question would have been more succinct if I had written, how does one do REST with Ramaze? There appears to be a Google Groups answer to this one, but I can't access it for some reason.
class ToController < Controller
map '/path/to'
provide( :json, :type => "application/json") { |action, val| val.to_json }
def bar
#barInfo = {name: "Fonzie's", poison: "milk"}
end
end
This controller returns plain JSON when you request /path/to/bar.json and uses the layout+view wrapping when you request /path/to/bar (Ramaze has no default layout setting, the layout in this example comes from the Controller parent class).

Resources