Skip to content
This repository was archived by the owner on Jan 17, 2020. It is now read-only.

Commit 3e6381f

Browse files
committed
Merge pull request #24 from cryogenian/ready/screenshots
screenshots, window management
2 parents 5096e84 + 06e08ac commit 3e6381f

File tree

7 files changed

+337
-38
lines changed

7 files changed

+337
-38
lines changed

docs/Selenium.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,18 @@ isEnabled :: forall e. Element -> Aff (selenium :: SELENIUM | e) Boolean
216216
getInnerHtml :: forall e. Element -> Aff (selenium :: SELENIUM | e) String
217217
```
218218

219+
#### `getSize`
220+
221+
``` purescript
222+
getSize :: forall e. Element -> Aff (selenium :: SELENIUM | e) Size
223+
```
224+
225+
#### `getLocation`
226+
227+
``` purescript
228+
getLocation :: forall e. Element -> Aff (selenium :: SELENIUM | e) Location
229+
```
230+
219231
#### `clearEl`
220232

221233
``` purescript
@@ -235,4 +247,54 @@ takeScreenshot :: forall e. Driver -> Aff (selenium :: SELENIUM | e) String
235247

236248
Returns png base64 encoded png image
237249

250+
#### `saveScreenshot`
251+
252+
``` purescript
253+
saveScreenshot :: forall e. String -> Driver -> Aff (selenium :: SELENIUM | e) Unit
254+
```
255+
256+
Saves screenshot to path
257+
258+
#### `getWindow`
259+
260+
``` purescript
261+
getWindow :: forall e. Driver -> Aff (selenium :: SELENIUM | e) Window
262+
```
263+
264+
#### `getWindowPosition`
265+
266+
``` purescript
267+
getWindowPosition :: forall e. Window -> Aff (selenium :: SELENIUM | e) Location
268+
```
269+
270+
#### `getWindowSize`
271+
272+
``` purescript
273+
getWindowSize :: forall e. Window -> Aff (selenium :: SELENIUM | e) Size
274+
```
275+
276+
#### `maximizeWindow`
277+
278+
``` purescript
279+
maximizeWindow :: forall e. Window -> Aff (selenium :: SELENIUM | e) Unit
280+
```
281+
282+
#### `setWindowPosition`
283+
284+
``` purescript
285+
setWindowPosition :: forall e. Location -> Window -> Aff (selenium :: SELENIUM | e) Unit
286+
```
287+
288+
#### `setWindowSize`
289+
290+
``` purescript
291+
setWindowSize :: forall e. Size -> Window -> Aff (selenium :: SELENIUM | e) Unit
292+
```
293+
294+
#### `getWindowScroll`
295+
296+
``` purescript
297+
getWindowScroll :: forall e. Driver -> Aff (selenium :: SELENIUM | e) Location
298+
```
299+
238300

docs/Selenium/Monad.md

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,48 @@ getDriver :: forall e o. Selenium e o Driver
2222

2323
get driver from context
2424

25+
#### `getWindow`
26+
27+
``` purescript
28+
getWindow :: forall e o. Selenium e o Window
29+
```
30+
31+
#### `getWindowPosition`
32+
33+
``` purescript
34+
getWindowPosition :: forall e o. Selenium e o Location
35+
```
36+
37+
#### `getWindowSize`
38+
39+
``` purescript
40+
getWindowSize :: forall e o. Selenium e o Size
41+
```
42+
43+
#### `maximizeWindow`
44+
45+
``` purescript
46+
maximizeWindow :: forall e o. Selenium e o Unit
47+
```
48+
49+
#### `setWindowPosition`
50+
51+
``` purescript
52+
setWindowPosition :: forall e o. Location -> Selenium e o Unit
53+
```
54+
55+
#### `setWindowSize`
56+
57+
``` purescript
58+
setWindowSize :: forall e o. Size -> Selenium e o Unit
59+
```
60+
61+
#### `getWindowScroll`
62+
63+
``` purescript
64+
getWindowScroll :: forall e o. Selenium e o Location
65+
```
66+
2567
#### `apathize`
2668

2769
``` purescript
@@ -90,14 +132,14 @@ locator :: forall e o. (Element -> Selenium e o Element) -> Selenium e o Locator
90132

91133
get element by action returning an element
92134
```purescript
93-
locator \el -> do
94-
commonElements <- byCss ".common-element" >>= findElements el
135+
locator \el -> do
136+
commonElements <- byCss ".common-element" >>= findElements el
95137
flaggedElements <- traverse (\el -> Tuple el <$> isVisible el) commonElements
96138
maybe err pure $ foldl foldFn Nothing flaggedElements
97139
where
98140
err = throwError $ error "all common elements are not visible"
99141
foldFn Nothing (Tuple el true) = Just el
100-
foldFn a _ = a
142+
foldFn a _ = a
101143
```
102144

103145
#### `findElement`
@@ -106,7 +148,7 @@ locator \el -> do
106148
findElement :: forall e o. Locator -> Selenium e o (Maybe Element)
107149
```
108150

109-
Tries to find element and return it wrapped in `Just`
151+
Tries to find element and return it wrapped in `Just`
110152

111153
#### `findElements`
112154

@@ -134,6 +176,18 @@ findChildren :: forall e o. Element -> Locator -> Selenium e o (List Element)
134176
getInnerHtml :: forall e o. Element -> Selenium e o String
135177
```
136178

179+
#### `getSize`
180+
181+
``` purescript
182+
getSize :: forall e o. Element -> Selenium e o Size
183+
```
184+
185+
#### `getLocation`
186+
187+
``` purescript
188+
getLocation :: forall e o. Element -> Selenium e o Location
189+
```
190+
137191
#### `isDisplayed`
138192

139193
``` purescript
@@ -224,7 +278,7 @@ getTitle :: forall e o. Selenium e o String
224278
sequence :: forall e o. Sequence Unit -> Selenium e o Unit
225279
```
226280

227-
Run sequence of actions
281+
Run sequence of actions
228282

229283
#### `actions`
230284

@@ -260,6 +314,12 @@ quit :: forall e o. Selenium e o Unit
260314
takeScreenshot :: forall e o. Selenium e o String
261315
```
262316

317+
#### `saveScreenshot`
318+
319+
``` purescript
320+
saveScreenshot :: forall e o. String -> Selenium e o Unit
321+
```
322+
263323
#### `findExact`
264324

265325
``` purescript

docs/Selenium/Types.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ data SELENIUM :: !
1818
data Driver :: *
1919
```
2020

21+
#### `Window`
22+
23+
``` purescript
24+
data Window :: *
25+
```
26+
2127
#### `Until`
2228

2329
``` purescript
@@ -131,7 +137,7 @@ data Method
131137
```
132138

133139
Copied from `purescript-affjax` because the only thing we
134-
need from `affjax` is `Method`
140+
need from `affjax` is `Method`
135141

136142
##### Instances
137143
``` purescript
@@ -157,7 +163,13 @@ instance xhrStateIsForeign :: IsForeign XHRState
157163
#### `Location`
158164

159165
``` purescript
160-
type Location = { x :: Number, y :: Number }
166+
type Location = { x :: Int, y :: Int }
167+
```
168+
169+
#### `Size`
170+
171+
``` purescript
172+
type Size = { width :: Int, height :: Int }
161173
```
162174

163175
#### `ControlKey`

src/Selenium.js

Lines changed: 104 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// module Selenium
22

33
var webdriver = require("selenium-webdriver"),
4-
By = webdriver.By;
4+
By = webdriver.By,
5+
fs = require("fs"),
6+
path = require("path");
57

68
exports.get = function(driver) {
79
return function(url) {
@@ -253,6 +255,18 @@ exports.getInnerHtml = function(el) {
253255
};
254256
};
255257

258+
exports.getSize = function(el) {
259+
return function(cb, eb) {
260+
el.getSize().then(cb).thenCatch(eb);
261+
};
262+
};
263+
264+
exports.getLocation = function(el) {
265+
return function(cb, eb) {
266+
el.getLocation().then(cb).thenCatch(eb);
267+
};
268+
};
269+
256270
function execute(driver) {
257271
return function(action) {
258272
return function(cb, eb) {
@@ -284,3 +298,92 @@ exports.takeScreenshot = function(driver) {
284298
.thenCatch(eb);
285299
};
286300
};
301+
302+
303+
exports.saveScreenshot = function(fileName) {
304+
return function(driver) {
305+
return function(cb, eb) {
306+
driver.takeScreenshot()
307+
.then(function(str) {
308+
fs.writeFile(path.resolve(fileName),
309+
str.replace(/^data:image\/png;base64,/,""),
310+
{
311+
encoding: "base64",
312+
flag: "w+"
313+
},
314+
function(err) {
315+
if (err) return eb(err);
316+
return cb();
317+
});
318+
})
319+
.thenCatch(eb);
320+
};
321+
};
322+
};
323+
324+
325+
exports.getWindow = function(window) {
326+
return function(cb, eb) {
327+
try {
328+
return cb(window.manage().window());
329+
}
330+
catch (e) {
331+
return eb(e);
332+
}
333+
};
334+
};
335+
336+
exports.getWindowPosition = function(window) {
337+
return function(cb, eb) {
338+
return window.getPosition()
339+
.then(cb)
340+
.thenCatch(eb);
341+
};
342+
};
343+
344+
exports.getWindowSize = function(window) {
345+
return function(cb, eb) {
346+
return window.getSize()
347+
.then(cb)
348+
.thenCatch(eb);
349+
};
350+
};
351+
352+
exports.maximizeWindow = function(window) {
353+
return function(cb, eb) {
354+
return window.maximize()
355+
.then(cb)
356+
.thenCatch(eb);
357+
};
358+
};
359+
360+
exports.setWindowPosition = function(loc) {
361+
return function(window) {
362+
return function(cb, eb) {
363+
return window.setPosition(loc.x, loc.y)
364+
.then(cb)
365+
.thenCatch(eb);
366+
};
367+
};
368+
};
369+
370+
exports.setWindowSize = function(size) {
371+
return function(window) {
372+
return function(cb, eb) {
373+
return window.setSize(size.width, size.height)
374+
.then(cb)
375+
.thenCatch(eb);
376+
};
377+
};
378+
};
379+
380+
exports.getWindowScroll = function(driver) {
381+
return function(cb, eb) {
382+
driver.executeScript(function() {
383+
return {
384+
x: window.scrollX,
385+
y: window.scrollY
386+
};
387+
}).then(cb).thenCatch(eb);
388+
};
389+
};

0 commit comments

Comments
 (0)