Using “switch” to combine Cucumberjs step functions

Those who write automated tests all know that we commonly end up repeating code, especially as web pages tend to contain many common elements. Instead of ending up with blocks of step code that vary very little, using Switch is a neat way to combine steps. Switch checks for a certain value, then ascribes variables with certain values based on that.


this.Given(/^I should see quantity (.*) for product (.*) on the (.*) page$/, function (quantity, productid, sitepage, callback) {
var url, i;
switch (sitepage) {
case 'hardware section':
url = 'http://ashop.com/category/hardware-section/';
i = 11;
break;
case 'product page':
url = 'http://ashop.com/category/hardware-section/product/1234/';
i = 9;
break;
case 'shopping cart':
url = 'http://ashop.com/cart/';
i = 5;
break;
}
browser.get(settings.url(url)).then(function () {
var product = element(by.cssContainingText('tr',productid))
.all(by.css('td')).get(i).getText();
expect(product).to.eventually.contain(quantity).notify(callback);
})
});




No Comments


You can leave the first : )