Altoros is a big data and Platform-as-a-Service specialist that provides system integration for IaaS/cloud providers, software companies, and information-driven enterprises. Areas of expertise include Cloud Foundry, Hadoop, and NoSQL solutions, as well as Microsoft .NET, Java, Ruby on Rails, and mobile technologies.

Written by altoros
RSS Subscribe

Hi colleagues. Are you concerned about how your application works in IE? Do you believe that checking in latest version using different modes for emulating previous versions is enough? No way! Even is you skip some legacy versions you can face problems with lack of support json, html5 tags, ECMAScript, limit for css selectors and many other problems that can not be detected just by changing browser mode. You will be surprised that some bugs can be reproduced only on certain platform and browser. To find such problems I propose you run your capybara tests in remote browsers.

Using personal virtual machine

  • Install virtual machine with desired browser. You can use your own distributive or ievms
  • Make sure java and desired browsers are installed on VM
  • Run on VM Selenium Server

java –jar selenium-server-standalone-2.35.0.jar -role hub -multiWindow -browserSessionReuse

  • Run on VM webdriver

java –jar selenium-server-standalone-2.35.0.jar -role webdriver -hub http://127.0.0.1:4444/grid/register -port 5555

  • Configure forwarding TCP port 4444 from host machine to guest machine.
  • Add to spec_helper.rb

if ENV[“SELENIUM”] == ‘remote’
   require ‘selenium-webdriver’
    url = http://127.0.0.1:4444/wd/hub
   capabilities = Selenium::WebDriver::Remote::Capabilities.internet_explorer
    Capybara.register_driver :remote_browser do |app|
     Capybara::Selenium::Driver.new(app,
                                    :browser => :remote, :url => url,
                                    :desired_capabilities => capabilities)
   end
    Capybara.server_port = 3010
   ip = ifconfig | grep 'inet ' | grep -v 127.0.0.1 | cut -d ' ' -f2.strip
   Capybara.app_host = http://#{ip}:#{Capybara.server_port}
   Capybara.current_driver = :remote_browser
   Capybara.javascript_driver = :remote_browser
 end

  • Run tests

SELENIUM=remote bundle exec rspec spec/features/

Using browserstack

  if ENV[“SELENIUM”] == ‘browserstack’
    require ‘selenium-webdriver’

<span class="n">url</span> <span class="o">=</span> <span class="s2">"https://</span><span class="si">#{</span><span class="no">AppConfig</span><span class="p">.</span><span class="nf">browserstack</span><span class="p">[</span><span class="s1">'username'</span><span class="p">]</span><span class="si">}</span><span class="s2">:</span><span class="si">#{</span><span class="no">AppConfig</span><span class="p">.</span><span class="nf">browserstack</span><span class="p">[</span><span class="s1">'accesskey'</span><span class="p">]</span><span class="si">}</span><span class="s2">@hub.browserstack.com/wd/hub"</span>
<span class="n">capabilities</span> <span class="o">=</span> <span class="no">Selenium</span><span class="o">::</span><span class="no">WebDriver</span><span class="o">::</span><span class="no">Remote</span><span class="o">::</span><span class="no">Capabilities</span><span class="p">.</span><span class="nf">new</span>
<span class="n">capabilities</span><span class="p">[</span><span class="s1">'browser'</span><span class="p">]</span> <span class="o">=</span> <span class="no">ENV</span><span class="p">[</span><span class="s1">'browser'</span><span class="p">]</span> <span class="o">||</span> <span class="s1">'IE'</span>
<span class="n">capabilities</span><span class="p">[</span><span class="s1">'browser_version'</span><span class="p">]</span> <span class="o">=</span> <span class="no">ENV</span><span class="p">[</span><span class="s1">'browser_version'</span><span class="p">]</span> <span class="o">||</span> <span class="s1">'8.0'</span>
<span class="n">capabilities</span><span class="p">[</span><span class="s1">'os'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Windows'</span>
<span class="n">capabilities</span><span class="p">[</span><span class="s1">'os_version'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'7'</span>
<span class="n">capabilities</span><span class="p">[</span><span class="s1">'browserstack.tunnel'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'true'</span>
<span class="n">capabilities</span><span class="p">[</span><span class="s1">'browserstack.debug'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'true'</span>

<span class="no">Capybara</span><span class="p">.</span><span class="nf">register_driver</span> <span class="ss">:browser_stack</span> <span class="k">do</span> <span class="o">|</span><span class="n">app</span><span class="o">|</span>
  <span class="no">Capybara</span><span class="o">::</span><span class="no">Selenium</span><span class="o">::</span><span class="no">Driver</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="n">app</span><span class="p">,</span>
                                 <span class="ss">:browser</span> <span class="o">=&gt;</span> <span class="ss">:remote</span><span class="p">,</span> <span class="ss">:url</span> <span class="o">=&gt;</span> <span class="n">url</span><span class="p">,</span>
                                 <span class="ss">:desired_capabilities</span> <span class="o">=&gt;</span> <span class="n">capabilities</span><span class="p">)</span>
<span class="k">end</span>

<span class="no">Capybara</span><span class="p">.</span><span class="nf">server_port</span> <span class="o">=</span> <span class="mi">3010</span>
<span class="no">Capybara</span><span class="p">.</span><span class="nf">default_wait_time</span> <span class="o">=</span> <span class="mi">10</span>
<span class="no">Capybara</span><span class="p">.</span><span class="nf">current_driver</span> <span class="o">=</span> <span class="ss">:browser_stack</span>
<span class="no">Capybara</span><span class="p">.</span><span class="nf">javascript_driver</span> <span class="o">=</span> <span class="ss">:browser_stack</span>

<span class="no">RSpec</span><span class="p">.</span><span class="nf">configure</span> <span class="k">do</span> <span class="o">|</span><span class="n">config</span><span class="o">|</span>
  <span class="n">config</span><span class="p">.</span><span class="nf">before</span><span class="p">(</span><span class="ss">:all</span><span class="p">)</span> <span class="k">do</span>
    <span class="sb">`java -jar spec/support/BrowserStackTunnel.jar </span><span class="si">#{</span><span class="no">AppConfig</span><span class="p">.</span><span class="nf">browserstack</span><span class="p">[</span><span class="s1">'accesskey'</span><span class="p">]</span><span class="si">}</span><span class="sb"> 127.0.0.1,</span><span class="si">#{</span><span class="no">Capybara</span><span class="p">.</span><span class="nf">server_port</span><span class="si">}</span><span class="sb">,0 -v &gt;log/browserstack.log 2&gt;&amp;1 &amp;`</span>
    <span class="n">visit</span> <span class="s1">'/'</span>
    <span class="k">until</span> <span class="p">(</span><span class="sb">`curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1:45691`</span><span class="p">.</span><span class="nf">to_i</span> <span class="o">==</span> <span class="mi">200</span><span class="p">)</span>
      <span class="nb">sleep</span> <span class="mi">1</span>
    <span class="k">end</span>
  <span class="k">end</span>
  <span class="n">config</span><span class="p">.</span><span class="nf">after</span><span class="p">(</span><span class="ss">:all</span><span class="p">)</span> <span class="k">do</span>
    <span class="sb">`ps -ef | awk '/BrowserStackTunnel.*,</span><span class="si">#{</span><span class="no">Capybara</span><span class="p">.</span><span class="nf">server_port</span><span class="si">}</span><span class="sb">,/{print $2}' | xargs kill -9`</span>
  <span class="k">end</span>
<span class="k">end</span>

end

  • Run tests

SELENIUM=browserstack browser=IE browser_version=11.0 bundle exec rspec spec/features/

Assamption

Testing in remote browsers assume using selenium webdriver. So you should make sure all your tests are passed using selenium if previously you used webkit or something else.

Conclusion

By using personal VM you have more control on your browsers, you can even change profiles of browsers and other options. But browserstack allows you run your tests on a lot of different combinations of platforms and browsers and even mobile emulators without any additional installations. However speed of testing depends on network latency and selected plan. Fortunately you can run tests in several treads across different browsers or tests. In additional browserstack gives you possibility to do live testing and debug your local application or folder with prototype. You can even check responsiveness.

Guys, feel free to improve my integration capybara with browserstack. I’ll be happy if it inspire someone on creation new gem. You can ask any questions here or in skype of browserstack support.