Selenium/Watir: 'InvalidCookieDomain' error when trying to add cookie - ruby-on-rails

I am new into WATIR, or Selenium, but I was trying to add a cookie into my WATIR browser, as below:
browser = Watir::Browser.new :firefox
browser.goto(url)
browser.cookies.add name,value, domain: ".www.example.com"
I am prompted with the following error:
Selenium::WebDriver::Error::UnknownError: ReferenceError: InvalidCookieDomainError is not defined
But, when I delete the domain option, it works.
I was wondering why I can't add a different domain into my cookies?

Like WATIR, all Selenium-based frameworks create language-specific wrappers to Selenium pre-defined commands. These commands are defined here in the Selenium Webdriver W3C standard.
If you go to Cookies section, specifically, the addCookie sub-section, you will see the following:
If the current browsing context’s document element is a cookie-averse Document object, return error with error code invalid cookie domain.
Your domain attribute is bound to the same domain as your given url. So, basically, your domain HAS to be the same as your current url, or a sub-domain of the url.
Example: For url=global.nba.com, you can set cookies for both the sub-domain ({domain: "global.nba.com"}), as well as for the root-domain ({domain: "nba.com"}).
browser = Watir::Browser.new :firefox
browser.goto(url)
browser.cookies.add("<yourCookieName>","<yourCookieValue>", {domain: "<sameUrlOriginDomain>"})
Hope this helps!

in my case i had to go to the url first before setting a cookie, e.g.
visit root_path
('f'..'k').each.with_index do |cookie, index|
Helper::Browser.set_cookie(:esid, cookie)
visit root_path
end

Related

How to get the hostname or entire url of the visitor website in Ruby on Rails

If the user clicks a link on http://facebook.com/my_path/ that points to http://example.com/ the value of request.domain will be example.com
But in my case, I would like to get the visitor's host name facebook.com and entire url http://facebook.com/my_path/
How to get that? Any help would be appreciated. Thanks!
Use request.referer, it is from a library called Rack, which Rails itself uses, more info here https://apidock.com/rails/Rack/Request/referer
Can you try putting debugger in the controller and access that controller in your browser. When you reach the byebug shell, try to check the output of the request variable like below
(byebug) request
#<ActionDispatch::Request:0x007fe49c151840 #env={"rack.version"=>[1, 3], "rack.errors"=>#<
the output should have your referrer path if it exists

Capybara returning session data for the body when I stub application controller methods

I have
allow_any_instance_of(ApplicationController).to receive(:current_user).and_return(user)
But for some reason when I print page.body I am getting
"Rack session data \"session_id\" : \"{session_id}\" \"user_id\" : 1 \"uid\" : \"{user uid}\"
Instead of HTML for a page. I'm very confused by this behavior. I'm on capybara 2.4.4
Did you visit the correct page?
The command for visiting a given page is visit <insert path here>. Make sure you visit your desired page before trying to retrieve its body.

Mechanize: not logged in the site

I'm trying to authorise to other site with gem mechanize
My code:
def login_zenit
agent = Mechanize.new
agent.get('http://mobile.zenitbet.com/') do |p|
f = p.forms.first
f.login = 'login'
f.fields[1].value = 'password'
f.submit
end
agent.get('http://mobile.zenitbet.com/')
redirect_to root_url
end
The problem is when I run login_zenit it doesn't work - I'm not authorising to the site in web browser. Although if I run this code in rails console it works perfectly. Where did I make a mistake? maybe there is a problem with redirect_to root_url?
Thanks!
In general is not possible. For this to work visiting your app would need to set the appropriate cookies for mobile.zenibet.com.
You do have those cookie values - they're inside the mechanize object, but even if you were to extract them you wouldn't be able to set them on the correct domain. If your app is being served from foo.com then the browser will let you set cookies on foo.com or any subdomain of it, but it won't let you set cookies on another arbitrary domain (see point 5 in section 5.3 of the rfc)
Unless your app runs on a subdomain of zenibet.com I think you are out of luck

Testing dynamic URL in bundler application with cucumber/capybara

I am using a Cucumber-Capybara combination for testing. I am not using Rails application but a simple bundler application.
I want to test some dynamic URLs for my front-end application. For example, the URL for item-show page is:
/suitability/items/197/
(Assertion: Then I go to suitability-item-show page)
In the above URL, 197 is an :id. I want to test similar pages which contains dynamic data.
I have tried following two ways:
Then(/^I should be on suitability\-item\-show page$/) do
visit '/suitability/items/:id/'
end
and
Then(/^I should be on suitability\-item\-show page$/) do
visit "/suitability/items/#{:id}/"
end
But they didn't work out for me
Please help me with the solution to this.
If var="id", then
visit "/suitability/items/#{var}/"
should work.
I'm not getting how u got ':id'...can u throw some more light on that

Rails/Rack: retrieving request params from within canonical_host middleware

I'm using the Rack Canonical Host middleware (https://github.com/tylerhunt/rack-canonical-host) with Rails to force the www for all root requests (example.com becomes www.example.com). However, if a visitor is attempting to access a valid subdomain of our app, we obviously don't want to force www. Here's the example usage of the middleware:
Rails.application.config.middleware.use Rack::CanonicalHost do
# the following return value will be used to set the canonical host
'www.example.com'
end
As you can see it's somewhat static, which is a problem. However, if I had access to the request parameters (eg subdomain, domain, etc) I could check against them, and redirect accordingly (and only if need be).
Anyone have any pointers?
Specify a parameter for this block
Rails.application.config.middleware.use Rack::CanonicalHost do |params|
puts "PATH_INFO #{params['PATH_INFO']}"
end

Resources