Add the library to the test section in your application's Gemfile:
group :test do
gem 'testcontainers-selenium'
end
And then execute:
$ bundle install
Or install it yourself as:
$ gem install testcontainers-selenium
To use the library, you first need to require it:
require "testcontainers/selenium"
Create a new instance of the Testcontainers::SeleniumContainer
class:
container = Testcontainers::SeleniumContainer.new
This creates a new container with the default Selenium configuration for firefox, the vnc password will be secret
. You can customise by passing arguments to the constructor:
container = Testcontainers::SeleniumContainer.new(capabilities: :chrome, vnc_no_password: true)
Start the container
container.start
Stop the container when you're done
container.stop
Once the container is running, you can obtain the connection details using the following methods:
host = container.host
post = container.first_mapped_port
Or, you can generate a full Selenium URL
selenium_url = container.selenium_url
There are complete examples of how to use testcontainers-selenium to create containers, connects to it, and navigate through different browsers:
require "testcontainers/selenium"
require "selenium-webdriver"
container = Testcontainers::SeleniumContainer.new
container.start
driver = Selenium::WebDriver.for(:firefox, :url => @container.selenium_url)
driver.navigate.to "https://www.google.com"
driver.screenshot
The previous example creates a container and after create a client for do a connections wiht the google page,finally we take a screenshot from the current page
Bug reports and pull requests are welcome on GitHub at https://github.com/testcontainers/testcontainers-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in the Testcontainers project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.