The phone keyboard covers your form field: test every input
Your end-to-end tests run at full window height, so they never see a keyboard. Add one Playwright test that opens each screen at phone size, focuses every input in turn, shrinks the viewport to the height a keyboard leaves, and checks two things. The field’s box has to sit inside the visible area, and document.elementFromPoint at the field’s centre has to return the field itself and not a pinned button. I ran it on a made-up checkout below. It failed on 5 fields before the fix and passed after.
Why the field ends up under the keyboard
Since Chrome 108, Chrome on Android no longer resizes the layout viewport when the on-screen keyboard opens. Only the visual viewport shrinks. That’s the default, resizes-visual, and a page gets it whenever its viewport tag has no interactive-widget key.
So take a page whose html and body are 100dvh, with the form scrolling inside and a fixed “Continue” bar at the bottom. The keyboard opens, the layout stays full height, and the keyboard covers the bottom part of it. Nothing in the page reacts to the smaller visible area or moves the focused field into it. A field near the bottom was already under the fixed bar before the keyboard came up.
Your tests don’t catch it because nothing in them opens a keyboard. Every field is visible at 412×915, clicks work, and the run is green.
The made-up checkout
The demo page has a short order summary, 7 fields (first name, last name, email, phone number, card number, billing ZIP code, promo code), and a 72px bar pinned to the bottom with “Due now $48.00” and a Continue button. An “Add a gift note” button opens a bottom sheet with 3 more fields: To, From and Message.
It’s one static HTML file. The viewport tag is width=device-width, initial-scale=1, nothing else.
The test
// keyboard-fields.spec.js
const { test, expect } = require('@playwright/test');
const path = require('node:path');
const { pathToFileURL } = require('node:url');
const PHONE = { width: 412, height: 915 };
const KEYBOARD_OPEN = { width: 412, height: 500 }; // stand-in for the keyboard
const url = pathToFileURL(path.join(__dirname, process.env.PAGE || 'checkout.html')).href;
const FIELDS = [
'input:not([type=hidden],[type=checkbox],[type=radio],[type=button],[type=submit],[type=file],[type=range],[type=color],[type=image])',
'textarea', 'select', '[contenteditable]',
].join(', ');
// Every screen and sheet that has fields, not only the one in the bug report.
const SCREENS = {
'checkout': { root: 'main', open: async () => {} },
'gift note sheet': {
root: '#note-sheet',
open: async (page) => page.getByRole('button', { name: 'Add a gift note' }).click(),
},
};
test.use({ viewport: PHONE, hasTouch: true, isMobile: true });
for (const [screen, { root, open }] of Object.entries(SCREENS)) {
test(`keyboard leaves every field visible: ${screen}`, async ({ page }) => {
const fields = page.locator(root).locator(FIELDS).filter({ visible: true });
await page.goto(url);
await open(page);
const count = await fields.count();
expect(count).toBeGreaterThan(0);
const problems = [];
for (let i = 0; i < count; i++) {
// fresh page for each field, at full phone height, keyboard closed
await page.setViewportSize(PHONE);
await page.goto(url);
await open(page);
const field = fields.nth(i);
await field.focus();
await page.setViewportSize(KEYBOARD_OPEN);
// let resize handlers and any scroll run
await page.evaluate(() => new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(r))));
const r = await field.evaluate((el) => {
const box = el.getBoundingClientRect();
const vv = window.visualViewport;
const x = box.left + box.width / 2;
const y = box.top + box.height / 2;
const hit = document.elementFromPoint(x, y);
const name = (n) => n ? n.tagName.toLowerCase() + (n.className ? '.' + n.className : '') : 'nothing';
return {
label: el.labels?.[0]?.textContent.trim().split('\n')[0] || el.name,
top: Math.round(box.top), bottom: Math.round(box.bottom), visibleHeight: Math.round(vv.height),
inside: box.top >= 0 && box.bottom <= vv.height,
onTop: hit === el || el.contains(hit),
hit: name(hit),
};
});
if (!r.inside) problems.push(`${r.label}: off screen (top ${r.top}, bottom ${r.bottom}, visible height ${r.visibleHeight})`);
else if (!r.onTop) problems.push(`${r.label}: covered by ${r.hit}`);
}
expect(problems).toEqual([]);
});
}
The field list comes from the DOM. When someone adds a “company name” field next month, it’s in the test without anyone touching the spec. Checkboxes, radios, buttons and the other input types that don’t open a keyboard are left out.
Each field gets a fresh page. Otherwise a scroll from the previous field decides whether the next one passes.
You need the elementFromPoint check for the pinned bar, because a field under a fixed bar has a perfectly good bounding box inside the viewport. Only a hit test at its centre tells you something else is drawn on top.
And the problems go into one list that’s compared to [], so a single run names every broken field instead of stopping at the first.
Headless Chromium has no on-screen keyboard, so the test fakes one by shrinking the whole window from 915 to 500. That’s how a phone behaves once the page opts into resizes-content (the first fix below). The test checks that the page keeps the focused field in the 500px that are left. Whether a real phone gives the page those 500px is what the device checklist at the end is for.
What it printed on the unfixed page
$ PAGE=checkout.html npx playwright test
Running 2 tests using 1 worker
1) keyboard-fields.spec.js:27:3 › keyboard leaves every field visible: checkout ──────────────────
Error: expect(received).toEqual(expected) // deep equality
- Expected - 1
+ Received + 6
- Array []
+ Array [
+ "Phone number: covered by div.pay-bar",
+ "Card number: off screen (top 504, bottom 548, visible height 500)",
+ "Billing ZIP code: off screen (top 580, bottom 624, visible height 500)",
+ "Promo code: off screen (top 655, bottom 699, visible height 500)",
+ ]
2) keyboard-fields.spec.js:27:3 › keyboard leaves every field visible: gift note sheet ───────────
+ Array [
+ "Message: off screen (top 471, bottom 567, visible height 500)",
+ ]
That’s trimmed to the failures. Phone number is inside the 500px, but the Continue bar takes the bottom 72px of the screen, and the hit test at the field’s centre came back with div.pay-bar. Card number starts 4px below the visible area. Promo code stayed exactly where it was, top 655, because nothing on the page reacts to the resize. The Message box in the gift note sheet runs from 471 to 567, so 67 of its 96px are below the visible area.
Three fixes
First, the viewport tag:
<meta name="viewport" content="width=device-width, initial-scale=1, interactive-widget=resizes-content">
With resizes-content, the keyboard resizes the layout viewport as well. MDN’s viewport page says the initial containing block resizes with it, which changes the computed size of viewport units, so a 100dvh page gets shorter. MDN’s compatibility table lists the key for Chrome on Android from 108 and Firefox on Android from 133.
The test can’t see this line. I ran it with only the tag added and got the same 5 failures, because the simulated keyboard already resizes everything.
Turning it on changes fixed elements too. Once the layout resizes, a position: fixed; bottom: 0 bar rides up with it and sits right above the keyboard, on top of the field you’re typing in.
Second, scroll the focused field into view when the space shrinks:
// keyboard-fix.js: load once, on every page
const vv = window.visualViewport;
const TYPING = 'input:not([type=checkbox],[type=radio],[type=button],[type=submit]), textarea, [contenteditable]';
let width = vv?.width;
let tallest = vv?.height; // tallest visible height seen at this width
let lastHeight = vv?.height;
function typingField() {
const el = document.activeElement;
return el?.matches(TYPING) ? el : null;
}
function update(shrank) {
// "keyboard open": a field is focused and the visible height dropped by more than 150px
const open = !!typingField() && tallest - vv.height > 150;
document.documentElement.classList.toggle('keyboard-open', open);
const field = typingField();
if (!field || !shrank) return;
requestAnimationFrame(() => { // a frame later, so a scroll the browser makes itself lands first
const box = field.getBoundingClientRect();
if (box.top < 0 || box.bottom > vv.height) field.scrollIntoView({ block: 'center' });
});
}
if (vv) {
vv.addEventListener('resize', () => {
if (vv.width !== width) { width = vv.width; tallest = vv.height; }
tallest = Math.max(tallest, vv.height);
const shrank = vv.height < lastHeight; // only the keyboard opening, not the URL bar coming back
lastHeight = vv.height;
update(shrank);
});
document.addEventListener('focusin', () => update(true));
document.addEventListener('focusout', () => requestAnimationFrame(() => update(false)));
}
The 150px threshold sits well above the height the browser’s own bars take when they slide in and out as you scroll, so a scroll never counts as the keyboard opening. The shrank check is there because the URL bar coming back also fires a resize, and if every resize re-centres the focused field, someone scrolling away from it gets yanked back.
Third, hide the pinned bar while typing:
.keyboard-open .pay-bar { display: none; }
Keep the bar mounted and only hide it. If the bar owns a summary sheet, unmounting it closes that sheet halfway through someone’s typing.
With all three in, the same command:
$ PAGE=checkout-fixed.html npx playwright test
Running 2 tests using 1 worker
2 passed (7.6s)
Promo code now lands at top 305 with the bar gone, and Message sits at 388 to 484 inside the sheet. I also took the bar-hiding CSS line out again to make sure it was doing something: Phone number failed again, covered by div.pay-bar, and everything else passed.
Cover every screen and every sheet
Bug reports name one field on one screen, so the fix and its test both go to that screen, and the sheet next to it keeps the bug. To see it in the demo, I scoped the scroll fix to the checkout form, the way you’d write it if the report said “phone number on checkout”:
return el?.matches(TYPING) && document.getElementById('contact').contains(el) ? el : null;
Checkout passed. The gift note sheet failed with the exact line from before the fix:
+ "Message: off screen (top 471, bottom 567, visible height 500)",
So put every screen and every sheet with a text field in SCREENS, and attach the fix to document, so it covers the sheets too. When a screen has no test fixtures yet, write it on the device list below.
What to check on a real phone
The test can’t open a real keyboard, and it can’t show iOS. Chrome’s article says iOS browsers resize only the visual viewport, and that position: fixed elements there stay put and can be hidden by the keyboard. Headless Chromium also never hides the address bar. So after the test is green, take an Android phone with Chrome and an iPhone with Safari, and on each:
- Tap every field on every screen and sheet in
SCREENS, plus the screens that aren’t in it yet. Each field stays fully above the keyboard while you type. - Check that the pinned bar disappears while you type and comes back when the keyboard closes.
- Focus a field, then scroll the page away from it. The page shouldn’t jump back to the field.
- On the iPhone, look at where the field lands after the scroll. It should sit above the keyboard, and the simulation can’t show you where
block: 'center'puts it there. - If your checkout also runs inside an iframe on another site, repeat steps 1 and 2 there.
If something breaks on a phone but passed in Playwright, add that screen or that check to the test first, then fix it.