Giter Site home page Giter Site logo

Add `BASE_URL` support about replay HOT 7 CLOSED

puskuruk avatar puskuruk commented on August 26, 2024 2
Add `BASE_URL` support

from replay.

Comments (7)

OrKoN avatar OrKoN commented on August 26, 2024 1

@puskuruk I think it'd be nice to allow importing custom runner extensions so that use case/app-specific logic could be implemented. But I think it's something that we need to take a look into!

from replay.

OrKoN avatar OrKoN commented on August 26, 2024

Hey, thanks for the feature request. How would it work if the recording involves multiple domains? I guess using this variable might result in a broken recording in that case?

from replay.

puskuruk avatar puskuruk commented on August 26, 2024

Thank you @OrKoN

Yes, when you have multiple domains it'll fail. In our case we deploy our application to different environments and I wanted be able to test those environments with replacing base URLs.

How we can improve this implementation?

from replay.

puskuruk avatar puskuruk commented on August 26, 2024

In our codebase,I solved this problem with a custom runner but I thought using an environment variable is more straightforward solution. Because it's a common practice.

https://playwright.dev/docs/test-advanced
https://docs.cypress.io/guides/references/best-practices#Setting-a-global-baseUrl

/**
 * Run a recording from a json file
 * @param {string} recordingPath
 * @param {Extension} extension
 */
async function runRecording(recordingPath, extension) {
  const content = await fs.readFile(recordingPath, 'utf8');
  const object = JSON.parse(content);
  const recordingWithBaseURL = addBaseURLToRecording(object);

  const recording = parse(recordingWithBaseURL);
  const runner = await createRunner(recording, extension);

  await runner.run();
}

But importing custom runner extensions also makes sense, and I can implement this feature ๐Ÿ™

from replay.

OrKoN avatar OrKoN commented on August 26, 2024

I see! Thanks for sharing those. It looks like in other frameworks it works differently in an important way: baseUrl applies only to the relative URLs in the script and does not rewrite absolute URLs. We currently don't support relative URLs though. I think we will need some time to re-think the UX here, so thanks for the feedback!

from replay.

s4l4x avatar s4l4x commented on August 26, 2024

The issue for us is that the starting url may and usually is different from where we want to run the tests. This wouldn't be an issue if we were only testing production, but we test PRs, development, staging, and local environments, all with different URLs. This allows us to catch bugs before going to production.

It seems like there is an implicit starting url with the first navigate step. One idea would be if there's a BASE_URL defined, then we could override the starting url of a recording's first step and any subsequent URL bases which match the starting URL. Something like this:

Recording 1

steps: [
    {
      "type": "navigate",
      "url": "http://localhost:3001/doawesome",
      ...
    },
    ...
    {
      "type": "navigate",
      "url": "http://someotherurl.com",
      ...
    },
    ...
    {
      "type": "navigate",
      "url": "http://localhost:3001/doawesome",
      ...
    },
]

Recording 2

steps: [
    {
      "type": "navigate",
      "url": "http://localhost:3008/doawesome",
      ...
    },
    ...
    {
      "type": "navigate",
      "url": "http://someotherurl.com",
      ...
    },
    ...
    {
      "type": "navigate",
      "url": "http://localhost:3008/doawesome",
      ...
    },
]

With a BASE_URL = website.ftw these would become:

Recording 1

steps: [
    {
      "type": "navigate",
      "url": "http://website.ftw/doawesome",
      ...
    },
    ...
    {
      "type": "navigate",
      "url": "http://someotherurl.com",
      ...
    },
    ...
    {
      "type": "navigate",
      "url": "http://website.ftw/doawesome",
      ...
    },
]

Recording 2

steps: [
    {
      "type": "navigate",
      "url": "http://website.ftw/doawesome",
      ...
    },
    ...
    {
      "type": "navigate",
      "url": "http://someotherurl.com",
      ...
    },
    ...
    {
      "type": "navigate",
      "url": "http://website.ftw/doawesome",
      ...
    },
]

Thoughts on this approach?

from replay.

puskuruk avatar puskuruk commented on August 26, 2024

Since we have a solution to modify URLs with an extension we can close this issue. ๐Ÿ™

Example implementation:

const { PuppeteerRunnerExtension } = require('@puppeteer/replay')

class Extension extends PuppeteerRunnerExtension {
  async runStep(step, flow) {
    if ('url' in step) {
      // modify URL here
      // reassign step here
    }

    await super.runStep(step, flow)
  }
}

from replay.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.