allure-client
v0.2.2
Published
Wrapper for [allure-server](https://github.com/KnowledgeExpert/allure-server) http client for NodeJS in Typescript. Can be used for working with allure-server and generating allure XML from allure-server JSON data.
Downloads
4
Readme
Allure http client
Wrapper for allure-server http client for NodeJS in Typescript. Can be used for working with allure-server and generating allure XML from allure-server JSON data.
Description
Repository contains Runtime module which handles http layer, and Session module which wraps Runtime
and provides concise API for tests.
Example
For Jasmine you can simple inject allure-client as custom reporter:
base-spec.ts
const Configuration = require("allure-client").Configuration;
// setup some configurations
Configuration.baseUrl = "http://localhost:3000";
Configuration.baseDir = "build/allure-results";
// create Session object (it will automatically generate unic UUID for further operations)
const session = await Session.create();
jasmine.getEnv().addReporter({
suiteStarted: async function (suite) {
await session.startSuite(suite.fullName);
},
specStarted: async function (spec) {
await session.startTest(spec.description)
},
specDone: async function (spec) {
const status = getTestcaseStatus(spec.status);
if (status !== 'passed') {
await session.endTest(status);
} else {
await session.endTest(status, getTestcaseError(spec));
}
},
suiteDone: async function (result) {
await session.endSuite();
},
jasmineDone: async function () {
// after all tests done, you should call writeToXML() method
// it will automatically pull collected data in JSON format
// and write it in local directory as XML structure
// which can be handled be allure-cli
await session.writeToXML();
}
});
// util functions
function getTestcaseStatus(status): 'skipped' | 'passed' | 'failed' {
if (status === 'disabled' || status === 'pending') {
return 'skipped';
} else if (status === 'passed') {
return 'passed';
} else {
return 'failed';
}
}
function getTestcaseError(result) {
if (result.status === 'disabled') {
return {
message: 'This test was ignored',
stack: ''
};
} else if (result.status === 'pending') {
return {
message: result.pendingReason,
stack: ''
};
}
return result.failedExpectations
? result.failedExpectations[0]
: {
message: 'No failure expectations found.',
stack: ''
};
}
Configuration
Configuration for module consists of:
Configuration.baseUrl
- allure-server uriConfiguration.baseAuth
- allure-server basic auth (optional)Configuration.baseDir
- directory where allure-client will store XML and attachments for Allure-Cli (default -./allure-results
)
Note: Configuration.baseUrl
should be initialized before calling any Session
or Runtime
methods;
Session API
- Create new
Session
- Entry point for
Session
, should be called to getSession
object. - sample:
const session = await Session.create();
- Entry point for
- Start new suite
- starts new test suite
- parameters:
name: string
- suite nametimestamp: number
- operation time- default value -
Date.now()
- default value -
- sample:
await session.startSuite('My Suite Name');
- End suite
- ends current test suite
- parameters:
timestamp: number
- operation time- default value -
Date.now()
- default value -
- sample:
await session.endSuite();
- Start test case
- starts new test case in currrent test suite
- parameters:
name: string
- test case nametimestamp: number
- operation time- default value -
Date.now()
- default value -
- sample:
await session.startTest('My Test Name');
- parameters:
- starts new test case in currrent test suite
- End test case
- ends current test case
- parameters:
status: string
- test case status- possible values:
broken
failed
passed
skipped
- possible values:
error: Error
- error object (optional)timestamp: number
- operation time- default value -
Date.now()
- default value -
- sample:
const error = new Error('Something went wrong'); await session.endTest('failed', error);
- parameters:
- ends current test case
- Start test step
- starts new test step in current test case
- parameters:
*
name: string
- test step name *timestamp: number
- operation time * default value -Date.now()
- sample:
await session.startStep('My Test Name');
- sample:
- parameters:
*
- starts new test step in current test case
- End test step
- ends test step in current test case
- parameters:
*
status: string
- test step status * possible options: *failed
*passed
*timestamp: number
- operation time * default value -Date.now()
- sample:
await session.endStep('passed');
- sample:
- parameters:
*
- ends test step in current test case
- Write to XML
- pop's accumulated session data from server and writes it to local storage
- parameters:
name: string
- test label namevalue: string
- test label content
- sample:
await session.writeToXML();
- Set description
- sets description for current test case
- parameters:
*
type: string
- description type * possible options: *text
*html
*markdown
*content: string
- description content- sample:
await session.setDescription('text', 'some custom test description');
- sample:
- parameters:
*
- sets description for current test case
- Add attachment
- add attachment to current test case or step
- parameters:
*
title: string
- attachment title *content : ReadStream | Object
- attachment data *ReadStream
- just pass attachment asReadStream
*Object: { mime: string, buffer: Buffer }
- if you had a buffer, you should pass it with specifying mime-type for file- sample:
const readStream = fs.createReadStream('file1.txt'); const buff = fs.readFileSync('fil2.txt'); await session.addAttachment('file 1', readStream); await session.addAttachment('file 2', {mime: 'text/plain', buffer: buff});
- sample:
- parameters:
*
- add attachment to current test case or step
- Set Severity
- set test case severity
- parameters:
*
severity: string
- test severity * possible options: *blocker
*critical
*normal
*minor
*trivial
- sample:
await session.setSeverity('critical');
- sample:
- parameters:
*
- set test case severity
- Add Epic
- add test case Epic
- parameters:
*
epic: string
- test Epic- sample:
await session.addEpic('Super Epic');
- sample:
- parameters:
*
- add test case Epic
- Add Story
- add test case Story
- parameters:
*
story: string
- test Story- sample:
await session.addEpic('Super Story');
- sample:
- parameters:
*
- add test case Story
- Add Feature
- add test case Feature
- parameters:
*
feature: string
- test Feature- sample:
await session.addFeature('Super Feature');
- sample:
- parameters:
*
- add test case Feature
- Add Environment
- add test case Environment
- parameters:
*
name: string
- Environment name *value: string
- Environment value- sample:
await session.addEnvironment('Chrome', '56');
- sample:
- parameters:
*
- add test case Environment
- Add label
- add test case label
- parameters:
*
name: string
- test label name *value: string
- test label content- sample:
await session.addLabel('some label name', 'mapped value');
- sample:
- parameters:
*
- add test case label
- Add label
- add test case label
- parameters:
*
name: string
- test label name *value: string
- test label content- sample:
await session.addLabel('some label name', 'mapped value');
- sample:
- parameters:
*
- add test case label