Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: test refactoring to use separate environments per spec #125

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 168 additions & 108 deletions test/customCommands.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,195 @@
import {Key, WebElement} from 'selenium-webdriver';
import {nativeText, finputSwitchOptionsButton} from './pageObjects/index';
import {isMac, isChrome, getModifierKey, driver} from './helpers';
import {isMac, isChrome, getModifierKey} from './helpers';
import {mapKeys} from './keys';

const shouldSkipModifierKeyTest = async () => {
const mac = await isMac();
const chrome = await isChrome();
const shouldSkipModifierKeyTest = async (driver) => {
const mac = await isMac(driver);
const chrome = await isChrome(driver);
return mac && chrome;
};

export default (finputElement) => {
const typing = (keys) => {
export default () => {

let driver;
let finputElement;
let finputSwitchOptionsButton;
let nativeText;

const test = () => {
const chainFunctions = {};
const commands = [];
let testMessage = '';

let startPos;
let textCut;
let blurAfter = false;
let pressModifier = false;
let switchDelimiter = false;

const chainFunctions = {};

chainFunctions.copyingAndPasting = (text) => {
commands.push({
action: async () => {
const modifierKey = await getModifierKey(driver);

await nativeText().clear();
await nativeText().click();
await nativeText().sendKeys(text);
await nativeText().sendKeys(Key.chord(modifierKey, 'a'));
await nativeText().sendKeys(Key.chord(modifierKey, 'c'));
await nativeText().clear();

await finputElement().clear();
await finputElement().click();
await finputElement().sendKeys(Key.chord(modifierKey, 'v'));
},
message: `copying and pasting "${text}" `
});

chainFunctions.thenSwitchingDelimiters = () => {
switchDelimiter = true;
return chainFunctions;
};

chainFunctions.thenBlurring = () => {
blurAfter = true;
chainFunctions.cutting = (count) => {
commands.push({
action: async () => {
const modifierKey = await getModifierKey(driver);

await finputElement().clear();
await finputElement().click();
await finputElement().sendKeys(textCut);
await finputElement().sendKeys(Key.chord(modifierKey, 'a'));
await finputElement().sendKeys(mapKeys('←'));
await finputElement().sendKeys(Array(startPos + 1).join(mapKeys('→')));
await finputElement().sendKeys(Key.chord(Key.SHIFT, Array(count + 1).join(mapKeys('→'))));
await finputElement().sendKeys(Key.chord(modifierKey, 'x'));
},
message: `cutting "${count}" `
});

return chainFunctions;
};

chainFunctions.whileModifierPressed = () => {
pressModifier = true;
chainFunctions.characters = () => {
commands.push({
action: () => {},
message: `characters `,
setup: true
});

return chainFunctions;
};

chainFunctions.shouldShow = (expected) => {
const withModifierMsg = pressModifier ? "with modifier key" : "";
const testName = `should show "${expected}" when "${keys}" ${keys.length === 1 ? 'is' : 'are' } pressed ${withModifierMsg}`;

it(testName, async () => {
await finputElement().clear();
await finputElement().click();
chainFunctions.from = (text) => {
commands.push({
action: () => (textCut = text),
message: `from "${text}" `,
setup: true
});

if (pressModifier) {
const mac = await isMac();
const chrome = await isChrome();
return chainFunctions;
};

if (mac && chrome) {
console.warn(`Skipping test as Command key fails on Chrome/Mac. Note that this will show as a passing test. Test: '${testName}'`);
return;
}
chainFunctions.startingFrom = (start) => {
commands.push({
action: () => (startPos = start),
message: `starting from "${start}" `,
setup: true
});

const modifierKey = await getModifierKey();
await finputElement().sendKeys(Key.chord(modifierKey, mapKeys(keys)));
} else {
await finputElement().sendKeys(mapKeys(keys));
}
return chainFunctions;
};

if (switchDelimiter) {
await finputSwitchOptionsButton().click();
}
chainFunctions.typing = (keys) => {
commands.push({
action: async () => {
await finputElement().clear();
await finputElement().click();

if (pressModifier) {
const mac = await isMac(driver);
const chrome = await isChrome(driver);

if (mac && chrome) {
console.warn(`Skipping test as Command key fails on Chrome/Mac. Note that this will show as a passing test. Test: '${testName}'`);
return false;
}

const modifierKey = await getModifierKey(driver);
await finputElement().sendKeys(Key.chord(modifierKey, mapKeys(keys)));
} else {
await finputElement().sendKeys(mapKeys(keys));
}

if (blurAfter) {
await nativeText().click();
}
if (switchDelimiter) {
await finputSwitchOptionsButton().click();
}

const observed = await finputElement().getAttribute('value');
expect(observed).toBe(expected);
if (blurAfter) {
await nativeText().click();
}
},
message: `typing "${keys}" `
});

return chainFunctions;
};

chainFunctions.shouldHaveFocus = (expected) => {
it(`should have focus: ` + expected, async () => {
const element = await finputElement();
const activeElement = await driver.switchTo().activeElement();
const observed = await WebElement.equals(element, activeElement);
expect(observed).toBe(expected);
chainFunctions.thenSwitchingDelimiters = () => {
commands.push({
action: () => (switchDelimiter = true),
message: `then switching delimiters `,
setup: true
});

return chainFunctions;
};

chainFunctions.shouldHaveCaretAt = (expected) => {
it('should have caret at index: ' + expected, async () => {
const selection = await driver.executeScript(() => {
return [document.activeElement.selectionStart, document.activeElement.selectionEnd];
});

expect(selection[0]).toEqual(selection[1]); // no selection, only caret cursor
expect(selection[0]).toEqual(expected);
chainFunctions.thenBlurring = () => {
commands.push({
action: () => (blurAfter = true),
message: `then blurring `,
setup: true
});

return chainFunctions;
};

return chainFunctions;
};
chainFunctions.whileModifierPressed = () => {
commands.push({
action: () => (pressModifier = true),
message: `with modifier key `,
setup: true
});

const copyingAndPasting = (text) => {
const chainFunctions = {};
return chainFunctions;
};

chainFunctions.shouldShow = (expected) => {
const testName = `should show "${expected}" when "${text}" is copied and pasted`;
it(testName, async () => {
if (await shouldSkipModifierKeyTest()) {
const testMessage =
commands
.map(command => command.message)
.join('')
.concat(`should show "${expected}" `);

it(testMessage, async () => {
if (await shouldSkipModifierKeyTest(driver)) {
console.warn(`Skipping test as Command key fails on Chrome/Mac. Note that this will show as a passing test. Test: '${testName}'`)
return;
}
const modifierKey = await getModifierKey();

await nativeText().clear();
await nativeText().click();
await nativeText().sendKeys(text);
await nativeText().sendKeys(Key.chord(modifierKey, 'a'));
await nativeText().sendKeys(Key.chord(modifierKey, 'c'));
await nativeText().clear();
const execCommands = [];

for (let command of commands)
if (command.setup === true)
await command.action();
else
execCommands.push(command);

await finputElement().clear();
await finputElement().click();
await finputElement().sendKeys(Key.chord(modifierKey, 'v'));
let result;
for (let command of execCommands) {
result = await command.action();
if (result === false)
return;
}

const observed = await finputElement().getAttribute('value');
expect(observed).toBe(expected);
Expand All @@ -128,51 +198,41 @@ export default (finputElement) => {
return chainFunctions;
};

return chainFunctions;
};

const cutting = (count) => {
let text, startPos;
const chainFunctions = {};

chainFunctions.characters = () => chainFunctions;

chainFunctions.from = (t) => {
text = t;
return chainFunctions;
};
chainFunctions.shouldHaveFocus = (expected) => {
it(`should have focus: ` + expected, async () => {
const element = await finputElement();
const activeElement = await driver.switchTo().activeElement();
const observed = await WebElement.equals(element, activeElement);
expect(observed).toBe(expected);
});

chainFunctions.startingFrom = (start) => {
startPos = start;
return chainFunctions;
};

chainFunctions.shouldShow = (expected) => {
const testName = `should show "${expected}" when "${text}" has chars cut`;
it(testName, async () => {
if (await shouldSkipModifierKeyTest()) {
console.warn(`Skipping test as Command key fails on Chrome/Mac. Note that this will show as a passing test. Test: '${testName}'`)
return;
}
const modifierKey = await getModifierKey();

await finputElement().clear();
await finputElement().click();
await finputElement().sendKeys(text);
await finputElement().sendKeys(Key.chord(modifierKey, 'a'));
await finputElement().sendKeys(mapKeys('←'));
await finputElement().sendKeys(Array(startPos + 1).join(mapKeys('→')));
await finputElement().sendKeys(Key.chord(Key.SHIFT, Array(count + 1).join(mapKeys('→'))));
await finputElement().sendKeys(Key.chord(modifierKey, 'x'));
chainFunctions.shouldHaveCaretAt = (expected) => {
it('should have caret at index: ' + expected, async () => {
const selection = await driver.executeScript(() => {
return [document.activeElement.selectionStart, document.activeElement.selectionEnd];
});

const observed = await finputElement().getAttribute('value');
expect(observed).toBe(expected);
expect(selection[0]).toEqual(selection[1]); // no selection, only caret cursor
expect(selection[0]).toEqual(expected);
});

return chainFunctions;
};

return chainFunctions;
};

return {typing, copyingAndPasting, cutting};
test.withEnvironment = (env) => {
({
driver,
finputElement,
finputSwitchOptionsButton,
nativeText
} = env);
};

return test;
};
17 changes: 8 additions & 9 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,29 @@ const getSeleniumURL = () => {
return 'http://localhost:4444/wd/hub'
};



export const driver = new Builder()
const driver = new Builder()
.withCapabilities(capabilities)
.usingServer(getSeleniumURL())
.build();

export const isMac = async () => {
export const getDriver = () => driver;

export const isMac = async (driver) => {
const capabilities = await driver.getCapabilities();
const os = capabilities.get(Capability.PLATFORM);
return os.toUpperCase().indexOf(Platform.MAC) >= 0;
};

export const isBrowser = async (browserName) => {
const isBrowser = (driver) => async (browserName) => {
const capabilities = await driver.getCapabilities();
const browser = capabilities.get(Capability.BROWSER_NAME);
return browser.indexOf(browserName) >= 0;
};

export const isChrome = async () => isBrowser(Browser.CHROME);
export const isChrome = async (driver) => isBrowser(driver)(Browser.CHROME);

export const getModifierKey = async () => {
const mac = await isMac();
export const getModifierKey = async (driver) => {
const mac = await isMac(driver);
return mac ? Key.COMMAND : Key.CONTROL;
};

Expand All @@ -46,7 +46,6 @@ afterAll(async () => {
listener();
process.removeListener('exit', listener);
}
await driver.quit();
});

export const defaultTimeout = 10e3;
Loading