📜 ⬆️ ⬇️

CodeceptJS - modern end2end tests for NodeJS

image


In the world of NodeJS, total chaos is created, new frameworks appear every day, the dependencies of the usual package.json grow by hundreds of megabytes, and the library that you added to the project yesterday, today is already obsolete. And if in the world of frontend frameworks, there are already obvious favorites: AngularJS, React, Vue, Ember, then what is completely incomprehensible for acceptance testing. Each framework provides its own syntax and cool features, and each has its own characteristic set of problems. For example, each in its own way implements interaction with the browser, each struggling in different ways with asynchrony.


Oh, this notorious asynchrony ...


Yes, asynchrony is cool, but in the context of end-2-end acceptance testing, asynchrony is an eternal problem. Tests should have a linear view, the sequence of commands: I open the page, I click the button, I want to see the text I need. Each such command in the context of JS will be executed asynchronously, and therefore such a construction will be written down by a chain of calls:


client .url('https://github.com/nightwatchjs/nightwatch') .waitForElementVisible('body', 1000) .assert.visible('.container h1 strong a') 

(all commands are mixed into one, it is difficult to understand which commands are available)


or using multiple yield or await :


 function*() { yield browser.url('http://google.com'); yield browser.click('#link'); var title = yield browser.getTitle() console.log(title); } 

(the excess of control structures makes the test less readable).


A test is first of all a code, but it should not only work, it should be easy to read and easily updated.
Ideally, even non-technical personnel, such as a manager or a business analyst, should understand what exactly is being tested in a project and how. Describing the end2end tests at a low level, we deprive ourselves of this opportunity.


To solve the problems described above (multi-instrumentality and asynchrony) CodeceptJS appeared.


This is a framework for describing high-level DSL tests. The tests themselves will be performed by one of the popular libraries to choose from: webdriverio , Protractor , NightmareJS . In most cases, you will not have to learn the syntax of each of the libraries, but use the ready-made universal API. Here is a simple test in CodeceptJS:


 Scenario('search github', (I) => { I.amOnPage('https://github.com/search'); I.fillField('Search GitHub', 'CodeceptJS'); I.pressKey('Enter'); I.see('Codeception/CodeceptJS', 'a'); }); Scenario('register', (I) => { I.amOnPage('https://github.com'); within('.js-signup-form', function () { I.fillField('user[login]', 'User'); I.fillField('user[email]', 'user@user.com'); I.fillField('user[password]', 'user@user.com'); I.click('button'); }); I.see('There were problems creating your account.'); }); 

How does CodeceptJS allow you to write linear scripts for tests? I will reveal the secret: the global chain of promises is used everywhere, to which new and new teams are added. Thus, CodeceptJS can easily implement the same PageObjects :


 Scenario('register', (I, RegisterPage) => { RegisterPage.open(); RegisterPage.register({login: 'User', email: 'user@user.com', password: '123435'}); I.see('There were problems creating your account.'); I.click('Explore'); }); 

Thus, we moved the commands from the previous example to the separately created class.


And indeed, it turned out readable: all commands are described as actions from the first person, selectors minimally overload the code with technical information. It would seem that you can do to make the test even more readable. Let's write it in Russian !


 Scenario('  ', () => { ._('http://yandex.ru/referats'); .("  "); ._(''); .(" "); .("  "); }); 

Yes, this is a valid JS code that can be executed in NodeJS without any transpilers!


As already mentioned in the configuration, you can easily choose the engine to complete the tests. You want a stable and good webdriver API, use WebDriverIO , work with Angulyar - turn on Protractor , you want to speed up the tests 3 times , abandon the webdriver, and launch the application inside Electron, use Nightmare.


Of course, it is impossible to embrace the uncomfortable and write the full implementation of all available actions for all engines. But CodeceptJS allows you to write your own extensions and complement the current functionality. Yes, this way you will have to work a little with the low-level API of the same webdriverio, but by implementing the necessary functionality you can use it in scripts.


What else is in CodeceptJS:



How to install CodeceptJS? Not so difficult


 [sudo] npm install -g codeceptjs codeceptjs init 

Then you will need to select one of the drivers to complete the tests (Protractor, webdriverio, Nightmare) and install the packages for them. The codeceptjs init command will help you with this and tell you how to write the first test.


The project has been around for almost a year now. CodeceptJS has been used in British and Brazilian state-owned companies. It is free and free as MIT.
If you have not tried it yet, you have a great opportunity! If you already use - write your comments. And also join the project development.


')

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


All Articles