Skip to content

Commit

Permalink
feat: support preview URL
Browse files Browse the repository at this point in the history
Preview URL allow to set a custom URL to override the default URL
and allow to navigate easily to a live environment deployed
remotely instead of having a "localhost" URL.
  • Loading branch information
gregberge committed Jan 18, 2025
1 parent 7ae1fa3 commit cb541de
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/api-client/src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface components {
/** @description The build number */
number: number;
/** @description The status of the build */
status: ("accepted" | "rejected") | ("stable" | "diffDetected") | ("expired" | "pending" | "progress" | "error" | "aborted");
status: ("accepted" | "rejected") | ("no-changes" | "changes-detected") | ("expired" | "pending" | "progress" | "error" | "aborted");
/**
* Format: uri
* @description The URL of the build
Expand Down Expand Up @@ -142,6 +142,7 @@ export interface components {
baseName?: string | null;
metadata?: {
url?: string;
previewUrl?: string;
viewport?: {
width: number;
height: number;
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ const schema = {
default: null,
nullable: true,
},
previewBaseUrl: {
env: "ARGOS_PREVIEW_BASE_URL",
format: String,
default: null,
nullable: true,
},
};

export interface Config {
Expand All @@ -187,6 +193,7 @@ export interface Config {
mode: "ci" | "monitoring" | null;
ciProvider: string | null;
threshold: number | null;
previewBaseUrl: string | null;
}

const createConfig = () => {
Expand Down Expand Up @@ -229,6 +236,7 @@ export async function readConfig(options: Partial<Config> = {}) {
parallelIndex: options.parallelIndex ?? config.get("parallelIndex") ?? null,
mode: options.mode || config.get("mode") || null,
ciProvider: ciEnv?.key || null,
previewBaseUrl: config.get("previewBaseUrl") || null,
});

config.validate();
Expand Down
37 changes: 37 additions & 0 deletions packages/core/src/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ export interface UploadParameters {
* Build metadata.
*/
metadata?: BuildMetadata;
/**
* Preview URL configuration.
* Accepts a base URL or a function that receives the URL and returns the preview URL.
*/
previewUrl?:
| {
baseUrl: string;
}
| ((url: string) => string);
}

async function getConfigFromOptions({
Expand Down Expand Up @@ -143,6 +152,23 @@ async function uploadFilesToS3(
}
}

/**
* Format the preview URL.
*/
function formatPreviewUrl(
url: string,
formatter: NonNullable<UploadParameters["previewUrl"]>,
) {
if (typeof formatter === "function") {
return formatter(url);
}
const urlObj = new URL(url);
return new URL(
urlObj.pathname + urlObj.search + urlObj.hash,
formatter.baseUrl,
).href;
}

/**
* Upload screenshots to Argos.
*/
Expand All @@ -154,6 +180,10 @@ export async function upload(params: UploadParameters) {
getConfigFromOptions(params),
getArgosCoreSDKIdentifier(),
]);
const previewUrlFormatter: UploadParameters["previewUrl"] =
params.previewUrl ??
(config.previewBaseUrl ? { baseUrl: config.previewBaseUrl } : undefined);

const files = params.files ?? ["**/*.{png,jpg,jpeg}"];
debug("Using config and files", config, files);

Expand Down Expand Up @@ -191,6 +221,13 @@ export async function upload(params: UploadParameters) {

if (metadata) {
delete metadata.transient;

if (metadata.url && previewUrlFormatter) {
metadata.previewUrl = formatPreviewUrl(
metadata.url,
previewUrlFormatter,
);
}
}

return {
Expand Down
24 changes: 24 additions & 0 deletions packages/cypress/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ While Cypress inherently provides screenshot functionality, the Argos Cypress in

Please refer to our [Quickstart guide](/quickstart/cypress) to get started with Argos and Cypress.

## Set a Preview URL

Argos displays the URL of the page when a screenshot is taken, helping you understand the screenshot’s context in the Argos UI. If you run tests locally and deploy your pull requests (PRs) to a preview URL, you can link the two by setting the `ARGOS_PREVIEW_URL` environment variable or configuring the `previewUrl` option in the Cypress configuration.

### Example Configuration

```ts title="cypress.config.js"
const { defineConfig } = require("cypress");
const { registerArgosTask } = require("@argos-ci/cypress/task");

module.exports = defineConfig({
e2e: {
async setupNodeEvents(on, config) {
registerArgosTask(on, config, {
uploadToArgos: !!process.env.CI,
previewUrl: {
baseUrl: "https://my-site.com", // Use a dynamic value here for different environments if needed.
},
});
},
},
});
```

## API Overview

### cy.argosScreenshot([name][, options])
Expand Down
2 changes: 1 addition & 1 deletion packages/cypress/src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { rename } from "node:fs/promises";

export type RegisterArgosTaskOptions = Omit<
UploadParameters,
"files" | "root"
"files" | "root" | "metadata"
> & {
/**
* Upload the report to Argos.
Expand Down
24 changes: 24 additions & 0 deletions packages/playwright/docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,30 @@ export default defineConfig({
});
```

## Set a Preview URL

Argos displays the URL of the page when a screenshot is taken, helping you understand the screenshot’s context in the Argos UI. If you run tests locally and deploy your pull requests (PRs) to a preview URL, you can link the two by setting the `ARGOS_PREVIEW_URL` environment variable or configuring the `previewUrl` option in the Argos reporter.

### Example Configuration

```ts title="playwright.config.ts"
import { defineConfig } from "@playwright/test";
import { createArgosReporterOptions } from "@argos-ci/playwright/reporter";

export default defineConfig({
reporter: [
[
"@argos-ci/playwright/reporter",
createArgosReporterOptions({
previewUrl: {
baseUrl: "https://my-site.com", // Use a dynamic value here for different environments if needed.
},
}),
],
],
});
```

## API Overview

### argosScreenshot(page, name[, options])
Expand Down
2 changes: 1 addition & 1 deletion packages/playwright/src/reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type DynamicBuildName<T extends readonly string[]> = {

export type ArgosReporterOptions<T extends string[] = string[]> = Omit<
UploadParameters,
"files" | "root" | "buildName"
"files" | "root" | "buildName" | "metadata"
> & {
/**
* Upload the report to Argos.
Expand Down
3 changes: 2 additions & 1 deletion packages/playwright/src/screenshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,10 @@ export async function argosScreenshot(
}
const browserName = browser.browserType().name();
const browserVersion = browser.version();
const url = page.url();

const metadata: ScreenshotMetadata = {
url: page.url(),
url,
viewport: viewportSize,
colorScheme,
mediaType,
Expand Down
1 change: 1 addition & 0 deletions packages/util/src/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type ScreenshotMetadata = {
url?: string;
previewUrl?: string;
viewport?: {
width: number;
height: number;
Expand Down

0 comments on commit cb541de

Please sign in to comment.