Best Selenium code snippet using Selenium.WebDriver.Remote.Http.use_proxy
homeaway_spec.rb
Source:homeaway_spec.rb  
...15    116  end17  it 'create account', type: 'signup' do18    report = []19    use_proxy = ENV['proxy'] == 'true'20    driver = Selenium::WebDriver.for :firefox21    if use_proxy22      server = BrowserMob::Proxy::Server.new ENV['BROWSERMOB']23      server.start24      proxy = Selenium::WebDriver::Proxy.new(http: server.create_proxy.selenium_proxy.http)25      caps = Selenium::WebDriver::Remote::Capabilities.chrome(proxy: proxy)26      driver = Selenium::WebDriver.for(:chrome, desired_capabilities: caps)27    end28    site = 'https://www.homeaway.com'29    account_limit = ENV['account_limit'].to_i30    report << "creating #{account_limit} accounts..."31    account_limit.times do32      first_name = ['michelle', 'michal', 'donna', 'jeann', 'carol'].sample33      last_name = ['wong', 'lee', 'chin', 'chan', 'li'].sample34      email = "#{ENV['base_email']}+#{Random.new.rand(1...10000)}@gmail.com"35      while BotAccount.where(email: email, source_cd: 1).present?36        email = "#{ENV['base_email']}+#{Random.new.rand(1...10000)}@gmail.com"37      end38      pwd = 'airbnb338'39      driver.navigate.to site40      driver.find_element(:xpath, '//ul[@class="nav"]//a[@class="traveler-sign-in"]').click41      sleep 342      driver.find_element(:xpath, '//a[contains(., "Sign Up")]').click43      sleep 344      login_form = driver.find_element(:xpath, '//form[@id="login-form"]')45      login_form.find_element(:xpath, '//input[@id="firstName"]').send_keys first_name46      login_form.find_element(:xpath, '//input[@id="lastName"]').send_keys last_name47      login_form.find_element(:xpath, '//input[@id="emailAddress"]').send_keys email48      login_form.find_element(:xpath, '//input[@id="password"]').send_keys pwd49      login_form.find_element(:xpath, '//input[@id="form-submit"]').click50      sleep 351      driver.find_element(:xpath, '//a[contains(., "Continue")]').click52      begin53        acct = BotAccount.new({'email' => email,54                               'password' => pwd,55                               'status' => :active,56                               'source' => :homeaway,57                               'last_run' => Date.yesterday})58        acct.save59        report << "homeaway account created: #{email}"60        sleep 561        logout driver62      rescue Exception => e63        report << e64      end65    end66    driver.quit67    server.stop if use_proxy68    send_report 'signup', report69  end70  it 'scrape properties', type: 'scrape' do71    report = []72    use_proxy = ENV['proxy'] == 'true'73    driver = Selenium::WebDriver.for :firefox74    if use_proxy75      server = BrowserMob::Proxy::Server.new ENV['BROWSERMOB']76      server.start77      proxy = Selenium::WebDriver::Proxy.new(http: server.create_proxy.selenium_proxy.http)78      caps = Selenium::WebDriver::Remote::Capabilities.chrome(proxy: proxy)79      driver = Selenium::WebDriver.for(:chrome, desired_capabilities: caps)80    end81    site = 'https://www.homeaway.com'82    location = URI.unescape ENV['location']83    puts "scraping properties at #{location}..."84    report << "scraping properties at #{location}..."85    driver.navigate.to site86    search_form = driver.find_element(:xpath, '//form[@name="searchForm"]')87    search_form.find_element(:xpath, '//input[@id="searchKeywords"]').send_keys location88    search_form.find_element(:xpath, '//button[@type="button"]').click89    sleep 590    #loop through each result91    base_url = driver.current_url92    per_page = 3093    total = driver.find_element(:xpath, '//div[@class="pager pager-right pager-gt-search"]//li[@class="page"]').text.split('of').last.strip.remove(',').to_i94    last_page = (total.to_f / per_page.to_f).ceil.to_i95    puts "#{base_url}, #{per_page}, #{total}, #{last_page}"96    (28..last_page).each do |i|97      puts "#{base_url}/page:#{i}"98      driver.navigate.to "#{base_url}/page:#{i}"99      sleep 1100      collection = driver.find_element(:xpath, '//div[@class="js-listHitCollectionView preview-container hits js-hits"]')101      listings = collection.find_elements(:xpath, '//h3[@class="listing-title"]//a[@class="listing-url js-hitLink"]')102      puts "page: #{i} -> listings: #{listings.size}"103      report << "page: #{i} -> listings: #{listings.size}"104      result_hash = {}105      listings.each do |listing|106        property_url = listing.attribute('href')107        property_id = property_url.split('/').last.remove('p')108        property_name = listing.text109        result_hash[property_id] = {property_name: property_name,110                                    property_url: property_url}111      end112      result_hash.each do |key, value|113        #skip if already been scraped114        if Bot.where(source_cd: 1, property_id: key).present?115          puts "already scraped property id: #{key}"116          next117        end118        begin119          puts value[:property_url]120          driver.get value[:property_url]121          sleep 1122          address = ''123          list = driver.find_elements(:xpath, '//ol[@class="breadcrumb breadcrumb-gt-header hidden-phone"]//li[@itemtype="http://data-vocabulary.org/Breadcrumb"]')124          list = list[2..-1]125          list.reverse.each_with_index do |l, index|126            address += (index > 0 ? ', ' : '') + l.text.delete('>')127          end128          puts "address: #{address}"129          user_name = driver.find_element(:xpath, '//div[@class="about-the-owner"]//span[@class="owner-name"]').text rescue nil130          property_type = ''131          num_bedrooms = 0132          num_bathrooms = 0133          table = driver.find_elements(:xpath, '//table[@class="table table-striped amenity-table"]').first134          rows = table.find_elements(:xpath, '//tbody//tr')135          rows.each do |row|136            if row.text.include?('Property type') || row.text.include?('Bedrooms') || row.text.include?('Bathrooms')137              if row.text.include? 'Property type'138                property_type = row.text.split(' ').last.downcase139              elsif row.text.include? 'Bedrooms'140                num_bedrooms = row.text.split(' ').last == 'Studio' ? 0 : row.text.split(' ').last.to_i141              elsif row.text.include? 'Bathrooms'142                num_bathrooms = row.text.split(' ').last.to_i143              end144            end145          end146          #store all scraped data147          report << "name: #{user_name}, property id: #{key}, property name: #{value[:property_name]}, property url: #{value[:property_url]}, property type: #{property_type}, bedrooms: #{num_bedrooms}, bathrooms: #{num_bathrooms}"148          bot = Bot.new({'host_name' => user_name,149                         'property_id' => key,150                         'property_name' => value[:property_name],151                         'property_url' => value[:property_url],152                         'property_type' => property_type,153                         'num_bedrooms' => num_bedrooms,154                         'num_bathrooms' => num_bathrooms,155                         'status' => :active,156                         'source' => :homeaway,157                         'address' => address,158                         'super_host' => false})159          bot.save160        rescue Exception => e161          report << "HomeAway error for #{key} #{value}: #{e}"162        end163      end164    end165    driver.quit166    server.stop if use_proxy167    send_report 'scrape', report168  end169  it 'make inquiry', type: 'booking' do170    report = []171    test = ENV['test'] == 'true'172    use_proxy = ENV['proxy'] == 'true'173    driver = Selenium::WebDriver.for :firefox174    if use_proxy175      server = BrowserMob::Proxy::Server.new ENV['BROWSERMOB']176      server.start177      proxy = Selenium::WebDriver::Proxy.new(http: server.create_proxy.selenium_proxy.http)178      caps = Selenium::WebDriver::Remote::Capabilities.chrome(proxy: proxy)179      driver = Selenium::WebDriver.for(:chrome, desired_capabilities: caps)180    end181    site = 'https://www.homeaway.com'182    message_limit = ENV['message_limit'].to_i183    account_limit = ENV['account_limit'].to_i184    messages = ["Hey |name|!\n\nI love your vacation rental. You should check out HostWise.com, (first clean free) I use them and if I refer a free service then I get another free service! :) You can do the same!",185                "Hey |name|!\n\nLooks like your vacation rental would be a perfect fit for our company, HostWise.com. Our company was created by hosts, for hosts. We automate the entire home turnover for you and guarantee a 5 star clean rating every time. Give us a try for first time for free, no strings attached. Not sure if you have many more properties, but if so we do offer enterprise pricing discounts as well! :)",186                "Hey |name|!\n\nI just started using HostWise.com to clean and turnover my property and think your property would be a perfect fit for them too. First service is free, no strings attached, I just got a coupon code for 10% off 3 services if your are interested I can give it to you!\n\nCheers!",187                "Hi |name|!\n\nDo you use HostWise.com to clean and restock your property? The last rental we booked in LA did and the presentation was hotel caliber - from linens and towels to toiletries just for the guests. They're still doing FREE TRIALS at HostWise.com, by the way.\n\nCheers!",188                "Hi |name|,\n\nDo you use HostWise.com for housekeeping, linens, towels?"]189    accounts = BotAccount.where('status_cd = 1 and source_cd = 1').limit(account_limit)190    report << "accounts: #{accounts.count}"191    accounts.each do |account|192      username = account.email193      password = account.password194      puts "logging into account: #{username}"195      driver.navigate.to site196      driver.find_element(:xpath, '//ul[@class="nav"]//a[@class="traveler-sign-in"]').click197      sleep 3198      login_form = driver.find_element(:xpath, '//form[@id="login-form"]')199      login_form.find_element(:xpath, '//input[@id="username"]').send_keys username200      login_form.find_element(:xpath, '//input[@id="password"]').send_keys password201      login_form.submit202      sleep 3203      total_message = 0204      records = Bot.where(source_cd: 1, status_cd: 1)205      report << "records: #{records.count}"206      records.each do |record|207        break if total_message >= message_limit  #STOP when limit reaches208        next if Bot.where(source_cd: 1, host_name: record.host_name, status_cd: 2).present? #SKIP when same host already been messaged209        begin210          puts record.property_url211          driver.navigate.to record.property_url212          sleep 5213          anchor = driver.find_element(:xpath, '//div[@class="pdp-left-col"]//div[@id="summary"]') rescue nil214          unless anchor.present?215            puts "page no longer exist: #{record.property_url}"216            report << "page no longer exist: #{record.property_url}"217            record.status = :deleted218            record.save219            next220          end221          contact_btn = driver.find_element(:xpath, '//a[@class="btn-inquiry-link cta btn btn-inquiry js-emailOwnerButton btn-link"]') rescue nil222          if contact_btn.present?223            contact_btn.click224          else225            driver.find_element(:xpath, '//a[@class="btn-inquiry-button cta btn btn-inquiry js-emailOwnerButton btn-primary cta-primary"]').click226          end227          sleep 3228          booking_form = driver.find_element(:xpath, '//form[@id="propertyInquiryForm"]')229          flex_date_cb = booking_form.find_element(:xpath, '//input[@name="flexibleInquiryDates"]')230          flex_date_cb.click unless flex_date_cb.selected?231          input_num_adults = booking_form.find_element(:xpath, '//input[@name="numberOfAdults"]')232          input_num_adults.clear233          input_num_adults.send_keys '2'234          message = messages[3].gsub '|name|', record.host_name ||= 'Host'235          textarea = booking_form.find_element(:xpath, '//textarea[@name="comments"]')236          textarea.clear237          textarea.send_keys message238          sleep 3239          booking_form.submit unless test240          sleep 5241          unless test242            alert = booking_form.find_element(:xpath, '//div[@id="inquiry-error"]') rescue nil243            if alert.present?244              puts "deactivated property #{record.property_name}"245              report << "deactivated property #{record.property_name}"246              record.status = :deleted247              record.save248            else249              total_message += 1250              puts "contacted host #{record.host_name} for property #{record.property_name}"251              report << "contacted host #{record.host_name} for property #{record.property_name}"252              record.status = :contacted253              record.last_contacted = Date.today254              record.save255            end256          else257            total_message += 1258          end259        rescue Exception => e260          puts e261          report << "HomeAway error for #{record.id}: #{e}"262        end263      end264      if total_message > 0265        account.last_run = Date.today266        account.save267      end268      driver.navigate.to site269      sleep 2270      logout driver271    end272    driver.quit273    server.stop if use_proxy274    send_report 'booking', report275  end276  it 'scrape and book properties', type: 'scrape_and_book' do277    report = []278    use_proxy = ENV['proxy'] == 'true'279    test = ENV['test'] == 'true'280    start_page = ENV['start_page'].present? ? ENV['start_page'].to_i : 1281    message_limit = ENV['message_limit'].to_i282    account_limit = ENV['account_limit'].to_i283    total_all_msg = 0284    messages = ["Hey |name|!\n\nI love your vacation rental. You should check out HostWise.com, (first clean free) I use them and if I refer a free service then I get another free service! :) You can do the same!",285                "Hey |name|!\n\nLooks like your vacation rental would be a perfect fit for our company, HostWise.com. Our company was created by hosts, for hosts. We automate the entire home turnover for you and guarantee a 5 star clean rating every time. Give us a try for first time for free, no strings attached. Not sure if you have many more properties, but if so we do offer enterprise pricing discounts as well! :)",286                "Hey |name|!\n\nI just started using HostWise.com to clean and turnover my property and think your property would be a perfect fit for them too. First service is free, no strings attached, I just got a coupon code for 10% off 3 services if your are interested I can give it to you!\n\nCheers!",287                "Hi |name|!\n\nDo you use HostWise.com to clean and restock your property? The last rental we booked in LA did and the presentation was hotel caliber - from linens and towels to toiletries just for the guests. They're still doing FREE TRIALS at HostWise.com, by the way.\n\nCoupon code: TRYHOSTWISE100\n\nCheers!",288                "Hi |name|,\n\nDo you use HostWise.com for housekeeping, linens, towels?"]289    driver = Selenium::WebDriver.for :chrome290    if use_proxy291      server = BrowserMob::Proxy::Server.new ENV['BROWSERMOB']292      server.start293      proxy = Selenium::WebDriver::Proxy.new(http: server.create_proxy.selenium_proxy.http)294      caps = Selenium::WebDriver::Remote::Capabilities.chrome(proxy: proxy)295      driver = Selenium::WebDriver.for(:chrome, desired_capabilities: caps)296    end297    site = 'https://www.homeaway.com'298    location = URI.unescape ENV['location']299    puts "scraping properties at #{location}..."300    report << "scraping properties at #{location}..."301    accounts = BotAccount.where('status_cd = 1 and source_cd = 1 and last_run < ?', Date.today).limit(account_limit)302    report << "accounts: #{accounts.count}"303    accounts.each do |account|304      username = account.email305      password = account.password306      puts "logging into account: #{username}"307      driver.navigate.to site308      driver.find_element(:xpath, '//ul[@class="nav"]//a[@class="traveler-sign-in"]').click309      sleep 3310      login_form = driver.find_element(:xpath, '//form[@id="login-form"]')311      login_form.find_element(:xpath, '//input[@id="username"]').send_keys username312      login_form.find_element(:xpath, '//input[@id="password"]').send_keys password313      login_form.submit314      sleep 3315      driver.navigate.to site316      search_form = driver.find_element(:xpath, '//form[@name="searchForm"]')317      search_form.find_element(:xpath, '//input[@id="searchKeywords"]').send_keys location318      search_form.find_element(:xpath, '//button[@type="button"]').click319      sleep 5320      #loop through each result321      total_message = 0322      base_url = driver.current_url323      per_page = 30324      total = driver.find_element(:xpath, '//div[@class="pager pager-right pager-gt-search"]//li[@class="page"]').text.split('of').last.strip.remove(',').to_i325      last_page = (total.to_f / per_page.to_f).ceil.to_i326      puts "#{base_url}, #{per_page}, #{total}, #{start_page}, #{last_page}"327      (start_page..last_page).each do |i|328        puts "#{base_url}/page:#{i}"329        driver.navigate.to "#{base_url}/page:#{i}"330        sleep 1331        collection = driver.find_element(:xpath, '//div[@class="js-listHitCollectionView preview-container hits js-hits"]')332        listings = collection.find_elements(:xpath, '//h3[@class="hit-headline"]//a[@class="hit-url js-hitLink"]')333        if listings.size == 0334          listings = collection.find_elements(:xpath, '//h3[@class="listing-title"]//a[@class="listing-url js-hitLink"]')335        end336        puts "page: #{i} -> listings: #{listings.size}"337        report << "page: #{i} -> listings: #{listings.size}"338        result_hash = {}339        listings.each do |listing|340          property_url = listing.attribute('href')341          property_id = property_url.split('/').last.remove('p')342          property_name = listing.text343          result_hash[property_id] = {property_name: property_name,344                                      property_url: property_url}345        end346        result_hash.each do |key, value|347          #skip if already been scraped348          if Bot.where(source_cd: 1, property_id: key).present?349            puts "already scraped property id: #{key}"350            next351          end352          break if total_message >= message_limit  #STOP when limit reaches353          begin354            puts value[:property_url]355            driver.get value[:property_url]356            sleep 1357            address = ''358            list = driver.find_elements(:xpath, '//ol[@class="breadcrumb breadcrumb-gt-header hidden-phone"]//li[@itemtype="http://data-vocabulary.org/Breadcrumb"]')359            list = list[2..-1]360            list.reverse.each_with_index do |l, index|361              address += (index > 0 ? ', ' : '') + l.text.delete('>')362            end363            user_name = driver.find_element(:xpath, '//div[@class="about-the-owner"]//span[@class="owner-name"]').text rescue nil364            property_type = ''365            num_bedrooms = 0366            num_bathrooms = 0367            table = driver.find_elements(:xpath, '//table[@class="table table-striped amenity-table"]').first368            rows = table.find_elements(:xpath, '//tbody//tr')369            rows.each do |row|370              if row.text.include?('Property type') || row.text.include?('Bedrooms') || row.text.include?('Bathrooms')371                if row.text.include? 'Property type'372                  property_type = row.text.split(' ').last.downcase373                elsif row.text.include? 'Bedrooms'374                  num_bedrooms = row.text.split(' ').last == 'Studio' ? 0 : row.text.split(' ').last.to_i375                elsif row.text.include? 'Bathrooms'376                  num_bathrooms = row.text.split(' ').last.to_i377                end378              end379            end380            #store all scraped data381            report << "name: #{user_name}, property id: #{key}, property name: #{value[:property_name]}, property url: #{value[:property_url]}, property type: #{property_type}, bedrooms: #{num_bedrooms}, bathrooms: #{num_bathrooms}"382            record = Bot.new({'host_name' => user_name,383                              'property_id' => key,384                              'property_name' => value[:property_name],385                              'property_url' => value[:property_url],386                              'property_type' => property_type,387                              'num_bedrooms' => num_bedrooms,388                              'num_bathrooms' => num_bathrooms,389                              'status' => :active,390                              'source' => :homeaway,391                              'address' => address,392                              'super_host' => false})393            record.save394            if Bot.where('source_cd = ? and host_name = ? and last_contacted > ?', source, record.host_name, Date.today - 7.days).present? #SKIP when same host already been messaged recently395              puts "already messaged this host #{record.host_name}"396              next397            end398            #book399            contact_btn = driver.find_element(:xpath, '//a[@class="btn-inquiry-link cta btn btn-inquiry js-emailOwnerButton btn-link"]') rescue nil400            if contact_btn.present?401              contact_btn.click402            else403              driver.find_element(:xpath, '//a[@class="btn-inquiry-button cta btn btn-inquiry js-emailOwnerButton btn-primary cta-primary"]').click404            end405            sleep 3406            booking_form = driver.find_element(:xpath, '//form[@id="propertyInquiryForm"]')407            flex_date_cb = booking_form.find_element(:xpath, '//input[@name="flexibleInquiryDates"]')408            flex_date_cb.click unless flex_date_cb.selected?409            input_num_adults = booking_form.find_element(:xpath, '//input[@name="numberOfAdults"]')410            input_num_adults.clear411            input_num_adults.send_keys '2'412            message = messages[3].gsub '|name|', record.host_name ||= 'Host'413            textarea = booking_form.find_element(:xpath, '//textarea[@name="comments"]')414            textarea.clear415            textarea.send_keys message416            sleep 3417            booking_form.submit unless test418            sleep 5419            unless test420              alert = booking_form.find_element(:xpath, '//div[@id="inquiry-error"]') rescue nil421              if alert.present?422                puts "deactivated property #{record.property_name}"423                report << "deactivated property #{record.property_name}"424                record.status = :deleted425                record.save426              else427                total_message += 1428                total_all_msg += 1429                puts "contacted host #{record.host_name} for property #{record.property_name}"430                report << "contacted host #{record.host_name} for property #{record.property_name}"431                record.status = :contacted432                record.last_contacted = Date.today433                record.save434              end435            else436              total_message += 1437            end438          rescue Exception => e439            report << "HomeAway error for #{property_id}: #{e}"440          end441        end442        if total_message >= message_limit  #STOP when limit reaches443          puts "next run starts at page:#{i}"444          report << "next run starts at page:#{i}"445          start_page = i446          break447        end448      end449      account.last_run = Date.today450      account.save451      puts "total sent: #{total_all_msg}"452      report << "total sent: #{total_all_msg}"453      driver.navigate.to site454      sleep 2455      logout driver456    end457    driver.quit458    server.stop if use_proxy459    send_report 'scrape', report460  end461end...driver_configuration.rb
Source:driver_configuration.rb  
...48    def chrome49      no_proxy = config.proxy.no_proxy.tr(",", ";")50      options = Selenium::WebDriver::Chrome::Options.new51      options.headless! if config.headless52      options.add_argument("--proxy-server=#{config.proxy.host}:#{config.proxy.port}") if config.proxy.use_proxy?53      options.add_argument("--proxy-bypass-list=#{no_proxy}") unless config.proxy.no_proxy.empty?54      options.add_argument("--user-agent=#{config.user_agent}") unless config.user_agent.empty?55      options56    end57    # Returns an instance of Selenium::WebDriver::Firefox::Options to be58    # used when registering an instance of Capybara::Selenium::Driver,59    # configured to run using Firefox (the default).60    #61    # For example when initialising the driver like this62    #63    #     my_profile = Selenium::WebDriver::Firefox::Profile.new64    #     my_profile.proxy = Selenium::WebDriver::Proxy.new(65    #       http: "10.10.2.70:8080",66    #       ssl: "10.10.2.70:8080"67    #     )68    #     my_profile['general.useragent.override'] = "Mozilla/5.0 (MSIE 10.0; Windows NT 6.1; Trident/5.0)"69    #     Capybara::Selenium::Driver.new(70    #       app,71    #       browser: :firefox,72    #       options: Selenium::WebDriver::Firefox::Options.new(profile: my_profile)73    #     )74    #75    # You can instead call Quke::DriverConfiguration.firefox which will76    # manage instantiating and setting up the77    # Selenium::WebDriver::Firefox::Options instance based on the78    # properties of the Quke::Configuration instance its initialised with79    #80    #     Capybara::Selenium::Driver.new(81    #       app,82    #       browser: :firefox,83    #       options: my_driver_config.firefox84    #     )85    #86    def firefox87      options = Selenium::WebDriver::Firefox::Options.new(profile: firefox_profile)88      options.headless! if config.headless89      options90    end91    # Returns an instance of Selenium::WebDriver::Remote::Capabilities to be92    # used when registering an instance of Capybara::Selenium::Driver,93    # configured to run using the Browserstack[https://www.browserstack.com/]94    # service.95    #96    # For example when initialising the driver like this97    #98    #     my_capabilites = Selenium::WebDriver::Remote::Capabilities.new99    #     my_capabilites['build'] = my_config.browserstack['build']100    #     # ... set rest of capabilities101    #     Capybara::Selenium::Driver.new(102    #       app,103    #       browser: :remote,104    #       url: 'http://jdoe:123456789ABCDE@hub.browserstack.com/wd/hub',105    #       desired_capabilities: my_capabilites106    #     )107    #108    # You can instead call Quke::DriverConfiguration.browserstack which will109    # manage instantiating and setting up the110    # Selenium::WebDriver::Remote::Capabilities instance based on the111    # properties of the Quke::Configuration instance its initialised with112    #113    #     Capybara::Selenium::Driver.new(114    #       app,115    #       browser: :remote,116    #       url: my_driver_config.browserstack_url,117    #       desired_capabilities: my_driver_config.browserstack118    #     )119    #120    # For further reference on browserstack capabilities121    # https://www.browserstack.com/automate/capabilities122    # https://www.browserstack.com/automate/ruby#configure-capabilities123    def browserstack124      # Documentation and the code for this class can be found here125      # http://www.rubydoc.info/gems/selenium-webdriver/0.0.28/Selenium/WebDriver/Remote/Capabilities126      # https://github.com/SeleniumHQ/selenium/blob/master/rb/lib/selenium/webdriver/remote/capabilities.rb127      capabilities = Selenium::WebDriver::Remote::Capabilities.new128      config.browserstack.capabilities.each do |key, value|129        capabilities[key] = value130      end131      capabilities132    end133    private134    def firefox_profile135      profile = Selenium::WebDriver::Firefox::Profile.new136      profile["general.useragent.override"] = config.user_agent unless config.user_agent.empty?137      profile.proxy = Selenium::WebDriver::Proxy.new(config.proxy.firefox_settings) if config.proxy.use_proxy?138      profile139    end140  end141end...default.rb
Source:default.rb  
...40              request = new_request_for(verb, url, headers, payload)41              retries += 142              retry43            rescue Errno::ECONNREFUSED => ex44              if use_proxy?45                raise ex.class, "using proxy: #{proxy.http}"46              else47                raise48              end49            end50            if response.kind_of? Net::HTTPRedirection51              raise Error::WebDriverError, "too many redirects" if redirects >= MAX_REDIRECTS52              request(:get, URI.parse(response['Location']), DEFAULT_HEADERS.dup, nil, redirects + 1)53            else54              create_response response.code, response.body, response.content_type55            end56          end57          def new_request_for(verb, url, headers, payload)58            req = Net::HTTP.const_get(verb.to_s.capitalize).new(url.path, headers)59            if server_url.userinfo60              req.basic_auth server_url.user, server_url.password61            end62            req.body = payload if payload63            req64          end65          def response_for(request)66            http.request request67          end68          def new_http_client69            if use_proxy?70              unless proxy.respond_to?(:http) && url = @proxy.http71                raise Error::WebDriverError, "expected HTTP proxy, got #{@proxy.inspect}"72              end73              proxy = URI.parse(url)74              clazz = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password)75              clazz.new(server_url.host, server_url.port)76            else77              Net::HTTP.new server_url.host, server_url.port78            end79          end80          def proxy81            @proxy ||= (82              proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']83              no_proxy = ENV['no_proxy'] || ENV['NO_PROXY']84              if proxy85                proxy = "http://#{proxy}" unless proxy.start_with?("http://")86                Proxy.new(:http => proxy, :no_proxy => no_proxy)87              end88            )89          end90          def use_proxy?91            return false if proxy.nil?92            if proxy.no_proxy93              ignored = proxy.no_proxy.split(",").any? do |host|94                host == "*" ||95                host == server_url.host || (96                  begin97                    IPAddr.new(host).include?(server_url.host)98                  rescue ArgumentError99                    false100                  end101                )102              end103              not ignored104            else...spec_helper.rb
Source:spec_helper.rb  
...7require "capybara/poltergeist"8require_relative "../benchmark/fixture_server"9fixture_server = FixtureServer.new10headless = ENV.fetch("HEADLESS", "false") == "true"11use_proxy = ENV.fetch("USE_PROXY", "false") == "true"12Capybara.register_driver :shimmer do |app|13  Capybara::Shimmer::Driver.new(app, use_proxy: use_proxy, headless: headless)14end15Capybara.register_driver :headless_chrome do |app|16  capabilities = Selenium::WebDriver::Remote::Capabilities.chrome(17    chrome_options: { "args" => %w[headless] }18  )19  Capybara::Selenium::Driver.new(20    app,21    browser: :chrome,22    desired_capabilities: capabilities23  )24end25# Capybara.current_driver = :headless_chrome26# Capybara.default_driver = :headless_chrome27# Capybara.current_driver = :poltergeist...use_proxy
Using AI Code Generation
1http_client.use_proxy(proxy_addr: 'localhost', proxy_port: 8080)2options.add_argument('--proxy-server=localhost:8080')3options.add_argument('--proxy-server=localhost:8080')4options.add_argument('--proxy-server=localhost:8080')5options.add_argument('--proxy-server=localhost:8080')6options.add_argument('--proxy-server=localhost:8080')7caps.proxy = Selenium::WebDriver::Proxy.new(http: 'localhost:8080')8caps.proxy = Selenium::WebDriver::Proxy.new(http: 'localhost:8080')use_proxy
Using AI Code Generation
1  proxy = Selenium::WebDriver::Proxy.new(http: 'http://localhost:8080')2  http_client = Selenium::WebDriver::Remote::Http::Default.new(proxy: proxy)3  driver = Selenium::WebDriver.for(:firefox, http_client: http_client)4driver = Selenium::WebDriver.for(:chrome, http_client: http_client)5  proxy = Selenium::WebDriver::Proxy.new(http: 'http://localhost:8080')6  driver = Selenium::WebDriver.for(:firefox, profile: profile)7driver = Selenium::WebDriver.for(:chrome, profile: profile)Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
