-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: refactor specs to make it functions and expectations explicit
- Loading branch information
Marzio Superina
committed
Jul 24, 2018
1 parent
7730ece
commit acb3d72
Showing
12 changed files
with
668 additions
and
465 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.