How to get URL while navigating to other site - ruby-on-rails

I am developing a test where I need to validate the URL while its navigating to the retailer's site
For instance, when I click the Product, it navigates to retailer's page. I need to capture the URL when it is navigating to the retailer's page but it should not be of the previous page & even not of the retailer's page.
Is there any method in Ruby or Capybara to get that URL? I have tried to implement this method but it's not giving the correct URL.
url = URI.parse(current_url)
def validate_url(url)
browser= Capybara.current_session.driver.browser
puts "current URL = #{browser.current_url}" if #verbose
assert_includes browser.current_url, url
end
#Actual Output url =http://www.shopstyleqa.com/browse/womens-clothes
#Expected Output url=http://www.shopstyleqa.com/action/loadRetailerProductPage

Since Capybara is designed to emulate a users use of a browser it doesn't report on redirects so there is no way to get the intermediate url if the browser is being redirected to a different one. You could verify the original URL that is going to be requested if it's in a links href attribute or a data attribute on whatever element represents the project, etc.
On a side note comparing current_url with eq or includes is a recipe for flaky tests. Instead you should use the assert_current_path assertion which uses Capybaras waiting/retrying behavior - In your case it would be something like
assert_current_path("http://www.expected.com/expected_url", url: true)

Related

What is the meaning of # in this url - http://projectname/controller/viewname#

On clicking the submit button of a asp.net mvc webpage, it does not show any change other than #sign succeeds my webpage url like this - http://projectname/controller/viewname# Why?
It refers to a specific anchor in the source page.
I wouldn't worry about it

In an iframe, build url from parent page hostname and new params

I have two sites which communicate with each other. I'll call them mainsite.com and backend.com for this example.
mainsite.com is the site that people go to, and this loads an iframe which loads in backend.com, which is a Rails site that handles all of the login & signup stuff. At the end of the login process, backend.com generates a url which has an encrypted set of params which are used to log the user into mainsite.com.
The way that this is currently working is that backend.com controller generates a url like this "http://mainsite.com?auth=2347qweiuyqi" and saves it in a variable called #redirect_url, then (in the iframe) renders out some html which contains some javascript to redirect the parent page:
<script type="text/javascript">
$(function(){
window.top.location.href = "<%= #redirect_url %>";
});
</script>
This is working fine. However, we have a new situation where people can now access mainsite.com via a load of different urls (all subdomains on other sites, like mainsite.foo.com and mainsite.bar.com)
Now, when i redirect them to the auth url on mainsite, i want them to stay on whichever domain they're on. So, if they're on mainsite.foo.com, and loading backend.com in the iframe, then at the end of the login process i want to redirect the parent window to mainsite.foo.com?auth=2347qweiuyqi. Similarly, if they're on mainsite.bar.com they should be redirected to mainsite.bar.com?auth=2347qweiuyqi
My initial attempt at this was to change the javascript to get the domain name from the parent window, and build the redirect url by adding the params onto the end, then redirecting to this url. However, the iframe can't access the parent domain name because of cross-domain protection (ie the domains, "mainsite.foo.com", and "backend.com", don't match.
My next attempt was to have a javascript function on mainsite.com which takes the auth param and redirects to a url consisting of the current domain with that param added onto the end. But, i can't call a parent window function because of the same cross-domain restrictions.
How can I get past this? I have complete control over the content of both sites.
One solution which i know would work, but is a bit of hassle to set up, is to pass through the mainsite domain name through as a parameter in the src attribute of the iframe to backend.com, and then make sure that backend.com hangs on to that through the whole login/signup process, so that at the end of the process it can use it to build the whole url to redirect to. But, it feels like there should be a simpler way.
thanks, Max
EDIT - i've solved this by, when the first request comes through from mainsite.com to backend.com, saving the hostname of request.referer into the session, and then, later on when i come to do the redirect, looking it up from the session and adding the auth params on. This works but it feels a bit unsatisfactory so i'm interested in nicer solutions.

JSF login forward address bar / url -- redirect does not solve problem

Okay we have a single - sign - on and the user will likely enter www.blabla.com/AppName/ to reach our site. We then define a welcome site, use a phaselistener to check:
is user trying to access the welcome site? yes -> try to login - works? yes -> get user roles -> forward to the appropriate site for this specific user.
E.g. user niceBelly goes to page /somewhere/in/many/folders/beer.jsf and user barbie goes to /breasts/pink.jsf
a redirect is in this application not possible for some reasons.
the result is that when reaching e.g. page pink.jsf the address bar still shows blablaba.com/AppName/
clicking the first link will result in the browser using the form address as new URL e.g. on welcome.jsf i navigate to coolstuff.jsf. On the page coolstuff i now have the url of the last form, e.g. welcome.jsf. Then on cool stuff i click a link, and get coolstuff on the next page as url, and so on.
Is there a way to solve this / work around it?
Given the symptoms, you are actually not redirecting the requests, but you are actually forwarding the requests. A real redirect will take place when you call
externalContext.redirect(url);
in JSF context, or when you add
<redirect />
to the navigation case. All other ways are effectively forwards. As per the symptoms, you're using commandlinks instead of outputlinks to navigate to other page. Commandlinks will submit a POST request to current URL and JSF will under the covers use RequestDispatcher to set the destination of the request/response when the navigation case doesn't contain <redirect />. A forward does not instruct the browser to fire a new GET request on the destination URL and hence the URL in browser address bar does not change. A real redirect will do exactly that.
See also:
When should I use outputlink instead of commandlink?
Bookmarkable URLs in JSF 1.x

Ruby: get address of the redirect when posting a page using Restclient

Hi I was wondering if this is possible, this is my scenario, I have a stand alone file that tries to get information from pages using RestClient and Nokogiri
I need to get the information of all the videos available on a page "http://www.koldcast.tv/" so far I havent found a way to get these results on a HTML page (no flash) other than tricking the search page into returning the whole list back using 3 underscores as the search keywords, the problem is that the search is doing a post to a page which I assume is then redirecting to the final page and it gives you something like this "http://www.koldcast.tv/index.php/landingpage/search_results/921c6b6e491005d91d117b0fa88f31d1/" the problem there is that this url is only alive for 5 or 10 minutes so I cannot use this url everytime i need to run the stand alone file the search is in a form with a post to "http://www.koldcast.tv/index.php" which I imagine takes all the data from that form (there is some other hidden fields) and then redirects to that results page is there a way I can do the post with all the data and then get the page that is being redirected
I thank you for taking your time into helping, if I am not explaining myself complete I'll be happy to clear any doubts thanks a lot!
As that isn't a REST interface, RestClient may not be your best choice. You probably want something that more closely emulates a browser. For example, using mechanize:
require 'mechanize'
a = Mechanize.new
a.get('http://www.koldcast.tv') do |page|
search_result = page.form_with(:action =>"http://www.koldcast.tv/index.php") do |search|
search.keywords = "___"
end.submit
# Print all relative links (starting with "/")
search_result.links_with(:href => /^\//).each do |link|
puts link.href
end
end
This gets you partway there. You can see all the video links.

rails ajax redirect

Here is my use case
I have a search model with two actions search_set and search_show.
1 - A user loads the home page which contains a search_form, rendered
via a partial (search_form).
2 - User does a search, and the request goes to search_set, the search
is saved and a redirect happens to search_show page which again
renders the search_form with the saved search preferences. This search
form is different than the one if step1, because it's a remote form
being submitted to the same action (search set)
3 - Now the user does another search, and the search form is submitted
via ajax to the search_set action. The search is saved and executed
and now I need to present the result via rjs templates (corresponding
to search_show). I am told that if the request is xhr then I can't
redirect to the search_show action? Is that right? If yes, how do I
handle this?
Here is my controller class
http://pastie.org/993460
Thanks
That's right. Either make the request non-XHR and redirect as normal, or you could try rendering the URL you want to redirect to as text or part of a JSON object which your AJAX request then uses to call document.location.href = [whatever] (but this seems hacky).
Right now what's happening is your XHR request is returning the result of the redirect, and not actually redirecting the page that made the XHR request.

Resources