All Downloads are FREE. Search and download functionalities are using the official Maven repository.

js.GalenPages.js Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright 2014 Ivan Shubin http://galenframework.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ******************************************************************************/


String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');};


var GalenPages = {
    allowReporting: true,
    report: function (name, details) {
        if (GalenPages.allowReporting) {
            var testSession = TestSession.current();
            if (testSession != null) {
                var node = testSession.getReport().info(name);

                if (details != undefined && details != null) {
                    node.withDetails(details);
                }
            }
        }
    },
    Locator: function (type, value) {
        this.type = type;
        this.value = value;
    },
    parseLocator: function (locatorText) {
        var index = locatorText.indexOf(":");
        if (index > 0 ) {
            var typeText = locatorText.substr(0, index).trim();
            var value = locatorText.substr(index + 1, locatorText.length - 1 - index).trim();
            var type = "css";
            if (typeText == "id") {
                type = typeText;
            }
            else if (typeText == "xpath") {
                type = typeText;
            }
            else if (typeText == "css") {
                type = typeText;
            }
            else {
                throw new Error("Unknown locator type: " + typeText);
            }

            return new this.Locator(type, value);
        }
        else return {
            type: "css",
            value: locatorText
        };
    },
    Page: function (driver, mainFields, secondaryFields) {
        this.driver = driver;
        this.waitTimeout = "10s";
        this.waitPeriod = "1s";
        this._report = function (name) {
            try {
                GalenPages.report(name);
            }
            catch (err) {

            }
        },
        this.open = function (url) {
            this._report("Open " + url);
            this.driver.get(url);
        },
        this.findChild = function (locator) {
            if (typeof locator == "string") {
                locator = GalenPages.parseLocator(locator);
            }

            if (this.parent != undefined ) {
                return this.parent.findChild(locator);
            }
            else {
                try {
                    var element = this.driver.findElement(GalenPages.convertLocator(locator));
                    if (element == null) {
                        throw new Error("No such element: " + locator.type + " " + locator.value);
                    }
                    return element;
                }
                catch(error) {
                    throw new Error("No such element: " + locator.type + " " + locator.value);
                }
            }
        };
        this.findChildren = function (locator) {
            if (typeof locator == "string") {
                locator = GalenPages.parseLocator(locator);
            }

            if (this.parent != undefined ) {
                return this.parent.findChildren(locator);
            }
            else {
                return this.driver.findElements(GalenPages.convertLocator(locator));
            }
        };
        this.set = function (props) {
            for (var property in props) {
                if (props.hasOwnProperty(property)) {
                    this[property] = props[property];
                }
            }
            return this;
        };
        this.waitForIt = function () {
            if (this.primaryFields.length > 0 ) {
                var conditions = {};
                var primaryFields = this.primaryFields;
                var page = this;

                for (var i = 0; i 0) {
                var t = 0;
                while (t < this.time) {
                    t = t + this.period;
                    if (this._checkAll(waitFuncs)) {
                        return;
                    }
                    GalenPages.sleep(this.period);
                }

                var errors = "";
                for (var i = 0; i 0) {
                    throw  new Error(this.message + errors);
                }
            }
            else throw  new Error("You are waiting for nothing");
        };
        this.forEach = function (items, itemConditionName, conditionFunc) {
            var conditions = {};
            for (var i=0; i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy