I am new to RubyMine and trying to debug an project that I did not write myself.
The error when I ran the Project.feature and it stopped at the first line 'Given I go to this Website'.
"NoMethodError: Undefined method 'get' for nil:nilClass" in filepath/Common_steps_json.rb:14 in '/^I go to this website$'
Going to the link provided, it directed me to my Common_steps_json.rb file where the code written was
Given /^I go to this website$/ do
#driver.get 'https://www.somewebsite.com/field1/field2/'
sleep 3
end
I believe this is the correct format to navigate to a URL using Selenium-Webdriver. So this prompted me to check the environment file env.rb to see how the driver was called. There I found this.
require "selenium-webdriver"
require "webdriver-user-agent"
.
.
.
Before('#driver_iphone_portrait') do
#driver = UserAgent.driver(:browser => :firefox, :agent => :iphone, :orientation => :portrait)
#base_url = 'https://www.somewebsite.com/'
#driver.manage.timeouts.implicit_wait = 2
#verification_errors = []
end
I checked this link to make sure that the selenium webdriver user agent was called correctly but I am not so sure that it is. I used Interactive Ruby to see if I could call the driver using the UserAgent.Driver format but it ran into a error
NameError: uninitialized constant UserAgent
If anyone has any advice on how to properly format UserAgent for RubyMine, I would really appreciate it. Thanks!
It looks like your code is trying to run some tests using Firefox with a profile that uses a user agent property to emulate the iPhone.
Unfortunately, your code is using a depreciated gem written by Alister Scott. You will be unable to continue to use this gem, and should look for a work around.
UPDATE Alister Scott just announced that the maintenance of the gem has been taken over by Jeff "Cheezy" Morgan and is available on the Github repo.
In fact, it looks like this was a pretty simple and standard gem wrapper that you can employ without all the sugary wrapper goodness. Actually, you can easily configure this setting in your WebDriver Profile setup
Try something like this to replace the UserAgent string (and take out the require 'webdriver-user-agent' line too):
profile = Selenium::WebDriver::Firefox::Profile.new
profile["general.useragent.override"] = "Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3"
#driver = Selenium::WebDriver.for :firefox, :profile => profile
That should create the #driver object and allow you to use the get method to open the website with the Firefox browser. The browser should be sending in the headers of the requests the user agent you specified in the code, and your web pages should be rendering as they would if an iPhone was browsing.
Related
I am very new at BDD testing and I am trying to figure out how to open a chrome browser in a specific URL using these technologies. I already installed bddfire gem and already ran it.
Now I have a feature:
#openingChrome
Scenario: Opening Chrome on facebook page
Given I open chrome and write "url"
bddfir_steps.rb
Given(/^I open chrome and write "([^"]*)"$/) do |arg1|
$session.visit("https://www.facebook.com.br")
end
In the hooks file I wrote
Before do
$session = Capybara::Session.new(:selenium, browser: :chrome)
end
And in the env.rb file that came with bddfire gem there is this
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome)
end
So chrome is already added and I already installed chrome webdriver. Unfortunately this line of code is not working: $session = Capybara::Session.new(:selenium, browser: :chrome)
it throws this error: The second parameter to Session::new should be a rack app if passed. (TypeError)
does anyone know why?
Session#new takes the name of the driver and optionally a rack app instance to start. Since you don't seem to be starting an app and you're registering your driver with a name of :chrome you want
Before do
$session = Capybara::Session.new(:chrome)
end
I try to open internet explorer via watir-webdriver with my code:
require "watir-webdriver"
profile = Selenium::WebDriver::Firefox::Profile.new
profile.proxy = Selenium::WebDriver::Proxy.new :http => 'http://username:password#xx.xxx.xxx.xx:xxxxx', :ssl => 'http://username:password#xx.xxx.xxx.xx:xxxxx'
And I get this error stating something about the I'm guessing?
This is what it reads:
C:/Ruby/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/firefox/profile.rb:205:in `Integer': invalid value for Integer(): "//username:password#xx.xxx.xxx.xx:xxxxx" (ArgumentError)
from C:/Ruby/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/firefox/profile.rb:205:in `set_manual_proxy_preference'
from C:/Ruby/lib/ruby/gems/2.2.0/gems/selenium-webdriver-2.53.0/lib/selenium/webdriver/firefox/profile.rb:176:in `proxy='
from test1.rb:6:in `<main>'
I can explain the error at least. You can't pass in basic authentication to a proxy url because the code expects that a colon in the string is distinguishing a host from a port (which needs to be an integer.
I do not have much experience with using proxies with Selenium. Poking around on the internet for the problem give suggestions such as setting signon.autologin.proxy to true in your designated Firefox profile, or adding the Firefox autoauth extension. Essentially you can save your password once manually in your profile, and then automated tests won't have to input it.
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"
Using Ruby 2.0.0 p481 in RubyMine and chromedriver 2.10
When Chrome starts it displays a message in a yellow popup bar: "You are using an unsupported command-line flag: --ignore-certificate-errors. Stability and security will suffer." This simple example reproduces the problem.
require "selenium-webdriver"
driver = Selenium::WebDriver.for :chrome
driver.navigate.to login_url
This question has been answered for Java and Python. I have looked everywhere for a Ruby analog. Does anyone have a suggestion or know how to translate the Python answer (Unsupported command-line flag: --ignore-certificate-errors) to Ruby? Thank you!
The Ruby selenium-webdriver API doesn't expose a separate Chrome options object like Java/Python but you can set the options via "Capabilities".
The Capabilities web page provides a Ruby example and the table of recognized capabilities that you can inject. Plugging those together with excludeSwitches:
caps = Selenium::WebDriver::Remote::Capabilities.chrome("chromeOptions" => {"excludeSwitches" => [ "--ignore-certificate-errors" ]})
driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps
Take a look at Watir too, it's a front end for WebDriver.
Their examples show how you can send a :switches array which is passed straight through to the web driver so you can do the same. That makes adding other switches a bit easier rather than going through capabilities.
There is a chromedriver issue on the topic as well. There are posts detailing that you can add a --test-type argument to work around the certificate issue and ruby code examples like above.
I adjusted:
driver = Selenium::WebDriver.for :chrome
to read:
driver = Selenium::WebDriver.for :chrome, :switches => %w[--test-type]
...and the script ran successfully without the yellow flag. Clearly, other command-line switches could be added.
Thank you to Nguyen Vu Hoang and mtm.
I donot know ruby, however my approach is set mode "test-type" to ChromeDriver capabilities
Here's my sample code in Java
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type", "start-maximized",
"no-default-browser-check");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
With Capybara:
Capybara.register_driver :chrome do |app|
Capybara::Selenium::Driver.new(app, browser: :chrome, switches: ['--test-type'])
end
This error caused my rspec tests to fail. I think it was causing Chrome to slow down so the above fixes did remove the error messages but did not resolve my problem of my tests failing.
If you are using chromedriver-helper then this should fix the problem. Run this from the command line
chromedriver-update
This is described more in chromedriver-helper, "This might be necessary on platforms on which Chrome auto-updates, which has been known to introduce incompatibilities with older versions of chromedrive"
Once I ran this I was able to remove the fixes described elsewhere and Chrome ran with out warnings.
I am automating test cases for a website using selenium-webdriver and cucumber in ruby. I need each feature to run in a particular order and using the same browser window. Atm each feature creates a new window to run test in. Though in some test cases this behavior is desired- in many cases it is not. From my research so far it seems there are mixed answers about whether or not it is possible to drive the same browser window with selenium throughout test cases. Most answers I have run into were for other languages and were work arounds specific to a browser (I am developing my test while testing IE but will be expected to run these test in other browsers). I am working in Ruby and from what I have read it seems as though I'd have to make a class for the page? I'm confused as to why I would have to do this or how that helps.
my env.rb file:
require 'selenium-webdriver'
require 'rubygems'
require 'nokogiri'
require 'rspec/expectations'
Before do
#driver ||= Selenium::WebDriver.for :ie
#accept_next_alert = true
#driver.manage.timeouts.implicit_wait = 30
#driver.manage.timeouts.script_timeout = 30
#verification_errors = []
end
After do
##driver.quit
##verification_errors.should == []
end
Some information I've gathered so far of people with similar problems:
https://code.google.com/p/selenium/issues/detail?id=18
Is there any way to attach an already running browser to selenium webdriver in java?
Please ask me questions if anything about my question is not clear. I have many more test to create but I do not want to move on creating test if my foundation is sloppy and missing requested capabilities. (If you notice any other issues within my code please point them out in a comment)
The Before hook is run before each scenario. This is why a new browser is opened each time.
Do the following instead (in the env.rb):
require "selenium-webdriver"
driver = Selenium::WebDriver.for :ie
accept_next_alert = true
driver.manage.timeouts.implicit_wait = 30
driver.manage.timeouts.script_timeout = 30
verification_errors = []
Before do
#driver = driver
end
at_exit do
driver.close
end
In this case, a browser will be opened at the start (before any tests). Then each test will grab that browser and continue using it.
Note: While it is usually okay to re-use the browser across tests. You should be careful about tests that need to be run in a specific order (ie become dependent). Dependent tests can be hard to debug and maintain.
I had a similar problem in creating a spec_helper file. I did the following (simplified for locally-run firefox) for my purposes and it works very, very reliably. RSpec will use the same browser window for all it blocks in your _spec.rb file.
Rspec.configure do |config|
config.before(:all) do
#driver = Selenium::WebDriver.for :firefox
end
config.after(:all) do
#driver.quit
end
end
If you switch to :each instead of :all, you can use a separate browser instance for each assertion block... again, with :each RSpec will give a new browser instance for each it. Both are useful depending on the circumstance.
As the answers solve the problem but do not answer the question "How to connect to an existing session".
I managed to do this with the following code since it is not officially supported.
# monkey-patch 2 methods
module Selenium
module WebDriver
class Driver
# Be able to set the driver
def set_bridge_to(b)
#bridge = b
end
# bridge is a private method, simply create a public version
def public_bridge
#bridge
end
end
end
end
caps = Selenium::WebDriver::Remote::Capabilities.send("chrome")
driver = Selenium::WebDriver.for(
:remote,
url: "http://chrome:4444/wd/hub",
desired_capabilities: caps
)
used_bridge = driver.bridge
driver.get('https://www.google.com')
# opens a new unused chrome window
driver2 = Selenium::WebDriver.for(
:remote,
url: "http://chrome:4444/wd/hub",
desired_capabilities: caps
)
driver2.close() # close unused chrome window
driver2.set_bridge_to(used_bridge)
driver2.title # => "Google"
Sadly this did not test work between 2 rescue jobs, will update this in the future when I made it work for my own use case.