1

Something like;

element(by.model('formData.name')).sendKeys('David Cameron');

I am trying to find a way to test my admin side when a user signs up. Somewhere I'll have to use expect (to check whether entry was submitted to backend) but I want to use a python dictionary of about 10 names where the protractor test selects a name at random to sendKeys(). How do I establish the link?

colin_dev256
  • 805
  • 2
  • 16
  • 36

2 Answers2

1

Dump your Python dictionary into a JSON file via, for example, json.dump(), then you can import the JSON file and set the browser.params in onPrepare():

onPrepare: function () {
    var fs = require('fs');
    var obj = JSON.parse(fs.readFileSync('file', 'utf8'));

    browser.params.names = obj;
},

Then, you can use browser.params.names in your test. To pick up a random value from an array, see this topic: Getting a random value from a JavaScript array.

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

I figured it out by creating javaScript objects straight away in my testing *spec.js file e.g for 3 random people (objects);

Inside my sell-spec.js file;

var random = function(){
    return Math.floor((Math.random() * 3) + 1);
}

var users = [];
var user1 = {
    name : "Jacob",
    tel : "123",
    email: "1@1.com",
    reg: "1234",
    vin : "2345"
};
var user2 = {
    name : "Jacob2",
    tel : "1232",
    email: "1@12.com",
    reg: "12342",
    vin : "23452"
};
var user3 = {
    name : "Jacob3",
    tel : "1233",
    email: "1@13.com",
    reg: "12343",
    vin : "23453"
};

users.push(user1);
users.push(user2);
users.push(user3);

var selectedUserIndex = random();
selectedUser = users[selectedUserIndex]

element(by.model('formData.name')).sendKeys(selectedUser.name);
element(by.model('formData.cell')).sendKeys(selectedUser.tel);

and in my other (admin) spec admin-spec.js file;

expect(element.all(by.binding('name')).first().getText()).toEqual('Name: ' + seller_details.selectedUser.name );
expect(element.all(by.binding('cell')).first().getText()).toEqual('Cell: ' + seller_details.selectedUser.tel);
colin_dev256
  • 805
  • 2
  • 16
  • 36