Показаны сообщения с ярлыком selenide. Показать все сообщения
Показаны сообщения с ярлыком selenide. Показать все сообщения

воскресенье, 7 июля 2019 г.

Getting started with Selenide

Today we are talking about how to create a simple application based on selenide framework.

Show me the code!©

First of all, the selenide framework is a wrapper of selenium framework with a more convenient API. Very often it is used to test UI. However, there is another way of using selenide. Recently, I have faced that our partner did not provide any API except UI panel. It was very sad news for me because I have to scope with that strict.

Within bounds of this task, I had to make a process of getting some reports automatic. The first thing which came to my mind is using selenium or something like that. Unfortunately, selenium framework has quite rigid and cumbersome API and I had a look at selenide. Otherwise, work with it seems to be more enjoyable. It has fluent API and simple and clear work mechanics.

The first step is adding selenide to our dependency manager.

Next step is to implement some business logic. In this example, we create a program, which would enter LinkedIn.com and accept all friend requests. As you can see, it was pretty trivial and not rocket science.

The code below does what we want to do.

First of all, we need to login to the site. Method login does it.

Then we need to go to a page "My network" and find all requests there and accept them. According to name, the method acceptRequests does what we expect:

In addition, we have to create a web driver. A web driver provides some API for working with a web browser. Almost every popular browser has own web driver implementation. However, we use a remote web driver. I will explain why we do it later.

How it works?

For sure, we do not run our app outside of a docker container. In order to do it, we need a docker image. I have chosen the official selenium docker image, you can choose what you wish. This docker image contains a chrome browser and a web driver for working with this browser.

I feel I have to give more details about how it works. Have a look at the picture below.

This diagram shows how we work with a chrome browser. There are several elements of an interaction chain. On the client-side (our program) we use remote driver API. Apparently, a remote driver connects to a chrome driver. A chrome driver is provided by browser maintainers so it knows exactly how to deal with a chrome browser.

You can ask me: "Why it's so difficult?". I agree with you. On the face of it, this architecture is quite overwhelmed, but there are several points of it. We use a remote browser because it lets us connect to a browser running on a docker container. Moreover, this approach has some pros. For instance, we want to use a browser which does not have a version for our target platform. (A target platform is Linux or Mac OS, but we want to use Microsoft Edge Browser located on a remote host)

Run app and enjoy

The last step is to run our application and see a result of its work. In order to do it, we should to run the docker image:

I hope this topic was useful to you 😉. Here is code hosted on Github.