Skip to content

Commit

Permalink
feat(copilot): rename testing framework driver methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
asafkorem committed Sep 1, 2024
1 parent 20746a7 commit a24b080
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions detox-copilot/src/Copilot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ describe('Copilot', () => {
beforeEach(() => {
mockConfig = {
frameworkDriver: {
takeSnapshot: jest.fn(),
getViewHierarchy: jest.fn(),
captureSnapshotImage: jest.fn(),
captureViewHierarchyString: jest.fn(),
availableAPI: {
matchers: [],
actions: [],
Expand Down
4 changes: 2 additions & 2 deletions detox-copilot/src/actions/StepPerformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ describe('StepPerformer', () => {
codeEvaluationResult = 'success'
}: SetupMockOptions = {}) => {
mockPromptHandler.isSnapshotImageSupported.mockReturnValue(isSnapshotSupported);
mockSnapshotManager.takeSnapshot.mockResolvedValue(snapshotData as string);
mockSnapshotManager.getViewHierarchy.mockResolvedValue(viewHierarchy);
mockSnapshotManager.captureSnapshotImage.mockResolvedValue(snapshotData as string);
mockSnapshotManager.captureViewHierarchyString.mockResolvedValue(viewHierarchy);
mockPromptCreator.createPrompt.mockReturnValue('generated prompt');
mockPromptHandler.runPrompt.mockResolvedValue(promptResult);
mockCodeEvaluator.evaluate.mockResolvedValue(codeEvaluationResult);
Expand Down
4 changes: 2 additions & 2 deletions detox-copilot/src/actions/StepPerformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export class StepPerformer {
private promptHandler: PromptHandler
) {}
async perform(step: ExecutionStep, previous: ExecutionStep[] = []): Promise<any> {
const snapshot = await this.snapshotManager.takeSnapshot();
const viewHierarchy = await this.snapshotManager.getViewHierarchy();
const snapshot = await this.snapshotManager.captureSnapshotImage();
const viewHierarchy = await this.snapshotManager.captureViewHierarchyString();

const isSnapshotImageAttached =
snapshot !== undefined && this.promptHandler.isSnapshotImageSupported();
Expand Down
20 changes: 10 additions & 10 deletions detox-copilot/src/integration tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ describe('Integration', () => {
jest.clearAllMocks();

mockFrameworkDriver = {
takeSnapshot: jest.fn().mockResolvedValue('mock_snapshot'),
getViewHierarchy: jest.fn().mockResolvedValue('<view><button>Login</button></view>'),
captureSnapshotImage: jest.fn().mockResolvedValue('mock_snapshot'),
captureViewHierarchyString: jest.fn().mockResolvedValue('<view><button>Login</button></view>'),
availableAPI: {
matchers: [],
actions: [],
Expand Down Expand Up @@ -52,8 +52,8 @@ describe('Integration', () => {

await expect(copilot.act('Tap on the login button')).resolves.not.toThrow();

expect(mockFrameworkDriver.takeSnapshot).toHaveBeenCalled();
expect(mockFrameworkDriver.getViewHierarchy).toHaveBeenCalledWith();
expect(mockFrameworkDriver.captureSnapshotImage).toHaveBeenCalled();
expect(mockFrameworkDriver.captureViewHierarchyString).toHaveBeenCalledWith();

expect(mockPromptHandler.runPrompt).toHaveBeenCalledWith(
expect.stringContaining('Tap on the login button'),
Expand All @@ -74,8 +74,8 @@ describe('Integration', () => {

await copilot.assert('The welcome message should be visible');

expect(mockFrameworkDriver.takeSnapshot).toHaveBeenCalled();
expect(mockFrameworkDriver.getViewHierarchy).toHaveBeenCalled();
expect(mockFrameworkDriver.captureSnapshotImage).toHaveBeenCalled();
expect(mockFrameworkDriver.captureViewHierarchyString).toHaveBeenCalled();
expect(mockPromptHandler.runPrompt).toHaveBeenCalledWith(
expect.stringContaining('The welcome message should be visible'),
'mock_snapshot'
Expand All @@ -102,14 +102,14 @@ describe('Integration', () => {
await expect(copilot.act('Perform action')).rejects.toThrow(/API error/);
});

it('should throw error when takeSnapshot fails', async () => {
mockFrameworkDriver.takeSnapshot.mockRejectedValue(new Error('API error'));
it('should throw error when captureSnapshotImage() fails', async () => {
mockFrameworkDriver.captureSnapshotImage.mockRejectedValue(new Error('API error'));

await expect(copilot.act('Perform action')).rejects.toThrow(/API error/);
});

it('should throw error when getViewHierarchy fails', async () => {
mockFrameworkDriver.getViewHierarchy.mockRejectedValue(new Error('API error'));
it('should throw error when captureViewHierarchyString() fails', async () => {
mockFrameworkDriver.captureViewHierarchyString.mockRejectedValue(new Error('API error'));

await expect(copilot.act('Perform action')).rejects.toThrow(/API error/);
});
Expand Down
4 changes: 2 additions & 2 deletions detox-copilot/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ interface TestingFrameworkDriver {
* Takes a snapshot of the current screen and returns the path to the saved image.
* If the driver does not support image, return undefined.
*/
takeSnapshot: () => Promise<string | undefined>;
captureSnapshotImage: () => Promise<string | undefined>;

/**
* Returns the current view hierarchy in a string representation.
*/
getViewHierarchy: () => Promise<string>;
captureViewHierarchyString: () => Promise<string>;

/**
* The available API methods of the testing framework.
Expand Down
8 changes: 4 additions & 4 deletions detox-copilot/src/utils/SnapshotManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export class SnapshotManager {
constructor(private driver: TestingFrameworkDriver) {}

async takeSnapshot(): Promise<string | undefined> {
return this.driver.takeSnapshot();
async captureSnapshotImage(): Promise<string | undefined> {
return this.driver.captureSnapshotImage();
}

async getViewHierarchy(): Promise<string> {
return this.driver.getViewHierarchy();
async captureViewHierarchyString(): Promise<string> {
return this.driver.captureViewHierarchyString();
}
}
6 changes: 3 additions & 3 deletions detox/src/copilot/CopilotDriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ class DetoxDriver {
};
}

async takeSnapshot() {
async captureSnapshotImage() {
const fileName = `snapshot_${Date.now()}.png`;
await device.takeScreenshot(fileName);
return fileName;
}

async getViewHierarchy() {
return device.getViewHierarchyXml();
async captureViewHierarchyString() {
return device.generateViewHierarchyXml();
}
}

Expand Down

0 comments on commit a24b080

Please sign in to comment.