简体   繁体   中英

How to pass on variable defined certain command, into some other external command(in the test) in nightwatch

I have a custom command: initialCount.js

module.exports = {
    command: function () {
        this
            .getText('.header-mini-cart-menu-cart-legend', function (result) {
                cartCountInitial = Number(result.value); <--- I want this output into some other command.
                console.log(cartCountInitial); <---- Prints the count in number.
                return cartCountInitial;  <--- I want this output into some other command
            });
    }

I want to use the value into a IF condition which is in different file(In a testcase).

test case file name: addToCartFromTile.test.js

var initialCartCount = require('../../../commands/shopping/cart/initialCartCount');

module.exports= {
    'Determining whether the product is MATRIX or NON-MATRIX': client => {
        client
            .url(client.launchUrl)
            .getLocationInView(noOfSliders)
            .execute(function () {
                  var cartCount = document.querySelector('.header-mini-cart-menu-cart-legend').innerText;
                  return cartCount;
             },[],function(res){
                     var cartCount1 = Number(res.value);


                     if (initialCartCount.command == cartCount1) { <---- "initialCartCount.command" returns me {command: [Function: command]}.

                     }

"initialCartCount.command" in the IF condition, returns me {command: [Function: command]} . How do I get the variable value.

Is there a hack or the approach is wrong. If my approach is wrong, then please suggest on how do I achieve this?

I've come up with a solution myself.

module.exports= {
    'Determining whether the product is MATRIX or NON-MATRIX': client => {
        client
            .url(client.launchUrl)
            .getLocationInView(noOfSliders)
            .initialCount()  <============ Called the external command.
   ===>>    .perform(function(done){  <=== Extending the scope of external command.

                        var abc = cartCountInitial; <=== Got the value
                        console.log('Perform', abc);

                 .execute(function () {
                  var cartCount = document.querySelector('.header-mini-cart-menu-cart-legend').innerText;
                  return cartCount;
             },[],function(res){
                     var cartCount1 = Number(res.value);


                     if (abc == cartCount1) { <=== Matched :)

                     }
             })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM