📜 ⬆️ ⬇️

The Tale of Selenium Testing

When your team is faced with the task of writing a really large project, it always becomes a task to test the written code. If the server is relatively easy to test, then JS code is often impossible to test due to its nature.

JS decided to get around a great development team that created a unique product of its kind that allows you to write acceptance tests that will interact directly with the browser. They turned out very, very cool, but the rakes were, are and will be. For this, I will talk about the rakes, which I usually meet while working with this wonderful product.


And let's talk about the following:
  1. Test Architecture
  2. Running tests
  3. Code base
  4. Timeouts
  5. Interaction test code and browser.
  6. Different execution environments
  7. Bugs drivers and Selenium

')
This article is more focused on beginners than on professionals.



Test Architecture


Often the question is how to design tests to reduce the writing of code and not to do copy-paste. Commonly used are generic files that inherit the unitTest class of your language. We always have the opportunity to change the behavior for a group of tests without affecting other groups. For example:

unittest.py seleniun/ - generic.py menu/ - generic.py - menuActionTest.py - menuDisableTest.py articles/ - generic.py - articlesLoadTest.py - articlesActionsTest.py 


When you separate the functionality into categories, put it in directories, and create your own generic in each directory. Each test must inherit the generic class with which it lies in the directory. Generic may even be empty, in the future it will come in handy, I give a guarantee.

Running tests


The difference between unit tests and acceptance tests lies in the fact that in unittest algorithms and logic are usually tested, and acceptance tests already test the system itself, with real data, which is obtained from a running database.

Database

The task of all tests to be isolated from each other. So for each test run, you need to create a new test database. Each unittest language functional has a setUp method, which is called before running the test. Try changing the settings in it before initializing the database. So you can completely isolate from other tests and existing data, and if you still use caching mechanisms, clear the cache or use the empty cache storage as with the database.

Fixtures

In fact, this is the data that will be written to your test database, so that the browser opens the site and does not receive errors. Baseline data should be written in the setUp method of the main generic `a. And the rest of the record as the data needs in the tests themselves.

Saving database data

Create a path for possible debag, it often happens that tests for not understandable reason stall with an error, it often happens that you need to save the database and leave the browser alive. Maybe you and sleep fit? If you have made the database saving functionality, do the same functionality for cleaning old databases. For me, for example, mongoDB could create 80GB of databases in a week of development.

Connection Transfer

When the browser opens, remember, it has nothing to do with your test. If selenium simply opens the browser and starts performing operations, most likely it will perform them on an existing project. Send to the browser the parameters that will help identify the desired test database. At one time, the browser will have to transmit information to the server.

Code base


Here you will need to sit down and think about how to make life more beautiful for yourself.
For example, you have an asynchronous multi-user real time application (game), you need to write tests where two or more users can interact. See if it will be convenient for you to switch between browser windows using a driver? No, write your simple implementation.

Need your own asserts? For example, check the existence of an element in the tree?

It is likely that your project has its own characteristics and you need to prepare some functional part of the tests for this.

Timeouts


Always when testing, we will need to perform timeouts. Tolley it will be loading the page, ajax or socket request to the server. With the loading page, the drivers contain an inline method for waiting. But with ajax and socket requests everything is more complicated. You will need to write the counter of sent requests and received answers. When you send a request, the counter is incremented, when the answer comes, decremented. In the most generic `we implement a method that will check this counter, and if it is not 0, then let it wait, but limit the wait, the response from the server may not come.

Interaction test code and browser.


Sometimes we will need to receive states or some data to check their values ​​with the expected ones. Immediately write ways to get this data. For example, if you use AngularJS, then to get access to the data of the service, write a layer for this.

If there is no possibility to emulate user actions using tests, then you need to call handlers for these events. Allow tests to knock on JS executable code.

Different execution environments


Most often it turns out that the test works on your computer, but does not work for others. This may be related to the OS, the version of the selenium, the resolution of the monitor, and even the browser versions. I advise you, get yourself a cheap VPS on which you can run tests. On server Linux, you can run tests directly from the browser. For example, the man who helped me www.alittlemadness.com/2008/03/05/running-selenium-headless In addition, you will save your time while waiting for the completion of the tests. Do not forget, the browser is very demanding on RAM, add a good swap margin.

Bugs drivers and Selenium


First, visit code.google.com/p/selenium/issues/list and run around the bugs.

Second, if you are sure that it is obliged to work, go through the JS code debugging. It is likely that you will find a place that bazht through the fault of the selenium itself or the driver. For example, I stumbled upon a bug when, when a mouse move, chomedriver does not transmit the which event, which is responsible for the current mouse button.

It is also a known nuisance, which is connected with scrolls. Selenium can not scroll anything except the body scroll. There are two solutions. Write a scroll function or on the server to set a large resolution virtual monitor.

Often tests can fail due to timeouts. Use the functionality that can repeat the tests. Give the server the opportunity to restart the test 3 times on the server. If the test fails, it means that it is not working, if it crashes periodically, there are problems in timeouts that are not visible to the user, but visible to the selenium itself.

Lastly


Test and always test fixed bugs with tests. It does not matter how long the tests will be performed, the main thing is that your child always works stably.

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


All Articles