0
votes

I'm new to protractor using javascript, I created a simple script that calls data from another js file.

I have a file called jsobjectdemo.js that contains

<pre>
this.productnames = element.all(by.tagName("app-card")); 
this.cartblock = element(by.css("h4 a"));
this.addcartbutton = element(by.css("button[class='btn btn-info']"));
</pre>

And another file called jscalldemo.js

var cart = require('./jsobjectdemo.js');

    function addCart(productname){

        cart.productnames.each(function(item){
            cart.cartblock.getText().then(function(text){
                if (text==productname){
                    cart.addcartbutton.click();
                }
            })
        })
        }

When i ran the script, it returns an error

Message:
  Failed: Cannot read property 'cartblock' of undefined
Stack:
  TypeError: Cannot read property 'cartblock' of undefined
      at D:\Trainings\Protractor\protractor-workspace\Protractor\jscalldemo.js:8:14
1
Still facing the issue?Bharath Kumar S

1 Answers

0
votes
Const loc = {
productnames = element.all(by.tagName("app-card")), cartblock = element(by.css("h4 a")), addcartbutton = element(by.css("button[class='btn btn-info']"))
}
exports.cart = loc;

Change your jsobjectdemo.js to the above.

And in your jscalldemo.js

Const cart = require('./jsobjectdemo.js').cart;

Now you can use cart.cartblock in jscalldemo.js