1

I have to write tests for web application and also I have to use them on mobile chrome browser.It is any possibility to use chrome devtools and mobile device emulator during test?

Thanks for help

vott
  • 53
  • 5

2 Answers2

1

For Puppeteer use chrome option in config with defaultViewport value.

https://codecept.io/helpers/Puppeteer/#configuration https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions

"Puppeteer": {
  "url": "https://rambler.ru",
  "browser": "chrome",
  ...
  "chrome": {
      "defaultViewport": {
          "width": 640,
          "height": 360,
          "deviceScaleFactor": 1,
          "isMobile": true,
          "hasTouch": true,
          "isLandscape": false
      }
  }
}

Or use page.emulate() before each test https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions

UPD: add page.emulate example

For page.emulate using: In custom helper, create own function, which will work with page for example:

async emulateDevice(options) {
  const currentPage = this.helpers['Puppeteer'].page;
  await currentPage.emulate(options);
}

Where option is object with viewport and userAgent: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pageemulateoptions https://codecept.io/helpers/Puppeteer/#access-from-helpers

Then in test: Create options:

const myCustomViewPort = {
  viewport: {
    width: 640,
    height: 360,
    deviceScaleFactor: 1,
    isMobile: true,
    hasTouch: true,
    isLandscape: false
  },
  userAgent: ""
}

And you can call it in your code:

Before(async (I) => {
  await I.emulateDevice(myCustomViewPort);
});
  • Could you provide any examples for page.emulate() using codeceptjs? Couse this examples from puppeteer documentation doesn't work for me. I am not sure that is my mistake or this is problem with my specific project. – vott Nov 11 '18 at 21:10
0

You can use Chrome mobile emulation by passing chrome options to webdriver.

For example, if you use WebDriverIO helper and want to use Nexus 5:

helpers: {
  WebDriverIO: {
    url: "https://rambler.ru",
    browser: "chrome",
    ...
    desiredCapabilities: {
      chromeOptions: {
        mobileEmulation: {
          deviceName: "Nexus 5"
        }
      }
    }
  }
}

Or if you want to specify something more specific:

chromeOptions: {
    mobileEmulation: {
        deviceMetrics: { width: 360, height: 640, pixelRatio: 3.0 },
        userAgent:
            "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"
        }
    }
}

More information here: http://chromedriver.chromium.org/mobile-emulation