Skip to content

Commit 0ee9fac

Browse files
authored
feat: Make JavaScript generator auto step insertion optional (#457)
* Make JavaScript generator ignore opinionated step insertion if flag is passed. * Fixup. * Add ability to accept custom steps structure and output corresponding code. * Modify `generateFromSteps` helper to work with full `Steps` param. Add `Steps` type definition to Synthetics instead of Recorder repo. * Export `Step` and `Steps`. * Add option to preserve whitespace lines when generating step code. * Add `Action` type to package exports. * Remove unneeded modification to `package.json`. * Add test to cover `throw` condition. * Add JSDoc comment for generator function. * Delete unused function and tests. * Rename "new step" flag. * Export `actionTitle` helper function. * Make generator join code lines to single string. * Remove override params and simplify action generator function. * Simplify code. * Delete obsolete code.
1 parent 17f7bc7 commit 0ee9fac

File tree

4 files changed

+228
-186
lines changed

4 files changed

+228
-186
lines changed
Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,18 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`Synthetics JavaScript formatter inline journeys 1`] = `
4-
"step('Go to https://vigneshh.in/', async () => {
5-
await page.goto('https://vigneshh.in/');
6-
expect(await page.isVisible('text=Babel Minify')).toBeTruthy();
7-
expect(await page.isEditable('text=Babel Minify')).toBeTruthy();
8-
expect(await page.textContent('text=Babel Minify')).toMatch('Babel');
9-
});
10-
step('Click text=Babel Minify', async () => {
11-
const [page1] = await Promise.all([
12-
page.waitForEvent('popup'),
13-
page.click('text=Babel Minify')
14-
]);
15-
await Promise.all([
16-
page1.waitForNavigation(/*{ url: 'https://github.com/babel/minify' }*/),
17-
page1.click('a:has-text(\\"smoke\\")')
18-
]);
19-
await page1.close();
20-
});"
21-
`;
22-
23-
exports[`Synthetics JavaScript formatter suite journeys 1`] = `
3+
exports[`Synthetics JavaScript formatter accepts custom step organization 1`] = `
244
"const { journey, step, expect } = require('@elastic/synthetics');
255
266
journey('Recorded journey', async ({ page, context }) => {
27-
step('Go to https://vigneshh.in/', async () => {
7+
step('Open new page', async () => {
288
await page.goto('https://vigneshh.in/');
9+
});
10+
step('Assert text=Babel Minify isVisible', async () => {
2911
expect(await page.isVisible('text=Babel Minify')).toBeTruthy();
3012
expect(await page.isEditable('text=Babel Minify')).toBeTruthy();
31-
expect(await page.textContent('text=Babel Minify')).toMatch('Babel');
3213
});
33-
step('Click text=Babel Minify', async () => {
14+
step('Assert text=Babel Minify textContent', async () => {
15+
expect(await page.textContent('text=Babel Minify')).toMatch('Babel');
3416
const [page1] = await Promise.all([
3517
page.waitForEvent('popup'),
3618
page.click('text=Babel Minify')
@@ -39,13 +21,17 @@ journey('Recorded journey', async ({ page, context }) => {
3921
page1.waitForNavigation(/*{ url: 'https://github.com/babel/minify' }*/),
4022
page1.click('a:has-text(\\"smoke\\")')
4123
]);
24+
});
25+
step('Close page', async () => {
4226
await page1.close();
4327
});
4428
});"
4529
`;
4630

47-
exports[`Synthetics JavaScript formatter use modified title if available 1`] = `
48-
"step('Visiting profile', async () => {
31+
exports[`Synthetics JavaScript formatter uses custom step names 1`] = `
32+
"step('test-name', async () => {
4933
await page.goto('https://vigneshh.in/');
34+
expect(await page.isVisible('text=Babel Minify')).toBeTruthy();
35+
expect(await page.isEditable('text=Babel Minify')).toBeTruthy();
5036
});"
5137
`;

__tests__/formatter/javascript.test.ts

Lines changed: 154 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -24,162 +24,176 @@
2424
*/
2525
import {
2626
SyntheticsGenerator,
27-
ActionInContext,
27+
Step,
28+
Steps,
2829
} from '../../src/formatter/javascript';
2930

30-
const recorderActions: Array<ActionInContext> = [
31-
{
32-
pageAlias: 'page',
33-
isMainFrame: true,
34-
frameUrl: 'about:blank',
35-
committed: true,
36-
action: {
37-
name: 'openPage',
38-
url: 'about:blank',
39-
signals: [],
31+
const recorderStep: Step = {
32+
actions: [
33+
{
34+
pageAlias: 'page',
35+
isMainFrame: true,
36+
frameUrl: 'about:blank',
37+
committed: true,
38+
action: {
39+
name: 'openPage',
40+
url: 'about:blank',
41+
signals: [],
42+
},
4043
},
41-
},
42-
{
43-
pageAlias: 'page',
44-
isMainFrame: true,
45-
frameUrl: 'https://vigneshh.in/',
46-
committed: true,
47-
action: {
48-
name: 'navigate',
49-
url: 'https://vigneshh.in/',
50-
signals: [],
44+
{
45+
pageAlias: 'page',
46+
isMainFrame: true,
47+
frameUrl: 'https://vigneshh.in/',
48+
committed: true,
49+
action: {
50+
name: 'navigate',
51+
url: 'https://vigneshh.in/',
52+
signals: [],
53+
},
5154
},
52-
},
53-
{
54-
pageAlias: 'page',
55-
isMainFrame: true,
56-
frameUrl: 'https://vigneshh.in/',
57-
action: {
58-
name: 'assert',
59-
isAssert: true,
60-
command: 'isVisible',
61-
selector: 'text=Babel Minify',
62-
signals: [],
55+
{
56+
pageAlias: 'page',
57+
isMainFrame: true,
58+
frameUrl: 'https://vigneshh.in/',
59+
action: {
60+
name: 'assert',
61+
isAssert: true,
62+
command: 'isVisible',
63+
selector: 'text=Babel Minify',
64+
signals: [],
65+
},
6366
},
64-
},
65-
{
66-
pageAlias: 'page',
67-
isMainFrame: true,
68-
frameUrl: 'https://vigneshh.in/',
69-
action: {
70-
name: 'assert',
71-
isAssert: true,
72-
command: 'isEditable',
73-
selector: 'text=Babel Minify',
74-
signals: [],
67+
{
68+
pageAlias: 'page',
69+
isMainFrame: true,
70+
frameUrl: 'https://vigneshh.in/',
71+
action: {
72+
name: 'assert',
73+
isAssert: true,
74+
command: 'isEditable',
75+
selector: 'text=Babel Minify',
76+
signals: [],
77+
},
7578
},
76-
},
77-
{
78-
pageAlias: 'page',
79-
isMainFrame: true,
80-
frameUrl: 'https://vigneshh.in/',
81-
action: {
82-
name: 'assert',
83-
isAssert: true,
84-
command: 'textContent',
85-
selector: 'text=Babel Minify',
86-
value: 'Babel',
87-
signals: [],
79+
{
80+
pageAlias: 'page',
81+
isMainFrame: true,
82+
frameUrl: 'https://vigneshh.in/',
83+
action: {
84+
name: 'assert',
85+
isAssert: true,
86+
command: 'textContent',
87+
selector: 'text=Babel Minify',
88+
value: 'Babel',
89+
signals: [],
90+
},
8891
},
89-
},
90-
{
91-
pageAlias: 'page',
92-
isMainFrame: true,
93-
frameUrl: 'https://vigneshh.in/',
94-
action: {
95-
name: 'click',
96-
selector: 'text=Babel Minify',
97-
signals: [
98-
{
99-
name: 'popup',
100-
popupAlias: 'page1',
101-
isAsync: true,
102-
},
103-
],
104-
button: 'left',
105-
modifiers: 0,
106-
clickCount: 1,
92+
{
93+
pageAlias: 'page',
94+
isMainFrame: true,
95+
frameUrl: 'https://vigneshh.in/',
96+
action: {
97+
name: 'click',
98+
selector: 'text=Babel Minify',
99+
signals: [
100+
{
101+
name: 'popup',
102+
popupAlias: 'page1',
103+
isAsync: true,
104+
},
105+
],
106+
button: 'left',
107+
modifiers: 0,
108+
clickCount: 1,
109+
},
110+
committed: true,
107111
},
108-
committed: true,
109-
},
110-
{
111-
pageAlias: 'page1',
112-
isMainFrame: true,
113-
frameUrl: 'https://github.com/babel/minify',
114-
action: {
115-
name: 'click',
116-
selector: 'a:has-text("smoke")',
117-
signals: [
118-
{
119-
name: 'navigation',
120-
url: 'https://github.com/babel/minify',
121-
},
122-
{
123-
name: 'navigation',
124-
url: 'https://github.com/babel/minify/tree/master/smoke',
125-
},
126-
{
127-
name: 'navigation',
128-
url: 'https://github.com/babel/minify/tree/master/smoke',
129-
isAsync: true,
130-
},
131-
{
132-
name: 'navigation',
133-
url: 'https://github.com/babel/minify',
134-
isAsync: true,
135-
},
136-
],
137-
button: 'left',
138-
modifiers: 0,
139-
clickCount: 1,
112+
{
113+
pageAlias: 'page1',
114+
isMainFrame: true,
115+
frameUrl: 'https://github.com/babel/minify',
116+
action: {
117+
name: 'click',
118+
selector: 'a:has-text("smoke")',
119+
signals: [
120+
{
121+
name: 'navigation',
122+
url: 'https://github.com/babel/minify',
123+
},
124+
{
125+
name: 'navigation',
126+
url: 'https://github.com/babel/minify/tree/master/smoke',
127+
},
128+
{
129+
name: 'navigation',
130+
url: 'https://github.com/babel/minify/tree/master/smoke',
131+
isAsync: true,
132+
},
133+
{
134+
name: 'navigation',
135+
url: 'https://github.com/babel/minify',
136+
isAsync: true,
137+
},
138+
],
139+
button: 'left',
140+
modifiers: 0,
141+
clickCount: 1,
142+
},
140143
},
141-
},
142-
{
143-
pageAlias: 'page1',
144-
isMainFrame: true,
145-
frameUrl: 'https://github.com/babel/minify',
146-
committed: true,
147-
action: {
148-
name: 'closePage',
149-
signals: [],
144+
{
145+
pageAlias: 'page1',
146+
isMainFrame: true,
147+
frameUrl: 'https://github.com/babel/minify',
148+
committed: true,
149+
action: {
150+
name: 'closePage',
151+
signals: [],
152+
},
150153
},
151-
},
152-
{
153-
pageAlias: 'page',
154-
isMainFrame: true,
155-
frameUrl: 'https://vigneshh.in/',
156-
committed: true,
157-
action: {
158-
name: 'closePage',
159-
signals: [],
154+
{
155+
pageAlias: 'page',
156+
isMainFrame: true,
157+
frameUrl: 'https://vigneshh.in/',
158+
committed: true,
159+
action: {
160+
name: 'closePage',
161+
signals: [],
162+
},
160163
},
161-
},
162-
];
164+
],
165+
};
163166

164167
describe('Synthetics JavaScript formatter', () => {
165-
it('inline journeys', async () => {
166-
const formatter = new SyntheticsGenerator(false);
167-
expect(formatter.generateText(recorderActions)).toMatchSnapshot();
168+
it('accepts custom step organization', () => {
169+
const generator = new SyntheticsGenerator(true);
170+
const testSteps: Steps = [
171+
{ actions: recorderStep.actions.slice(0, 2) },
172+
{ actions: recorderStep.actions.slice(2, 4) },
173+
{ actions: recorderStep.actions.slice(4, 7) },
174+
{ actions: recorderStep.actions.slice(7, 9) },
175+
];
176+
expect(generator.generateFromSteps(testSteps)).toMatchSnapshot();
168177
});
169178

170-
it('suite journeys', async () => {
171-
const formatter = new SyntheticsGenerator(true);
172-
expect(formatter.generateText(recorderActions)).toMatchSnapshot();
179+
it('uses custom step names', () => {
180+
const generator = new SyntheticsGenerator(false);
181+
const testSteps: Steps = [{ actions: recorderStep.actions.slice(0, 4) }];
182+
expect(
183+
generator.generateFromSteps(
184+
testSteps.map((s: Step) => {
185+
s.name = 'test-name';
186+
return s;
187+
})
188+
)
189+
).toMatchSnapshot();
173190
});
174191

175-
it('use modified title if available', async () => {
176-
const formatter = new SyntheticsGenerator(false);
177-
const actions = [
178-
{
179-
...recorderActions[1],
180-
title: 'Visiting profile',
181-
},
182-
];
183-
expect(formatter.generateText(actions)).toMatchSnapshot();
192+
it('throws error if processing empty step', () => {
193+
const generator = new SyntheticsGenerator(false);
194+
const testSteps: Steps = [{ actions: [] }];
195+
expect(() => generator.generateFromSteps(testSteps)).toThrowError(
196+
'Cannot process an empty step'
197+
);
184198
});
185199
});

0 commit comments

Comments
 (0)