Skip to content

Commit b51d97d

Browse files
committed
PRO-21872 feat: add function readJsonDataFromHtmlReport to public API
feat: export function for getting reference of scenarios and tests fix: use correct expires in getCookies/setCookies actions
1 parent 008e2f3 commit b51d97d

23 files changed

Lines changed: 282 additions & 36 deletions

‎src/actions/getCookies.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {assertValueIsDefined} from '../utils/asserts';
55

66
import type {Cookie} from '../types/internal';
77

8+
const msInSecond = 1_000;
9+
810
/**
911
* Returns page's cookies with the specified cookies parameters.
1012
* If there are no cookies parameters, returns all the cookies.
@@ -24,8 +26,15 @@ export const getCookies = async (
2426
await step(
2527
logMessage,
2628
async () => {
27-
const page = getPlaywrightPage();
28-
const allCookies = await page.context().cookies(page.url());
29+
const playwrightPage = getPlaywrightPage();
30+
const playwrightCookies = await playwrightPage.context().cookies(playwrightPage.url());
31+
32+
// Playwright's `cookies` returns `expires` in unix time in seconds (`-1` for session cookies),
33+
// while `Cookie` type uses milliseconds (as in `Date.now()`).
34+
const allCookies: readonly Cookie[] = playwrightCookies.map(({expires, ...cookie}) => ({
35+
...cookie,
36+
...(expires !== -1 ? {expires: expires * msInSecond} : undefined),
37+
}));
2938

3039
if (parameters.length === 0) {
3140
cookies = allCookies;

‎src/actions/setCookies.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {getPlaywrightPage} from '../useContext';
44

55
import type {Cookie} from '../types/internal';
66

7+
const msInSecond = 1_000;
8+
79
/**
810
* Set cookies with the specified cookies parameters.
911
*/
@@ -15,7 +17,14 @@ export const setCookies = (cookies: readonly Cookie[]): Promise<void> =>
1517

1618
const browserContext = page.context();
1719

18-
await browserContext.addCookies(cookies);
20+
// Playwright's `addCookies` expects `expires` in unix time in seconds,
21+
// while `Cookie` type uses milliseconds (as in `Date.now()`).
22+
const playwrightCookies = cookies.map(({expires, ...cookie}) => ({
23+
...cookie,
24+
...(expires !== undefined ? {expires: Math.round(expires / msInSecond)} : undefined),
25+
}));
26+
27+
await browserContext.addCookies(playwrightCookies);
1928
},
2029
{payload: {cookies}, type: LogEventType.InternalAction},
2130
);

‎src/types/http/cookie.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ import type {Brand} from '../brand';
55
*/
66
export type Cookie = Readonly<{
77
domain?: string;
8+
/**
9+
* Unix time in milliseconds (as in `Date.now()`).
10+
*/
811
expires?: number;
912
httpOnly?: boolean;
1013
name: string;

‎src/types/http/http.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export type MapOptions = Readonly<{
4141
* HTTP method.
4242
*/
4343
export type Method =
44-
'CONNECT' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT' | 'TRACE';
44+
'CONNECT' | 'DELETE' | 'GET' | 'HEAD' | 'OPTIONS' | 'PATCH' | 'POST' | 'PUT' | 'QUERY' | 'TRACE';
4545

4646
/**
4747
* Object with query (search) part of the url, or query string itself.

‎src/types/index.ts‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export type {
9191
PropertyDescriptor,
9292
PropertyKey,
9393
} from './properties';
94-
export type {LiteReport, LiteRetry} from './report';
94+
export type {HtmlReportJsonData, LiteReport, LiteRetry, ReportClientData} from './report';
9595
export type {
9696
ApiRouteClassType,
9797
ApiRouteClassTypeWithGetParamsFromUrl,
@@ -104,6 +104,19 @@ export type {PackageInfo, StartInfo} from './startInfo';
104104
export type {StepBody, StepOptions} from './step';
105105
export type {StringForLogs} from './string';
106106
export type {Tab} from './tab';
107+
export type {
108+
FullTestRun,
109+
LiteTestRun,
110+
RejectTestRun,
111+
RunError,
112+
RunHash,
113+
RunId,
114+
TestFn,
115+
TestFunction,
116+
TestOptions,
117+
TestRun,
118+
TestStaticOptions,
119+
} from './testRun';
107120
export type {MergeTuples, TupleRest} from './tuples';
108121
export type {
109122
CloneWithoutUndefinedProperties,

‎src/types/internal.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,14 @@ export type {
134134
PropertyDescriptor,
135135
PropertyKey,
136136
} from './properties';
137-
export type {LiteReport, LiteRetry} from './report';
137+
export type {HtmlReportJsonData, LiteReport, LiteRetry, ReportClientData} from './report';
138138
/** @internal */
139139
export type {
140-
ReportClientData,
141140
ReportClientState,
142141
ReportData,
143142
Retry,
144143
RetryButtonProps,
145144
RetryProps,
146-
ScriptJsonData,
147145
TestRunButtonProps,
148146
} from './report';
149147
/** @internal */
@@ -171,6 +169,7 @@ export type {Tab} from './tab';
171169
/** @internal */
172170
export type {InternalTab} from './tab';
173171
export type {
172+
FullTestRun,
174173
LiteTestRun,
175174
RejectTestRun,
176175
RunError,
@@ -183,7 +182,7 @@ export type {
183182
TestStaticOptions,
184183
} from './testRun';
185184
/** @internal */
186-
export type {CompletedTestRun, FullTestRun, RunTest, Test, TestUnit} from './testRun';
185+
export type {CompletedTestRun, RunTest, Test, TestUnit} from './testRun';
187186
export type {MergeTuples, TupleRest} from './tuples';
188187
export type {
189188
CloneWithoutUndefinedProperties,

‎src/types/report.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ import type {
1515
TestMetaPlaceholder,
1616
} from './userland';
1717

18+
/**
19+
* JSON data in `<script>` tags with JSON presentation of report data.
20+
*/
21+
export type HtmlReportJsonData = ReportClientData | readonly FullTestRun[];
22+
1823
/**
1924
* The lite report data (for printing lite JSON report) with userland meta.
2025
*/
@@ -81,7 +86,6 @@ export type ReportData = Readonly<{
8186

8287
/**
8388
* The general report data that needed on client for rendering parts of HTML report.
84-
* @internal
8589
*/
8690
export type ReportClientData = Readonly<{
8791
apiStatistics: ApiStatistics;
@@ -140,12 +144,6 @@ export type RetryProps = Readonly<{
140144
testRunButtons: readonly TestRunButtonProps[];
141145
}>;
142146

143-
/**
144-
* JSON data in `<script>` tags with JSON presentation of report data.
145-
* @internal
146-
*/
147-
export type ScriptJsonData = ReportClientData | readonly FullTestRun[];
148-
149147
/**
150148
* TestRunButton component props.
151149
* @internal

‎src/types/testRun.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ export type CompletedTestRun<TestMeta = TestMetaPlaceholder> = Readonly<{
2222

2323
/**
2424
* Full test run object result of userland hooks (like mainParams and runHash).
25-
* @internal
2625
*/
2726
export type FullTestRun = Readonly<{mainParams: string; runHash: RunHash}> & TestRun;
2827

‎src/utils/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,16 @@ export {deepMerge, getEntries, getKeys, setReadonlyProperty} from './object';
4444
export {createPageObjectsFromMultiLocator} from './pageObjects';
4545
export {
4646
getCodeReport,
47+
getFullStepDefinition,
4748
getLinesIndexes,
4849
getScenarioReference,
50+
getStepReference,
4951
getTestReference,
5052
parseMaybeEmptyValueAsJson,
5153
parseTest,
5254
ParseTestError,
5355
parseValueAsJsonIfNeeded,
56+
readJsonDataFromHtmlReport,
5457
} from './parse';
5558
export {
5659
addTimeoutToPromise,

‎src/utils/parse/codeReport/fillTestErrors.ts‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {getScenarioReference} from './getScenarioReference';
22
import {getScenarioStepsWithReference} from './getScenarioStepsWithReference';
33
import {getStepComparisonErrors} from './getStepComparisonErrors';
4+
import {getStepReference} from './getStepReference';
45
import {getTestReference} from './getTestReference';
56
import {getTestStepsWithReference} from './getTestStepsWithReference';
67

@@ -28,17 +29,21 @@ export const fillTestErrors = (
2829
for (const step of scenario.steps) {
2930
if (step.definition === '') {
3031
scenarioHasError = true;
31-
errors.push(
32-
`Step in ${scenario.featurePath}:${step.lineNumber + 1}:${step.column + 1} (in ${scenarioReference}) has no definition.`,
32+
33+
const reference = getStepReference(
34+
{column: step.column + 1, line: step.lineNumber + 1},
35+
scenario.featurePath,
3336
);
37+
38+
errors.push(`Step ${reference} (in ${scenarioReference}) has no definition.`);
3439
}
3540
}
3641

3742
for (const step of test.steps) {
3843
if (step.definition === undefined || step.definition === '') {
39-
errors.push(
40-
`Step in ${test.path}:${step.line}:${step.column} (in ${testReference}) has no definition.`,
41-
);
44+
const reference = getStepReference(step, test.path);
45+
46+
errors.push(`Step ${reference} (in ${testReference}) has no definition.`);
4247
}
4348
}
4449

0 commit comments

Comments
 (0)