'use strict';

module.exports = function () {
    this.World = require('../support/world.js').World;

    this.When(/^я выберу в выпадающем списке "([^"]*)" значение "([^"]*)"$/, function (comboboxLabel, value) {
        comboboxLabel = this.helpers.fixQuotes(comboboxLabel);

        var selector = this.selectors.XPATH.ComboBox.button(comboboxLabel);
        var xpath = this.by.xpath(selector);
        var that = this;

        return this.currentView.findElement(xpath).then(function (element) {
            return element.click().then(function () {
                selector = that.selectors.XPATH.ComboBox.dropDown(value);
                xpath = that.by.xpath(selector);

                return that.driver.findElement(xpath).then(function (dropDownItem) {
                    return dropDownItem.click();
                })
            });
        });
    });

    this.When(/^я выберу в выпадающем списке "([^"]*)" с фильтром "([^"]*)" значение "([^"]*)"$/, function (comboBoxLabel, filter, value) {
        comboBoxLabel = this.helpers.fixQuotes(comboBoxLabel);

        var selector = this.selectors.XPATH.ComboBox.button(comboBoxLabel);
        var xpath = this.by.xpath(selector);
        var that = this;

        return this.currentView.findElement(xpath).then(function (element) {
            return element.click().then(function () {
                selector = that.selectors.XPATH.ComboBox.filter();
                xpath = that.by.xpath(selector);

                return that.driver.findElement(xpath).then(function (filteredField) {
                    return filteredField.sendKeys(filter).then(function () {
                        selector = that.selectors.XPATH.ComboBox.dropDown(value);
                        xpath = that.by.xpath(selector);

                        return that.driver.findElement(xpath).then(function (dropDownItem) {
                            return dropDownItem.click();
                        });
                    });
                });
            });
        });
    });

    this.When(/^значение в выпадающем списке "([^"]*)" равно "([^"]*)"$/, function (comboBoxLabel, value) {
        comboBoxLabel = this.helpers.fixQuotes(comboBoxLabel);

        var selector = this.selectors.XPATH.ComboBox.caption(comboBoxLabel);
        var xpath = this.by.xpath(selector);
        var that = this;
        
        return this.currentView.findElement(xpath).then(function (label) {
            return label.getAttribute('for').then(function (id) {
                return that.currentView.findElement(that.by.id(id)).then(function (control) {
                    selector = that.selectors.XPATH.ComboBox.value();
                    xpath = that.by.xpath(selector);

                    return control.findElement(xpath).then(function (comboBoxValue) {
                        return comboBoxValue.getText().then(function (text) {
                            that.assert.equal(text, value);
                        });
                    });
                });
            });
        });
    });
};
@@ -260,32 +260,79 @@
 (comboboxLabel);
+%0A        value = this.helpers.fixQuotes(value);
 %0A%0A        var se
@@ -1103,32 +1103,79 @@
 (comboBoxLabel);
+%0A        value = this.helpers.fixQuotes(value);
 %0A%0A        var se
@@ -2287,16 +2287,59 @@
 xLabel);
+%0A        value = value.replace(/''/g, '%22');
 %0A%0A      

fix integration tests