Rayrun
← Back to Discord Forum

How to append string to the current userAgent value

flyingchief888posted in #help-playwright
Open in Discord
flyingchief888
flyingchief888

One of my mobile project config looks like this, how can I get the current userAgent and append string to it?

{
    name: "mobile",
    use: {
      browserName: "chromium",
      isMobile: true,
      userAgent: `${userAgent} appendString`,
    },
  },

I did some research but most of people are using DeviceDescriptor and easy to get it but that's not my case. https://github.com/microsoft/playwright/issues/12199

This thread is trying to answer question "How can I get the current userAgent and append string to it in my mobile project configuration?"

5 replies
flyingchief888
flyingchief888

How to append string to the current userAgent value

If you're using fixtures then you can override user agent fixture, this is for example:

userAgent: async ({ viewport, defaultBrowserType }, use) => {
    const isDesktopViewport = JSON.stringify(viewport) === JSON.stringify(viewports.desktop)
    const mobileUserAgent =
      defaultBrowserType === 'chromium'
        ? devices['Galaxy S8'].userAgent
        : devices['iPhone 12 Pro'].userAgent
    const desktopUserAgent =
      defaultBrowserType === 'chromium'
        ? devices['Desktop Chrome'].userAgent
        : devices['Desktop Safari'].userAgent

    const userAgent = isDesktopViewport
      ? `${desktopUserAgent} append whatever`
      : `${mobileUserAgent} append whatever`

    await use(userAgent)
  },

With your config, you could probably do

userAgent: `${devices['Desktop Chrome']} append whatever`

choose whatever device you need

flyingchief888
flyingchief888

Thank you so much!! @skorp32 !

Answer

Modifying User Agent in Playwright's AndroidDevice Class

First, let's import the necessary modules and initialize a new instance of the AndroidDevice class. We'll use the 'Galaxy S9' as an example.

import { android } from '@playwright/test';

const device = await android.devices['Galaxy S9'];

Next, let's get the current user agent. The userAgent property returns a promise that resolves to a string representing the current user agent of the emulated device.

const userAgent = await device.userAgent();

Now, let's append a string to the user agent. You can do this by concatenating your desired string to the user agent.

const modifiedUserAgent = `${userAgent} MyCustomString`;

That's it! You've successfully modified the user agent. You can now use this modified user agent in your mobile project configuration as needed.

Remember, this guide is based on the AndroidDevice class in Playwright. If your mobile project or framework has additional requirements or constraints, you might need to adjust this process accordingly.

For more insights on Playwright, check out our blog post on Playwright Mobile App Testing for Android: A Comprehensive Guide.

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].