This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[terra-functional-testing] Set test page to the viewport size instead…
… of the browser's window size (#673) * Run tests using viewport size instead of window size * return viewport size * Fix getViewportSize time outs.
- Loading branch information
Showing
11 changed files
with
110 additions
and
35 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
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
20 changes: 20 additions & 0 deletions
20
packages/terra-functional-testing/src/commands/utils/getViewportSize.js
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Gets the viewport size of the current browser window. | ||
* @return {Object} - viewport width and height of the current browser window. | ||
*/ | ||
const getViewportSize = () => { | ||
// eslint-disable-next-line prefer-arrow-callback | ||
const resolution = global.browser.execute(function getResolution() { | ||
return { | ||
screenWidth: Math.max(document.documentElement.clientWidth, window.innerWidth || 0), | ||
screenHeight: Math.max(document.documentElement.clientHeight, window.innerHeight || 0), | ||
}; | ||
}); | ||
|
||
return { | ||
width: resolution.screenWidth, | ||
height: resolution.screenHeight, | ||
}; | ||
}; | ||
|
||
module.exports = getViewportSize; |
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
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
25 changes: 25 additions & 0 deletions
25
packages/terra-functional-testing/src/commands/utils/setViewportSize.js
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
const getViewportSize = require('./getViewportSize'); | ||
|
||
const MAX_RETRIES = 5; | ||
|
||
const setViewportSize = (viewport, retryNo = 0) => { | ||
const { width, height } = viewport; | ||
|
||
const windowSize = global.browser.getWindowSize(); | ||
const viewportSize = getViewportSize(); | ||
|
||
const widthDiff = windowSize.width - viewportSize.width; | ||
const heightDiff = windowSize.height - viewportSize.height; | ||
|
||
// change window size with indent | ||
global.browser.setWindowSize(width + widthDiff, height + heightDiff); | ||
|
||
const newViewportSize = getViewportSize(); | ||
|
||
// if viewport size is not equal to the desired size, execute process again | ||
if (retryNo < MAX_RETRIES && (newViewportSize.width !== width || newViewportSize.height !== height)) { | ||
setViewportSize(viewport, retryNo + 1); | ||
} | ||
}; | ||
|
||
module.exports = setViewportSize; |
23 changes: 1 addition & 22 deletions
23
...ctional-testing/src/services/wdio-visual-regression-service/modules/getTerraFormFactor.js
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
16 changes: 16 additions & 0 deletions
16
packages/terra-functional-testing/tests/jest/commands/utils/getViewportSize.test.js
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const { getViewportSize } = require('../../../../src/commands/utils'); | ||
|
||
describe('getViewportSize', () => { | ||
it('should get the current viewport size', () => { | ||
const mockExecute = jest.fn().mockReturnValue({ screenWidth: 1000, screenHeight: 768 }); | ||
global.browser = { | ||
execute: mockExecute, | ||
}; | ||
|
||
const viewportSize = getViewportSize(); | ||
|
||
expect(mockExecute).toHaveBeenCalled(); | ||
expect(viewportSize.width).toEqual(1000); | ||
expect(viewportSize.height).toEqual(768); | ||
}); | ||
}); |
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
14 changes: 5 additions & 9 deletions
14
packages/terra-functional-testing/tests/jest/commands/utils/setViewport.test.js
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,24 +1,20 @@ | ||
const { setViewport } = require('../../../../src/commands/utils'); | ||
const { setViewport, setViewportSize } = require('../../../../src/commands/utils'); | ||
const { TERRA_VIEWPORTS } = require('../../../../src/constants'); | ||
|
||
const mockSetWindowSize = jest.fn(); | ||
|
||
global.browser = { | ||
setWindowSize: mockSetWindowSize, | ||
}; | ||
jest.mock('../../../../src/commands/utils/setViewportSize'); | ||
|
||
describe('setViewport', () => { | ||
it('should set specified viewport', () => { | ||
const { width, height } = TERRA_VIEWPORTS.tiny; | ||
const tinyViewport = TERRA_VIEWPORTS.tiny; | ||
|
||
setViewport('tiny'); | ||
|
||
expect(global.browser.setWindowSize).toHaveBeenCalledWith(width, height); | ||
expect(setViewportSize).toHaveBeenCalledWith(tinyViewport); | ||
}); | ||
|
||
it('should not set the window size for unsupported viewport', () => { | ||
setViewport('unsupported-viewport'); | ||
|
||
expect(global.browser.setWindowSize).not.toHaveBeenCalled(); | ||
expect(setViewportSize).not.toHaveBeenCalled(); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
packages/terra-functional-testing/tests/jest/commands/utils/setViewportSize.test.js
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 |
---|---|---|
@@ -0,0 +1,27 @@ | ||
const { setViewportSize, getViewportSize } = require('../../../../src/commands/utils'); | ||
|
||
const mockSetWindowSize = jest.fn(); | ||
const mockGetWindowSize = jest.fn().mockReturnValue({ width: 1010, height: 778 }); | ||
|
||
jest.mock('../../../../src/commands/utils/getViewportSize', () => { | ||
const mockGetViewportSize = jest.fn(() => ( | ||
{ width: 910, height: 760 } | ||
)); | ||
|
||
return mockGetViewportSize; | ||
}); | ||
|
||
global.browser = { | ||
setWindowSize: mockSetWindowSize, | ||
getWindowSize: mockGetWindowSize, | ||
}; | ||
|
||
describe('setViewportSize', () => { | ||
it('should set specified viewport size', () => { | ||
setViewportSize({ width: 1000, height: 768 }, 5); | ||
|
||
expect(getViewportSize).toHaveBeenCalledTimes(2); | ||
expect(mockSetWindowSize).toHaveBeenCalled(); | ||
expect(mockGetWindowSize).toHaveBeenCalled(); | ||
}); | ||
}); |