UNPKG

7.56 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
8
9var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /**
10 * Dependencies
11 */
12
13var _url2 = require('url');
14
15var _url3 = _interopRequireDefault(_url2);
16
17var _bluebird = require('bluebird');
18
19var _bluebird2 = _interopRequireDefault(_bluebird);
20
21var _webdriverio = require('webdriverio');
22
23var _debug = require('debug');
24
25var _debug2 = _interopRequireDefault(_debug);
26
27var _detect_series = require('./utils/detect_series.js');
28
29var _detect_series2 = _interopRequireDefault(_detect_series);
30
31function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
32
33function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
34
35/**
36 * Private
37 */
38
39var debug = (0, _debug2.default)('mink:driver');
40
41/**
42 * Interface
43 */
44
45var Driver = function () {
46 function Driver(parameters) {
47 _classCallCheck(this, Driver);
48
49 this.parameters = parameters;
50 this.client = (0, _webdriverio.remote)(parameters);
51 this.baseUrl = parameters.baseUrl;
52 }
53
54 _createClass(Driver, [{
55 key: 'click',
56 value: function click(selector) {
57 if ((typeof selector === 'undefined' ? 'undefined' : _typeof(selector)) === 'object' && selector.ELEMENT) {
58 return this.client.elementIdClick(selector.ELEMENT);
59 }
60 return this.client.click(selector);
61 }
62 }, {
63 key: 'html',
64 value: function html(selector) {
65 if (!selector) {
66 return this.client.getSource();
67 }
68 return this.client.getHTML(selector).then(function (item) {
69 if (Array.isArray(item)) return item.join('');
70 return item;
71 });
72 }
73 }, {
74 key: 'url',
75 value: function url(input) {
76 if (!input) {
77 return this.client.getUrl().then(function (text) {
78 return _url3.default.parse(text);
79 });
80 }
81 return this.client.url(input);
82 }
83 }, {
84 key: 'sendKey',
85 value: function sendKey(selector, key) {
86 var _this = this;
87
88 return this.client.click(selector).then(function () {
89 return _this.client.keys(key);
90 });
91 }
92 }, {
93 key: 'elements',
94 value: function elements(selector) {
95 return this.client.elements(selector).then(function (response) {
96 return response.value;
97 });
98 }
99 }, {
100 key: 'elementsCount',
101 value: function elementsCount(selector) {
102 return this.elements(selector).then(function (items) {
103 return items.length;
104 });
105 }
106 }, {
107 key: 'elementsWithText',
108 value: function elementsWithText(selector, text) {
109 var _this2 = this;
110
111 return this.elements(selector).then(function (items) {
112 return _bluebird2.default.filter(items, function (WebElement) {
113 return _this2.client.elementIdText(WebElement.ELEMENT).then(function (result) {
114 return result.value === text;
115 });
116 });
117 });
118 }
119 }, {
120 key: 'elementsWithValue',
121 value: function elementsWithValue(selector, value) {
122 var _this3 = this;
123
124 return this.elements(selector).then(function (items) {
125 return _bluebird2.default.filter(items, function (WebElement) {
126 return _this3.client.elementIdAttribute(WebElement.ELEMENT, 'value').then(function (result) {
127 return result.value === value;
128 });
129 });
130 });
131 }
132 }, {
133 key: 'button',
134 value: function button(mixed) {
135 var _this4 = this;
136
137 return (0, _detect_series2.default)([function () {
138 return _this4.elements(mixed).catch(function (err) {
139 debug(err);
140 return [];
141 });
142 }, function () {
143 return _this4.elementsWithText('button', mixed);
144 }, function () {
145 return _this4.elementsWithValue('input[type=submit]', mixed);
146 }], function (fn) {
147 return fn();
148 }, function (WebElements) {
149 return !!WebElements.length;
150 }).then(function (_ref) {
151 var result = _ref.result;
152
153 if (!result) throw new Error('Button not found !');
154 return result[0];
155 });
156 }
157 }, {
158 key: 'link',
159 value: function link(mixed) {
160 var _this5 = this;
161
162 return (0, _detect_series2.default)([function () {
163 return _this5.elements(mixed).catch(function (err) {
164 debug(err);
165 return [];
166 });
167 }, function () {
168 return _this5.elementsWithText('body a', mixed);
169 }], function (fn) {
170 return fn();
171 }, function (WebElements) {
172 return !!WebElements.length;
173 }).then(function (_ref2) {
174 var result = _ref2.result;
175
176 if (!result) throw new Error('Link not found !');
177 return result[0];
178 });
179 }
180 }]);
181
182 return Driver;
183}();
184
185// WebDriverIO client symlinks
186
187
188exports.default = Driver;
189['init', 'refresh', 'back', 'end', 'setValue', 'selectByVisibleText', 'submitForm', 'windowHandleSize', 'saveScreenshot', 'getValue', 'moveToObject', 'isEnabled', 'isExisting', 'isSelected', 'isVisible'].forEach(function (method) {
190 Driver.prototype[method] = function () {
191 for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
192 args[_key] = arguments[_key];
193 }
194
195 return this.client[method].apply(this, args);
196 };
197});
198
199// Aliases
200Driver.prototype.isChecked = Driver.prototype.isSelected;
201Driver.prototype.check = Driver.prototype.click;
202Driver.prototype.uncheck = Driver.prototype.click;
203Driver.prototype.hover = Driver.prototype.moveToObject;
204Driver.prototype.setViewportSize = Driver.prototype.windowHandleSize;
205module.exports = exports['default'];
\No newline at end of file