|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { asLocator } from '../../utils/isomorphic/locatorGenerators'; |
| 18 | +import { escapeWithQuotes, formatObjectOrVoid } from '../../utils/isomorphic/stringUtils'; |
| 19 | + |
| 20 | +import type * as actions from './actions'; |
| 21 | +import type { Language } from '../../utils/isomorphic/locatorGenerators'; |
| 22 | + |
| 23 | +export async function generateCode(sdkLanguage: Language, action: actions.Action) { |
| 24 | + switch (action.method) { |
| 25 | + case 'click': { |
| 26 | + const locator = asLocator(sdkLanguage, action.selector); |
| 27 | + return `await page.${locator}.click(${formatObjectOrVoid(action.options)});`; |
| 28 | + } |
| 29 | + case 'drag': { |
| 30 | + const sourceLocator = asLocator(sdkLanguage, action.sourceSelector); |
| 31 | + const targetLocator = asLocator(sdkLanguage, action.targetSelector); |
| 32 | + return `await page.${sourceLocator}.dragAndDrop(${targetLocator});`; |
| 33 | + } |
| 34 | + case 'hover': { |
| 35 | + const locator = asLocator(sdkLanguage, action.selector); |
| 36 | + return `await page.${locator}.hover(${formatObjectOrVoid(action.options)});`; |
| 37 | + } |
| 38 | + case 'pressKey': { |
| 39 | + return `await page.keyboard.press(${escapeWithQuotes(action.key, '\'')});`; |
| 40 | + } |
| 41 | + case 'selectOption': { |
| 42 | + const locator = asLocator(sdkLanguage, action.selector); |
| 43 | + return `await page.${locator}.selectOption(${action.labels.length === 1 ? escapeWithQuotes(action.labels[0]) : '[' + action.labels.map(label => escapeWithQuotes(label)).join(', ') + ']'});`; |
| 44 | + } |
| 45 | + case 'pressSequentially': { |
| 46 | + const locator = asLocator(sdkLanguage, action.selector); |
| 47 | + const code = [`await page.${locator}.pressSequentially(${escapeWithQuotes(action.text)});`]; |
| 48 | + if (action.submit) |
| 49 | + code.push(`await page.keyboard.press('Enter');`); |
| 50 | + return code.join('\n'); |
| 51 | + } |
| 52 | + case 'fill': { |
| 53 | + const locator = asLocator(sdkLanguage, action.selector); |
| 54 | + const code = [`await page.${locator}.fill(${escapeWithQuotes(action.text)});`]; |
| 55 | + if (action.submit) |
| 56 | + code.push(`await page.keyboard.press('Enter');`); |
| 57 | + return code.join('\n'); |
| 58 | + } |
| 59 | + case 'setChecked': { |
| 60 | + const locator = asLocator(sdkLanguage, action.selector); |
| 61 | + if (action.checked) |
| 62 | + return `await page.${locator}.check();`; |
| 63 | + else |
| 64 | + return `await page.${locator}.uncheck();`; |
| 65 | + } |
| 66 | + case 'expectVisible': { |
| 67 | + const locator = asLocator(sdkLanguage, action.selector); |
| 68 | + return `await expect(page.${locator}).toBeVisible();`; |
| 69 | + } |
| 70 | + case 'expectValue': { |
| 71 | + const locator = asLocator(sdkLanguage, action.selector); |
| 72 | + return `await expect(page.${locator}).toHaveValue(${escapeWithQuotes(action.value)});`; |
| 73 | + } |
| 74 | + } |
| 75 | + // @ts-expect-error |
| 76 | + throw new Error('Unknown action ' + action.method); |
| 77 | +} |
0 commit comments