代码之家  ›  专栏  ›  技术社区  ›  Stefan Kloß

实习生JS CSS选择器使用变量

  •  1
  • Stefan Kloß  · 技术社区  · 11 年前

    嘿,伙计们,我需要帮助进行功能测试,我希望能够在findCsSeclector中使用变量。我使用一个名为Persona的数组,它应该读取放在变量中的国家代码,而不是放在实习函数中。但它不起作用,有人有主意吗?

    此工作罚款:

    .type(persona["firstName"])
    

    这不是

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

    谢谢你的帮助。

    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;
    });
    

    解决方案

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

    代码示例:

    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;
        });
    
    1 回复  |  直到 11 年前
        1
  •  2
  •   jason0x43    11 年前

    Intern和JS都不会在字符串中插入简单的变量名,因此

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

    只是一个简单的字符串。如果您想使用countrycode变量

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

    或者,因为你只是把它从 persona 对象,您可以这样做

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

    countrycode 听起来不像什么 nth-child 但会理解(数字、“奇数”、“偶数”、“2n+1”等),因此让选择器使用变量可能不会给你带来太多好处。