diff --git a/lib/chrome.js b/lib/chrome.js index 3be26de..c32f8e8 100644 --- a/lib/chrome.js +++ b/lib/chrome.js @@ -350,9 +350,15 @@ var ChromeObject = function() { } }; - this.close = function() { + this.exit = function() { return this.sendPageRPC("Browser.close", {}); }; + + this.close = function() { + var response = this.sendPageRPC("Page.close", {}); + this.setPageId(null); + return response; + }; this.terminate = function() { try { @@ -640,15 +646,40 @@ var ChromeObject = function() { return this.evaluate('document.querySelector("' + selector + '").dispatchEvent(new Event("' + eventName + '"))'); } } else { - var probability = parseFloat(selector.substring(selector.indexOf('(') + 1, selector.indexOf(')'))); - return this.evaluate('(function(obj, p) { var a = Array.from(obj).filter(function() { return (Math.random() < p); }); if(a.length > 0) a[0].click(); })(document.querySelectorAll("' + selector.substring(0, selector.indexOf(':')) + '"), ' + probability + ')'); + var p = parseFloat(selector.substring(selector.indexOf('(') + 1, selector.indexOf(')'))); + var _selector = selector.substring(0, selector.indexOf(':')); + return this.evaluate('(function(obj, p) { var element = Object.values(obj).find(function() { return (Math.random() < p); }); if(element) element.click(); })(document.querySelectorAll("' + _selector + '"), ' + p + ')'); } }; - - this.scrollBySelector = function(selector, dx, dy) { - return this.evaluate('(function(rect, dx, dy) { window.scrollTo(rect.x + dx, rect.y + dy); })(document.querySelector("' + selector + '").getBoundingClientRect(), parseInt("' + (dx || 0) + '"), parseInt("' + (dy || 0) + '"))'); + + this.triggerEventByFind = function(eventName, selector, searchText) { + var s = '(function() {' + + ' var element = Object.values(document.querySelectorAll("' + selector + '")).find(function(x) {' + + ' return (x.innerText.indexOf(decodeURIComponent("' + encodeURIComponent(searchText) + '")) > -1);' + + ' });' + + ' if (element) {' + + ' element.' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + + ' }' + + '})()' + ; + return this.evaluate(s); }; + this.triggerEventOnNestedFind = function(eventName, selector, subSelector, searchText) { + var s = '(function() {' + + ' var element = Object.values(document.querySelectorAll("' + selector + '")).find(function(x) {' + + ' return (x.querySelector("' + subSelector + '").innerText.indexOf(decodeURIComponent("' + encodeURIComponent(searchText) + '")) > -1);' + + ' });' + + ' if (element) {' + + ' element.' + (eventName == 'click' ? 'click()' : 'dispatchEvent(new Event("' + eventName + '"))') + ';' + + ' }' + + '})()' + ; + return this.evaluate(s); + }; + + + this.scrollTo = function(x, y) { return this.evaluate('window.scrollTo(parseInt(' + x + '), parseInt(' + y + '))'); }; @@ -657,6 +688,10 @@ var ChromeObject = function() { return this.evaluate('window.scrollBy(parseInt(' + dx + '), parseInt(' + dy + '))'); }; + this.scrollToElement = function(selector, dx, dy) { + return this.evaluate('(function(rect, dx, dy) { window.scrollTo(rect.x + dx, rect.y + dy); })(document.querySelector("' + selector + '").getBoundingClientRect(), parseInt("' + dx + '"), parseInt("' + dy + '"))'); + }; + this.reload = function() { return this.sendPageRPC("Page.reload", {}); }; @@ -684,15 +719,15 @@ var ChromeObject = function() { this.setValue = function(selector, value, repeat) { var s = encodeURIComponent(value), i = 0; - while (i < repeat) { - this.evaluate('document.querySelector("' + selector + '").value = decodeURIComponent("' + s + '")'); - i++; - } + while (i < repeat) { + this.evaluate('document.querySelector("' + selector + '").value = decodeURIComponent("' + s + '")'); + i++; + } + }; + + this.getText = function(selector) { + return this.getEvaluatedValue('document.querySelector("' + selector + '").innerText'); }; - - this.getText = function(selector) { - return this.getEvaluatedValue('document.querySelector("' + selector + '").innerText'); - }; this.create(); };