Skip to content

Instantly share code, notes, and snippets.

@varlog23
Last active December 12, 2019 10:05
Show Gist options
  • Save varlog23/708f5d7e2c5c4b8db61a7a5a4a2a760f to your computer and use it in GitHub Desktop.
Save varlog23/708f5d7e2c5c4b8db61a7a5a4a2a760f to your computer and use it in GitHub Desktop.
To run Selenium IDE's code export for all SIDE files within a folder to generate Java for JUnit files
import fs from 'fs'
var path = require('path');
const FileHound = require('filehound');
import { project as projectProcessor } from '@seleniumhq/side-utils'
import { emitTest, emitSuite } from '@seleniumhq/code-export-java-junit'
const filesPath = '.' // path to the folder containing SIDE files
const downloadPath = path.join(filesPath,'Junit') // Will by default create a JUnit folder in {filesPath}
const filesType = 'side'
function readFile(file) {
return JSON.parse(
fs.readFileSync(file)
)
}
function normalizeProject(project) {
let _project = { ...project }
_project.suites.forEach(suite => {
projectProcessor.normalizeTestsInSuite({ suite, tests: _project.tests })
})
return _project
}
/**
* Emit an individual test, wrapped in a suite
* @param {full path to the SIDE file} file
*/
async function emitTestcaseCode(file) {
const project = normalizeProject(readFile(file))
const results = await emitTest({
baseUrl: project.url,
test: project.tests[0],
tests: project.tests,
})
return results
}
/**
* Emit a suite with all of its tests
* @param {full path to the SIDE file} file
*/
async function emitSuitCode(file) {
const project = normalizeProject(readFile(file))
const results = await emitSuite({
baseUrl: project.url,
suite: project.suites[0],
tests: project.tests,
})
return results
}
/**
* Export JUnit code for SIDE files in a entire folder
* SIDE files must be present in {filesPath}
* Java JUnit files will be saved in {downloadPath}
*/
async function batchExportSuitCode(){
try {
fs.mkdirSync(downloadPath, { recursive: true } )
} catch (e) {
console.log('Cannot create folder ', e)
}
var files = []
await FileHound.create()
.paths(filesPath)
.ext(filesType)
.find().then((arr) => {files = arr}).catch(error => console.log(error.message));
for (const item of files) {
var filename = path.parse(item).base;
console.log("Found",filename)
await emitSuitCode(item).then((result) => {
fs.writeFile(path.join(downloadPath, result.filename), result.body, function(err) {
if(err) {
return console.log(err);
}
console.log("The file:",result.filename," was saved!")
})
}).catch(error => console.log(error.message, ' in ' ,item));
}
}
// Call the batch process function
batchExportSuitCode()
@vsix27
Copy link

vsix27 commented Dec 11, 2019

which packages do i need to use this?
after googling i found that
import { emitTest, emitSuite } from './packages/code-export-java-junit/src' could be resolved by
npm i @seleniumhq/code-export-java-junit
and changing to
import { emitTest, emitSuite } from './node_modules/@seleniumhq/code-export-java-junit/src'

but the first reference
import { normalizeTestsInSuite } from './packages/selenium-ide/src/neo/IO/normalize'
is not found

there is no npm i selenium-ide or npm i @seleniumhq/selenium-ide ...
any advice?
thanks

@varlog23
Copy link
Author

You are right about the code-export imports. The package wasn't published to npm at the time of writing this gist.
I notice that this commit has moved the normalize function out of selenium-ide and into side-utils.
This package is already published on npm.

And now it should be possible to replace the line
import { normalizeTestsInSuite } from './packages/selenium-ide/src/neo/IO/normalize'
with
import { project as projectProcessor } from '@seleniumhq/side-utils'

And the line
normalizeTestsInSuite({ suite, tests: _project.tests })
with
projectProcessor.normalizeTestsInSuite({ suite, tests: _project.tests })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment