Using if statement inside it function

1,747 views
Skip to first unread message

Kevin Gatchalian

unread,
Feb 27, 2015, 4:08:40 AM2/27/15
to moc...@googlegroups.com
Is it possible to insert an if statement or for loop inside the it statement of mocha? I am writing some automated test using chai assertion library with mocha and runs in wd.js web driver library. I'm writing it using chain commands. My goal is to find the text on a button and do some actions based on its value. The code looks something like this:

describe('Some test', function() {
it('do this test', function () {

return browser

.elementByCssSelector("section.widgets > div.widget.primary > div > div.top > a.huge.button", function() {
if (driver.elementByCssSelector("section.widgets > div.widget.primary > div > div.top > a.huge.button").text() === "some text on the button") {
//continue chain commands here
}
else {
//do other commands instead
}

})

Vlad GURDIGA

unread,
Feb 28, 2015, 12:32:06 AM2/28/15
to moc...@googlegroups.com
Mocha tests are just JS code, so you can do all the usual things. But I usually try to keep tests as linear as possible so that they are clear to read.

Kevin Gatchalian

unread,
Mar 1, 2015, 8:41:49 PM3/1/15
to moc...@googlegroups.com
Thanks for the reply but can you please help me for the syntax of using if or even for loop inside the if statement in mocha? I really need to do this since the latter part of my test steps would differ depending on the state of an element (in my case it's the button text). Thanks in advance.

On Sat, Feb 28, 2015 at 1:32 PM, Vlad GURDIGA <gur...@gmail.com> wrote:
Mocha tests are just JS code, so you can do all the usual things. But I usually try to keep tests as linear as possible so that they are clear to read.

--
You received this message because you are subscribed to a topic in the Google Groups "Mocha" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mochajs/lj7GQnAk2_I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mochajs+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Vlad GURDIGA

unread,
Mar 2, 2015, 4:39:58 AM3/2/15
to moc...@googlegroups.com
When I have to test the behavior of an element depending on a particular property, I do one separate test for every relevant property value:

it('works for enabled items', function() {
var item = new Item({enabled: true})
var expectedState = 'the expected state of an enabled item';
expect(item.getState()).to.equal(expectedState);
});
 
it('works for disabled items', function() {
var item = new Item({enabled: false})
var expectedState = 'the expected state of a disabled item';
expect(item.getState()).to.equal(expectedState);
});

for loops, I would probably do something like this:

describe('strlen', function() {
var outputPerInput = {
'tdd': 3,
'expectance': 10,
'': 0
};
for (input in outputPerInput) {
it('returns the appropriate string length', function() {
var expectedResult = outputPerInput[input];
expect(strlen(input)).to.equal(expectedOutput);
});
}
});

I hope this helps.
Reply all
Reply to author
Forward
0 new messages