Skip to content

Instantly share code, notes, and snippets.

@cassioKenji
Last active February 22, 2019 11:17
Show Gist options
  • Save cassioKenji/ec9902db2ff627aaf70198411ed31a61 to your computer and use it in GitHub Desktop.
Save cassioKenji/ec9902db2ff627aaf70198411ed31a61 to your computer and use it in GitHub Desktop.
cool capybara stuff cheat sheet
# set and load profile
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.cache.disk.enable'] = false
profile['browser.cache.memory.enable'] = false
Capybara::Selenium::Driver.new(app, browser: :firefox, profile: profile)
# https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html
# multiple flags (Capabilities.chrome('chromeOptions' => { 'args' => [] }) does not work with multple flags)
Capybara.register_driver :selenium do |app|
options = Selenium::WebDriver::Chrome::Options.new(args: ['start-fullscreen','proxy-server=localhost:3128'])
Capybara::Selenium::Driver.new(app, :browser => :chrome, options: options)
end
# https://seleniumhq.github.io/selenium/docs/api/rb/Selenium/WebDriver/Chrome/Options.html
# loading a chrome extension
Capybara.register_driver :selenium do |app|
options = Selenium::WebDriver::Chrome::Options.new(extensions: ['path-to-the-crx-file'])
Capybara::Selenium::Driver.new(app, :browser => :chrome, options: options)
end
# https://github.com/SeleniumHQ/selenium/blob/master/rb/spec/integration/selenium/webdriver/mouse_spec.rb
it 'can drag and drop' do
driver.navigate.to url_for('droppableItems.html')
draggable = long_wait.until do
driver.find_element(id: 'draggable')
end
droppable = driver.find_element(id: 'droppable')
driver.mouse.down draggable
driver.mouse.move_to droppable
driver.mouse.up droppable
text = droppable.find_element(tag_name: 'p').text
expect(text).to eq('Dropped!')
end
#return user agent
driver.execute_script 'return window.navigator.userAgent'
# browser current session, resizing window and deleting cookies
browser = Capybara.current_session.driver.browser
browser.manage.window.maximize
browser.manage.window.resize_to(1024, 768)
browser.manage.delete_all_cookies
class Browser
def self.execute_javascript(javascript)
Capybara.current_session.driver.browser.execute_script(javascript)
end
end
# qualquer_teste.rb
require "browser.rb"
Browser.execute_javascript("var btn = document.createElement('BUTTON');")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment