Stubbing request.host in Capybara 2.0 / Cucumber - ruby-on-rails-3

My application needs to act differently on certain request.host values. I test this behavior with Cucumber. Before Capybara 2.0 I was able to mimic the right request.host value by executing this Cucumber step:
Given /^the url starts with "([^"]*)"$/ do |url|
Capybara.app_host = "http://#{url}"
end
But now with Capybara 2.0.1, my browser actually navigates to the set URL, instead of staying on my test server and pretending to be from that URL.
So my question is: how do I correctly "stub request.host" in Capybara 2.0?

I managed to get through not by stubbing request.host but setting these:
default_url_options[:host] = host
Capybara.app_host = "http://" + host
Hope that helps.

Related

autoauth firefox plugin didn't work

Good day to all.
I'm use Selenium WebDriver to automatically test execute. But on development site using HTTP base autentification. I found AutoAuth addon for Firefox. It save login/password and don't need type credentional each time.
But this plugin don't save credentions. I'm reinstall addon and firefox, delete cookie, but nothing. On this machine in other user plugin work successfylly. Maybe, anybody have and resolve this problem?
To author of addon I wrote already.
Way:https://login:passwd#host don't help too...
Do you mean plugin not working on invoking with webdriver? simple way to create profile and call that provide in webdriver.
Here is the way to create firefox profile. Install that add-in and save credentials.
Call above saved profile in webdriver
ProfilesIni allProfiles = new ProfilesIni();
FirefoxProfile profile = allProfiles.getProfile("selenium");
WebDriver driver = FirefoxDriver(profile);
Thank You,
Murali
If it's a HTTP Basic Authentication, then you can set the credentials in the URL. Note that it requires to set the "network.http.phishy-userpass-length" preference to enable it.
Here is a working example with Selenium / Firefox / Python:
from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("network.http.phishy-userpass-length", 255)
driver = webdriver.Firefox(profile)
driver.get("http://admin:admin#the-internet.herokuapp.com/basic_auth")
The approach I've used very successfully is to set up an embedded Browsermob proxy server (in Java code) and register a RequestInterceptor to intercept all incoming requests (that match the host / URL pattern in question).
When you have a request that would otherwise need Basic auth, add an Authorization HTTP header with the credentials required ('Basic ' + the Base64-encoded 'user:pass' string. So for 'foo:bar' you'd set the value Basic Zm9vOmJhcg==)
Start the server, set it as a web proxy for Selenium traffic, and when a request is made that requires authentication, the proxy will add the header, the browser will see it, verify the credentials, and not need to pop up the dialog.
You won't need to deal with the dialog at all.
Other benefits:
It's a pure HTTP solution, it works the same across all browsers and operating systems.
No need for any hard-to-automate add-ons and plugins, any manual intervention.
No need for custom profiles, custom preferences etc.
You control credentials in your test code, and don't store them elsewhere.

Robotframework Gherkin - How to log cookies to console?

Just started using the Robot Framework to test our application and I really like it so far. The option to combine it with Selenium, Appium and Locust.io in the end sounds appealing. Anyhow, I have a basic question:
I'm trying to log the cookies from a browser session to the console. Selenium2Library provides the keyword:
Get Cookies
Then I try in my test case:
Log Cookies To Console
${cookies}= Get Cookies
Log To Console ${cookies}
This doesn't seem to give me anything in the console.
Are you 100% sure there is an cookie anyway?
What I would do it :
try with another URL on which you know there actually is a cookie (www.google.com?)
try to add a cookie (keyword: add cookie) just before you log, so that you can see, at least, that the one you add is displayed
That could help you find out the source of your problem.

Is it possible to run selenium rc code in 2 different URLs?

The AUT contains the login page which validates the user credentials. Once successful, the user needs to hit a new URL in a new tab to access the actual application.
Is it possible to automate the above scenario using selenium RC?
Yes it's possible, but I'd recommend using Selenium WebDriver as it's the current version. Selenium RC is very old.
Using Selenium WebDriver:
driver.navigate().to("http://aut/login");
driver.... // login logic like setting the user/pass clicking login.
...
driver.navigate().to("http://somenewurl.com");
// continue testing here...
If you are adamant on remaining with Selenium 1 (which i could understand if you already have a big suite in it..):
selenium.open("http://aut/login");
selenium... // login logic here
...
selenium.open("http://somenewurl.com");
// continue testing here.

Anti forgery token and web testing

I am trying to do a web test in VS2012 for an MVC site.
One of the scenarios is to login and go through a list of products, select the one you want and follow through to the purchase page.
Problem is that when the web test is run, I get an error about the anti forgery token and that it does not match.
How on earth is it possible to do the testing with the anti forgery token? The user must login - there will be thousands of users for the load test (eventually) but need to make it work for 1 user first.
the login view/action does do an AntiForgeryToken in the view and validation on the controller.
any advice and tips are appreciated.
Once you run your script and it fails, go to the call proceeding the one that fails.
Go to the response tab
In the body, find the __RequestVerificationToken name which is in an input tag and extract everything in between the value attribute.
Select the value and right click > add extraction rule and press OK.
You will find an Extraction rules folder and underneath it, the Extraction rule we just created. Feel free to rename the Context Parameter Name.
Go to the next page , which should be the one that failed, and find the Form Post Parameter named "__RequestVerificationToken". View it's properties
Bind it to the Context Parameter Name created previously. To do so, view the properties of this post parameter and set the "Value" to be:
{{Name Of Context Parameter}}
(Include the 2x curly braces)
Press enter to confirm/save
Next time you run the script - all works
This is how it worked for me...
I was seeing a similar problem. After recording a web test script, the script would fail at the point of log-in on with the following message:
The provided anti-forgery token was meant for user "Domain\UserName", but the current user is "".
The solution was to set the PreAuthenticate property to false in the test properties. By default the web tests will pass an authentication header to the server which was being used in the generation the token.
I am not familiar with "web testing in VS2012" but as I know "Anti-Forgery Token" requires sending the token from browser to the server back.
I had an experience with Selenium-Webdriver and suggest you use it because it provides an API to interact with supported browsers as real user does.
You can easily start using Selenium WebDriver if you add Selenium WebDriver 2.37.0 NuGet package to you test project.
Selenium-WebDriver makes direct calls to the browser using each
browser’s native support for automation. How these direct calls are
made, and the features they support depends on the browser you are
using.
Selenium-webdriver currently supports the following drivers:
Chrome
Internet Explorer
Firefox
Opera
HtmlUnit
Android

Set headers - capybara mechanize or selenium

In my cucumbers, I need to add a key/value pair to the http headers when I request a page with capybara using the mechanize driver or perhaps the selenium driver.
I'm using capybara 1.1.1 and mechanize 2.0.1 and selenium 2.5.0
But how?
Here are my step definitions:
When /^set some headers$/ do
#set some headers here
visit('/url')
end
Then /^some result$/ do
#check page responds to header
end
Many thanks,
Rim
If you're using Mechanize you should be able to set headers in the request like this:
When /^set some headers$/ do
#set some headers here
page.driver.agent.request_headers = {"X-Header" => "value"}
visit('/url')
end