Create a gist now

Instantly share code, notes, and snippets.

@mistakster /index.md
Last active Dec 21, 2016

What would you like to do?
Functional tests of the Yandex’s homepage
var assert = require('assert');
var until = require('selenium-webdriver').until;
var promise = require('selenium-webdriver').promise;
var By = require('selenium-webdriver').By;
var tabsLocator = By.css('.b-head-tabs');
var tabItemLocator = By.css('.b-head-tabs__tab');
var selectedTabItemLocator = By.css('.b-head-tabs__tab_selected');
var linkLocator = By.css('.b-head-tabs__tab-link');
var Component = function (driver) {
this._driver = driver;
};
Component.prototype._getTabs = function () {
var driver = this._driver;
return driver.isElementPresent(tabsLocator)
.then(function (result) {
if (!result) {
throw new Error('Menu component is not found');
}
return driver.findElement(tabsLocator);
});
};
Component.prototype.validate = function () {
return this._getTabs()
.then(function (widget) {
assert.ok(widget);
return this.getItems();
}.bind(this))
.then(function (items) {
assert.ok(items);
assert.ok(items.length > 0);
});
};
Component.prototype.getItems = function () {
function itemMapper(item) {
return item.isElementPresent(linkLocator)
.then(function (result) {
var link, out;
if (result) {
link = item.findElement(linkLocator);
out = promise.all([
link.getText(),
link.getAttribute('href')
]);
} else {
out = promise.all([
item.getText(),
null
]);
}
return out;
})
.then(function (hash) {
return hash && {
text: hash[0],
href: hash[1]
};
});
}
return this._getTabs()
.then(function (widget) {
return widget.findElements(tabItemLocator);
})
.then(function (items) {
return promise.map(items, itemMapper);
});
};
module.exports = Component;
var assert = require('assert');
var until = require('selenium-webdriver').until;
var promise = require('selenium-webdriver').promise;
var By = require('selenium-webdriver').By;
var widgetLocator = By.css('.b-weather');
var titleLocator = By.css('.b-content-item__title > .b-link:nth-child(1)');
var temperatureLocator = By.css('.b-content-item__title > .b-link:nth-child(3)');
var Component = function (driver) {
this._driver = driver;
};
Component.prototype._getWidget = function () {
var driver = this._driver;
return driver.isElementPresent(widgetLocator)
.then(function (result) {
if (!result) {
throw new Error('Weather component is not found');
}
return driver.findElement(widgetLocator);
});
};
Component.prototype.validate = function () {
return this._getWidget()
.then(function (widget) {
var title = widget.findElement(titleLocator);
return title.getText()
.then(function (text) {
assert.equal(text, '\u041f\u043e\u0433\u043e\u0434\u0430');
return true;
});
});
};
Component.prototype.getTemperature = function () {
return this._getWidget()
.then(function (widget) {
var temperature = widget.findElement(temperatureLocator);
return temperature.getText()
.then(function (text) {
assert.notEqual(text, '');
return text;
});
});
};
module.exports = Component;
var DriverProvider = require('./selenium-server/driver-provider');
var assert = require('assert');
var test = require('selenium-webdriver/testing');
var Page = require('./page-objects/yandex');
test.describe('Yandex home page', function () {
var driverProvider = new DriverProvider();
test.before(function () {
return driverProvider.startUp();
});
test.after(function () {
return driverProvider.tearDown();
});
test.it('should be valid', function () {
var driver = driverProvider.getDriver();
var page = new Page(driver);
return page
.open(page.getReadyCondition())
.validate();
});
test.it('should have at least 9 tabs above the search', function () {
var driver = driverProvider.getDriver();
var page = new Page(driver);
return page
.open(page.getReadyCondition())
.getMenuComponent()
.getItems()
.then(function (items) {
/*
items == [
{ text: 'Поиск', href: null }
{ text: 'Карты', href: 'http://maps.yandex.ru/' }
{ text: 'Маркет', href: 'http://market.yandex.ru/?clid=505' }
{ text: 'Новости', href: 'http://news.yandex.ru/' }
{ text: 'Словари', href: 'http://slovari.yandex.ru/' }
{ text: 'Картинки', href: 'http://yandex.ru/images/' }
{ text: 'Видео', href: 'http://yandex.ru/video' }
{ text: 'Музыка', href: 'http://music.yandex.ru/' }
{ text: 'Перевод', href: 'http://translate.yandex.ru/' }
{ text: 'ещё', href: 'http://www.yandex.ru/all' }
]
*/
assert.ok(items.length > 9);
});
});
test.it('should have a weather widget', function () {
var driver = driverProvider.getDriver();
var page = new Page(driver);
return page
.open(page.getReadyCondition())
.getWeatherComponent()
.getTemperature()
.then(function (temperature) {
/* temperature == '+4 °C' */
assert.ok(temperature);
});
});
});
var until = require('selenium-webdriver').until;
var promise = require('selenium-webdriver').promise;
var By = require('selenium-webdriver').By;
var logoLocator = By.css('.b-logo');
var Page = function (driver) {
this._driver = driver;
};
Page.prototype.url = 'http://www.yandex.ru/';
Page.prototype.open = function (condition) {
this._driver.get(this.url);
this.waitReady(condition);
return this;
};
Page.prototype.waitReady = function (condition, timeout) {
if (condition) {
timeout = timeout || 10 * 1000;
this._driver.wait(condition, timeout);
}
return this;
};
Page.prototype.getReadyCondition = function () {
return until.elementLocated(logoLocator);
};
Page.prototype.getWeatherComponent = function () {
var Component = require('./weather-component');
return new Component(this._driver);
};
Page.prototype.getMenuComponent = function () {
var Component = require('./menu-component');
return new Component(this._driver);
};
Page.prototype.validate = function () {
return promise
.all([
this.getMenuComponent().validate(),
this.getWeatherComponent().validate()
])
.then(function (results) {
return results[0] && results[1];
});
};
module.exports = Page;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment