Search code examples
angularjsangularjs-e2e

Pass a variable in sendKeys e2e testing using protractor


I am new to e2e testing and I am writing e2e test cases for my angular aplication. Here I am creating a patient and I want to book an appointment for the same patient. This is the code

 it('Add Patient', function(){   

    var fname = element(by.model('newrecord.firstName')).sendKeys('Riaz');
    element( by.css('[ng-click="ok()"]') ).click();
});

it('Create Appointment', function(){ 

     element(by.model('newrecord.patientId')).sendKeys(fname);
});

I am getting the below error

ReferenceError: fname is not defined

How to pass variable to sendKeys?


Solution

  • // Page Object
    var recordPage = {
     firstNameElm: element(by.model('newrecord.firstName')),
     patientIdElm: element(by.model('newrecord.patientId')),
     okBtnElm:     element(by.css('[ng-click="ok()"]')),
    };
    
    // Test data
    var testData = {
      patient: {
        idTxt: 'Riaz001',
        firstName: 'Riaz',
      },
    };
    
    it('adds patient', function() {
      recordPage.firstNameElm.sendKeys(testData.patient.firstName);
      recordPage.okBtnElm.click();
    });
    
    it('creates an appointment', function() {
      // not sure what you want to send here but should be text not an ElementFinder
      recordPage.patientIdElm.sendKeys(testData.patient.idTxt);
    });