Revert "Add helper functions for adding breakpoints in sources"

This reverts commit 2fe65225504734e15c365e62394a02b97b0715e8.

Reason: Causing flaky tests on waterfall

Bug: chromium:1178804
Change-Id: I38e46d9e786467e9bbdcbbf7301da1e363bd0f57
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/2697200
Auto-Submit: Kim-Anh Tran <kimanh@chromium.org>
Reviewed-by: Philip Pfaffe <pfaffe@chromium.org>
Reviewed-by: Almothana Athamneh <almuthanna@chromium.org>
Commit-Queue: Almothana Athamneh <almuthanna@chromium.org>
diff --git a/front_end/sources/UISourceCodeFrame.js b/front_end/sources/UISourceCodeFrame.js
index 8322a77..d50edad 100644
--- a/front_end/sources/UISourceCodeFrame.js
+++ b/front_end/sources/UISourceCodeFrame.js
@@ -306,7 +306,6 @@
     }
     this._decorateAllTypes();
     this._ensurePluginsLoaded();
-    Common.EventTarget.fireEvent('source-file-loaded', this._uiSourceCode.displayName());
   }
 
   /**
diff --git a/test/e2e/helpers/sources-helpers.ts b/test/e2e/helpers/sources-helpers.ts
index a1c6a6c..20364b0 100644
--- a/test/e2e/helpers/sources-helpers.ts
+++ b/test/e2e/helpers/sources-helpers.ts
@@ -6,6 +6,7 @@
 import * as puppeteer from 'puppeteer';
 
 import {$$, click, getBrowserAndPages, getPendingEvents, getTestServerPort, goToResource, pressKey, step, timeout, typeText, waitFor, waitForFunction} from '../../shared/helper.js';
+import {AsyncScope} from '../../shared/mocha-extensions.js';
 
 export const ACTIVE_LINE = '.CodeMirror-activeline > pre > span';
 export const PAUSE_ON_EXCEPTION_BUTTON = '[aria-label="Don\'t pause on exceptions"]';
@@ -20,25 +21,6 @@
 export const TURNED_ON_PAUSE_BUTTON_SELECTOR = 'button.toolbar-state-on';
 export const DEBUGGER_PAUSED_EVENT = 'DevTools.DebuggerPaused';
 
-export async function navigateToLine(frontend: puppeteer.Page, lineNumber: number|string) {
-  await frontend.keyboard.down('Control');
-  await frontend.keyboard.press('KeyG');
-  await frontend.keyboard.up('Control');
-  await frontend.keyboard.type(`${lineNumber}`);
-  await frontend.keyboard.press('Enter');
-}
-
-export async function getLineNumberElement(lineNumber: number|string) {
-  const visibleLines = await $$(CODE_LINE_SELECTOR);
-  for (let i = 0; i < visibleLines.length; i++) {
-    const lineValue = await visibleLines[i].evaluate(node => node.textContent);
-    if (lineValue === `${lineNumber}`) {
-      return visibleLines[i];
-    }
-  }
-  return null;
-}
-
 export async function doubleClickSourceTreeItem(selector: string) {
   const item = await waitFor(selector);
   await click(item, {clickOptions: {clickCount: 2}, maxPixelsFromLeft: 40});
@@ -105,15 +87,11 @@
 }
 
 export async function openFileInEditor(sourceFile: string) {
-  const {frontend} = getBrowserAndPages();
-
-  await clearSourceFilesLoaded(frontend);
-  await listenForSourceFilesLoaded(frontend);
-
   // Open a particular file in the editor
   await doubleClickSourceTreeItem(`[aria-label="${sourceFile}, file"]`);
 
-  await waitForSourceLoadedEvent(frontend, sourceFile);
+  // Wait for the file to be formattable, this process is async after opening a file
+  await waitFor(`[aria-label="Pretty print ${sourceFile}"]`);
 }
 
 export async function openSourceCodeEditorForFile(sourceFile: string, testInput: string) {
@@ -137,41 +115,68 @@
   });
 }
 
-export async function addBreakpointForLine(
-    frontend: puppeteer.Page, index: number|string, expectedFail: boolean = false) {
-  await navigateToLine(frontend, index);
-  const breakpointLine = await getLineNumberElement(index);
-  assert.isNotNull(breakpointLine, 'Line is not visible or does not exist');
+// We can't use the click helper, as it is not possible to select a particular
+// line number element in CodeMirror.
+export async function addBreakpointForLine(frontend: puppeteer.Page, index: number, expectedFail: boolean = false) {
+  const asyncScope = new AsyncScope();
+  await asyncScope.exec(() => frontend.waitForFunction((index: number, CODE_LINE_SELECTOR: string) => {
+    return document.querySelectorAll(CODE_LINE_SELECTOR).length >= (index - 1);
+  }, {timeout: 0}, index, CODE_LINE_SELECTOR));
+  const breakpointLineNumber = await frontend.evaluate((index, CODE_LINE_SELECTOR) => {
+    const element = document.querySelectorAll(CODE_LINE_SELECTOR)[index - 1];
 
-  await waitForFunction(async () => !(await isBreakpointSet(index)));
-  await breakpointLine?.click();
+    const {left, top, width, height} = element.getBoundingClientRect();
+    return {
+      x: left + width * 0.5,
+      y: top + height * 0.5,
+    };
+  }, index, CODE_LINE_SELECTOR);
 
-  // FIXME(crbug/1172294): add an assertion to check that breakpoint hasn't been set
+  const currentBreakpointCount = await frontend.$$eval('.cm-breakpoint', nodes => nodes.length);
+
+  await frontend.mouse.click(breakpointLineNumber.x, breakpointLineNumber.y);
+
   if (expectedFail) {
     return;
   }
 
-  await waitForFunction(async () => await isBreakpointSet(index));
-}
-
-export async function removeBreakpointForLine(frontend: puppeteer.Page, index: number|string) {
-  await navigateToLine(frontend, index);
-  const breakpointLine = await getLineNumberElement(index);
-  assert.isNotNull(breakpointLine, 'Line is not visible or does not exist');
-
-  await waitForFunction(async () => await isBreakpointSet(index));
-  await breakpointLine?.click();
-  await waitForFunction(async () => !(await isBreakpointSet(index)));
+  await asyncScope.exec(() => frontend.waitForFunction((bpCount: number) => {
+    return document.querySelectorAll('.cm-breakpoint').length > bpCount &&
+        document.querySelectorAll('.cm-breakpoint-unbound').length === 0;
+  }, {timeout: 0}, currentBreakpointCount));
 }
 
 export function sourceLineNumberSelector(lineNumber: number) {
   return `div.CodeMirror-code > div:nth-child(${lineNumber}) div.CodeMirror-linenumber.CodeMirror-gutter-elt`;
 }
 
-export async function isBreakpointSet(lineNumber: number|string) {
-  const breakpointLineParentClasses =
-      await (await getLineNumberElement(lineNumber))?.evaluate(n => n.parentElement?.className);
-  return breakpointLineParentClasses?.includes('cm-breakpoint');
+export function waitForSourceCodeLines(noOfLines: number) {
+  return waitForFunction(async () => {
+    const elements = await $$(SOURCES_LINES_SELECTOR);
+    return elements.length >= noOfLines ? elements : undefined;
+  });
+}
+
+export async function checkBreakpointIsActive(lineNumber: number) {
+  await step(`check that the breakpoint is still active at line ${lineNumber}`, async () => {
+    const sourcesLines = await waitForSourceCodeLines(lineNumber);
+    const codeLineNums = await Promise.all(sourcesLines.map(elements => {
+      return elements.evaluate(el => el.className);
+    }));
+    assert.deepInclude(codeLineNums[lineNumber - 1], 'cm-breakpoint');
+    assert.notDeepInclude(codeLineNums[lineNumber - 1], 'cm-breakpoint-disabled');
+    assert.notDeepInclude(codeLineNums[lineNumber - 1], 'cm-breakpoint-unbound');
+  });
+}
+
+export async function checkBreakpointIsNotActive(lineNumber: number) {
+  await step(`check that the breakpoint is not active at line ${lineNumber}`, async () => {
+    const sourcesLines = await waitForSourceCodeLines(lineNumber);
+    const codeLineNums = await Promise.all(sourcesLines.map(elements => {
+      return elements.evaluate(el => el.className);
+    }));
+    assert.notDeepInclude(codeLineNums[lineNumber - 1], 'cm-breakpoint');
+  });
 }
 
 export async function checkBreakpointDidNotActivate() {
@@ -274,43 +279,9 @@
   interface Window {
     /* eslint-disable @typescript-eslint/naming-convention */
     __sourceFilesAddedEvents: string[];
-    __sourceFilesLoadedEvents: string[];
-    __sourceFilesLoadedEventListenerAdded: boolean;
-    /* eslint-enable @typescript-eslint/naming-convention */
   }
 }
 
-export async function reloadPageAndWaitForSourceFile(
-    frontend: puppeteer.Page, target: puppeteer.Page, sourceFile: string) {
-  await clearSourceFilesLoaded(frontend);
-  await listenForSourceFilesLoaded(frontend);
-  await target.reload();
-  await waitForSourceLoadedEvent(frontend, sourceFile);
-}
-
-export function listenForSourceFilesLoaded(frontend: puppeteer.Page) {
-  return frontend.evaluate(() => {
-    if (!window.__sourceFilesLoadedEvents) {
-      window.__sourceFilesLoadedEvents = [];
-    }
-    if (!window.__sourceFilesLoadedEventListenerAdded) {
-      window.addEventListener('source-file-loaded', (event: Event) => {
-        const customEvent = event as CustomEvent<string>;
-        window.__sourceFilesLoadedEvents.push(customEvent.detail);
-      });
-      window.__sourceFilesLoadedEventListenerAdded = true;
-    }
-  });
-}
-
-export function waitForSourceLoadedEvent(frontend: puppeteer.Page, fileName: string) {
-  return waitForFunction(async () => {
-    return frontend.evaluate(fileName => {
-      return window.__sourceFilesLoadedEvents.includes(fileName);
-    }, fileName);
-  });
-}
-
 export function listenForSourceFilesAdded(frontend: puppeteer.Page) {
   return frontend.evaluate(() => {
     window.__sourceFilesAddedEvents = [];
@@ -324,11 +295,9 @@
 }
 
 export function waitForAdditionalSourceFiles(frontend: puppeteer.Page, count = 1) {
-  return waitForFunction(async () => {
-    return frontend.evaluate(count => {
-      return window.__sourceFilesAddedEvents.length >= count;
-    }, count);
-  });
+  return frontend.waitForFunction((count: number) => {
+    return window.__sourceFilesAddedEvents.length >= count;
+  }, undefined, count);
 }
 
 export function clearSourceFilesAdded(frontend: puppeteer.Page) {
@@ -337,12 +306,6 @@
   });
 }
 
-export function clearSourceFilesLoaded(frontend: puppeteer.Page) {
-  return frontend.evaluate(() => {
-    window.__sourceFilesLoadedEvents = [];
-  });
-}
-
 export function retrieveSourceFilesAdded(frontend: puppeteer.Page) {
   // Strip hostname, to make it agnostic of which server port we use
   return frontend.evaluate(() => window.__sourceFilesAddedEvents.map(file => new URL(`https://${file}`).pathname));
diff --git a/test/e2e/sources/can-break-with-wasm-sourcemaps_test.ts b/test/e2e/sources/can-break-with-wasm-sourcemaps_test.ts
index dc95b60..59056af 100644
--- a/test/e2e/sources/can-break-with-wasm-sourcemaps_test.ts
+++ b/test/e2e/sources/can-break-with-wasm-sourcemaps_test.ts
@@ -4,9 +4,9 @@
 
 import {assert} from 'chai';
 
-import {getBrowserAndPages, step, timeout, waitForFunction} from '../../shared/helper.js';
+import {getBrowserAndPages, step, timeout} from '../../shared/helper.js';
 import {describe, it} from '../../shared/mocha-extensions.js';
-import {addBreakpointForLine, checkBreakpointDidNotActivate, isBreakpointSet, openFileInEditor, openSourceCodeEditorForFile, reloadPageAndWaitForSourceFile, retrieveTopCallFrameScriptLocation, retrieveTopCallFrameWithoutResuming, sourceLineNumberSelector} from '../helpers/sources-helpers.js';
+import {addBreakpointForLine, checkBreakpointDidNotActivate, checkBreakpointIsActive, checkBreakpointIsNotActive, openFileInEditor, openSourceCodeEditorForFile, retrieveTopCallFrameScriptLocation, retrieveTopCallFrameWithoutResuming, sourceLineNumberSelector, waitForSourceCodeLines} from '../helpers/sources-helpers.js';
 
 describe('The Sources Tab', async () => {
   it('can add breakpoint for a sourcemapped wasm module', async () => {
@@ -21,10 +21,14 @@
 
   it('hits two breakpoints that are set and activated separately', async function() {
     const {target, frontend} = getBrowserAndPages();
-    const fileName = 'with-sourcemap.ll';
 
     await step('navigate to a page and open the Sources tab', async () => {
-      await openSourceCodeEditorForFile(fileName, 'wasm/wasm-with-sourcemap.html');
+      await openSourceCodeEditorForFile('with-sourcemap.ll', 'wasm/wasm-with-sourcemap.html');
+    });
+    const numberOfLines = 11;
+
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
     });
 
     await step('add a breakpoint to line No.5', async () => {
@@ -32,10 +36,17 @@
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
     });
 
-    await waitForFunction(async () => await isBreakpointSet(5));
+    // FIXME(crbug/1112692): Refactor test to remove the timeout.
+    await timeout(100);
+
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(5);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
@@ -57,7 +68,11 @@
       await openFileInEditor('with-sourcemap.ll');
     });
 
-    await waitForFunction(async () => !(await isBreakpointSet(5)));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsNotActive(5);
     await checkBreakpointDidNotActivate();
 
     await step('add a breakpoint to line No.6', async () => {
@@ -65,10 +80,17 @@
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
     });
 
-    await waitForFunction(async () => await isBreakpointSet(6));
+    // FIXME(crbug/1112692): Refactor test to remove the timeout.
+    await timeout(100);
+
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(6);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
diff --git a/test/e2e/sources/can-open-linear-memory-inspector_test.ts b/test/e2e/sources/can-open-linear-memory-inspector_test.ts
index d8c33e2..a7e826b 100644
--- a/test/e2e/sources/can-open-linear-memory-inspector_test.ts
+++ b/test/e2e/sources/can-open-linear-memory-inspector_test.ts
@@ -7,7 +7,7 @@
 import {$, $$, click, enableExperiment, getBrowserAndPages, goToResource, step, waitFor} from '../../shared/helper.js';
 import {describe, it} from '../../shared/mocha-extensions.js';
 import {checkIfTabExistsInDrawer, DRAWER_PANEL_SELECTOR} from '../helpers/cross-tool-helper.js';
-import {addBreakpointForLine, inspectMemory, openSourceCodeEditorForFile, PAUSE_BUTTON, reloadPageAndWaitForSourceFile, RESUME_BUTTON} from '../helpers/sources-helpers.js';
+import {addBreakpointForLine, inspectMemory, openSourceCodeEditorForFile, PAUSE_BUTTON, RESUME_BUTTON, waitForSourceCodeLines} from '../helpers/sources-helpers.js';
 
 const LINEAR_MEMORY_INSPECTOR_TAB_SELECTOR = '#tab-linear-memory-inspector';
 const LINEAR_MEMORY_INSPECTOR_TABBED_PANE_SELECTOR = DRAWER_PANEL_SELECTOR + ' .tabbed-pane';
@@ -19,8 +19,8 @@
     await enableExperiment('wasmDWARFDebugging');
 
     const {frontend, target} = getBrowserAndPages();
-    const breakpointLine = '0x039';
-    const fileName = 'memory.wasm';
+    const breakpointLine = 5;
+    const numberOfLines = 7;
 
     await step('navigate to a page and open the Sources tab', async () => {
       await openSourceCodeEditorForFile('memory.wasm', 'wasm/memory.html');
@@ -31,7 +31,11 @@
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+    });
+
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
     });
 
     await step('expand the module scope', async () => {
diff --git a/test/e2e/sources/can-show-wasm-scopes_test.ts b/test/e2e/sources/can-show-wasm-scopes_test.ts
index e8890c7..799d0d9 100644
--- a/test/e2e/sources/can-show-wasm-scopes_test.ts
+++ b/test/e2e/sources/can-show-wasm-scopes_test.ts
@@ -4,15 +4,15 @@
 
 import {assert} from 'chai';
 
-import {click, getBrowserAndPages, step, waitFor, waitForFunction} from '../../shared/helper.js';
+import {click, getBrowserAndPages, step, timeout, waitFor, waitForFunction} from '../../shared/helper.js';
 import {describe, it} from '../../shared/mocha-extensions.js';
-import {addBreakpointForLine, getScopeNames, getValuesForScope, openSourceCodeEditorForFile, PAUSE_INDICATOR_SELECTOR, reloadPageAndWaitForSourceFile, RESUME_BUTTON} from '../helpers/sources-helpers.js';
+import {addBreakpointForLine, getScopeNames, getValuesForScope, openSourceCodeEditorForFile, PAUSE_INDICATOR_SELECTOR, RESUME_BUTTON, waitForSourceCodeLines} from '../helpers/sources-helpers.js';
 
 describe('Source Tab', async () => {
   it('shows and updates the module, local, and stack scope while pausing', async () => {
     const {frontend, target} = getBrowserAndPages();
-    const breakpointLine = '0x05f';
-    const fileName = 'scopes.wasm';
+    const breakpointLine = 12;
+    const numberOfLines = 16;
     let moduleScopeValues: string[];
     let localScopeValues: string[];
 
@@ -25,7 +25,13 @@
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
+    });
+
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
     });
 
     await step('check that the module, local, and stack scope appear', async () => {
diff --git a/test/e2e/sources/debug-raw-wasm_test.ts b/test/e2e/sources/debug-raw-wasm_test.ts
index 80898bd..11e67cf 100644
--- a/test/e2e/sources/debug-raw-wasm_test.ts
+++ b/test/e2e/sources/debug-raw-wasm_test.ts
@@ -5,9 +5,10 @@
 import {assert} from 'chai';
 import * as puppeteer from 'puppeteer';
 
-import {$, click, getBrowserAndPages, goToResource, installEventListener, step, waitFor, waitForFunction} from '../../shared/helper.js';
+import {$, click, getBrowserAndPages, goToResource, installEventListener, step, timeout, waitFor, waitForFunction} from '../../shared/helper.js';
 import {describe, it} from '../../shared/mocha-extensions.js';
-import {addBreakpointForLine, checkBreakpointDidNotActivate, clearSourceFilesAdded, DEBUGGER_PAUSED_EVENT, getBreakpointDecorators, getCallFrameLocations, getCallFrameNames, getNonBreakableLines, getValuesForScope, isBreakpointSet, listenForSourceFilesAdded, openSourceCodeEditorForFile, openSourcesPanel, reloadPageAndWaitForSourceFile, removeBreakpointForLine, RESUME_BUTTON, retrieveSourceFilesAdded, retrieveTopCallFrameScriptLocation, retrieveTopCallFrameWithoutResuming, SELECTED_THREAD_SELECTOR, sourceLineNumberSelector, stepThroughTheCode, switchToCallFrame, TURNED_OFF_PAUSE_BUTTON_SELECTOR, waitForAdditionalSourceFiles} from '../helpers/sources-helpers.js';
+
+import {addBreakpointForLine, checkBreakpointDidNotActivate, checkBreakpointIsActive, checkBreakpointIsNotActive, clearSourceFilesAdded, DEBUGGER_PAUSED_EVENT, getBreakpointDecorators, getCallFrameLocations, getCallFrameNames, getNonBreakableLines, getValuesForScope, listenForSourceFilesAdded, openSourceCodeEditorForFile, openSourcesPanel, RESUME_BUTTON, retrieveSourceFilesAdded, retrieveTopCallFrameScriptLocation, retrieveTopCallFrameWithoutResuming, SELECTED_THREAD_SELECTOR, sourceLineNumberSelector, stepThroughTheCode, switchToCallFrame, TURNED_OFF_PAUSE_BUTTON_SELECTOR, waitForAdditionalSourceFiles, waitForSourceCodeLines} from '../helpers/sources-helpers.js';
 
 describe('Sources Tab', async function() {
   // The tests in this suite are particularly slow, as they perform a lot of actions
@@ -42,7 +43,7 @@
     const {target, frontend} = getBrowserAndPages();
 
     await openSourceCodeEditorForFile('add.wasm', 'wasm/call-to-add-wasm.html');
-    await addBreakpointForLine(frontend, '0x023');
+    await addBreakpointForLine(frontend, 3);
 
     const scriptLocation = await retrieveTopCallFrameScriptLocation('main();', target);
     assert.deepEqual(scriptLocation, 'add.wasm:0x23');
@@ -50,47 +51,69 @@
 
   it('hits two breakpoints that are set and activated separately', async function() {
     const {target, frontend} = getBrowserAndPages();
-    const fileName = 'add.wasm';
 
     await step('navigate to a page and open the Sources tab', async () => {
-      await openSourceCodeEditorForFile(fileName, 'wasm/call-to-add-wasm.html');
+      await openSourceCodeEditorForFile('add.wasm', 'wasm/call-to-add-wasm.html');
     });
 
-    await step('add a breakpoint to line No.0x027', async () => {
-      await addBreakpointForLine(frontend, '0x027');
+    const numberOfLines = 7;
+
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+    await step('add a breakpoint to line No.5', async () => {
+      await addBreakpointForLine(frontend, 5);
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await waitForFunction(async () => await isBreakpointSet('0x027'));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(5);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
       assert.deepEqual(scriptLocation, 'add.wasm:0x27');
     });
 
-    await step('remove the breakpoint from the line 0x027', async () => {
-      await removeBreakpointForLine(frontend, '0x027');
+    await step('remove the breakpoint from the fifth line', async () => {
+      await click(sourceLineNumberSelector(5));
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await waitForFunction(async () => !(await isBreakpointSet('0x027')));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsNotActive(5);
     await checkBreakpointDidNotActivate();
 
-    await step('add a breakpoint to line No.0x028', async () => {
-      await addBreakpointForLine(frontend, '0x028');
+    await step('add a breakpoint to line No.6', async () => {
+      await addBreakpointForLine(frontend, 6);
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await waitForFunction(async () => await isBreakpointSet('0x028'));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(6);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
@@ -100,21 +123,31 @@
 
   it('shows variable value in popover', async function() {
     const {target, frontend} = getBrowserAndPages();
-    const fileName = 'add.wasm';
 
     await step('navigate to a page and open the Sources tab', async () => {
       await openSourceCodeEditorForFile('add.wasm', 'wasm/call-to-add-wasm.html');
     });
 
-    await step('add a breakpoint to line No.0x023', async () => {
-      await addBreakpointForLine(frontend, '0x023');
+    const numberOfLines = 7;
+
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+    await step('add a breakpoint to line No.3', async () => {
+      await addBreakpointForLine(frontend, 3);
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await step('hover over the $var0 in line No.0x023', async () => {
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await step('hover over the $var0 in line No.3', async () => {
       const pausedPosition = await waitForFunction(async () => {
         const element = await $('.cm-variable-2.cm-execution-line-tail');
         if (element && await element.evaluate(e => e.isConnected)) {
@@ -142,30 +175,41 @@
       0x04b,
     ]);
     // Line 1 is non-breakable.
-    await addBreakpointForLine(frontend, '0x000', true);
+    await addBreakpointForLine(frontend, 1, true);
     assert.deepEqual(await getBreakpointDecorators(frontend), []);
     // Line 3 is breakable.
-    await addBreakpointForLine(frontend, '0x023');
+    await addBreakpointForLine(frontend, 3);
     assert.deepEqual(await getBreakpointDecorators(frontend), [0x023]);
   });
 
   it('is able to step with state', async () => {
     const {target, frontend} = getBrowserAndPages();
-    const fileName = 'stepping-with-state.wasm';
 
     await step('navigate to a page and open the Sources tab', async () => {
       await openSourceCodeEditorForFile('stepping-with-state.wasm', 'wasm/stepping-with-state.html');
     });
 
-    await step('add a breakpoint to line No.0x060', async () => {
-      await addBreakpointForLine(frontend, '0x060');
+    const numberOfLines = 32;
+
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await step('add a breakpoint to line No.23', async () => {
+      await addBreakpointForLine(frontend, 23);
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await waitForFunction(async () => await isBreakpointSet('0x060'));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(23);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
@@ -186,19 +230,25 @@
       ]);
     });
 
-    await step('remove the breakpoint from the line 0x060', async () => {
-      await removeBreakpointForLine(frontend, '0x060');
+    await step('remove the breakpoint from the 23rd line', async () => {
+      await click(sourceLineNumberSelector(23));
     });
 
-    await step('add a breakpoint to line No.0x048', async () => {
-      await addBreakpointForLine(frontend, '0x048');
+    await step('add a breakpoint to line No.8', async () => {
+      await addBreakpointForLine(frontend, 8);
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await waitForFunction(async () => await isBreakpointSet('0x048'));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(8);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
@@ -230,11 +280,13 @@
   // Flakey e2e test on Windows bot.
   it.skip('[crbug.com/1177714] is able to step with state in multi-threaded code in main thread', async () => {
     const {target, frontend} = getBrowserAndPages();
-    const fileName = 'stepping-with-state.wasm';
+
     await step('navigate to a page and open the Sources tab', async () => {
       await openSourceCodeEditorForFile('stepping-with-state.wasm', 'wasm/stepping-with-state-and-threads.html');
     });
 
+    const numberOfLines = 32;
+
     await step('check that the main thread is selected', async () => {
       const selectedThreadElement = await waitFor(SELECTED_THREAD_SELECTOR);
       const selectedThreadName = await selectedThreadElement.evaluate(element => {
@@ -243,15 +295,21 @@
       assert.strictEqual(selectedThreadName, 'Main', 'the Main thread is not active');
     });
 
-    await step('add a breakpoint to line No.0x060', async () => {
-      await addBreakpointForLine(frontend, '0x060');
+    await step('add a breakpoint to line No.23', async () => {
+      await addBreakpointForLine(frontend, 23);
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await waitForFunction(async () => await isBreakpointSet('0x060'));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(23);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
@@ -272,19 +330,25 @@
       ]);
     });
 
-    await step('remove the breakpoint from the line 0x060', async () => {
-      await removeBreakpointForLine(frontend, '0x060');
+    await step('remove the breakpoint from the 23rd line', async () => {
+      await click(sourceLineNumberSelector(23));
     });
 
-    await step('add a breakpoint to line No.0x048', async () => {
-      await addBreakpointForLine(frontend, '0x048');
+    await step('add a breakpoint to line No.8', async () => {
+      await addBreakpointForLine(frontend, 8);
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await waitForFunction(async () => await isBreakpointSet('0x048'));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(8);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
@@ -313,7 +377,7 @@
     });
 
     await step('remove the breakpoint from the 8th line', async () => {
-      await removeBreakpointForLine(frontend, '0x048');
+      await click(sourceLineNumberSelector(8));
     });
 
     await step('resume script execution', async () => {
@@ -327,12 +391,12 @@
   // Setting a breakpoint on a worker does not hit breakpoint until reloaded a couple of times.
   it.skip('[crbug.com/1134120] is able to step with state in multi-threaded code in worker thread', async () => {
     const {target, frontend} = getBrowserAndPages();
-    const fileName = 'stepping-with-state.wasm';
 
     await step('navigate to a page and open the Sources tab', async () => {
-      await openSourceCodeEditorForFile(fileName, 'wasm/stepping-with-state-and-threads.html');
+      await openSourceCodeEditorForFile('stepping-with-state.wasm', 'wasm/stepping-with-state-and-threads.html');
     });
 
+    const numberOfLines = 32;
 
     await step('check that the main thread is selected', async () => {
       const selectedThreadElement = await waitFor(SELECTED_THREAD_SELECTOR);
@@ -347,10 +411,16 @@
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await waitForFunction(async () => await isBreakpointSet(30));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(30);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
@@ -385,10 +455,16 @@
     });
 
     await step('reload the page', async () => {
-      await reloadPageAndWaitForSourceFile(frontend, target, fileName);
+      await target.reload();
+      // FIXME(crbug/1112692): Refactor test to remove the timeout.
+      await timeout(100);
     });
 
-    await waitForFunction(async () => await isBreakpointSet(13));
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
+    await checkBreakpointIsActive(13);
 
     await step('check that the code has paused on the breakpoint at the correct script location', async () => {
       const scriptLocation = await retrieveTopCallFrameWithoutResuming();
diff --git a/test/e2e/sources/debugger-language-plugins_test.ts b/test/e2e/sources/debugger-language-plugins_test.ts
index b676bc1..897ca96 100644
--- a/test/e2e/sources/debugger-language-plugins_test.ts
+++ b/test/e2e/sources/debugger-language-plugins_test.ts
@@ -7,7 +7,7 @@
 import {$, click, enableExperiment, getBrowserAndPages, getResourcesPath, goToResource, pasteText, waitFor, waitForFunction, waitForMany, waitForNone} from '../../shared/helper.js';
 import {describe, it} from '../../shared/mocha-extensions.js';
 import {CONSOLE_TAB_SELECTOR, focusConsolePrompt, getCurrentConsoleMessages} from '../helpers/console-helpers.js';
-import {addBreakpointForLine, getCallFrameLocations, getCallFrameNames, getValuesForScope, isBreakpointSet, listenForSourceFilesAdded, openFileInEditor, openFileInSourcesPanel, openSourcesPanel, PAUSE_ON_EXCEPTION_BUTTON, RESUME_BUTTON, retrieveSourceFilesAdded, retrieveTopCallFrameScriptLocation, switchToCallFrame, waitForAdditionalSourceFiles} from '../helpers/sources-helpers.js';
+import {addBreakpointForLine, checkBreakpointIsNotActive, getCallFrameLocations, getCallFrameNames, getValuesForScope, listenForSourceFilesAdded, openFileInEditor, openFileInSourcesPanel, openSourcesPanel, PAUSE_ON_EXCEPTION_BUTTON, RESUME_BUTTON, retrieveSourceFilesAdded, retrieveTopCallFrameScriptLocation, switchToCallFrame, waitForAdditionalSourceFiles} from '../helpers/sources-helpers.js';
 
 
 // TODO: Remove once Chromium updates its version of Node.js to 12+.
@@ -296,7 +296,7 @@
     const scriptLocation = await retrieveTopCallFrameScriptLocation('main();', target);
     assert.deepEqual(scriptLocation, 'global_variable.ll:9');
 
-    await waitForFunction(async () => !(await isBreakpointSet(4)));
+    await checkBreakpointIsNotActive(4);
   });
 
   it('shows top-level and nested variables', async () => {
diff --git a/test/e2e/sources/syntax-highlighting_test.ts b/test/e2e/sources/syntax-highlighting_test.ts
index 51c6ddb..af87a2b 100644
--- a/test/e2e/sources/syntax-highlighting_test.ts
+++ b/test/e2e/sources/syntax-highlighting_test.ts
@@ -5,7 +5,7 @@
 import {assert} from 'chai';
 import {$$, step} from '../../shared/helper.js';
 import {describe, it} from '../../shared/mocha-extensions.js';
-import {openSourceCodeEditorForFile} from '../helpers/sources-helpers.js';
+import {openSourceCodeEditorForFile, waitForSourceCodeLines} from '../helpers/sources-helpers.js';
 
 describe('Sources Tab', async function() {
   it('is highlighting the syntax correctly', async () => {
@@ -15,6 +15,12 @@
       await openSourceCodeEditorForFile('syntax-highlighting.wasm', 'wasm/syntax-highlighting.html');
     });
 
+    const numberOfLines = 7;
+
+    await step('wait for all the source code to appear', async () => {
+      await waitForSourceCodeLines(numberOfLines);
+    });
+
     await step('check that variables have the correct class and has a different format', async () => {
       const expectedVariables = ['$add', '$p0', '$p1', '$p0', '$p1'];