|
| 1 | +import {expect, Page, test} from "@playwright/test"; |
| 2 | +import {goToNewPad} from "../helper/padHelper"; |
| 3 | + |
| 4 | +test.beforeEach(async ({ page })=>{ |
| 5 | + // create a new pad before each test run |
| 6 | + await goToNewPad(page); |
| 7 | +}) |
| 8 | + |
| 9 | +test.describe('embed links', function () { |
| 10 | + const objectify = function (str: string) { |
| 11 | + const hash = {}; |
| 12 | + const parts = str.split('&'); |
| 13 | + for (let i = 0; i < parts.length; i++) { |
| 14 | + const keyValue = parts[i].split('='); |
| 15 | + // @ts-ignore |
| 16 | + hash[keyValue[0]] = keyValue[1]; |
| 17 | + } |
| 18 | + return hash; |
| 19 | + }; |
| 20 | + |
| 21 | + const checkiFrameCode = async function (embedCode: string, readonly: boolean, page: Page) { |
| 22 | + // turn the code into an html element |
| 23 | + |
| 24 | + await page.setContent(embedCode, {waitUntil: 'load'}) |
| 25 | + const locator = page.locator('body').locator('iframe').last() |
| 26 | + |
| 27 | + |
| 28 | + // read and check the frame attributes |
| 29 | + const width = await locator.getAttribute('width'); |
| 30 | + const height = await locator.getAttribute('height'); |
| 31 | + const name = await locator.getAttribute('name'); |
| 32 | + expect(width).toBe('100%'); |
| 33 | + expect(height).toBe('600'); |
| 34 | + expect(name).toBe(readonly ? 'embed_readonly' : 'embed_readwrite'); |
| 35 | + |
| 36 | + // parse the url |
| 37 | + const src = (await locator.getAttribute('src'))!; |
| 38 | + const questionMark = src.indexOf('?'); |
| 39 | + const url = src.substring(0, questionMark); |
| 40 | + const paramsStr = src.substring(questionMark + 1); |
| 41 | + const params = objectify(paramsStr); |
| 42 | + |
| 43 | + const expectedParams = { |
| 44 | + showControls: 'true', |
| 45 | + showChat: 'true', |
| 46 | + showLineNumbers: 'true', |
| 47 | + useMonospaceFont: 'false', |
| 48 | + }; |
| 49 | + |
| 50 | + // check the url |
| 51 | + if (readonly) { |
| 52 | + expect(url.indexOf('r.') > 0).toBe(true); |
| 53 | + } else { |
| 54 | + expect(url).toBe(await page.evaluate(() => window.location.href)); |
| 55 | + } |
| 56 | + |
| 57 | + // check if all parts of the url are like expected |
| 58 | + expect(params).toEqual(expectedParams); |
| 59 | + }; |
| 60 | + |
| 61 | + test.describe('read and write', function () { |
| 62 | + test.beforeEach(async ({ page })=>{ |
| 63 | + // create a new pad before each test run |
| 64 | + await goToNewPad(page); |
| 65 | + }) |
| 66 | + test.describe('the share link', function () { |
| 67 | + test('is the actual pad url', async function ({page}) { |
| 68 | + |
| 69 | + const shareButton = page.locator('.buttonicon-embed') |
| 70 | + // open share dropdown |
| 71 | + await shareButton.click() |
| 72 | + |
| 73 | + // get the link of the share field + the actual pad url and compare them |
| 74 | + const shareLink = await page.locator('#linkinput').inputValue() |
| 75 | + const padURL = page.url(); |
| 76 | + expect(shareLink).toBe(padURL); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + test.describe('the embed as iframe code', function () { |
| 81 | + test('is an iframe with the the correct url parameters and correct size', async function ({page}) { |
| 82 | + |
| 83 | + const shareButton = page.locator('.buttonicon-embed') |
| 84 | + await shareButton.click() |
| 85 | + |
| 86 | + // get the link of the share field + the actual pad url and compare them |
| 87 | + const embedCode = await page.locator('#embedinput').inputValue() |
| 88 | + |
| 89 | + |
| 90 | + await checkiFrameCode(embedCode, false, page); |
| 91 | + }); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + test.describe('when read only option is set', function () { |
| 96 | + test.beforeEach(async ({ page })=>{ |
| 97 | + // create a new pad before each test run |
| 98 | + await goToNewPad(page); |
| 99 | + }) |
| 100 | + |
| 101 | + test.describe('the share link', function () { |
| 102 | + test('shows a read only url', async function ({page}) { |
| 103 | + |
| 104 | + // open share dropdown |
| 105 | + const shareButton = page.locator('.buttonicon-embed') |
| 106 | + await shareButton.click() |
| 107 | + const readonlyCheckbox = page.locator('#readonlyinput') |
| 108 | + await readonlyCheckbox.click({ |
| 109 | + force: true |
| 110 | + }) |
| 111 | + await page.waitForSelector('#readonlyinput:checked') |
| 112 | + |
| 113 | + // get the link of the share field + the actual pad url and compare them |
| 114 | + const shareLink = await page.locator('#linkinput').inputValue() |
| 115 | + const containsReadOnlyLink = shareLink.indexOf('r.') > 0; |
| 116 | + expect(containsReadOnlyLink).toBe(true); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + test.describe('the embed as iframe code', function () { |
| 121 | + test('is an iframe with the the correct url parameters and correct size', async function ({page}) { |
| 122 | + |
| 123 | + |
| 124 | + // open share dropdown |
| 125 | + const shareButton = page.locator('.buttonicon-embed') |
| 126 | + await shareButton.click() |
| 127 | + |
| 128 | + // check read only checkbox, a bit hacky |
| 129 | + const readonlyCheckbox = page.locator('#readonlyinput') |
| 130 | + await readonlyCheckbox.click({ |
| 131 | + force: true |
| 132 | + }) |
| 133 | + |
| 134 | + await page.waitForSelector('#readonlyinput:checked') |
| 135 | + |
| 136 | + |
| 137 | + // get the link of the share field + the actual pad url and compare them |
| 138 | + const embedCode = await page.locator('#embedinput').inputValue() |
| 139 | + |
| 140 | + await checkiFrameCode(embedCode, true, page); |
| 141 | + }); |
| 142 | + }); |
| 143 | + |
| 144 | + }) |
| 145 | + |
| 146 | +}) |
0 commit comments