Skip to content

Commit

Permalink
chore: refactor specs to make it functions and expectations explicit
Browse files Browse the repository at this point in the history
  • Loading branch information
Marzio Superina committed Jul 24, 2018
1 parent 7730ece commit acb3d72
Show file tree
Hide file tree
Showing 12 changed files with 668 additions and 465 deletions.
319 changes: 159 additions & 160 deletions test/customCommands.js
Original file line number Diff line number Diff line change
@@ -1,178 +1,177 @@
import {Key, WebElement} from 'selenium-webdriver';
import {nativeText, finputSwitchOptionsButton} from './pageObjects/index';
import {isMac, isChrome, getModifierKey, driver} from './helpers';
import { getDriver, load, unload } from './pageObjects/index';
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) => {
let blurAfter = false;
let pressModifier = false;
let switchDelimiter = false;

const chainFunctions = {};


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

chainFunctions.thenBlurring = () => {
blurAfter = true;
return chainFunctions;
};

chainFunctions.whileModifierPressed = () => {
pressModifier = 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();

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

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;
}

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

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

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

const observed = await finputElement().getAttribute('value');
expect(observed).toBe(expected);
});

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);
});

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);
});

return chainFunctions;
};
export const copyingAndPasting = ({ driver, finputElement, nativeText }) =>
async ({ tested, expected }) => {

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(driver);

await nativeText().clear();
await nativeText().click();
await nativeText().sendKeys(tested);
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'));

const observed = await finputElement().getAttribute('value');
expect(observed).toBe(expected);
};

return chainFunctions;
export const cutting = ({ driver, finputElement }) =>
async ({ cut, startingFrom, tested, expected }) => {

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(driver);

await finputElement().clear();
await finputElement().click();
await finputElement().sendKeys(tested);
await finputElement().sendKeys(Key.chord(modifierKey, 'a'));
await finputElement().sendKeys(mapKeys('←'));
await finputElement().sendKeys(Array(startingFrom + 1).join(mapKeys('→')));
await finputElement().sendKeys(Key.chord(Key.SHIFT, Array(cut + 1).join(mapKeys('→'))));
await finputElement().sendKeys(Key.chord(modifierKey, 'x'));

const observed = await finputElement().getAttribute('value');
expect(observed).toBe(expected);
};

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

chainFunctions.shouldShow = (expected) => {
const testName = `should show "${expected}" when "${text}" is copied and pasted`;
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 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'));

const observed = await finputElement().getAttribute('value');
expect(observed).toBe(expected);
export const typing = ({
driver,
finputElement,
finputSwitchOptionsButton,
nativeText
}) =>
async ({
blurAfter = false,
pressModifier = false,
switchDelimiter = false,
tested,
expected,
expectedCaret,
expectedFocus
}) => {

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;
}

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

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

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

const observed = await finputElement().getAttribute('value');
expect(observed).toBe(expected);

if (expectedCaret !== undefined) {
const selection = await driver.executeScript(() => {
return [document.activeElement.selectionStart, document.activeElement.selectionEnd];
});

return chainFunctions;
};
expect(selection[0]).toEqual(selection[1]); // no selection, only caret cursor
expect(selection[0]).toEqual(expectedCaret);
}

return chainFunctions;
if (expectedFocus !== undefined) {
const element = await finputElement();
const activeElement = await driver.switchTo().activeElement();
const observedFocus = await WebElement.equals(element, activeElement);
expect(observedFocus).toBe(expectedFocus);
}
};

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

chainFunctions.characters = () => chainFunctions;

chainFunctions.from = (t) => {
text = t;
return chainFunctions;
};

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'));

const observed = await finputElement().getAttribute('value');
expect(observed).toBe(expected);
});
return chainFunctions;
};
export const itCopyingAndPasting = ({ tested, expected }) => {
it(`should show "${expected}" when "${tested}" is copied and pasted`,
async () => await itCopyingAndPasting.expectCopyingAndPasting({
tested,
expected
}));
};

return chainFunctions;
};
export const itCutting = ({
cut,
startingFrom,
tested,
expected
}) => {
it(
`should show "${expected} when "${tested}" ` +
`has ${cut} chars cut starting from ${startingFrom}`,
async () => await itCutting.expectCutting({
cut,
startingFrom,
tested,
expected
}));
};

return {typing, copyingAndPasting, cutting};
export const itTyping = ({
blurAfter,
pressModifier,
switchDelimiter,
tested,
expected,
expectedCaret,
expectedFocus
}) => {
it(
`should show "${expected}"` +
` when "${tested}" ${tested.length === 1 ? 'is' : 'are'} pressed` +
`${pressModifier ? " with modifier key" : ""}` +
`${expectedCaret !== undefined ? (" should have caret at index: " + expectedCaret) : ""}` +
`${expectedFocus !== undefined ? (" should have focus: " + expectedFocus): ""}`,
async () => await itTyping.expectTyping({
blurAfter,
pressModifier,
switchDelimiter,
tested,
expected,
expectedCaret,
expectedFocus
}));
};

/*
const withModifierMsg = pressModifier ? "with modifier key" : "";
const testName = `should show "${expected}" when "${keys}" ${keys.length === 1 ? 'is' : 'are' } pressed ${withModifierMsg}`;
'should have caret at index: ' + expected
`should have focus: ` + expected
*/
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) => {
export 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

0 comments on commit acb3d72

Please sign in to comment.