MCPcopy
hub / github.com/codeceptjs/CodeceptJS / pageObject

Function pageObject

lib/command/generate.js:111–201  ·  view source on GitHub ↗
(genPath, opts)

Source from the content-addressed store, hash-verified

109`
110
111export async function pageObject(genPath, opts) {
112 const testsPath = getTestRoot(genPath)
113 const config = await getConfig(testsPath)
114 const kind = opts.T || 'page'
115 if (!config) return
116
117 let configFile = path.join(testsPath, `codecept.conf.${extension}`)
118
119 if (!fileExists(configFile)) {
120 extension = 'ts'
121 configFile = path.join(testsPath, `codecept.conf.${extension}`)
122 }
123 output.print(`Creating a new ${kind} object`)
124 output.print('--------------------------')
125
126 return inquirer
127 .prompt([
128 {
129 type: 'input',
130 name: 'name',
131 message: `Name of a ${kind} object`,
132 validate: val => !!val,
133 },
134 {
135 type: 'input',
136 name: 'filename',
137 message: 'Where should it be stored',
138 default: answers => `./${kind}s/${answers.name}.${extension}`,
139 },
140 {
141 type: 'list',
142 name: 'objectType',
143 message: 'What is your preferred object type',
144 choices: ['module', 'class'],
145 default: 'module',
146 },
147 ])
148 .then(result => {
149 const pageObjectFile = path.join(testsPath, result.filename)
150 const dir = path.dirname(pageObjectFile)
151 if (!fileExists(dir)) fs.mkdirSync(dir)
152
153 let actor = 'actor'
154
155 if (config.include.I) {
156 let actorPath = config.include.I
157 if (actorPath.charAt(0) === '.') {
158 // relative path
159 actorPath = path.relative(dir, path.dirname(path.join(testsPath, actorPath))) + actorPath.substring(1) // get an upper level
160 }
161 actor = `import('${actorPath}')`
162 }
163
164 const name = lcfirst(result.name) + ucfirst(kind)
165 if (result.objectType === 'module' && extension === 'ts') {
166 if (!safeFileWrite(pageObjectFile, poModuleTemplateTS.replace('{{actor}}', actor))) return
167 } else if (result.objectType === 'module' && extension === 'js') {
168 if (!safeFileWrite(pageObjectFile, pageObjectTemplate.replace('{{actor}}', actor))) return

Callers

nothing calls this directly

Calls 8

getTestRootFunction · 0.90
getConfigFunction · 0.90
fileExistsFunction · 0.90
lcfirstFunction · 0.90
ucfirstFunction · 0.90
safeFileWriteFunction · 0.90
readConfigFunction · 0.90
beautifyFunction · 0.90

Tested by

no test coverage detected