Skip to content

Commit

Permalink
Add function to get paused breakpoint (#625)
Browse files Browse the repository at this point in the history
Signed-off-by: Marian Lorinc <[email protected]>
  • Loading branch information
mlorinc authored Feb 16, 2023
1 parent 532b51a commit dbd482c
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 10 deletions.
17 changes: 14 additions & 3 deletions locators/lib/1.37.0.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { By } from "selenium-webdriver";
import { Locators } from "monaco-page-objects";
import { By, WebElement } from "selenium-webdriver";
import { Locators, hasClass, hasNotClass } from "monaco-page-objects";

const activityBar = {
ActivityBar: {
Expand Down Expand Up @@ -97,13 +97,24 @@ const editor = {
},
TextEditor: {
activeTab: By.css('div.tab.active'),
breakpoint: {
pauseSelector: By.className('codicon-debug-stackframe'),
generalSelector: By.className('codicon-debug-breakpoint'),
properties: {
enabled: hasNotClass('codicon-debug-breakpoint-unverified'),
line: {
selector: By.className('line-numbers'),
number: (line: WebElement) => line.getText().then((line) => Number.parseInt(line))
},
paused: hasClass('codicon-debug-stackframe'),
}
},
editorContainer: By.className('monaco-editor'),
dataUri: 'data-uri',
formatDoc: 'Format Document',
marginArea: By.className('margin-view-overlays'),
lineNumber: (line: number) => By.xpath(`.//div[contains(@class, 'line-numbers') and text() = '${line}']`),
lineOverlay: (line: number) => By.xpath(`.//div[contains(@class, 'line-numbers') and text() = '${line}']/..`),
breakPoint: By.className('codicon-debug-breakpoint'),
debugHint: By.className('codicon-debug-hint'),
selection: By.className('cslr selected-text top-left-radius bottom-left-radius top-right-radius bottom-right-radius'),
findWidget: By.className('find-widget')
Expand Down
36 changes: 36 additions & 0 deletions page-objects/src/components/editor/Breakpoint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { until, WebElement } from 'selenium-webdriver';
import { AbstractElement } from '../AbstractElement';

export class Breakpoint extends AbstractElement {
constructor(breakpoint: WebElement, private lineElement: WebElement) {
super(breakpoint, lineElement);
}

async isEnabled(): Promise<boolean> {
return await Breakpoint.locators.TextEditor.breakpoint.properties.enabled(this);
}

async isPaused(): Promise<boolean> {
return await Breakpoint.locators.TextEditor.breakpoint.properties.paused(this);
}

/**
* Return line number of the breakpoint.
* @returns number indicating line where breakpoint is set
*/
async getLineNumber(): Promise<number> {
const breakpointLocators = Breakpoint.locators.TextEditor.breakpoint;
const line = await this.lineElement.findElement(breakpointLocators.properties.line.selector);
const lineNumber = await breakpointLocators.properties.line.number(line);
return lineNumber;
}

/**
* Remove breakpoint.
* @param timeout time in ms when operation is considered to be unsuccessful
*/
async remove(timeout: number = 5000): Promise<void> {
await this.click();
await this.getDriver().wait(until.stalenessOf(this), timeout);
}
}
28 changes: 26 additions & 2 deletions page-objects/src/components/editor/TextEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { StatusBar } from "../statusBar/StatusBar";
import { Editor } from "./Editor";
import { ElementWithContexMenu } from "../ElementWithContextMenu";
import { AbstractElement } from "../AbstractElement";
import { Breakpoint } from "./Breakpoint";

export class BreakpointError extends Error {}

/**
* Page object representing the active text editor
Expand Down Expand Up @@ -379,7 +382,7 @@ export class TextEditor extends Editor {
await this.getDriver().actions().move({origin: lineNum}).perform();

const lineOverlay = await margin.findElement(TextEditor.locators.TextEditor.lineOverlay(line));
const breakPoint = await lineOverlay.findElements(TextEditor.locators.TextEditor.breakPoint);
const breakPoint = await lineOverlay.findElements(TextEditor.locators.TextEditor.breakpoint.generalSelector);
if (breakPoint.length > 0) {
await breakPoint[0].click();
await new Promise(res => setTimeout(res, 200));
Expand All @@ -395,6 +398,27 @@ export class TextEditor extends Editor {
return false;
}

/**
* Get paused breakpoint if available. Otherwise, return undefined.
* @returns promise which resolves to either Breakpoint page object or undefined
*/
async getPausedBreakpoint(): Promise<Breakpoint | undefined> {
const breakpointLocators = Breakpoint.locators.TextEditor.breakpoint;
const breakpoints = await this.findElements(breakpointLocators.pauseSelector);

if (breakpoints.length === 0) {
return undefined;
}

if (breakpoints.length > 1) {
throw new BreakpointError(`unexpected number of paused breakpoints: ${breakpoints.length}; expected 1 at most`);
}

// get parent
const lineElement = breakpoints[0].findElement(By.xpath('./..'));
return new Breakpoint(breakpoints[0], lineElement);
}

/**
* Get all code lenses within the editor
* @returns list of CodeLens page objects
Expand Down Expand Up @@ -683,4 +707,4 @@ export class FindWidget extends AbstractElement {
const input = await composite.findElement(FindWidget.locators.FindWidget.content);
return input.getAttribute('innerHTML');
}
}
}
1 change: 1 addition & 0 deletions page-objects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export * from './components/bottomBar/Views';
export * from './components/statusBar/StatusBar';

export * from './components/editor/EditorView';
export * from './components/editor/Breakpoint';
export * from './components/editor/TextEditor';
export * from './components/editor/Editor';
export * from './components/editor/SettingsEditor';
Expand Down
31 changes: 28 additions & 3 deletions page-objects/src/locators/locators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { By } from "selenium-webdriver";
import { By, WebElement } from "selenium-webdriver";
import { DeepPartial } from 'ts-essentials';

/**
Expand Down Expand Up @@ -99,13 +99,24 @@ export interface Locators {
}
TextEditor: {
activeTab: By
breakpoint: {
pauseSelector: By
generalSelector: By
properties: {
enabled: (el: WebElement) => Promise<boolean>
line: {
selector: By
number: (line: WebElement) => Promise<number>
}
paused: (el: WebElement) => Promise<boolean>
}
}
editorContainer: By
dataUri: string
formatDoc: string
marginArea: By
lineNumber: (line: number) => By
lineOverlay: (line: number) => By
breakPoint: By
debugHint: By
selection: By
findWidget: By
Expand Down Expand Up @@ -396,4 +407,18 @@ export interface Locators {
export interface LocatorDiff {
locators: DeepPartial<Locators>
extras?: Object
}
}

export function hasClass(klass: string): ((el: WebElement) => Promise<boolean>) {
return async (el: WebElement) => {
const klasses = await el.getAttribute('class');
const segments = klasses?.split(/\s+/g);
return segments.includes(klass);
}
}

export function hasNotClass(klass: string): ((el: WebElement) => Promise<boolean>) {
return async (el: WebElement) => {
return !(await hasClass(klass).call(undefined, el));
};
}
20 changes: 18 additions & 2 deletions test/test-project/src/test/debug/debug-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActivityBar, DebugConsoleView, DebugToolbar, DebugView, DefaultTreeSection, EditorView, InputBox, Key, TextEditor, TitleBar, until, VSBrowser, Workbench } from "vscode-extension-tester";
import { ActivityBar, Breakpoint, DebugConsoleView, DebugToolbar, DebugView, DefaultTreeSection, EditorView, InputBox, Key, TextEditor, TitleBar, until, VSBrowser, WebDriver, Workbench } from "vscode-extension-tester";
import * as path from 'path';
import { expect } from "chai";

Expand Down Expand Up @@ -46,9 +46,13 @@ import { expect } from "chai";
describe('Debug Session', () => {
let editor: TextEditor;
let debugBar: DebugToolbar;
let driver: WebDriver;
let breakpoint!: Breakpoint;
const line = 6;

before(async () => {
editor = (await new EditorView().openEditor('test.js')) as TextEditor;
driver = editor.getDriver();
});

after(async function() {
Expand All @@ -62,7 +66,7 @@ import { expect } from "chai";
});

it('set a breakpoint', async () => {
const result = await editor.toggleBreakpoint(6);
const result = await editor.toggleBreakpoint(line);
expect(result).to.be.true;
});

Expand All @@ -72,6 +76,18 @@ import { expect } from "chai";
await debugBar.waitForBreakPoint();
});

it('TextEditor: getPausedBreakpoint works', async function() {
breakpoint = await driver.wait<Breakpoint>(() => editor.getPausedBreakpoint(), 10000, 'could not find paused breakpoint') as Breakpoint;
});

it('Breakpoint: getLineNumber works', async function() {
expect(await breakpoint.getLineNumber()).equals(line);
});

it('Breakpoint: isPaused works', async function() {
expect(await breakpoint.isPaused()).to.be.true;
});

it('evaluate an expression', async () => {
const debugConsole = new DebugConsoleView();
await debugConsole.setExpression(`console.log('foo')`);
Expand Down

0 comments on commit dbd482c

Please sign in to comment.