Hello, reader! If you have been writing Selenuim tests a little more complicated than a couple of input fields and one button, then this article may be useful.
Surely the sense of incorrectness of what is happening is when a test case of a couple of minutes suddenly refused to work, forcing to poke blindly to find a broken css or xpath selector in a complex Single Page Application, running this slow script over and over again only to wait for the log output in the console. It turns out that you can write Selenium tests with comfort!
About Selenium. Once it was just a plugin for FireFox. You press the "record" button, and you begin to pull the interfaces of the test site (form, links). Practically without knowledge of programming, the resulting script can be edited by substituting the desired values ​​of the checks. All - the test is ready! Launch every time after deployment, and check what you moved with your elbow. Then in Selenium added API for different languages. In particular, there is also for JAVA. Through the API, you can do the same thing: launch the browser with the required site, check the values ​​passed in forms, follow links, etc. The trouble is that the script runs slowly. To test the tests, you need to restart the script, and this is shaped torture. Clojure (on top of JAVA) allows you to execute pieces of code in a running program through the REPL directly in the editor!
The project is available on GitHub , features:
In the project's dependencies, you can see the library - clj-webdriver, this is the main tool for working with the web driver, its documentation .
Let's analyze a simple test example-selenium-project.tests.gosuslugi-main :
(deftest gosuslugi-main-search-form (profile/open-browser "https://www.gosuslugi.ru/") (try (->> ($ ".index-slider-search input") (type-text "") (->>keys Keys/ARROW_DOWN) (->>keys Keys/ARROW_DOWN) (->>keys Keys/ENTER)) (when-not ($ ".title_search") (throw (Exception. "redirect to search result, error"))) (swap! profile/tests-success inc) (is true) (log/info "gosuslugi-main-search-form -> ok") (catch Exception e (log/info "gosuslugi-main-search-form -> fail" (.getMessage e)) (swap! profile/tests-fail inc) (is false))))
(deftest gosuslugi-main-search-form ...)
deftest
- macro, imported from the clojure.test
library. This is Clojure’s built-in test writing library. It has a standard for tests functionality. The deftest
macro simply creates a function.
The test itself is wrapped in (try ... catch)
for convenience in order to interrupt the execution of a form by calling Exception
, or in any non-standard cases (for example, if an element is not found), and execute a form in catch
that handles a test failure.
(->> ($ ".index-slider-search input") ...)
->>
- standard Clojure-macro for writing more readable code "upside down." Without it, the script would look like this:
(->>keys Keys/ENTER (->>keys Keys/ARROW_DOWN (->>keys Keys/ARROW_DOWN (type-text "" ($ ".index-slider-search input")))))
The macro ->>
takes a list of forms and opens them, passing successively the result of the execution, as the last argument, to the following form. To use the macro ->>
, I made several wrapper functions that follow a simple convention - they take an element object as the last argument and return it.
(swap! profile/tests-success inc)
and (swap! profile/tests-fail inc)
are simple counters of successful and failed tests.
The simple answer is a level of abstraction over complexity. You must have gone to Wikipedia , looked at the syntax of this Lisp dialect, and are going through the first stage of accepting the inevitable. Whether you go through it or not is your business, I can only give some advice based on experience.
If you spent your youth being a hippie in America in the 60s, you could taste the spirit of free democracyand soft drugs; if it wasn’t enough, nothing terrible - you can sense this spirit using in the work of the REPL Clojure.
REPL Clojure - takes the writing of Selenium tests to a completely different level! The key feature of this REPL from any other is that you can write code in an already running application without losing its states. This tool allows you not only to make working css or xpath selectors, but also to carry out tests in any order you want, and to check the performance of any of the nodes of a large test case without losing the status of a complex Single Page Application.
Execution of the code of our project line by line through the REPL in the editor LightTable
Clojure code editor - almost all popular editors and IDEs have plug-ins to support Clojure, I stopped at IntelliJ IDEA + Cursive , as it has the least low entry threshold. A small tutorial on setting up and using REPL in Cursive.
Also popular:
Leiningen is a dependency manager like npm from the world of Node.js. With the difference that, as a dependency, Leiningen also presents Clojure itself. Therefore, it will be enough for us to install Leiningen , he will download Clojure himself.
The main Clojure repository , you can also use libraries from the maven repository (untold wealth of the Java community). Selection of useful libraries .
After that you can create a new Clojure project by typing in the console:
lein new app example-selenium-project
Leiningen will create for us the entire project tree and configuration file - project.clj, (in npm its analogue is package.json).
Also in Leiningen, work with profiles is very well implemented, having defined a number of their profiles in the :profiles
section of the project.clj file, you can very flexibly divide the work of the program in develop or production environments, and not only.
You can run the application with any number of profiles, for example:
lein with-profile +windows,+selenium run
Or compile the jar with the necessary profiles:
lein with-profile +windows,+phantom uberjar
I would venture to suggest that Clojure is by far the most convenient tool for writing Selenium tests. Due to two factors: an interactive REPL and macros that allow you to create your own concise DSL subproject. As practice shows, you may not know the Clojure language itself and work successfully with DSL on the facade. This opens up good opportunities for the rapid connection of new specialists to the writing and support of tests.
yashaka
I have suspicions that for tray ketch in the test logic can not weakly pounce ... any complex constructions such as ifs, cycles, trays ketches are considered bad form in writing tests
in our example, this is in fact the logging functionality in essence, and such things usually endure to some sort of abstraction ...
maybe if there is a desire to show convenience, then it is worth showing how to render such a logging code into some kind of function, or if the function of the toli macro is additional around deftest
seryh
Well, unit tests and selenium tests, different things. in practice, try in selenium is very convenient. since almost all test rollbacks are due to outdated selectors (edited) or even some sudden garbage that is difficult to predict
yashaka
the logic of “not using complex constructions” grows from the fact that there are a lot of tests, they can change, and a large number of people write them - so there is no time to think much about the flow - tests should be literally obvious
at least as far as I understand it myself :)
in selenium tests - this is even more important, since tests are harder on their own ...
seryh
yes room for improvement big) hands have not reached yet
yashaka
or some other sudden stuff that's hard to predict
sudden bullshit is anyway
and the flying one is already a fallen test.
simply built-in seleniums - little informative
that's why people write their wrappers around selenium
as if because the test logic remains simple and obvious ...
seryh
I'm there in the article noted in the conclusion that you can write a simple dsl
and in the example, so knee crafts
yashaka
I am an automator myself, not a developer, and I hang out here in chat rooms on this topic,
and there, as soon as some newcomer shows a test with ifs and trays with ketches, mountains of morals immediately flow ...
the point here is not what is good and what is bad ...
but the fact is that we have a loud message for the informal language, and the people will start to criticize :)
and it turns out we give them with our “knee-up fakes” just to give even more reason
I’ve got a curse now, so unfortunately I don’t have time to help ... But if we don’t hurry, then somewhere in a week or two, I would be able to contribute to this article ...
motor4ik
And the case is the most important is not considered in the article? That the test fell and how to fix it with a repl?
I understand it was a killerphic article no?
seryh
publication on Monday is scheduled. In general, if you take off, you can write down the second article
and your dsl for testing gash = P
And so, a small example now, quite understandable to a beginner
PS I speak only as a mastermind and editor, the author of the article is seryh .
Source: https://habr.com/ru/post/308228/
All Articles