Search code examples
javascriptjquerycssinternleadfoot

Intern JS CSS Selector use Variable


Hey Guys i need help with my functional test, i want to be able to use variable in the findCssSeclector.I use a array called Persona, it should read out the countrycode put in the variable and than in the intern function. But it doesnt work, has somebody a idea?

This working fine:

.type(persona["firstName"])

this not

.findByCssSelector('select#billingAddress\\.country > option:nth-child(countrycode)')

Thanks for help.

define([
  'intern!object',
  'intern/chai!assert',
  'require',
  'intern',
  'tests/support/personas',
  'intern/dojo/node!fs'

], function (RegistrationPage, assert, require, intern, personas, fs) {

    var heute = new Date();
    var RegistrationPage;
    RegistrationPage = {

        DE: function (session, csv) {
            var persona = personas[csv]
            var countrycode = (persona["Country"]);
            this.timeout = 500000;
            return session

                //Adress Page

                //Firstname

                .setFindTimeout(500000)
                .findByCssSelector('input[id="billingAddress.firstName"]')
                .click()
                .type(persona["firstName"])
                .end()
                //Lastname
                .findByCssSelector('input[id="billingAddress.lastName"]')
                .click()
                .type(persona["lastName"])
                .end()

                //Country
                .findByCssSelector('select#billingAddress\\.country > option:nth-child(countrycode)')
                .click()
                .end()
                //1.Country 2.DT 3.AT 4.CH 5.NL 6.DK 7.SE 8.LU 9.BE

                //Streetname
                .findByCssSelector('input[id="billingAddress.street"]')
                .click()
                .type(persona["streetName"])
                .end()
                //streeNumber
                .findByCssSelector('input[id="billingAddress.streetNumber"]')
                .click()
                .type(persona["streetNumber"])
                .end()
                //Postcode

                .findByCssSelector('input[id="billingAddress.zip"]')
                .click()
                .type(persona["PostalCode"])
                .end()
                //City
                .findByCssSelector('input[id="billingAddress.city"]')
                .click()
                .type(persona["city"])
                .end()
                //Birthday
                //0-31 Days
                .findByCssSelector('select#customer\\.profile\\.dateOfBirth_day > option:nth-child(3)')
                .click()
                .end()
                //0-12 Months
                .findByCssSelector('select#customer\\.profile\\.dateOfBirth_month > option:nth-child(3)')
                .click()
                .end()
                //0-82 Years
                .findByCssSelector('select#customer\\.profile\\.dateOfBirth_year > option:nth-child(3)')
                .click()
                .sleep()
                .end()
                //Registration
                .findByCssSelector('input[id="email"]')
                .click()
                .type('clark' + Math.floor(Math.random()*10000000000)+'@testing.qa')
                .end()
                .findByCssSelector('input[id="password"]')
                .click()
                .type('qasecret')
                .end()
                .findByCssSelector('input[id="password2"]')
                .click()
                .type('qasecret')
                .end()
                .findByCssSelector('span.glyphicon.glyphicon-chevron-down')
                .click()
                .end()
                .findByCssSelector('input[id="voucherCode"]')
                .click()
                .type('SALES')
                .end()
                .takeScreenshot()
                .then(function(data) {
                    fs.writeFileSync("/tmp/Screenshot/Registration_" + heute + ".png", data, 'base64');
                })

                //Submit
                .findByCssSelector('#order_form_section input[type=submit]')
                .click()
                .sleep(5000)
                .end()
        }

    };

return RegistrationPage;
});

Solution

.findByCssSelector('select#billingAddress\\.country > option:nth-child(' + countrycode + ')')

Code Example:

define([
      'intern!object',
      'intern/chai!assert',
      'require',
      'intern',
      'tests/support/personas',
      'intern/dojo/node!fs'

    ], function (RegistrationPage, assert, require, intern, personas, fs) {

        var heute = new Date();
        var RegistrationPage;
        RegistrationPage = {

            DE: function (session, csv) {
                var persona = personas[csv]
                var countrycode = (persona["Country"]);
                this.timeout = 500000;
                return session

                    //Adress Page

                    //Firstname

                    .setFindTimeout(500000)
                    .findByCssSelector('input[id="billingAddress.firstName"]')
                    .click()
                    .type(persona["firstName"])
                    .end()
                    //Lastname
                    .findByCssSelector('input[id="billingAddress.lastName"]')
                    .click()
                    .type(persona["lastName"])
                    .end()

                    //Country
                    .findByCssSelector('select#billingAddress\\.country > option:nth-child(' + countrycode + ')')
                    .click()
                    .end()
                    //1.Country 2.DT 3.AT 4.CH 5.NL 6.DK 7.SE 8.LU 9.BE

                    //Streetname
                    .findByCssSelector('input[id="billingAddress.street"]')
                    .click()
                    .type(persona["streetName"])
                    .end()
                    //streeNumber
                    .findByCssSelector('input[id="billingAddress.streetNumber"]')
                    .click()
                    .type(persona["streetNumber"])
                    .end()
                    //Postcode

                    .findByCssSelector('input[id="billingAddress.zip"]')
                    .click()
                    .type(persona["PostalCode"])
                    .end()
                    //City
                    .findByCssSelector('input[id="billingAddress.city"]')
                    .click()
                    .type(persona["city"])
                    .end()
                    //Birthday
                    //0-31 Days
                    .findByCssSelector('select#customer\\.profile\\.dateOfBirth_day > option:nth-child(3)')
                    .click()
                    .end()
                    //0-12 Months
                    .findByCssSelector('select#customer\\.profile\\.dateOfBirth_month > option:nth-child(3)')
                    .click()
                    .end()
                    //0-82 Years
                    .findByCssSelector('select#customer\\.profile\\.dateOfBirth_year > option:nth-child(3)')
                    .click()
                    .sleep()
                    .end()
                    //Registration
                    .findByCssSelector('input[id="email"]')
                    .click()
                    .type('clark' + Math.floor(Math.random()*10000000000)+'@testing.qa')
                    .end()
                    .findByCssSelector('input[id="password"]')
                    .click()
                    .type('qasecret')
                    .end()
                    .findByCssSelector('input[id="password2"]')
                    .click()
                    .type('qasecret')
                    .end()
                    .findByCssSelector('span.glyphicon.glyphicon-chevron-down')
                    .click()
                    .end()
                    .findByCssSelector('input[id="voucherCode"]')
                    .click()
                    .type('SALES')
                    .end()
                    .takeScreenshot()
                    .then(function(data) {
                        fs.writeFileSync("/tmp/Screenshot/Registration_" + heute + ".png", data, 'base64');
                    })

                    //Submit
                    .findByCssSelector('#order_form_section input[type=submit]')
                    .click()
                    .sleep(5000)
                    .end()
            }

        };

    return RegistrationPage;
    }); 
            DE: function (session, csv) {
                var persona = personas[csv]
                var countrycode = (persona["Country"]);
                this.timeout = 500000;
                return session

                    //Adress Page

                    //Firstname

                    .setFindTimeout(500000)
                    .findByCssSelector('input[id="billingAddress.firstName"]')
                    .click()
                    .type(persona["firstName"])
                    .end()
                    //Lastname
                    .findByCssSelector('input[id="billingAddress.lastName"]')
                    .click()
                    .type(persona["lastName"])
                    .end()

                    //Country
                    **.findByCssSelector('select#billingAddress\\.country > option:nth-child(' + countrycode + ')')**
                    .click()
                    .end()
                    //1.Country 2.DT 3.AT 4.CH 5.NL 6.DK 7.SE 8.LU 9.BE

                    //Streetname
                    .findByCssSelector('input[id="billingAddress.street"]')
                    .click()
                    .type(persona["streetName"])
                    .end()
                    //streeNumber
                    .findByCssSelector('input[id="billingAddress.streetNumber"]')
                    .click()
                    .type(persona["streetNumber"])
                    .end()
                    //Postcode

                    .findByCssSelector('input[id="billingAddress.zip"]')
                    .click()
                    .type(persona["PostalCode"])
                    .end()
                    //City
                    .findByCssSelector('input[id="billingAddress.city"]')
                    .click()
                    .type(persona["city"])
                    .end()
                    //Birthday
                    //0-31 Days
                    .findByCssSelector('select#customer\\.profile\\.dateOfBirth_day > option:nth-child(3)')
                    .click()
                    .end()
                    //0-12 Months
                    .findByCssSelector('select#customer\\.profile\\.dateOfBirth_month > option:nth-child(3)')
                    .click()
                    .end()
                    //0-82 Years
                    .findByCssSelector('select#customer\\.profile\\.dateOfBirth_year > option:nth-child(3)')
                    .click()
                    .sleep()
                    .end()
                    //Registration
                    .findByCssSelector('input[id="email"]')
                    .click()
                    .type('clark' + Math.floor(Math.random()*10000000000)+'@testing.qa')
                    .end()
                    .findByCssSelector('input[id="password"]')
                    .click()
                    .type('qasecret')
                    .end()
                    .findByCssSelector('input[id="password2"]')
                    .click()
                    .type('qasecret')
                    .end()
                    .findByCssSelector('span.glyphicon.glyphicon-chevron-down')
                    .click()
                    .end()
                    .findByCssSelector('input[id="voucherCode"]')
                    .click()
                    .type('SALES')
                    .end()
                    .takeScreenshot()
                    .then(function(data) {
                        fs.writeFileSync("/tmp/Screenshot/Registration_" + heute + ".png", data, 'base64');
                    })

                    //Submit
                    .findByCssSelector('#order_form_section input[type=submit]')
                    .click()
                    .sleep(5000)
                    .end()
            }

        };

    return RegistrationPage;
    });

Solution

  • Neither Intern nor JS will interpolate simple variable names in strings, so

    'select#billingAddress\\.country > option:nth-child(countrycode)'
    

    is just a simple string. If you want to use the countrycode variable, you'd do

    .findByCssSelector('select#billingAddress\\.country > option:nth-child(' + countrycode + ')')
    

    Or, since you're just pulling it out of the persona object, you could just do

    .findByCssSelector('select#billingAddress\\.country > option:nth-child(' + persona['Country'] + ')')
    

    countrycode doesn't sound like something nth-child will understand, though (number, 'odd', 'even', '2n+1', etc.), so making the selector use the variable may not buy you much.