Hi Lukáš,
I'm not sure I follow you exactly, but I can give you a very basic rundown of how conditional logic is applied in nightwatch.
Nightwatch is an API that sits on top of selenium, providing a set of commands that users can use in their tests to build a command queue that gets executed asynchronously during a test run. This command queue is dynamic and additional commands can be added as tests are running. A test run starts with the initial set of browser (the value passed into a test case function) commands as defined in the main body of a test case. Once the run starts, additional commands are added within the current location of the command queue through callback functions when additional commands are called from within them.
'sample test': function (browser) {
browser
.url('example.com', function (result) {
this.click('button');
})
.end();
} In the above example, the test starts with the commands [url() and end()]. After the test is running, and the url() command completes, the click() command is added changing the command queue to be [url() <completed>, click(), and end()]. The execution of the js calls end() before click(), but in performing those commands in the test run, click() now comes before end().
Callbacks are what provide you with the ability for in-test conditionals. First, they allow you to - while the test is running - be able to retrieve real-time data from the page in test. Then, with that data you can make decisions about what additional commands to include within the test. For example, maybe your site might use anchors <a> or <buttons> for navigation, but you can't be sure until the test runs when you need to click on one. So what you can do something like:
browser.element('css selector', 'button', function (result) {
if (result.status === 0) {
this.click('button');
} else {
this.click('a');
}
});
What I'm not sure about for you is what you mean when talking about sync and async. Things will need to be async since the test takes time to go through and run commands - this all happening well after the initial pass of your test function (no page data is available in that first, sync pass). The callbacks (naturally async) are where you have an opportunity to make choices.
Depending on what you're after, there may not be a command that gives you what you need exactly. But if thats the case, you have the opportunity to create a custom command that gets you what you need.