Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues with playwright engine and target url as baseURL #2583

Closed
servohatred opened this issue Mar 16, 2024 · 5 comments
Closed

Issues with playwright engine and target url as baseURL #2583

servohatred opened this issue Mar 16, 2024 · 5 comments

Comments

@servohatred
Copy link

servohatred commented Mar 16, 2024

Version info:

2.0.7

We got this update on 2.0.6 and suddenly all my playwright tests stopped working

Playwright’s baseURL is now set from target (#2493) - Documentation

The reason : there are some cases where we use a dynamic base urls for tests (in my case we use one for the apis and a different one for the app in multiple environments). It should be selected depending on the environment or application but you cannot do that in the yaml file(In my case is a ts file with a method). A terrible solution is to create a script to expose the BASE URL to the next script (artillery) something via env vars , like BASE_URL=$(tsnode setmybaseurl.ts) && BASE_URL=$BASE_URL artillery run my.yml so it would be ready for the yaml to use it , this does not work for me because I wont be able to combine API with UI methods. After the change we are forced to use a BASE_URL because if we dont pass one in the context options is going to use the target (and this is mandatory). In my case . It was required to not use any base url so I can set up the whole url manually in my page.goto() and my page.request.post() since both use different urls (both api and page methods are required for my tests and according to pw docs they use the BASE_URL by default if it's assigned).
if I try to navigate to any page or post something I will always get this error:

apiRequestContext.post: Invalid URL

If I pass the whole URL to my page.goto is not going to override the BASE URL , is going to append the full url to the base url and it's going to end up as an invalid url.
I think, that forcing people to have a target URL is fine for the http engine since you are using only APIS and it's required, but for playwright is a limitation since you are not going to be able to use both, API and UI methods.
PS: A good solution would be to allow people to override the target url with a blank URL but that does not work in the current version:

const contextOptions = {
        baseURL: self.target,
        ...self.contextOptions
      }

For some reason if I pass a baseURL:"" in the context options is not overriding the target which is a dummy url that used to be ignored by the playwright engine in <2.0.6

@bernardobridge
Copy link
Contributor

Hey @servohatred

Could you share a sample yaml and test script so I can test it out? I'd like to have a look if there's something we can do for our upcoming Monday release. Thanks!

@servohatred
Copy link
Author

Sorry about the delay, I was trying to replicate the issue in another repo:

add-to-cart.api.spec.ts

import { Page, chromium, expect } from "@playwright/test";
import { EventEmitter, ScenarioContext } from "artillery";

export async function addToCart(thisPageDoesNotWork: Page, context: ScenarioContext, ee: EventEmitter) {
  await thisPageDoesNotWork.goto("/cart.html");
  const apiLoginUrl = "https://api.demoblaze.com/login";
  let thisPageWorks = await(await (await chromium.launch()).newContext()).newPage();
   // if you change the page on the next request for this it will work. The only reason is because of target being the baseURL, it will work for UI but not for API.
   // Since base url is setup for page and apirequestContext methods, it will be better to not have anything setup for baseURL, with 2.0.6 we are being forced to setup a target/baseURL
  const loginResponse = await thisPageDoesNotWork.request.post(apiLoginUrl, {
    data: {
      username: "demo-user-bullet",
      password: "VktqcGV6cEhhQjl2QGpI",
    },
    headers: {
      "Content-Type": "application/json",
      "User-Agent":
        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.36 OS/10.0.22621",
      Accept: "*/*",
      "Accept-Encoding": "gzip, deflate, br",
      host: "api.demoblaze.com",
    },
    timeout: 10000,
    failOnStatusCode: true,
  });
  expect(await loginResponse.text()).not.toContain("error");
  const authToken = (await loginResponse.text()).split(" ")[1].replace(/[\n\r"]/g, "");
  expect(authToken).not.toBeNull();
  };

add-to-cart.yaml

config:
  target: www.demoblaze.com
  plugins:
    ensure: {}
  engines:
    playwright:
      launchOptions:
        headless: false
  processor: processors/add-to-cart.api.spec.ts
  phases:
    - duration: 1
      arrivalRate: 1
      maxVusers: 1
  variables:
    articleId:
      - "1"
      - "6"
      - "8"
    vus:
      - 1

scenarios:
  - name: Checkout.AddToCart
    engine: playwright
    flowFunction: addToCart
    flow: []
    weight: 1

@bernardobridge
Copy link
Contributor

bernardobridge commented Mar 18, 2024

Hey @servohatred 👋

Thanks for being thorough, it was very helpful to have an example to test against!

From what I can tell, in the scenario you share, the issue is that you didn't add the protocol to your target (i.e. https://www.demoblaze.com instead of www.demoblaze.com). The same error of Invalid URL happens if you set without http/https in a playwright.config.ts file (i.e. doing a pure Playwright test).

If I make that change in the scenario you shared, then everything runs for me.

I am a bit confused as to why it suddenly stopped working for you after 2.0.6 though - I'd be curious to see how you were setting your base URL dynamically.

Let me know if that works for you! 🙇‍♂️

@servohatred
Copy link
Author

That just worked, looks like something related to playwright. Thank you for all your help !

@bernardobridge
Copy link
Contributor

Great! Glad it worked. 👍🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants