-
Notifications
You must be signed in to change notification settings - Fork 0
/
page.js
134 lines (120 loc) · 5.05 KB
/
page.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const { Builder, By, until, Capabilities, Key, ThenableWebDriver } = require('selenium-webdriver')
const { PageFindBy, PageFindAllBy, PageSwitchToFrame, PageWaitDisappear, pageFindConfigDefault } = require('./pageHelper')
const { createObjBaseOn } = require('./pageMisc')
const browserChrome = "chrome"
const browserSafari = "safari"
const browserFirefox = "firefox"
const browserIE = "internet explorer"
const browserEdge = "MicrosoftEdge"
const browserOpera = "opera"
const Max = "Max"
const PageDefs = {
browsers: { browserChrome, browserSafari, browserFirefox, browserIE, browserEdge, browserOpera },
resolutions: {
maximize: { width: Max, height: Max },
desktop: { width: 1920, height: 1080 },
mobile: { width: 420, height: 730 },
}
}
const pageConfigDefault = {
browser: browserChrome,
browserArgs: [],
browserCapability: [
{ prop: "setPageLoadStrategy", value: "normal" }
],
findConfig: pageFindConfigDefault,
resolution: PageDefs.resolutions.maximize,
/**
* if webdriverOverride is set, the params "browser", "browserArgs" and "browserCapability" will be ignored.
* Ensure webdriverOverride is correct and full configured.
* @type {ThenableWebDriver | Null}
* */
webdriverOverride: null
}
class Page {
constructor(pageConfig = pageConfigDefault) {
try {
const resolution = createObjBaseOn(pageConfig.resolution || {}, pageConfigDefault.resolution)
const findConfig = createObjBaseOn(pageConfig.findConfig || {}, pageConfigDefault.findConfig)
/** @type {pageConfigDefault} */
this.pageConfig = createObjBaseOn(pageConfig, pageConfigDefault)
this.pageConfig.findConfig = findConfig
this.pageConfig.resolution = resolution
if (this.pageConfig.webdriverOverride) {
console.info(" Page > Webdriver was built outside Selenium-Page")
this.driver = this.pageConfig.webdriverOverride
} else {
const caps = new Capabilities()
this.pageConfig.browserCapability.forEach(cap => {
caps.set(cap.prop, cap.value)
})
let options = null
switch (this.pageConfig.browser) {
case browserChrome:
const chrome = require('selenium-webdriver/chrome')
options = new chrome.Options()
break
case browserSafari:
const safari = require('selenium-webdriver/safari')
options = new safari.Options()
break
case browserFirefox:
const firefox = require('selenium-webdriver/firefox')
options = new firefox.Options()
break
case browserIE:
const ie = require('selenium-webdriver/ie')
options = new ie.Options()
break
case browserEdge:
const edge = require('selenium-webdriver/edge')
options = new edge.Options()
break
case browserOpera:
const opera = require('selenium-webdriver/opera')
options = new opera.Options()
break
}
if (!options) throw new Error(`"${this.pageConfig.browser}" is not supported browser. Selenium-Page supports: ${Object.values(PageDefs.browsers).join(", ")}.`)
this.pageConfig.browserArgs.forEach(arg => {
options.addArguments(arg)
})
/** @type {ThenableWebDriver} */
this.driver = new Builder()
.setChromeOptions(options)
.setSafariOptions(options)
.setFirefoxOptions(options)
.setIeOptions(options)
.setEdgeOptions(options)
.setOperaOptions(options)
.withCapabilities(caps)
.forBrowser(this.pageConfig.browser)
.build()
console.info(` Page > Browser: "${this.pageConfig.browser}". Resolution: ${this.pageConfig.resolution.width} x ${this.pageConfig.resolution.height}.`)
}
this.By = By
this.until = until
this.findBy = new PageFindBy(this.driver, By, this.pageConfig.findConfig)
this.findAllBy = new PageFindAllBy(this.driver, By, this.pageConfig.findConfig)
this.waitDisappearBy = new PageWaitDisappear(this.driver, By, this.pageConfig.findConfig)
this.switchToFrame = new PageSwitchToFrame(this.driver, By, this.pageConfig.findConfig)
} catch (error) {
return console.error(` Page > Failure: ${error.message}`)
}
}
/**
* @param {string} url
* @param {{width: number, height: number}} resolution PageDefs.resolutions has some templates. If resolution is not set, it will use the the PageConfig.resolution
*/
async open(url, resolution) {
resolution = resolution || this.pageConfig.resolution
if (resolution.height === Max && resolution.width === Max) await this.driver.manage().window().maximize()
else await this.driver.manage().window().setRect({ width: resolution.width, height: resolution.height })
if (resolution !== this.pageConfig.resolution) console.info(` Test > Resolution: ${JSON.stringify(this.pageConfig.resolution)}`)
await this.driver.get(url)
}
async close() {
await this.driver.close()
}
}
module.exports = { Page, Key, PageDefs }