I built a selenium-mocha test case to open google ,enter some text and click search. This is my selenium-mocha test case code snippet
But I need to perform click search only after 60 seconds on entering the search text
var assert = require('assert'),
test = require('selenium-webdriver/testing'),
webdriver = require('selenium-webdriver');
var urladd ='http://www.google.com/';
function clickLink(link){
link.click();
return link;
}
var testSimple=function(searchContent){
var browser = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
test.describe('\n\nGoogle Search\n', function() {
this.timeout(60000);
test.it('Enter element to be searched', function(done) {
browser.get(urladd);
browser.findElement(webdriver.By.name('q')).sendKeys(searchContent);
browser.getTitle().then(function(title) {
assert.equal(title,'Google');
})
});
test.it('Click search button', function(done) {
setTimeout(function(){
browser.findElement(webdriver.By.name('btnG')).then(clickLink).then(function(){
browser.getTitle().then(function(title) {
assert.equal(title,'selenium mocha test cases - Google Search');
});
});
},30000);
});
});
}
testSimple('selenium mocha test cases');
I used setTimeout to pause for 60 seconds but,mocha just passes the test case without executing the code inside timeout
Do anyone can suggest a better solution for this or any other APIs that could be used for pausing the mocha test
Google Search
√ Enter element to be searched (7771ms)
√ Click search button
2 passing (8s)
A couple notes before getting to the meat of the matter:
You don't need done
for the functions you pass to test.it
. You are using selenium-webdriver
wrapped Mocha functions, which make it so that all your tests are asynchronous. So done
is useless.
Your second test is dependent on the first. That's bad design. Tests should be independent. There are already questions on SO that address how to make tests independent so I'm not going to address this again here.
A for your wait issue here is how you can solve it. You can modify your second test to be like this:
test.it('Click search button', function() {
var flow = webdriver.promise.controlFlow();
flow.execute(
function () { return webdriver.promise.delayed(30 * 1000); });
browser.findElement(webdriver.By.name('btnG')).then(clickLink);
browser.wait(function () {
return browser.getTitle().then(function (title) {
return title === 'selenium mocha test cases - Google Search';
});
});
});
What this does is create a control flow in which a promise is executed. webdriver.promise.delayed
just creates a promise that will be resolved in the number of miliseconds passed to the function. Then you can just call methods on browser
. The net effect is to pause the operations on the browser for 30 seconds.
An additional issue with your code is that it was not waiting for the title to change. When the click happens, it takes a bit for the title to change so the Selenium code has to wait, hence the browser.wait
call at the end. If the title never changes the test will fail with a timeout.