├── .gitignore ├── Gemfile ├── README.md ├── driver-close.rb ├── firefox-with-profile.rb └── todo-click-test.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.lock -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source "https://rubygems.org" 4 | 5 | # gem "rails" 6 | gem 'selenium-webdriver', '~> 4.1', '>= 4.1.0' -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Run Selenium Tests With Capybara On LambdaTest 2 | 3 | ![Group 7552](https://user-images.githubusercontent.com/95698164/174256293-77d3c07c-0455-4671-bc07-4657ec3b03c7.png) 4 | 5 |

6 | Blog 7 |   ⋅   8 | Docs 9 |   ⋅   10 | Learning Hub 11 |   ⋅   12 | Newsletter 13 |   ⋅   14 | Certifications 15 |   ⋅   16 | YouTube 17 |

18 |   19 |   20 |   21 | 22 | *Learn how to use Ruby framework to configure and run your Java automation testing scripts on the LambdaTest platform* 23 | 24 | [](https://accounts.lambdatest.com/register) 25 | 26 | ## Table Of Contents 27 | 28 | * [Pre-requisites](#pre-requisites) 29 | * [Run-Your-First-Test](#run-your-first-test) 30 | * [Executing-The-Test](#executing-the-test) 31 | * [Testing Locally Hosted or Privately Hosted Projects](#testing-locally-hosted-or-privately-hosted-projects) 32 | 33 | ## Pre-requisites 34 | 35 | Before you can start performing Ruby automation testing with Selenium, you would need to: 36 | 37 | * Install **Ruby** and **gem** on your local system. Follow these instructions to install on different operating systems. 38 | 39 | * For **Windows**, you can download from the [official website](https://rubyinstaller.org/downloads/). 40 | * For **Linux** or **Ubuntu**, you can run a simple apt command like below: 41 | ```bash 42 | sudo apt-get install ruby-full 43 | ``` 44 | * For **macOS**, you can run a [Homebrew](https://brew.sh/) command like this: 45 | ```bash 46 | brew install ruby 47 | ``` 48 | * To run tests in parallel you will require the [parallel_tests](https://github.com/grosser/parallel_tests) gem. 49 | * LambdaTest binary file for running tests on your locally hosted web pages. 50 | 51 | ### Installing Selenium Dependencies and Tutorial Repo 52 | 53 | Clone the LambdaTest’s [ruby-selenium-sample repository](https://github.com/LambdaTest/ruby-selenium-sample) and navigate to the code directory as shown below: 54 | ```bash 55 | git clone https://github.com/LambdaTest/ruby-selenium-sample.git 56 | cd ruby-selenium-sample 57 | ``` 58 | Install selenium dependencies for Ruby automation testing. 59 | ```bash 60 | gem install bundler 61 | bundle install 62 | ``` 63 | ### Setting up Your Authentication 64 | Make sure you have your LambdaTest credentials with you to run test automation scripts with Jest on LambdaTest Selenium Grid. You can obtain these credentials from the [LambdaTest Automation Dashboard](https://automation.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample) or through LambdaTest Profile. 65 | 66 | Set LambdaTest Username and Access Key in environment variables. 67 | * For Linux/macOS: 68 | `export LT_USERNAME="YOUR_USERNAME" export LT_ACCESS_KEY="YOUR ACCESS KEY"` 69 | * For Windows: 70 | `set LT_USERNAME="YOUR_USERNAME" set LT_ACCESS_KEY="YOUR ACCESS KEY"` 71 | 72 | ## Run Your First Test 73 | 74 | ### Sample Test with Ruby 75 | 76 | **Test Scenario:** The to-do list example mentioned in the [lambdatest.rb](https://github.com/LambdaTest/ruby-selenium-sample/blob/master/todo-click-test.rb) sample file would help you to execute your automation test using Ruby. 77 | 78 | ### Configuration of Your Test Capabilities 79 | 80 | In the test script, you need to update your test capabilities. In this code, we are passing browser, browser version, and operating system information, along with LambdaTest Selenium grid capabilities via capabilities object. The capabilities in the above code are defined as: 81 | ```ruby 82 | caps = { 83 | :browserName => "chrome", 84 | :version => "latest", 85 | :platform => "windows 10", 86 | :geoLocation => "US", 87 | :name => "LambdaTest ruby google search name", 88 | :build => "LambdaTest ruby google search build", 89 | :network => false, 90 | :visual => false, 91 | :video => true, 92 | :console => false 93 | } 94 | ``` 95 | > You can generate capabilities for your test requirements with the help of our inbuilt **[Capabilities Generator tool](https://www.lambdatest.com/capabilities-generator/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample)**. 96 | 97 | ### Executing the Test 98 | 99 | To execute the test script, run the following script on terminal/cmd. 100 | ```bash 101 | bundle exec ruby todo-click-test.rb 102 | ``` 103 | Your test results would be displayed on the test console (or command-line interface if you are using terminal/cmd) and on [LambdaTest automation dashboard](https://automation.lambdatest.com/build/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample). LambdaTest Automation Dashboard will help you view all your text logs, screenshots and video recording for your entire automation tests. 104 | 105 | ## Testing Locally Hosted or Privately Hosted Projects 106 | 107 | You can test your locally hosted or privately hosted projects with [LambdaTest Selenium grid cloud](https://www.lambdatest.com/selenium-automation/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample) using LambdaTest Tunnel app. All you would have to do is set up an SSH tunnel using LambdaTest Tunnel app and pass toggle `tunnel = True` via desired capabilities. LambdaTest Tunnel establishes a secure SSH protocol based tunnel that allows you in testing your locally hosted or privately hosted pages, even before they are made live. 108 | 109 | >Refer our [LambdaTest Tunnel documentation](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) for more information. 110 | 111 | Here’s how you can establish LambdaTest Tunnel. 112 | 113 | >Download the binary file of: 114 | >* [LambdaTest Tunnel for Windows](https://downloads.lambdatest.com/tunnel/v3/windows/64bit/LT_Windows.zip) 115 | * [LambdaTest Tunnel for Mac](https://downloads.lambdatest.com/tunnel/v3/mac/64bit/LT_Mac.zip) 116 | * [LambdaTest Tunnel for Linux](https://downloads.lambdatest.com/tunnel/v3/linux/64bit/LT_Linux.zip) 117 | 118 | Open command prompt and navigate to the binary folder. 119 | 120 | Run the following command: 121 | ```bash 122 | LT -user {user’s login email} -key {user’s access key} 123 | ``` 124 | So if your user name is lambdatest@example.com and key is 123456, the command would be: 125 | ```bash 126 | LT -user lambdatest@example.com -key 123456 127 | ``` 128 | Once you are able to connect **LambdaTest Tunnel** successfully, you would just have to pass on tunnel capabilities in the code shown below : 129 | 130 | **Tunnel Capability** 131 | ```ruby 132 | caps = { 133 | ... 134 | :tunnel => true, 135 | ... 136 | } 137 | ``` 138 | ## Additional Links 139 | 140 | * [Advanced Configuration for Capabilities](https://www.lambdatest.com/support/docs/selenium-automation-capabilities/) 141 | * [How to test locally hosted apps](https://www.lambdatest.com/support/docs/testing-locally-hosted-pages/) 142 | * [How to integrate LambdaTest with CI/CD](https://www.lambdatest.com/support/docs/integrations-with-ci-cd-tools/) 143 | 144 | ## Documentation & Resources :books: 145 | 146 | 147 | Visit the following links to learn more about LambdaTest's features, setup and tutorials around test automation, mobile app testing, responsive testing, and manual testing. 148 | 149 | * [LambdaTest Documentation](https://www.lambdatest.com/support/docs/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample) 150 | * [LambdaTest Blog](https://www.lambdatest.com/blog/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample) 151 | * [LambdaTest Learning Hub](https://www.lambdatest.com/learning-hub/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample) 152 | 153 | ## LambdaTest Community :busts_in_silhouette: 154 | 155 | The [LambdaTest Community](https://community.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample) allows people to interact with tech enthusiasts. Connect, ask questions, and learn from tech-savvy people. Discuss best practises in web development, testing, and DevOps with professionals from across the globe 🌎 156 | 157 | ## What's New At LambdaTest ❓ 158 | 159 | To stay updated with the latest features and product add-ons, visit [Changelog](https://changelog.lambdatest.com/) 160 | 161 | ## About LambdaTest 162 | 163 | [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample) is a leading test execution and orchestration platform that is fast, reliable, scalable, and secure. It allows users to run both manual and automated testing of web and mobile apps across 3000+ different browsers, operating systems, and real device combinations. Using LambdaTest, businesses can ensure quicker developer feedback and hence achieve faster go to market. Over 500 enterprises and 1 Million + users across 130+ countries rely on LambdaTest for their testing needs. 164 | 165 | ### Features 166 | 167 | * Run Selenium, Cypress, Puppeteer, Playwright, and Appium automation tests across 3000+ real desktop and mobile environments. 168 | * Real-time cross browser testing on 3000+ environments. 169 | * Test on Real device cloud 170 | * Blazing fast test automation with HyperExecute 171 | * Accelerate testing, shorten job times and get faster feedback on code changes with Test At Scale. 172 | * Smart Visual Regression Testing on cloud 173 | * 120+ third-party integrations with your favorite tool for CI/CD, Project Management, Codeless Automation, and more. 174 | * Automated Screenshot testing across multiple browsers in a single click. 175 | * Local testing of web and mobile apps. 176 | * Online Accessibility Testing across 3000+ desktop and mobile browsers, browser versions, and operating systems. 177 | * Geolocation testing of web and mobile apps across 53+ countries. 178 | * LT Browser - for responsive testing across 50+ pre-installed mobile, tablets, desktop, and laptop viewports 179 | 180 | 181 | [](https://accounts.lambdatest.com/register) 182 | 183 | 184 | ## We are here to help you :headphones: 185 | 186 | * Got a query? we are available 24x7 to help. [Contact Us](support@lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=ruby-selenium-sample) 187 | * For more info, visit - [LambdaTest](https://www.lambdatest.com/?utm_source=github&utm_medium=repo&utm_campaign=Capybara-Cucumber-Ruby) 188 | 189 | -------------------------------------------------------------------------------- /driver-close.rb: -------------------------------------------------------------------------------- 1 | require 'selenium-webdriver' 2 | require 'test/unit' 3 | 4 | 5 | 6 | class LtTest < Test::Unit::TestCase 7 | """ 8 | LambdaTest selenium automation sample example 9 | Configuration 10 | ---------- 11 | username: Username can be found at automation dashboard 12 | accessToken: AccessToken can be generated from automation dashboard or profile section 13 | 14 | Result 15 | ------- 16 | Execute Ruby Automation Tests on LambdaTest Distributed Selenium Grid 17 | """ 18 | 19 | 20 | def setup 21 | """ 22 | Setup remote driver 23 | Params 24 | ---------- 25 | platform : Supported platform - (Windows 10, Windows 8.1, Windows 8, Windows 7, macOS High Sierra, macOS Sierra, OS X El Capitan, OS X Yosemite, OS X Mavericks) 26 | browserName : Supported platform - (chrome, firefox, Internet Explorer, MicrosoftEdge) 27 | version : Supported list of version can be found at https://www.lambdatest.com/capabilities-generator/ 28 | 29 | Result 30 | ------- 31 | """ 32 | username= ENV["LT_USERNAME"] || "{username}" 33 | accessToken= ENV["LT_ACCESS_KEY"] || "{accessToken}" 34 | gridUrl = "hub.lambdatest.com/wd/hub" 35 | 36 | caps = { 37 | :browserName => "chrome", 38 | :version => "latest", 39 | :platform => "win10", 40 | :name => "LambdaTest ruby google search name", 41 | :build => "LambdaTest ruby google search build", 42 | :network => false, 43 | :visual => false, 44 | :video => true, 45 | :console => false 46 | } 47 | 48 | puts (caps) 49 | # URL: https://{username}:{accessToken}@hub.lambdatest.com/wd/hub 50 | @driver = Selenium::WebDriver.for(:remote, 51 | :url => "https://"+username+":"+accessToken+"@"+gridUrl, 52 | :desired_capabilities => caps) 53 | end 54 | 55 | 56 | 57 | def test_Login 58 | """ 59 | Setup remote driver 60 | Params 61 | ---------- 62 | Execute test: navigate to https://lambdatest.github.io/sample-todo-app/ 63 | Result 64 | ------- 65 | print Success Message 66 | """ 67 | puts("Opening 3 tabs") 68 | sleep(2) 69 | @driver.get("https://lambdatest.github.io/sample-todo-app/") 70 | @driver.execute_script("window.open('https://google.com/')") 71 | @driver.execute_script("window.open('http://www.pdf995.com/samples')") 72 | tabs = @driver.window_handles 73 | assert_equal(3, tabs.size, "Expected 3 tabs but found " + tabs.size.to_s) 74 | sleep(2) 75 | @driver.switch_to.window(@driver.window_handles.last) 76 | @driver.execute_script("window.close('http://www.pdf995.com/samples')") 77 | tabs = @driver.window_handles 78 | assert_equal(2, tabs.size, "Expected 2 tabs but found " + tabs.size.to_s) 79 | sleep(2) 80 | @driver.switch_to.window(@driver.window_handles.last) 81 | @driver.close 82 | tabs = @driver.window_handles 83 | assert_equal(1, tabs.size, "Expected 1 tab but found " + tabs.size.to_s) 84 | sleep(5) 85 | @driver.switch_to.window(@driver.window_handles.last) 86 | elem1 = @driver.find_element(:name, 'li1') 87 | elem1.click; 88 | 89 | elem2 = @driver.find_element(:name, 'li2') 90 | elem2.click; 91 | puts("Test Ran Successfully.") 92 | end 93 | 94 | 95 | 96 | def teardown 97 | """ 98 | Quit selenium driver 99 | """ 100 | @driver.quit 101 | end 102 | 103 | 104 | end 105 | -------------------------------------------------------------------------------- /firefox-with-profile.rb: -------------------------------------------------------------------------------- 1 | # Firefox profile Selenium 3 on Ruby 2 | require 'selenium-webdriver' 3 | require 'test/unit' 4 | 5 | 6 | class LtTest < Test::Unit::TestCase 7 | 8 | def setup 9 | 10 | username= ENV["LT_USERNAME"] || "{username}" 11 | accessToken= ENV["LT_ACCESS_KEY"] || "{accessToken}" 12 | gridUrl = "hub.lambdatest.com/wd/hub" 13 | profile = Selenium::WebDriver::Firefox::Profile.new 14 | profile['browser.download.dir'] = 'C:\Users\ltuser\Downloads' 15 | profile['browser.download.useDownloadDir'] = true 16 | profile['browser.helperApps.neverAsk.saveToDisk'] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.presentationml.presentation, application/vnd.openxmlformats-officedocument.presentationml.slide, application/vnd.openxmlformats-officedocument.presentationml.slideshw, application/vnd.openxmlformats-officedocument.presentationml.template, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.spreadsheetml.template, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.wordprocessingml.template, application/vnd.ms-powerpoint, application/vnd.ms-powerpoint.addin.macroenabled.12, application/vnd.ms-powerpoint.slide.macroenabled.12, application/vnd.ms-powerpoint.presentation.macroenabled.12, appliation/vnd.ms-powerpoint.slideshow.macroenabled.12, application/vnd.ms-powerpoint.template.macroenabled.12, application/vnd.ms-word.document.macroenabled.12, application/vnd.ms-word.template.macroenabed.12, application/vnd.ms-excel, application/vnd.ms-excel.addin.macroenabled.12, application/vnd.ms-excelsheet.binary.macroenabled.12, application/vnd.ms-excel.template.macroenabled.12, application/vnd.ms-excel.sheet.macroenabled.12, application/x-mscardfile, application/onenote, application/msword, application/vnd.ms-xpsdocument, application/octet-stream, application/vn.contact.cmsg, application/doc, application/docx, application/vnd.3m.post-it-notes, application/x-7z-compressed, application/pdf, application/x-pdf, application/mp21, application/mp4, application/x-rar-compressed, application/rtf, application/xml, application/zip, image/x-ms-bmp, image/bmp, image/jpeg, image/png, image/x-xbitmap, image/tiff, text/plain-bas, text/csv, text/html, text/tab-separated-values, text/plain, text/x-vcalendar, text/x-vcard, text/yaml, video/x-msvieo, video/x-f4v, video/x-flv, video/x-fli, video/h261, video/h263, video/h264, video/jpm, video/jpeg, video/x-m4v, video/x-ms-wmv, video/x-ms-wvx, video/vnd.mpegurl, video/mpeg, video/mj2, video/mp4, audio/midi, audio/vnd.rip, audio/x-mpegurl, audio/mpeg, audio/mp4, audio/basic, audio/x-wav, message/rfc82" 17 | profile['browser.helperApps.neverAsk.openFile'] = "application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.presentationml.presentation, application/vnd.openxmlformats-officedocument.presentationml.slide, application/vnd.openxmlformats-officedocument.presentationml.slideshw, application/vnd.openxmlformats-officedocument.presentationml.template, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.openxmlformats-officedocument.spreadsheetml.template, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/vnd.openxmlformats-officedocument.wordprocessingml.template, application/vnd.ms-powerpoint, application/vnd.ms-powerpoint.addin.macroenabled.12, application/vnd.ms-powerpoint.slide.macroenabled.12, application/vnd.ms-powerpoint.presentation.macroenabled.12, appliation/vnd.ms-powerpoint.slideshow.macroenabled.12, application/vnd.ms-powerpoint.template.macroenabled.12, application/vnd.ms-word.document.macroenabled.12, application/vnd.ms-word.template.macroenabed.12, application/vnd.ms-excel, application/vnd.ms-excel.addin.macroenabled.12, application/vnd.ms-excelsheet.binary.macroenabled.12, application/vnd.ms-excel.template.macroenabled.12, application/vnd.ms-excel.sheet.macroenabled.12, application/x-mscardfile, application/onenote, application/msword, application/vnd.ms-xpsdocument, application/octet-stream, application/vn.contact.cmsg, application/doc, application/docx, application/vnd.3m.post-it-notes, application/x-7z-compressed, application/pdf, application/x-pdf, application/mp21, application/mp4, application/x-rar-compressed, application/rtf, application/xml, application/zip, image/x-ms-bmp, image/bmp, image/jpeg, image/png, image/x-xbitmap, image/tiff, text/plain-bas, text/csv, text/html, text/tab-separated-values, text/plain, text/x-vcalendar, text/x-vcard, text/yaml, video/x-msvieo, video/x-f4v, video/x-flv, video/x-fli, video/h261, video/h263, video/h264, video/jpm, video/jpeg, video/x-m4v, video/x-ms-wmv, video/x-ms-wvx, video/vnd.mpegurl, video/mpeg, video/mj2, video/mp4, audio/midi, audio/vnd.rip, audio/x-mpegurl, audio/mpeg, audio/mp4, audio/basic, audio/x-wav, message/rfc82" 18 | profile['pdfjs.disabled'] = true 19 | 20 | caps = { 21 | :browserName => "firefox", 22 | :version => "latest", 23 | :platform => "win10", 24 | :network => true, 25 | :selenium_version => "3.141.59", 26 | :visual => true, 27 | :video => true, 28 | :console => true, 29 | :javascriptEnabled => true, 30 | :build => "firefox-profile-test", 31 | :firefox_profile => profile 32 | } 33 | puts (caps) 34 | @driver = Selenium::WebDriver.for(:remote, 35 | :url => "https://"+username+":"+accessToken+"@"+gridUrl, 36 | :desired_capabilities => caps) 37 | end 38 | 39 | def test_Login 40 | 41 | puts("Download test") 42 | sleep(5) 43 | @driver.get("http://www.pdf995.com/samples") 44 | elem1 = @driver.find_element(:xpath, "//*[contains(text(), 'widgets.pdf')]") 45 | elem1.click; 46 | if (@driver.execute_script('lambda-file-exists=widgets.pdf')) 47 | @driver.execute_script('lambda-status=passed'); 48 | else 49 | @driver.execute_script('lambda-status=failed'); 50 | end 51 | 52 | end 53 | def teardown 54 | @driver.quit 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /todo-click-test.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'selenium-webdriver' 3 | 4 | # Input capabilities 5 | 6 | USERNAME = ENV["LT_USERNAME"] || "{username}" 7 | ACCESS_KEY = ENV["LT_ACCESS_KEY"] || "{accessToken}" 8 | 9 | options = Selenium::WebDriver::Options.chrome 10 | options.browser_version = "108.0" 11 | options.platform_name = "Windows 10" 12 | lt_options = {}; 13 | lt_options[:username] = "#{USERNAME}"; 14 | lt_options[:accessKey] = "#{ACCESS_KEY}"; 15 | lt_options[:project] = "Untitled"; 16 | lt_options[:sessionName] = "Ruby Test"; 17 | lt_options[:build] = "Ruby Job"; 18 | lt_options[:w3c] = true; 19 | lt_options[:plugin] = "ruby-ruby"; 20 | options.add_option('LT:Options', lt_options); 21 | 22 | driver = Selenium::WebDriver.for(:remote, 23 | :url => "https://hub.lambdatest.com/wd/hub", 24 | :capabilities => options) 25 | begin 26 | driver.navigate.to "https://lambdatest.github.io/sample-todo-app/" 27 | driver.find_element(:name, 'li1').click 28 | driver.find_element(:name, 'li2').click 29 | driver.find_element(:id, 'sampletodotext').send_keys("Yey, Let's add it to list") 30 | driver.find_element(:id, 'addbutton').click 31 | enteredText = driver.find_element(:xpath, '/html/body/div/div/div/ul/li[5]/span').text 32 | enteredText == "Fifth Item" ? status = "passed" : status = "failed" 33 | driver.execute_script('lambda-status='+ status) 34 | end 35 | print("Execution Successful\n") 36 | driver.quit --------------------------------------------------------------------------------