📜 ⬆️ ⬇️

Selenium: for apple lovers

Hello! This is my new article on Selenium. Earlier, I already spoke in detail about the organization of a scalable Selenium cluster ( Part I , Part II ). Then we considered the issue of using Selenium as a tool for debugging autotests ( one , two ). Finally, we managed to create order out of chaos on Windows ( link ). Today we will deal with apples, more precisely with one big Apple (i.e. with Apple).




Although Selenium is a relatively simple tool, life becomes more complicated when we try to run automated tests in browsers from Cupertino: Safari under MacOS and mobile Safari under iOS. To fully understand the browsers for desktop operating systems, let's talk today about Safari under MacOS. Today, the latest versions of Safari are developed only for MacOS. Historically, work with Safari was implemented in Selenium using a browser extension that translated Selenium commands into internal browser commands. Initially, the extension was loaded into Safari automatically. Later, due to a change in security rules in Safari, you had to install the extension once manually. Finally, with the release of Safari X, everything has changed significantly. Safari now uses a stand-alone web driver process - safaridriver , similar to chromedriver in Chrome and geckodriver in Firefox. To run tests in Safari:


  1. Run safaridriver (usually installed with Safari and located in /usr/bin/safaridriver ) on a free port, for example, 4444. The command to run will be this:


     $ /usr/bin/safaridriver --port 4444 

  2. Run tests using the following Selenium URL:


     http://localhost:4444/ 

  3. Stop the running driver process.

It sounds easy, is not it? And so it is! Nevertheless, there is a problem - safaridriver can work with only one copy of Safari in parallel. How to overcome this limitation? - Run the driver several times on different ports and run tests on different URLs with these ports. Simple, but requires a lot of manual work. Let's automate this process! How could this work? That's how:



  1. You start the server that provides the Selenium API and tell it at the start where the safaridriver is located.
  2. When a request comes in for a new session, the running server searches for a free port and occupies it. Then it starts the safaridriver process on this port and proxies all subsequent requests to the same place.
  3. When a request comes in to close a session, the safaridriver process also stops.

The described algorithm is very simple, so it should not require the development of a complex server, is it? If you take as a server the current standard - the Selenium server, then you will use an overly complex tool for such a simple task. Why is this so:


  1. Selenium server - huge as an elephant! The size of the distribution starts from 20 MB.
  2. It requires installing Java - another giant creature in your zoo. With all sorts of pop-up windows about the update and the graphical interface of the 90s.
  3. He is always hungry and eats memory for no reason.
  4. It is rather poorly documented and does not work out of the box. You need to be a wizard to make it work.

In short, let me show you a more suitable tool - Selenoid . Selenoid is a lightweight daemon, created to completely replace the fat Selenium server. Getting Selenoid with Safari is easy:


  1. Copy the slice of JSON below to a file (for example, in ~/browsers.json ):


     { "safari": { "default": "latest", "versions": { "latest": { "image": ["/usr/bin/safaridriver"] } } } } 

  2. Launch and download the Selenoid executable:


     $ curl -Lo ~/selenoid \ https://github.com/aerokube/selenoid/releases/download/1.3.3/selenoid_darwin_amd64 $ chmod +x ~/selenoid $ ~/selenoid -conf ~/browsers.json -disable-docker 

  3. Run tests using URL:

     http://localhost:4444/wd/hub 

No longer need to install Java and company! But if you, like me, do not want to do this with your hands, you can get a similar result using a single-line script:


 $ curl -Lo ~/cm https://github.com/aerokube/cm/releases/download/1.2.1/cm_darwin_amd64 && \ chmod +x ~/cm && \ ~/cm selenoid start --browsers safari 

If you wish, you can also download and run the lightweight UI for Selenoid. Read more about this in the documentation .


Simple Selenium testing in Safari has never been this close. Have a nice day!


')

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


All Articles