Skip to content

Commit

Permalink
Merge pull request #251 from simonradier/improve-test-speed
Browse files Browse the repository at this point in the history
Test speed improvement for browser & element
  • Loading branch information
simonradier committed Jan 9, 2023
2 parents 5855ad7 + 68bb988 commit b4f2014
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 36 deletions.
9 changes: 7 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ on:

jobs:
unit-test:
runs-on: ubuntu-latest
runs-on: ubuntu-latest
timeout-minutes: 5
strategy:
matrix:
node: [ 16, 18, 19 ]

env:
COVERALLS_REPO_TOKEN: '${{ secrets.COVERALLS_REPO_TOKEN }}'
Expand All @@ -23,7 +27,7 @@ jobs:
- name: Setup Node.js environment
uses: actions/setup-node@v3
with:
node-version: 18
node-version: ${{ matrix.node }}
cache: 'npm'

- name: Install npm dependencies
Expand All @@ -41,6 +45,7 @@ jobs:

e2e-test:
runs-on: ${{ matrix.os }}
timeout-minutes: 10
strategy:
matrix:
os: [macos-latest, ubuntu-latest, windows-latest]
Expand Down
70 changes: 40 additions & 30 deletions test/unit/browser.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ export function generateBrowserTest(browserType: string) {
// Clean previous sessions
await WebDriver.cleanSessions()
g_driver = new WebDriver(td.WD_SERVER_URL_HTTP[browserType])
// Required for session Start
const resp = td.WD_START_SESSION_RESPONSE.OK
nock(td.WD_SERVER_URL_HTTP[browserType])
.post('/session')
.reply(resp.code, resp.body, resp.headers)
g_browser = await g_driver.start(BrowserType[browserType])
})

afterEach(async function () {
after(async function () {
if (!nock.isActive()) {
if (browserType === 'Safari' || browserType === 'Firefox')
// wait 1.5 sec for Safari or Firefox to avoid "Could not create a session error"
Expand All @@ -30,37 +36,14 @@ export function generateBrowserTest(browserType: string) {

beforeEach(async function () {
nock.cleanAll()
// Required for session Start
const resp = td.WD_START_SESSION_RESPONSE.OK
// Reset of browser URL
const resp = td.WD_NAVIGATE_TO_RESPONSE.OK
nock(td.WD_SERVER_URL_HTTP[browserType])
.post('/session')
.post(`/session/${td.WD_SESSION_ID}/url`)
.reply(resp.code, resp.body, resp.headers)
g_browser = await g_driver.start(BrowserType[browserType])
})

describe('close', function () {
it('should close the browser and the associated windows if the webdriver response is successful', async function () {
const resp = td.WD_STOP_SESSION_RESPONSE.OK
nock(td.WD_SERVER_URL_HTTP[browserType])
.delete(`/session/${td.WD_SESSION_ID}`)
.reply(resp.code, resp.body, resp.headers)
await expect(g_browser.close()).to.be.fulfilled
expect(g_browser.closed).to.be.true
})

it('should throw an error if the webdriver server return an error | Nock Only', async function () {
const resp = td.WD_STOP_SESSION_RESPONSE.KO_ERROR
nock(td.WD_SERVER_URL_HTTP[browserType])
.delete(`/session/${td.WD_SESSION_ID}`)
.reply(resp.code, resp.body, resp.headers)
await expect(g_browser.close()).to.be.rejected
})

it('should throw an error if browser is closed', async function () {
//@ts-ignore required for test purpose
g_browser._closed = true
await expect(g_browser.close()).to.be.rejectedWith(/closed/)
})
// @ts-ignore
g_browser._closed = false
await g_browser.navigate().to('about:blank')
})

describe('getCurrentWindow', function () {
Expand Down Expand Up @@ -1082,5 +1065,32 @@ export function generateBrowserTest(browserType: string) {
})
})
})

describe('close', function () {

it('should throw an error if the webdriver server return an error | Nock Only', async function () {
const resp = td.WD_STOP_SESSION_RESPONSE.KO_ERROR
nock(td.WD_SERVER_URL_HTTP[browserType])
.delete(`/session/${td.WD_SESSION_ID}`)
.reply(resp.code, resp.body, resp.headers)
await expect(g_browser.close()).to.be.rejected
})

it('should close the browser and the associated windows if the webdriver response is successful', async function () {
const resp = td.WD_STOP_SESSION_RESPONSE.OK
nock(td.WD_SERVER_URL_HTTP[browserType])
.delete(`/session/${td.WD_SESSION_ID}`)
.reply(resp.code, resp.body, resp.headers)
await expect(g_browser.close()).to.be.fulfilled
expect(g_browser.closed).to.be.true
})

/*it('should throw an error if browser is closed', async function () {
//@ts-ignore required for test purpose
g_browser._closed = true
await expect(g_browser.close()).to.be.rejectedWith(/closed/)
})*/
})

})
}
16 changes: 12 additions & 4 deletions test/unit/element.generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,15 @@ export function generateElementTest(browserType: string) {
// Clean previous sessions
await WebDriver.cleanSessions()
g_driver = new WebDriver(td.WD_SERVER_URL_HTTP[browserType])
// Required for session Start
const resp = td.WD_START_SESSION_RESPONSE.OK
nock(td.WD_SERVER_URL_HTTP[browserType])
.post('/session')
.reply(resp.code, resp.body, resp.headers)
g_browser = await g_driver.start(BrowserType[browserType])
})

afterEach(async function () {
after(async function () {
if (!nock.isActive()) {
if (browserType === 'Safari' || browserType === 'Firefox')
// wait 1.5 sec for Safari or Firefox to avoid "Could not create a session error"
Expand All @@ -30,11 +36,13 @@ export function generateElementTest(browserType: string) {
beforeEach(async function () {
nock.cleanAll()
// Required for session Start
let resp = td.WD_START_SESSION_RESPONSE.OK
const resp = td.WD_NAVIGATE_TO_RESPONSE.OK
nock(td.WD_SERVER_URL_HTTP[browserType])
.post('/session')
.post(`/session/${td.WD_SESSION_ID}/url`)
.reply(resp.code, resp.body, resp.headers)
g_browser = await g_driver.start(BrowserType[browserType])
// @ts-ignore clean the _close set to true
g_browser._closed = false
await g_browser.navigate().to('about:blank')
// Required for Navigation To
let resp2 = td.WD_NAVIGATE_TO_RESPONSE.OK
nock(td.WD_SERVER_URL_HTTP[browserType])
Expand Down

0 comments on commit b4f2014

Please sign in to comment.