Dynamic fetch of URL based on the deployment environment

28 Views Asked by At

I have written a code to fetch URL dynamically using typescript and to use that to run my playwright test scripts. The problem is i dono whether the variables set for the URL fetch has to called in CI/CD tool or the code itself should fetch the url automatically when it pushed to different environnment.

Please help me , if there is a way that the code can automatically fetch the url of the environment that has been pushed.

datahelper.ts

import { BASE_URLS } from "./envConfig" // Import BASE_URLS from envConfig

interface Credentials {
  username: string
  password: string
}

export function getCredentials(): Credentials {
  const data = require("../data/loginData.json")
  const environment = process.env.DEPLOYMENT_ENV || "staging" // Use the deployment environment variable
  return data.credentials[BASE_URLS[environment]] || {} // Access BASE_URLS using the determined environment
}

envconfig.ts

export const BASE_URLS: Record<string, string> = {
  local: "example1.com",
  staging: "example2.com",
  production: "example3.com",
}

Playwright test script

import { expect, test } from "@playwright/test"
import { getCredentials } from "./dataHelper"
import { BASE_URLS } from "./denvConfig"

test.beforeEach(async ({ page }) => {
  const creds = getCredentials()
  const deploymentEnv = process.env.DEPLOYMENT_ENV || "staging"
  await page.goto(BASE_URLS[deploymentEnv])
  const signinmodule = await page.locator("div").filter({ hasText: "Hello Again!Welcome" }).nth(2)
  await signinmodule.getByRole("button", { name: "Login" }).click()
})

This is the code. Please suggest changes to automatically fetch the url on the playwright test script based on the deployment env.

0

There are 0 best solutions below