📜 ⬆️ ⬇️

How to raise test project on windows 10 (Cucumber + capybara + selenium-webdriver)

The article is intended for familiarity with auto-testing and scanning of the environment in Windows 10 and is intended for those who know Cucumber + Capybara + Selenium-webdriver even a little . The idea for the article came about because of the differences in deploying the environment on Windows 10 and Linux.


A few words about gem-ah (libraries) Cucumber + Capybara + Selenium-webdriver, which are needed to run and fill UI tests with tests.

Cucumber


ucumber is a heme that allows writing tests in human language. To do this, use the Gherkin notation, which defines the structure and rules for writing scripts. You can read in detail here.
')

Capybara


Capybara is a gem that allows you to search / click / ... by browser elements. Those. It is a link between the Cucumber steps (steps) of the test, and the webdriver (an instance of the browser being called). Here you can look at the methods of this heme.

Selenium-webdriver


Selenium-webdriver is a tool to automate web browser actions. Essentially, this is an instance of the browser.

Preliminary actions


Description of preliminary actions
For convenience of demonstration we will use RubyMine. You can download a trial version for 30 days.

Download Firefox and Chrome to run tests.

Download and install Git (version control system or similar VCS, official website ). But Git is needed when you already have a project or you want to store your code in the Git system.

So, let's begin


We already have RubyMine installed.

  1. You need to install the Ruby language itself. To do this, go here and set RubyInstaller. I chose the latest release (RubyInstaller 2.5.1-2) with the DevKit package. Through this setup package, you can run tests from the console, as in Linux, as well as flexibly manage gems.
  2. The RubyInstaller package is installed and we proceed to the configuration.
    If we want to create a new project, then open RubyMine and create an empty project, specifying the installed Ruby.

  3. Next, we need to create just such a folder and file structure according to the annotation


    Described in detail here.
  4. Gemfile - contains a list of gems that are used in the project

    Here are the contents of our gemfile, with the most basic gems for UI tests.
    source 'https://rubygems.org' gem 'cucumber' gem 'capybara' gem 'selenium-webdriver' gem 'chromedriver-helper' 


    These 4 gems must be specified in the gemfile.
    gem 'chromedriver-helper' is a chrome driver allowing you to run testers on Chrome
    With this Gemfile, you need to install our favorite gems. It is easiest to install on Windows from the RubyMine interface: Tools -> Bundler -> Install menu . A bundler is also a gem, but serves to control gems. But it can also be done from the command line, which is located in the program menu under the name Start Command Prompt ...
    By the way, using this command line, you can run tests bypassing RubyMine.
  5. The env.rb file is a key rb file for running UI tests. When initializing variables and test files, env.rb will be the very first. It registers the browser on which the testers will run. Ready example env.rb, where the registration of Chrome, Firefox takes place or let us make it clear that we don’t need a browser at all to run tests.
    Cases when a browser is not needed - we check rest requests, integration testing, although it is considered that Cucumber tests are not quite suitable for this.

    Writing env.rb
     require 'capybara/cucumber' require 'selenium-webdriver' Capybara.register_driver :driver do |app| case ENV['DRIVER'] when 'chrome' Capybara::Selenium::Driver.new(app, :browser => :chrome) when 'without_browser' Capybara.default_driver = :mechanize else client = Selenium::WebDriver::Remote::Http::Default.new Capybara::Selenium::Driver.new(app, :browser => :firefox, port: 10000 + Random.rand(1000), http_client: client) end end Capybara.default_driver = :driver Capybara.default_selector = :xpath 


    Also, here you need to mention the issue of versioning Firefox.
    If you have Firefox version 46 or lower installed, then to run the tests correctly, you need gem 'capybara' version '2.53.4' or lower.

    If the Firefox version is higher than 46, then it works according to other principles on the basis of “geckodriver” and therefore you need to install a geckodriver in order to correctly launch the test .

    A tour of the reasons for what you need geckodriver
    Before version 47, the Firefox automation driver was just an extension that was included in every client. But this extension was removed due to a policy change that now requires all extensions to be signed by Mozilla.

    Marionette is a new driver that comes with Firefox. This driver has its own protocol, which is incompatible with the Selenium / WebDriver protocol.

    Geckodriver is an application server that implements the Selenium / WebDriver protocol. He translates the Selenium commands and redirects them to the Marionette driver.

    An important nuance , after installing “geckodriver”, it is necessary to prescribe the system paths in order for our “geckodriver” to be found when executing env.rb.

    system paths


  6. Then you just have to write a test test and run it on Chrome, Firefox and without a browser :). For example, let's write a few steps to enter mail.ru mail

    Description of steps
     # encoding: UTF-8 # language: ru Given(/^   "(.*?)"$/) do |page| visit page end Given(/^  "(.*?)"   c id "(.*?)"$/) do |text, field_id| find("//input[@id='#{field_id}']").set(text) end Given(/^  "(.*?)"     id "(.*?)"$/) do |text, select_id| find("//select[@id='#{select_id}']/option[text()='#{text}']").click end Given(/^    "(.*?)"$/) do |text| find("//input[@value='#{text}']").click end Given(/^ (\d+) (?:|)$/) do |sec| sleep sec.to_i end 


  7. And also the cucumber test itself

    test.feature
     # encoding: UTF-8 # language: ru :   :       "https://mail.ru/"    "dorian.grey.2019"   c id "mailbox:login"    "********"   c id "mailbox:password"    "@inbox.ru"     id "mailbox:domain"      ""   5  


  8. It remains only to check all our efforts and enjoy the success of the UI tests (in example 1 test) :). Remained the last setting - setting up the run tests. Go to the menu RubyMine -> Edit Configurations -> Runner Options - Here we make the driver selection :)
    ENV ['DRIVER'] of env.rb is the launch setting. And we just need to specify in the Runner Options “DRIVER = firefox” or “DRIVER = chrome”.

Run



That's all, successful Cucumber tests!

Source: https://habr.com/ru/post/423123/


All Articles