Source:
public void testPwaOfflinePath() throws IOException { open(); Assert.assertTrue("Should have navigator.serviceWorker", (Boolean) executeScript("return !!navigator.serviceWorker;")); Assert.assertTrue("Should have service worker registered", (Boolean) ((JavascriptExecutor) getDriver()).executeAsyncScript( "const done = arguments[arguments.length - 1];" + "const timeout = new Promise(" + " resolve => setTimeout(resolve, 100000)" + ");" + "Promise.race([" + " navigator.serviceWorker.ready," + " timeout])" + ".then(result => done(!!result));")); Assert.assertNotNull("Should have outlet when loaded online", findElement(By.id("outlet"))); setConnectionType(NetworkConnection.ConnectionType.AIRPLANE_MODE); try { Assert.assertEquals("navigator.onLine should be false", false, executeScript("return navigator.onLine")); executeScript("window.location.reload();"); waitUntil(webDriver -> ((JavascriptExecutor) driver) .executeScript("return document.readyState") .equals("complete")); waitForElementPresent(By.tagName("head")); WebElement head = findElement(By.tagName("head")); waitForElementPresent(By.tagName("title")); WebElement title = head.findElement(By.tagName("title")); Assert.assertEquals(ParentLayout.PWA_NAME, executeScript("return arguments[0].textContent", title)); Assert.assertEquals(ParentLayout.PWA_NAME, executeScript("return document.title;")); WebElement body = findElement(By.tagName("body")); Assert.assertTrue("Should not have outlet when loaded offline", body.findElements(By.id("outlet")).isEmpty()); WebElement offline = body.findElement(By.id("offline")); Assert.assertEquals(ParentLayout.PWA_NAME, offline.findElement(By.tagName("h1")).getText()); WebElement message = offline.findElement(By.className("message")); Assert.assertTrue("Should have “offline” in message", message.getText().toLowerCase().contains("offline")); } finally { setConnectionType(NetworkConnection.ConnectionType.ALL); } }

Target:
public void testPwaOfflinePath() throws IOException { open(); waitForServiceWorkerReady(); Assert.assertNotNull("Should have outlet when loaded online", findElement(By.id("outlet"))); setConnectionType(NetworkConnection.ConnectionType.AIRPLANE_MODE); try { Assert.assertEquals("navigator.onLine should be false", false, executeScript("return navigator.onLine")); executeScript("window.location.reload();"); waitUntil(webDriver -> ((JavascriptExecutor) driver) .executeScript("return document.readyState") .equals("complete")); waitForElementPresent(By.tagName("head")); WebElement head = findElement(By.tagName("head")); waitForElementPresent(By.tagName("title")); WebElement title = head.findElement(By.tagName("title")); Assert.assertEquals(ParentLayout.PWA_NAME, executeScript("return arguments[0].textContent", title)); Assert.assertEquals(ParentLayout.PWA_NAME, executeScript("return document.title;")); WebElement body = findElement(By.tagName("body")); Assert.assertTrue("Should not have outlet when loaded offline", body.findElements(By.id("outlet")).isEmpty()); WebElement offline = body.findElement(By.id("offline")); Assert.assertEquals(ParentLayout.PWA_NAME, offline.findElement(By.tagName("h1")).getText()); WebElement message = offline.findElement(By.className("message")); Assert.assertTrue("Should have “offline” in message", message.getText().toLowerCase().contains("offline")); } finally { setConnectionType(NetworkConnection.ConnectionType.ALL); } }