Files
gsfdc/demo-dify/dify/e2e/features/step-definitions/apps/web-app-service.steps.ts
T
wuzilongandCursor 6519682885 chore: 纳入本地 Dify 1.19.0 源码及品牌与免登改动
演示目录只保留可重建的源码。运行密钥、数据卷、依赖和带口令的本地配置不进入仓库。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-18 13:59:25 +08:00

120 lines
4.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import type { DifyWorld } from '../../support/world.ts'
import { Given, Then, When } from '@cucumber/cucumber'
import { expect } from '@playwright/test'
import { createTestApp } from '../../../support/api/apps.ts'
import { getAppSiteURL } from '../../../support/api/web-apps.ts'
import { syncRunnableWorkflowDraft } from '../../../support/api/workflows.ts'
import { createE2EResourceName } from '../../../support/naming.ts'
import { baseURL, defaultLocale } from '../../../test-env.ts'
Given('a new runnable workflow app has been published', async function (this: DifyWorld) {
const client = this.getConsoleClient()
const app = await createTestApp(client, createE2EResourceName('App', 'WebApp'), 'workflow')
this.createdAppIds.push(app.id)
this.lastCreatedAppName = app.name
await syncRunnableWorkflowDraft(client, app.id)
await client.apps.byAppId.workflows.publish.post({
body: { marked_comment: '', marked_name: '' },
params: { app_id: app.id },
})
const appDetail = await client.apps.byAppId.get({ params: { app_id: app.id } })
expect(appDetail.enable_site).toBe(true)
this.shareURL = getAppSiteURL(appDetail)
})
const getWebAppSwitch = (world: DifyWorld) => {
const webAppCard = world.getPage().getByRole('region', { name: 'Web App' })
return webAppCard.getByRole('switch', { name: 'Web App' })
}
async function waitForPersistedWebAppState(world: DifyWorld, enabled: boolean) {
const appId = world.createdAppIds.at(-1)
if (!appId) throw new Error('No workflow app has been created.')
const client = world.getConsoleClient()
// The status label updates optimistically. A different browser must wait for
// the saved state before reloading the published app.
await expect
.poll(
async () => {
const app = await client.apps.byAppId.get({ params: { app_id: appId } })
return app.enable_site
},
{ timeout: 10_000 },
)
.toBe(enabled)
}
When('an anonymous visitor opens the Web App', async function (this: DifyWorld) {
if (!this.shareURL) throw new Error('No Web App URL is available.')
if (!this.context) throw new Error('Playwright browser context has not been initialized.')
const browser = this.context.browser()
if (!browser) throw new Error('Playwright browser has not been initialized.')
const anonymousContext = await browser.newContext({ baseURL, locale: defaultLocale })
this.registerCleanup(() => anonymousContext.close())
this.sharedAppPage = await anonymousContext.newPage()
await this.sharedAppPage.goto(this.shareURL, { timeout: 20_000 })
})
When('the anonymous visitor reloads the Web App', async function (this: DifyWorld) {
if (!this.sharedAppPage) throw new Error('The anonymous visitor has not opened the Web App.')
await this.sharedAppPage.reload({ timeout: 20_000 })
})
When('I disable the Web App', async function (this: DifyWorld) {
const webAppSwitch = getWebAppSwitch(this)
await expect(webAppSwitch).not.toHaveAttribute('aria-disabled', 'true', { timeout: 15_000 })
await expect(webAppSwitch).toHaveAttribute('aria-checked', 'true')
await webAppSwitch.click()
})
When('I enable the Web App', async function (this: DifyWorld) {
const webAppSwitch = getWebAppSwitch(this)
await expect(webAppSwitch).not.toHaveAttribute('aria-disabled', 'true', { timeout: 15_000 })
await expect(webAppSwitch).toHaveAttribute('aria-checked', 'false')
await webAppSwitch.click()
})
Then('the Web App should be in service', async function (this: DifyWorld) {
const webAppCard = this.getPage().getByRole('region', { name: 'Web App' })
await expect(webAppCard.getByText(/^In service$/i)).toBeVisible({
timeout: 10_000,
})
await waitForPersistedWebAppState(this, true)
})
Then('the Web App should be disabled', async function (this: DifyWorld) {
const webAppCard = this.getPage().getByRole('region', { name: 'Web App' })
await expect(webAppCard.getByText('Disabled', { exact: true })).toBeVisible({
timeout: 10_000,
})
await waitForPersistedWebAppState(this, false)
})
Then('the published workflow Web App should be accessible', async function (this: DifyWorld) {
if (!this.sharedAppPage) throw new Error('The anonymous visitor has not opened the Web App.')
await expect(this.sharedAppPage.getByRole('button', { name: 'Execute' })).toBeVisible({
timeout: 15_000,
})
})
Then('the published workflow Web App should be unavailable', async function (this: DifyWorld) {
if (!this.sharedAppPage) throw new Error('The anonymous visitor has not opened the Web App.')
await expect(
this.sharedAppPage.getByRole('heading', {
name: 'This app doesn’t exist or isn’t accessible from your network.',
}),
).toBeVisible({
timeout: 15_000,
})
await expect(
this.sharedAppPage.getByText('Contact the app administrator if you need help.', {
exact: true,
}),
).toBeVisible()
})