Skip to content

Commit

Permalink
Merge pull request #1712 from jdi-testing/issue_1655-bug-with-locator…
Browse files Browse the repository at this point in the history
…-data-in-edit-modal

Issue 1655: bug with locator data in edit modal
  • Loading branch information
Iogsotot authored Apr 16, 2024
2 parents 1674b17 + 539f7e5 commit 37ed138
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 7 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "JDN — Page Object Generator",
"description": "JDN – helps Test Automation Engineer to create Page Objects in the test automation framework and speed up test development",
"devtools_page": "index.html",
"version": "3.15.23",
"version": "3.15.24",
"icons": {
"128": "icon128.png"
},
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jdn-ai-chrome-extension",
"version": "3.15.23",
"version": "3.15.24",
"description": "jdn-ai chrome extension",
"scripts": {
"start": "webpack --watch --env devenv",
Expand Down
64 changes: 64 additions & 0 deletions src/__tests__/locators/utils/escapeLocatorString.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { escapeLocatorString } from '../../../features/locators/utils/escapeLocatorString';

describe('escapeLocatorString', () => {
describe('should properly escape, when received', () => {
it('strings with double quotes', () => {
expect(escapeLocatorString('He said "Hello"')).toBe('He said \\"Hello\\"');
});

it('strings with single quotes', () => {
expect(escapeLocatorString("It's a test")).toBe("It\\'s a test");
});

it('strings with backslashes', () => {
expect(escapeLocatorString('Path\\to\\file')).toBe('Path\\\\to\\\\file');
});

it('strings with mixed special characters', () => {
expect(escapeLocatorString('He said "It\'s alright"')).toBe('He said \\"It\\\'s alright\\"');
});

it('JSON strings', () => {
const input = '{"category":"Header","action":"go to dashboard","label":"icon:logo"}';
const expected = '{\\"category\\":\\"Header\\",\\"action\\":\\"go to dashboard\\",\\"label\\":\\"icon:logo\\"}';
expect(escapeLocatorString(input)).toBe(expected);
});
});

describe("shouldn't change", () => {
it('string without special characters', () => {
const normalString = 'Just a normal string';
expect(escapeLocatorString(normalString)).toBe(normalString);
});

it('empty strings', () => {
expect(escapeLocatorString('')).toBe('');
});

it('URL strings', () => {
const UrlString = '/orgs/jdi-testing/hovercard';
expect(escapeLocatorString(UrlString)).toBe(UrlString);
});
});

// tests for invalid data types:
describe('should throw an error or handle, when received', () => {
it('objects', () => {
const obj = { key: 'value' };
expect(() => escapeLocatorString(obj as any)).toThrow();
});

it('arrays', () => {
const arr = ['text1', 'text2'];
expect(() => escapeLocatorString(arr as any)).toThrow();
});

it('null', () => {
expect(() => escapeLocatorString(null as any)).toThrow();
});

it('undefined', () => {
expect(() => escapeLocatorString(undefined as any)).toThrow();
});
});
});
3 changes: 3 additions & 0 deletions src/features/locators/utils/escapeLocatorString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const escapeLocatorString = (selector: string): string => {
return selector.replace(/(["'\\])/g, '\\$1');
};
10 changes: 6 additions & 4 deletions src/features/locators/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
import { FormInstance } from 'rc-field-form/lib/interface';
import { FormValues } from '../components/LocatorEditDialog';
import { startsWithDigit } from '../../../app/utils/startsWithDigit';
import { escapeLocatorString } from './escapeLocatorString';

export const isValidJavaVariable = (value: string) => /^[a-zA-Z_$]([a-zA-Z0-9_])*$/.test(value);

Expand All @@ -43,10 +44,11 @@ export const evaluateStandardLocator = (
) => sendMessage.evaluateStandardLocator({ selector, locatorType, element_id, originJdnHash });

const prepareLocatorStringForEvaluation = (type: LocatorType, string: string): string => {
if (type === LocatorType.id) return `#${string}`;
if (type === LocatorType.className) return `.${string}`;
if (type === LocatorType.name) return `[name="${string}"]`;
return string;
const escapeString = escapeLocatorString(string);
if (type === LocatorType.id) return `#${escapeString}`;
if (type === LocatorType.className) return `.${escapeString}`;
if (type === LocatorType.name) return `[name="${escapeString}"]`;
return escapeString;
};

export const evaluateLocator = async (
Expand Down

0 comments on commit 37ed138

Please sign in to comment.