Using proxies with Selenium WebDriver and chrome - ruby

Im writing a selenium-webdriver script in ruby and im trying to figure out how to make the browser window that selenium opens use a proxy with user pass authentication.
i.e. (234.43.234:2345:user:pass)
so far my code is thus....
def scrape_n_vote
driver = Selenium::WebDriver.for(:chrome)
loop do
driver.get "https://poll.fm/poll_id"
driver.find_element(id: 'answer_id').click
sleep 1
driver.find_element(class: 'btn-large').click
sleep rand(1..3)
end
end
Does anyone know how to make the browser programatically accept a proxy via this ruby script for chrome to make the connection with?

Related

How do I use my own cookies in capybara?

I'm trying to (ab)use the capybara web testing framework to automate some tasks on github that are not accessible via the github API and which require me to be logged in and click on buttons to send AJAX requests.
Since capybara/selenium is a testing framework it helpfully creates a temporary session which has no cookies in it. I'd like to either stop it from doing that, or else I'd like to know how to load my cookie store into the browser session that it creates.
All I'm trying to do is this:
#!/usr/bin/env ruby
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.navigate.to "https://github.com"
Or this:
#!/usr/bin/env ruby
require 'capybara'
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
session = Capybara::Session.new(:selenium)
session.visit "https://www.github.com"
In both cases I get the github.com landing page you'd see as a logged-out user or incognito mode in the browser. I'd like to get my logged-in landing page like I just fired up a web browser myself and navigated to that URL.
Since I have 2FA setup on github that makes automating the login process from the github landing page somewhat annoying, so I'd like to avoid automating logging into github. The tasks that I want to automate do not require re-authenticating via 2FA.
ANSWER:
For MacOSX+Ruby+Selenium this works:
#!/usr/bin/env ruby
require 'selenium-webdriver'
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"debuggerAddress" => "127.0.0.1:20480"}, detach: false)
driver = Selenium::WebDriver.for :chrome, :desired_capabilities => caps
driver.navigate.to "https://github.com"
Then fire up chrome with this:
% /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=/Users/lamont/Library/Application\ Support/Google/Chrome --profile-directory=Default --remote-debugging-port=20480
Obviously the paths will need to be adjusted because they're OSX-centric and have my homedir in them.
There is also a bug in the selenium-webdriver gem for ruby where it inserts a 'detach' option which gets into a fight with 'debuggerAddress':
/Users/lamont/.rvm/gems/ruby-2.2.4/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/remote/response.rb:70:in `assert_ok': unknown error: cannot parse capability: chromeOptions (Selenium::WebDriver::Error::UnknownError)
from unknown error: unrecognized chrome option: detach
The lib/selenium/webdriver/chrome/bridge.rb file can be edited to take that out as a quick hack:
chrome_options['binary'] = Chrome.path if Chrome.path
chrome_options['nativeEvents'] = true if native_events
chrome_options['verbose'] = true if verbose
#chrome_options['detach'] = detach.nil? || !!detach
chrome_options['noWebsiteTestingDefaults'] = true if no_website_testing_defaults
chrome_options['prefs'] = prefs if prefs
To implement something similar in Ruby, check out this page that goes over that. Thanks to lamont for letting me know in the comments.
You can start chrome using a specific Chrome profile. I am not sure what the ruby implementation would look like, but in python it looks something like:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options as ChromeOptions
options = ChromeOptions()
# more on this line here later.
options.add_experimental_option('debuggerAddress', '127.0.0.1:7878')
driver = webdriver.Chrome(chrome_options=otpions)
In order for this to work you need to do a few things.
manually start chrome from terminal/command prompt with these command line arguments
--user-data-dir=/path/to/any/custom/directory/home/user/Desktop/Chromedir --profile-directory="Profile 1" --remote-debugging-port=7878
make sure "Profile 1" is already existing in the same --user-data-dir (make sure user Profile 1 has necessary chrome://components/
to run any apps that require those components)
you can use any free port in place of 7878
verify that http://localhost:7878 is running and returns value.
This should manually launch chrome with the "Profile 1" profile, and so long as it has logged into the site in question, it will stay logged in like a normal user so long as you follow these instructions to run the tests.
I used this to write a quick netflix bot that clicks the "continue playing" button when it pops up, and it's the only way to get DRM content to play as far as I have found. But it retains the cookies for the login, and also launches chrome with whatever components the profile is set up to have.
I have tried launching chrome with specific profiles before using different methodologies, but this was the only way to really force it to work how I wanted it to.
Edit: There are methods for saving cookie info as well although I don't know how well they work. Check out this link for more info, as my solution is probably not the best solution even if it works.
The show_me_the_cookies gem provides cross-driver cookie manipulation and can let you add new cookies. The one thing to be aware of when using selenium is that you need to visit the domain before you can create cookie for it, so you'll need to do something like
visit "https://www.github.com"
create_cookie(...)
visit "https://www.github.com"
for it to work - first visit just puts the browser/driver in a state where you can create the cookie, second visit actually goes to the page with the cookies set.
I had to tweak the OP's answer (from within her question) to get this going with Ruby in 2022.
Prerequisites
Chromedriver installed and allowed to run even though it's not signed:
> brew install chromedriver
> xattr -d com.apple.quarantine /usr/local/bin/chromedriver
Chrome launched and accepting commands on a specific port:
> /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --user-data-dir=~/Library/Application\ Support/Google/Chrome --profile-directory=Default --remote-debugging-port=20480
This created a new profile in Chrome so I signed in to my account and got the browser set up, ready to start interacting with the (legacy EdTech) site I'm trying to automate.
Actual use
require 'selenium-webdriver'
caps = Selenium::WebDriver::Remote::Capabilities.chrome("goog:chromeOptions" => {"debuggerAddress" => "127.0.0.1:20480"})
driver = Selenium::WebDriver.for :chrome, capabilities: caps
driver.navigate.to "https://www.google.com"

How to bypass website's security certificate in IE using Ruby/Selenium WebDriver

I am trying to automate some IE browser tests using Ruby/Selenium WebDriver.
When I run the following code, it opens a new IE browser with the url but it always tells that 'There is a problem with this website's security certificate.'
Is there any way to set the IE profile/capabilities using Ruby similar to the ones used in Java?
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :ie
driver.get "https://xxxxxxxxxxxxxxxxxx.com"
There's no way to set it by any capabilities, even if you were using Java. If you have found any approaches to achieve it in Java, please post it and see if it can be translated into Ruby.
But you can always simulate the clicking to bypass it.
# Tested under Windows 7, IE 10, Ruby 2.0.0
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :ie
driver.get "https://xxxxxxxxxxxxxxxxxx.com"
driver.get("javascript:document.getElementById('overridelink').click()");

send Ctrl-S to browser in Capybara Webkit test

I have JavaScript in my application that submits a form when the user hits Ctrl-S or Cmd-S. I want to write an automated test for this using RSpec, Capybara, and Capybara Webkit. I don't think I can just have Capybara execute JavaScript to trigger Ctrl-S/Cmd-S because that's not normally allowed with JavaScript in Chrome as a security concern. I see with Selenium there are page.driver.browser.action.key_down/key_up methods available. Is there anything similar with Capybara Webkit? If not, how can I send Ctrl-S and Cmd-S to the browser in my test?
Edit: I also can't get this to work using the regular Selenium driver with Firefox:
describe 'edit a template and hit Ctrl-S', js: true do
render_views
it 'saves the template' do
visit my_path
page.execute_script("$('#hidden_textarea').val('Fabulous new content')")
builder = page.driver.browser.action
builder.key_down(:control).send_keys('s').key_up(:control).perform
expect(page).to have_text('Record was saved.')
expect(page).to have_text('Fabulous new content')
end
end
It looks like the builder.key_down(:control).send_keys('s').key_up(:control).perform isn't doing anything--the page loads in Firefox but just sits there. This is with Firefox 19 on OS X with selenium-webdriver 2.35.1.
Any suggestions on how to get this to work in either Firefox or Chrome, with either Selenium or Capybara Webkit?
I'm trying to do a similar thing in Chrome using Capybara and SitePrism. This actually works for me in Firefox though:
page.element.native.send_keys :command, 'a'
so I suggest trying this
builder.native.send_keys :control, 's'

how to remove the log of ie web driver?

I am using watir-webdriver + ruby + win7 to test same pages. and I would get these logs while I start the ie explorer by using watir-webdriver:
Started InternetExplorerDriver server (32-bit)
2.32.3.0
Listening on port 5555
are there any methods to remove these logs? any help would be appreciated!
IEDriver supports a --silent flag that suppresses diagnostic output when the server is started.
Unfortunately, at least to my knowledge, it is not configurable when creating a browser instance. Instead, you need to directly modify the Selenium::Webdriver::IE::Server class' server_args method. You can modify the lib\selenium\webdriver\ie\server.rb file directly, but it is probably easier to monkey patch.
To monkey patch the silent flag, add the following to your code some point after requiring watir-webdriver (ie selenium-webdriver) but before opening the browser.
class Selenium::WebDriver::IE::Server
old_server_args = instance_method(:server_args)
define_method(:server_args) do
old_server_args.bind(self).() << "--silent"
end
end
For example, the following will no longer log any messages.
require 'watir-webdriver'
class Selenium::WebDriver::IE::Server
old_server_args = instance_method(:server_args)
define_method(:server_args) do
old_server_args.bind(self).() << "--silent"
end
end
b = Watir::Browser.new :ie

How do I prevent Watir from auto closing firefox?

I am automating test cases using Ruby and Watir. One of my methods opens the web browser, but as soon as my script leaves the "open browser" method and goes to the next method (filling out forms within the browser), the browser auto closes. When I automate using the IE browser it will not close until it hits the IE.close statement, but with firefox it closes automatically. Is there any way to avoid this?
Code:
require 'rubygems'
require 'watir-webdriver'
require 'rexml/document'
def openbrowser
$user = "user"
$pass = "password"
ff = Watir::Browser.new :firefox
ff.goto "http://<some website>"
ff.text_field(:name, "username").set($user)
ff.text_field(:name, "password").set($pass)
ff.button(:value,"Sign In").click
ff.link(:xpath => "html/body/div[1]/div[2]/a[1]").click
ff.text_field(:name,"userID").set($ID)
ff.button(:value,"View User").click
ff.link(:xpath => "html/body/div[1]/ul[1]/li[2]/a").click
sleep 20
end
# Run Program
openbrowser
I was attempting to run this code in NetBeans, so this behavior may just be specific to that editor.
There were two causes I have found for it shutting down, first is when there is an error in the code, the browser will shut down as soon as an exception is thrown. Second, the browser shuts down at the end of the program if there is no sleep established.
I use the Test Unit class, I open the browser in the setup method and generally close it down in the teardown method, this works for me in IE & Firefox.
More information here, http://wiki.openqa.org/display/WTR/Test+Unit

Resources