Skip to content

Instantly share code, notes, and snippets.

@Meir017
Last active February 14, 2020 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Meir017/c7bc32208dad86053ed8e36fee3ec6bd to your computer and use it in GitHub Desktop.
Save Meir017/c7bc32208dad86053ed8e36fee3ec6bd to your computer and use it in GitHub Desktop.
generate-cs-tests from playwright nodejs tests
const name = 'keyboard.spec';
const tests = require(`./test/${name}`);
const collected = [];
let current = {};
var options = {
CHROMIUM: true,
FFOX: true,
MAC: true,
WEBKIT: true,
expect: () => { },
testRunner: {
describe: (name, func) => {
if (current.name) {
collected.push(current);
current = {};
}
current.name = name;
current.it = [];
func();
},
xdescribe: (name, func) => options.testRunner.describe(name, func),
fdescribe: (name, func) => options.testRunner.describe(name, func),
beforeEach: func => { },
beforeAll: func => { },
afterEach: func => { },
afterAll: func => { },
it: (name, func) => {
current.it.push({ name, code: func.toString() });
},
xit: (name, func) => {
current.it.push({ name, code: func.toString(), skip: true });
}
}
};
options.testRunner.it.skip = (skip) => (name, func) => options.testRunner.xit(name, func);
options.testRunner.describe.skip = (skip) => (name, func) => options.testRunner.describe(name, func);
function convertToCsharp(code) {
var replaces = [
['async({newPage, httpsServer}) => {', '{'],
['async({page, context, server}) => {', '{'],
['async({newPage}) => {', '{'],
['async({server, newPage}) => {', '{'],
['async({page, server}) => {', '{'],
['async({page,server}) => {', '{'],
['async({page}) => {', '{'],
['page.', 'Page.'],
['.$(', '.QuerySelectorAsync('],
['.$$(', '.QuerySelectorAllAsync('],
['.evaluate(', '.EvaluateAsync<string>('],
['.evaluateHandle(', '.EvaluateHandleAsync('],
['.goto(', '.GoToAsync('],
['.frames()', '.Frames'],
['.Keyboard', '.Keyboard'],
['.pages()', '.Pages'],
['.browserContexts()', '.BrowserContexts'],
['.defaultContext()', '.DefaultContext'],
['.url()', '.Url'],
['.ok()', '.Ok'],
['.setContent(', '.SetContentAsync('],
['.toString()', '.ToString()'],
['.click(', '.ClickAsync('],
['.type(', '.TypeAsync('],
['.press(', '.PressAsync('],
['.down(', '.DownAsync('],
['.up(', '.UpAsync('],
['.close(', '.CloseAsync('],
['.disconnect(', '.DisconnectAsync('],
['.newContext(', '.NewContextAsync('],
['.setInputFiles(', '.SetInputFilesAsync('],
['.newPage(', '.NewPageAsync('],
['.clearCookies(', '.ClearCookiesAsync('],
['.clearPermissions(', '.ClearPermissionsAsync('],
['.close(', '.closeAsync('],
['.cookies(', '.GetCookiesAsync('],
['.newPage(', '.NewPageAsync('],
['.setCookies(', '.SetCookiesAsync('],
['.setGeolocation(', '.SetGeolocationAsync('],
['.setPermissions(', '.SetPermissionsAsync('],
['', ''],
['', ''],
['', ''],
['const ', 'var '],
['server.EMPTY_PAGE', 'TestConstants.EmptyPage'],
['server.PREFIX', 'TestConstants.ServerUrl'],
['server.CROSS_PROCESS_PREFIX', 'TestConstants.CrossProcessUrl'],
['utils.attachFrame(page', 'await FrameUtils.AttachFrameAsync(Page'],
['utils.detachFrame(page', 'await FrameUtils.DetachFrameAsync(Page'],
['.on(\'', '.'],
['(WEBKIT)', '(TestConstants.IsWebKit)'],
['(CHROMIUM)', '(TestConstants.IsChromium)'],
['(FFOX)', '(TestConstants.IsFirefox)'],
['', ''],
['', ''],
];
for (const [input, output] of replaces) {
code = code.split(input).join(output);
}
var start = code.indexOf('{');
var end = code.lastIndexOf('}');
return code.substring(start + 1, end - 1);
}
// console.info(convertToCsharp(func.toString())); return;
tests.describe(options);
process.on('beforeExit', () => {
collected.push(current);
for (const describe of collected) {
console.info(` ///<playwright-file>${name}.js</playwright-file>`);
console.info(` ///<playwright-describe>${describe.name}</playwright-describe>`);
console.info(` public class ${describe.name}Tests`)
console.info(` {`)
for (const it of describe.it) {
console.info(` ///<playwright-file>${name}.js</playwright-file>`);
console.info(` ///<playwright-describe>${describe.name}</playwright-describe>`);
console.info(` ///<playwright-it>${it.name}</playwright-it>`);
console.info(` [Fact${it.skip ? '(Skip = "Skipped in Playwright")' : ''}]`);
console.info(` public async Task ${[...it.name.split(' ')].map(x => x[0] ? (x[0].toUpperCase() + x.substring(1)) : x).join('')}()`)
console.info(` {`)
console.info(` ${convertToCsharp(it.code)}`)
console.info(` }`)
console.info()
}
console.info(` }`)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment