Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I fetch test data from JSON file to page object file in protractor

Tags:

protractor

Im working on protractor and I have created one test data (JSON) file for my project, also I'm using Page Object for my script. Sometime I need test data into my Page object file to identify the object on my screen once it is created.

I have multiple objects with same css locator, and I want to identify specific one with data i inserted using JSON. I'm using 'cssContainingText' locator to identify the object but unable to get the data directly from JSON.

Can anybody help me with solution? I don't know how to call data file (JSON) from page object file.

like image 403
Sonal Dalal Avatar asked Dec 24 '22 00:12

Sonal Dalal


1 Answers

In addition to @Tom's answer, you can assign the test data path to params object of conf.js file and access it in any file just by using the variable name.

Look at below example config.js file.

exports.config = {

  seleniumAddress: 'http://localhost:4444/wd/hub',
  capabilities: {
    'browserName': 'chrome'
  },

  onPrepare: function () {

  },
   params: {
     testdata: require('path/to/json/file.json')
  }
}

And inside you pageObjects or Spec, You can access the data using browser.params.testdata.users or whatever the name of testdata object.

like image 64
Sudharsan Selvaraj Avatar answered May 31 '23 17:05

Sudharsan Selvaraj