1
const username = process.env.USERNAME as string;
const password = process.env.PASSWORD as string;

test.beforeEach(async ({ page }) => {
    await page.goto(neo_bank_portal_login);
    await page.fill('[name="username"]', username);
    await page.fill('[name="password"]', password);
    const submitBtnSelector = "//button[contains(@class,'MuiButtonBase-root MuiButton-root')]"
    await page.waitForSelector(submitBtnSelector)
    const submitBtn = await page.$(submitBtnSelector)
    const isSubmitBtnEnabled = await submitBtn!.isEnabled()

    if (isSubmitBtnEnabled) {
      await submitBtn!.click()
    }
    await page.waitForLoadState("networkidle");
    await page.click("//p[text()='Artifacts']");
});

test.afterEach(async ({page}) => {
  await page.close();
});

test.describe("Artifacts", () => {
  test.only("Redirect Artifacts", async ({page}) => {
    // Artifacts Url
    const url = page.url();
    expect(url).toEqual("https://neo-bank-portal.dev.dcx.apps.piestory.net/dcx/artifacts")
  });
}

The issue here is that when i run this test case in my local machine it always passes and it's perfectly working, i also tried playwright's codegen just to make sure i'm getting the right xpath or its locator, but when i run Gitlab's Pipeline it always results to timeout and the locator can't be found.

Gitlab CI/CD Error

This is my gitlab-ci.yml

stages:
  - smoke-test

tests:
  stage: smoke-test
  image: mcr.microsoft.com/playwright:v1.32.0-focal
  script:
    - npm ci
    - npx playwright install --with-deps
    - export const USERNAME=$CI_USERNAME
    - export const PASSWORD=$CI_PASSWORD
    - npx playwright test

How can i Fix the error in pipeline? I also tried adding page.waitForTimeout just to make sure the page loads before interacting with its elements

I also switched the test browser from chromium to firefox, but there's no luck

Temporarily i'm storing my creds.env here

enter image description here

UPDATE:

So to simplify what i've been trying to do, i refactored the code

test.beforeEach(async ({ page }) => {
    await page.goto(neo_bank_portal_login);
    const username_field = await page.waitForSelector("//input[@class='MuiInputBase-input MuiInput-input']");
    await username_field.fill(username);
    const password_field = await page.waitForSelector("(//input[contains(@class,'MuiInputBase-input MuiInput-input')])[2]");
    await password_field.fill(password);

    const submitBtnSelector = "//button[contains(@class,'MuiButtonBase-root MuiButton-root')]"
    await page.waitForSelector(submitBtnSelector)
    const submitBtn = await page.$(submitBtnSelector)
    const isSubmitBtnEnabled = await submitBtn!.isEnabled()
    if (isSubmitBtnEnabled) {
      await submitBtn!.click()
    }
  });

  test.only("Redirect Artifacts", async ({page}) => {
    const artifacts_sec = await page.waitForSelector("(//div[@class='MuiListItemText-root']//p)[12]")
    await artifacts_sec.click();
    const url = page.url();
    expect(url).toEqual("link");
  });

What i noticed is it only renders the login page await page.goto(portal_login);

The code inside beforeEach works but when i tried to submit or login, that's when the test Fails i think it doesn't even render the home after Logging In, and i don't have any idea why this happens, the code is perfectly executed but only timeouts in CI/CD, any idea why this happens?

test.beforeEach(async ({ page }) => {
  await page.goto(portal_login);
});
        
test.only("Redirect Artifacts", async ({page}) => {
  // Test in portal_login interface
});

I Tested the portal_login which is the url of the page i only verified it by testing small interface which successfully passed in CI/CD in less than 10 sec

But when executing the login scenario it always timeout, and it shouldn't since it passed less than 5 sec in Local. I also tried setting the timeout longer than the default, so it means that it's not slow, but it does not render at all.

In conclusion, it successfully renders the login page, but it cannot render the home page after a successful login?

UPDATE:

I resolved this with playwright trace, downloaded the result in Gitlab's CI.

The issue here was the Gitlab Variable i set was protected i forgot to uncheck it, that's why it can't be accessed in unprotected branch.

Dids
  • 137
  • 2
  • 2
  • 18
  • Does your local Playwright version match the version of your Docker image? – Vishal Aggarwal Apr 24 '23 at 22:09
  • @VishalAggarwal yes and i don't think the version is the issue i tried another web app to test with local Playwright and Docker image with different versions and it still works. – Dids Apr 25 '23 at 12:01
  • Can you share the screenshot of error where it failed? Or see yourself exactly where it is on the UI when it fails even better if you have the video. The screenshots/videos helps a lot in investigating mysterious failures :) – Vishal Aggarwal Apr 25 '23 at 15:14
  • 1
    okay sure i'll edit the Question right now @VishalAggarwal – Dids Apr 25 '23 at 15:30
  • 1
    Thanks , however this video is of local execution but the issue is in CI. We can directly trace which is even better than videos during test execution in CI from playwright itself.https://playwright.dev/docs/best-practices#debugging-on-ci – Vishal Aggarwal Apr 25 '23 at 15:49
  • 1
    If things work for you locally but not on the CI , this suggests strongly that the way your web server configured to run on CI is not correct. – Vishal Aggarwal Apr 25 '23 at 20:58

1 Answers1

1

I believe your problem is that your username/password are not set correctly: you're defining the variables in the gitlab UI as USERNAME and PASSWORD, but then expect them to be called CI_USERNAME and CI_PASSWORD in your gitlab-ci.yaml. Since these do not exist

    - export const USERNAME=$CI_USERNAME
    - export const PASSWORD=$CI_PASSWORD

will override USERNAME & PASSWORD and set them to empty.

To fix this, you should either define your variables as CI_USERNAME & CI_PASSWORD in the UI (and remove the const keyword in the gitlab-ci.yaml, since it's not necessary) or just remove the export const lines entirely.

Mirco
  • 171
  • 5
  • I tried this and it will result to `Error: elementHandle.fill: value: expected string, got undefined` It means USERNAME & PASSWORD is not empty and is working fine – Dids Apr 25 '23 at 12:03
  • Which one of my suggestions did you try? Tbh I don't think that the current way could work, since CI_USERNAME & CI_PASSWORD are never set. They are probably interpreted as an empty string in your code. – Mirco Apr 25 '23 at 23:11
  • i did rename the variables and yeah you're right the problem is in USERNAME and PASSWORD thanks to @VishalAggarwal i did trace what's happening in CI, since the Logs doesn't show all problems, I saw that it is indeed empty, however, it did not work when removing `expost const` entirely since it will trigger `error value: expected string` the error is in my side, the Variable is `protected` that's why i can't use it when running CI – Dids Apr 26 '23 at 06:05