Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 30 additions & 52 deletions packages/amazonq/test/e2e_new/amazonq/helpers/pinContextHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,23 @@ export async function clickPinContextButton(webview: WebviewView): Promise<void>
* @returns Promise<boolean> Returns the items as a WebElement List and the labels in a string array
*/
export async function getPinContextMenuItems(webview: WebviewView): Promise<{ items: WebElement[]; labels: string[] }> {
const menuList = await waitForElement(webview, By.css('.mynah-detailed-list-items-block'))
// TODO: Fix the need for a sleep function to be required at all.
await sleep(100)
const menuListItems = await menuList.findElements(By.css('.mynah-detailed-list-item.mynah-ui-clickable-item'))
const items = await webview.findElements(
By.xpath(`//div[contains(@class, 'mynah-detailed-list-item') and contains(@class, 'mynah-ui-clickable-item')]`)
)

if (menuListItems.length === 0) {
if (items.length === 0) {
throw new Error('No pin context menu items found')
}

const labels: string[] = []
for (const item of menuListItems) {
const textWrapper = await item.findElement(By.css('.mynah-detailed-list-item-text'))
const nameElement = await textWrapper.findElement(By.css('.mynah-detailed-list-item-name'))
for (const item of items) {
const nameElement = await item.findElement(By.css('.mynah-detailed-list-item-description'))
const labelText = await nameElement.getText()
labels.push(labelText)
console.log('Menu item found:', labelText)
}

return { items: menuListItems, labels }
return { items, labels }
}

/**
Expand All @@ -66,49 +64,38 @@ export async function getPinContextMenuItems(webview: WebviewView): Promise<{ it
* NOTE: To find all possible text labels, you can call getPinContextMenuItems
*/
export async function clickPinContextMenuItem(webview: WebviewView, itemName: string): Promise<void> {
const menuList = await waitForElement(webview, By.css('.mynah-detailed-list-items-block'))
await sleep(100)
const menuListItems = await menuList.findElements(By.css('.mynah-detailed-list-item.mynah-ui-clickable-item'))
for (const item of menuListItems) {
const textWrapper = await item.findElement(By.css('.mynah-detailed-list-item-text'))
const nameElement = await textWrapper.findElement(By.css('.mynah-detailed-list-item-name'))
const labelText = await nameElement.getText()

if (labelText === itemName) {
console.log(`Clicking Pin Context menu item: ${itemName}`)
await item.click()
return
}
}

throw new Error(`Pin Context menu item not found: ${itemName}`)
const item = await waitForElement(
webview,
By.xpath(
`//div[contains(@class, 'mynah-detailed-list-item') and contains(@class, 'mynah-ui-clickable-item')]//div[contains(@class, 'mynah-detailed-list-item-name') and text()='${itemName}']`
)
)
console.log(`Clicking Pin Context menu item: ${itemName}`)
await item.click()
}
/**
* Lists all the possible Sub-menu items in the console.
* @param webview The WebviewView instance
* @returns Promise<boolean> Returns the items as a WebElement List and the labels in a string array
*/
export async function getSubMenuItems(webview: WebviewView): Promise<{ items: WebElement[]; labels: string[] }> {
const menuList = await waitForElement(webview, By.css('.mynah-detailed-list-items-block'))
await sleep(100)
const menuListItems = await menuList.findElements(By.css('.mynah-detailed-list-item.mynah-ui-clickable-item'))
const items = await webview.findElements(
By.xpath(`//div[contains(@class, 'mynah-detailed-list-item') and contains(@class, 'mynah-ui-clickable-item')]`)
)

if (menuListItems.length === 0) {
if (items.length === 0) {
throw new Error('No sub-menu items found')
}

const labels: string[] = []
for (const item of menuListItems) {
const textWrapper = await item.findElement(
By.css('.mynah-detailed-list-item-text.mynah-detailed-list-item-text-direction-row')
)
const nameElement = await textWrapper.findElement(By.css('.mynah-detailed-list-item-name'))
for (const item of items) {
const nameElement = await item.findElement(By.css('.mynah-detailed-list-item-name'))
const labelText = await nameElement.getText()
labels.push(labelText)
console.log('Menu item found:', labelText)
}

return { items: menuListItems, labels }
return { items, labels }
}
/**
* Clicks a specific item in the Sub-Menu by its label text
Expand All @@ -119,22 +106,13 @@ export async function getSubMenuItems(webview: WebviewView): Promise<{ items: We
* NOTE: To find all possible text labels, you can call getPinContextMenuItems
*/
export async function clickSubMenuItem(webview: WebviewView, itemName: string): Promise<void> {
const menuList = await waitForElement(webview, By.css('.mynah-detailed-list-items-block'))
await sleep(100)
const menuListItems = await menuList.findElements(By.css('.mynah-detailed-list-item.mynah-ui-clickable-item'))
for (const item of menuListItems) {
const textWrapper = await item.findElement(
By.css('.mynah-detailed-list-item-text.mynah-detailed-list-item-text-direction-row')
await sleep(0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is needed, but maybe it worth adding a comment about why?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes, good thought.

const item = await waitForElement(
webview,
By.xpath(
`//div[contains(@class, 'mynah-detailed-list-item') and contains(@class, 'mynah-ui-clickable-item')]//div[contains(@class, 'mynah-detailed-list-item-name') and text()='${itemName}']`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these strings are gnarly, how does one find this? Are they querying the css based on these attributes?

Copy link
Author

@surajrdy-aws surajrdy-aws Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think they are pretty complex, but I do think this is the best/reliable way to snipe the correct element. Yup, XPath is querying based on the css class attributes. I think in general we can use css selectors, but for this specific instance it may be best to use our XPath. You could find this by using the printElementHTML Function and dumping the entire overlay to figure out what you need if building/debugging a XPath selector.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow cool! Maybe in the future we can build/find a nicer interface on top of these queries? This looks good for now though, nice find!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I agree, I think building a helper function for XPaths could be useful, I think it has a lot of moving parameters but definitely doable.

)
const nameElement = await textWrapper.findElement(By.css('.mynah-detailed-list-item-name'))
const labelText = await nameElement.getText()

if (labelText === itemName) {
console.log(`Clicking Pin Context menu item: ${itemName}`)
await item.click()
return
}
}

throw new Error(`Pin Context menu item not found: ${itemName}`)
)
console.log(`Clicking Pin Context menu item: ${itemName}`)
await item.click()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/
import '../utils/setup'
import { WebviewView, By } from 'vscode-extension-tester'
import { closeAllTabs, dismissOverlayIfPresent } from '../utils/cleanupUtils'
import { closeAllTabs } from '../utils/cleanupUtils'
import { testContext } from '../utils/testContext'
import { clickPinContextButton, clickPinContextMenuItem, clickSubMenuItem } from '../helpers/pinContextHelper'
import { waitForElement } from '../utils/generalUtils'
Expand All @@ -19,13 +19,12 @@ describe('Amazon Q Pin Context Functionality', function () {
})

afterEach(async () => {
await dismissOverlayIfPresent(webviewView)
await closeAllTabs(webviewView)
})
it('Allows User to Add File Context', async () => {
await clickPinContextButton(webviewView)
await clickPinContextMenuItem(webviewView, 'Files')
await clickPinContextMenuItem(webviewView, 'Active file')
await clickSubMenuItem(webviewView, 'Active file')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does the File test work properly now? or do we still have to skip it?

})
it('Allows User to Pin Workspace Context', async () => {
await clickPinContextButton(webviewView)
Expand Down
Loading