Best JavaScript code snippet using webdriverio-monorepo
moveResizeWindow.js
Source:moveResizeWindow.js
...44function resizeWindow(hWnd, resize, hWndParent) {45 var tokens = resize.split("*");46 var rsW = tokens[0];47 var rsH = tokens[1];48 var rcWnd = getWindowRect(hWnd);49 var rcWork = hWndParent50 ? getWindowRect(hWndParent)51 : getWorkArea(hWnd);52 var ww = rcWork.right - rcWork.left;53 var wh = rcWork.bottom - rcWork.top;54 var w = rcWnd.right - rcWnd.left;55 var h = rcWnd.bottom - rcWnd.top;56 if(rsW.charAt(rsW.length - 1) == "%")57 rsW = Math.round(parseFloat(rsW)/100*ww);58 else if(!rsW)59 rsW = w;60 else61 rsW = parseInt(rsW);62 if(rsH.charAt(rsH.length - 1) == "%")63 rsH = Math.round(parseFloat(rsH)/100*wh);64 else if(!rsH)65 rsH = h;66 else67 rsH = parseInt(rsH);68 rsW = Math.min(ww, rsW);69 rsH = Math.min(wh, rsH);70 if(rsW == w && rsH == h)71 return false;72 return _resizeWindow(hWnd, rsW, rsH);73}74function moveWindow(hWnd, move, hWndParent) {75 var tokens = move.split("*");76 var mvX = tokens[0];77 var mvY = tokens[1];78 var rcWnd = getWindowRect(hWnd);79 var rcWork = hWndParent80 ? getWindowRect(hWndParent)81 : getWorkArea(hWnd);82 var w = rcWnd.right - rcWnd.left;83 var h = rcWnd.bottom - rcWnd.top;84 if (mvX == "left") mvX = rcWork.left;85 else if(mvX == "center") mvX = centerX(rcWnd, rcWork);86 else if(mvX == "right") mvX = rcWork.right - w;87 else if(!mvX) mvX = rcWnd.left;88 else mvX = parseInt(mvX);89 if (mvY == "top") mvY = rcWork.top;90 else if(mvY == "center") mvY = centerY(rcWnd, rcWork);91 else if(mvY == "bottom") mvY = rcWork.bottom - h;92 else if(!mvY) mvY = rcWnd.top;93 else mvY = parseInt(mvY);94 mvX = Math.max(rcWork.left, Math.min(rcWork.right - w, mvX));95 mvY = Math.max(rcWork.top, Math.min(rcWork.bottom - h, mvY));96 if(mvX == rcWnd.left && mvY == rcWnd.top)97 return false;98 return _moveWindow(hWnd, mvX, mvY);99}100function centerX(rcWnd, rcWndParent) {101 return rcWndParent.left + ((rcWndParent.right - rcWndParent.left)/2 - (rcWnd.right - rcWnd.left)/2);102}103function centerY(rcWnd, rcWndParent) {104 return rcWndParent.top + ((rcWndParent.bottom - rcWndParent.top)/2 - (rcWnd.bottom - rcWnd.top)/2);105}106function getWindowPos(hWnd) {107 var rcWnd = getWindowRect(hWnd);108 if(!rcWnd)109 return false;110 return rcWnd.left + "x" + rcWnd.top + "|"111 + (rcWnd.right - rcWnd.left) + "x" + (rcWnd.bottom - rcWnd.top);112}113function saveWindowPos(windowId, hWnd, pos) {114 if(!pos)115 pos = getWindowPos(hWnd);116 if(!pos)117 return;118 var oSet = AkelPad.ScriptSettings();119 if(!oSet.Begin(WScript.ScriptBaseName, 0x2 /*POB_SAVE*/))120 return false;121 oSet.Write(windowId, 3 /*PO_STRING*/, pos);122 return oSet.End();123}124function restoreWindowPos(windowId, hWnd) {125 var oSet = AkelPad.ScriptSettings();126 if(!oSet.Begin(WScript.ScriptBaseName, 0x1 /*POB_READ*/))127 return false;128 var pos = oSet.Read(windowId, 3 /*PO_STRING*/, "");129 oSet.End();130 if(!/^(\d+)x(\d+)\|(\d+)x(\d+)$/.test(pos))131 return false;132 var x = +RegExp.$1;133 var y = +RegExp.$2;134 var w = +RegExp.$3;135 var h = +RegExp.$4;136 return oSys.Call("user32::SetWindowPos", hWnd, 0, x, y, w, h, 0x14 /*SWP_NOZORDER|SWP_NOACTIVATE*/);137}138function getWorkArea(hWnd) {139 var lpRect = AkelPad.MemAlloc(16); //sizeof(RECT)140 if(!lpRect)141 return null;142 oSys.Call("user32::GetWindowRect", hWnd, lpRect);143 var hMonitor = oSys.Call("user32::MonitorFromRect", lpRect, 0x2 /*MONITOR_DEFAULTTONEAREST*/);144 if(hMonitor) {145 //typedef struct tagMONITORINFO {146 // DWORD cbSize;147 // RECT rcMonitor;148 // RECT rcWork;149 // DWORD dwFlags;150 //} MONITORINFO, *LPMONITORINFO;151 var sizeofMonitorInfo = 4 + 16 + 16 + 4;152 var lpMi = AkelPad.MemAlloc(sizeofMonitorInfo);153 if(lpMi) {154 AkelPad.MemCopy(lpMi, sizeofMonitorInfo, 3 /*DT_DWORD*/);155 oSys.Call("user32::GetMonitorInfo" + _TCHAR, hMonitor, lpMi);156 var rcWork = parseRect(lpMi + 4 + 16);157 AkelPad.MemFree(lpMi);158 }159 }160 else { //?161 oSys.Call("user32::SystemParametersInfo" + _TCHAR, 48 /*SPI_GETWORKAREA*/, 0, lpRect, 0);162 var rcWork = parseRect(lpRect);163 }164 AkelPad.MemFree(lpRect);165 return rcWork;166}167function _moveWindow(hWnd, x, y) {168 return oSys.Call("user32::SetWindowPos", hWnd, 0, x, y, 0, 0, 0x15 /*SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOSIZE*/);169}170function _resizeWindow(hWnd, w, h) {171 return oSys.Call("user32::SetWindowPos", hWnd, 0, 0, 0, w, h, 0x16 /*SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOMOVE*/);172}173function getWindowRect(hWnd) {174 var lpRect = AkelPad.MemAlloc(16); //sizeof(RECT)175 if(!lpRect)176 return null;177 oSys.Call("user32::GetWindowRect", hWnd, lpRect);178 var rcWnd = parseRect(lpRect);179 AkelPad.MemFree(lpRect);180 return rcWnd;181}182function parseRect(lpRect) {183 return {184 left: AkelPad.MemRead(lpRect, 3 /*DT_DWORD*/),185 top: AkelPad.MemRead(lpRect + 4, 3 /*DT_DWORD*/),186 right: AkelPad.MemRead(lpRect + 8, 3 /*DT_DWORD*/),187 bottom: AkelPad.MemRead(lpRect + 12, 3 /*DT_DWORD*/)...
billboard.js
Source:billboard.js
...113 y: canvasPos.y - this.centerY*scale,114 };115 },116 getRelativeCursor: function(x,y,pos) {117 var rect = this.getWindowRect(pos);118 // TODO: use center pos for this billboard instead of assuming we're in the perfect center.119 var midx = rect.x + rect.w/2;120 var midy = rect.y + rect.h/2;121 x -= midx;122 y -= midy;123 var nx,ny;124 125 // (x+yi)(cos + isin)126 // (xcos + ixsin + iycos - ysin)127 if (pos.angle) {128 var c = Math.cos(-pos.angle);129 var s = Math.sin(-pos.angle);130 nx = x*c - y*s;131 ny = x*s + y*c;132 }133 else {134 nx = x;135 ny = y;136 }137 nx += rect.w/2;138 ny += rect.h/2;139 return {x:nx, y:ny};140 },141 isInsideWindowRect: function(wx,wy,pos) {142 var rect = this.getWindowRect(pos);143 var p = this.getRelativeCursor(wx,wy,pos);144 if (0 <= p.x && p.x <= rect.w &&145 0 <= p.y && p.y <= rect.h) {146 return true;147 }148 return false;149 },150 isInsideCanvasRect: function(cx,cy,pos) {151 var rect = this.getCanvasRect(pos);152 return (153 rect.x <= cx && cx <= (rect.x + rect.w) &&154 rect.y <= cy && cy <= (rect.y + rect.h));155 },156 setBottomSide: function(wy, windowPos) {157 var rect = this.getWindowRect(windowPos);158 wy = Math.max(rect.y+1, wy);159 var relativeCenterY = this.centerY / this.h;160 this.h = (wy - rect.y) / this.scale;161 this.centerY = this.h * relativeCenterY;162 windowPos.y = rect.y + this.centerY * this.scale;163 },164 setTopSide: function(wy, windowPos) {165 var rect = this.getWindowRect(windowPos);166 wy = Math.min(rect.y+rect.h-1, wy);167 var relativeCenterY = this.centerY / this.h;168 this.h = (rect.y + rect.h - wy) / this.scale;169 this.centerY = this.h * relativeCenterY;170 windowPos.y = wy + this.centerY * this.scale;171 },172 setLeftSide: function(wx, windowPos) {173 var rect = this.getWindowRect(windowPos);174 wx = Math.min(rect.x+rect.w-1, wx);175 var relativeCenterX = this.centerX / this.w;176 this.w = (rect.x + rect.w - wx) / this.scale;177 this.centerX = this.w * relativeCenterX;178 windowPos.x = wx + this.centerX * this.scale;179 },180 setRightSide: function(wx, windowPos) {181 var rect = this.getWindowRect(windowPos);182 wx = Math.max(rect.x+1, wx);183 var relativeCenterX = this.centerX / this.w;184 this.w = (wx - rect.x) / this.scale;185 this.centerX = this.w * relativeCenterX;186 windowPos.x = rect.x + this.centerX * this.scale;187 },188 isOverRotationHandle: function(x,y,pos) {189 var rect = this.getWindowRect(pos);190 var p = this.getRelativeCursor(x,y,pos);191 var dx = (rect.w/2) - p.x;192 var dy = 0 - p.y;193 var dist_sq = dx*dx+dy*dy;194 return dist_sq <= 64;195 },196 transform: function(ctx,pos) {197 var rect = this.getWindowRect(pos);198 ctx.translate(rect.x, rect.y);199 var scale = rect.w / this.w;200 ctx.scale(scale, scale);201 },202 fill: function(ctx,pos) {203 var r = this.getWindowRect(pos);204 ctx.fillRect(r.x,r.y,r.w,r.h);205 },...
gyroscope.native.js
Source:gyroscope.native.js
...49 return gyroDesktop.mouseCaptured5051 },52 eulerToXY: function (euler) {53 var coordinates = gyroDesktop.getWindowRect();54 var x = coordinates[0];55 var y = coordinates[1];56 var w = coordinates[2];57 var h = coordinates[3];5859 vrMousePosX = (((euler['_y'] * -1.5) + 1) * (w / 2)) + x;60 vrMousePosY = (((euler['_x'] * -1.5) + 1) * (h / 2)) + y;6162 if (vrMousePosX > x + w) {63 vrMousePosX = x + w;64 }65 if (vrMousePosX < x) {66 vrMousePosX = x;67 }68 if (vrMousePosY > y + h) {69 vrMousePosY = y + h;70 }71 if (vrMousePosY < y) {72 vrMousePosY = y;73 }74 return [vrMousePosX, vrMousePosY]75 },76 eulerToRotationXY: function (euler) {77 var coordinates = gyroDesktop.getWindowRect();78 var x = coordinates[0];79 var y = coordinates[1];80 var w = coordinates[2];81 var h = coordinates[3];8283 vrMousePosX = (((euler['_y'] * -1.5) + 1) * (w / 2)) + x;84 vrMousePosY = (((euler['_x'] * -1.5) + 1) * (h / 2)) + y;8586 if (vrMousePosX > x + w) {87 vrMousePosX = x + w;88 }89 if (vrMousePosX < x) {90 vrMousePosX = x;91 }
...
geometry.js
Source:geometry.js
1import React from 'react';2import { findDOMNode } from 'react-dom';3import { clamp } from './math';4import { debounce } from './event';5export function getWindowRect() {6 return {7 top: window.scrollY,8 left: window.scrollX,9 height: window.innerHeight,10 width: window.innerWidth11 };12}13export function getRectFor(node) {14 const rect = findDOMNode(node);15 if (!rect) {16 return {};17 }18 return rect.getBoundingClientRect();19}20/**21 * Provides a hook that returns window dimensions22 * @param {number} [wait=100] Timer for debounced dimension calculation23 * @return {Shape} Same results as `getWindowRect``24 */25export function useWindowSize(wait = 100) {26 const [size, setSize] = React.useState(getWindowRect());27 const handleResize = debounce(() => {28 setSize(getWindowRect());29 }, wait);30 React.useEffect(() => {31 window.addEventListener('resize', handleResize);32 return () => {33 window.removeEventListener('resize', handleResize);34 };35 }, []);36 return size;37}38/**39 * Gets preferred direction for the provided react component40 * @param {React Node} node - a react component41 * @return {Shape}42 * Returns directional booleans for where the component should render. Eg:43 * top: true if component is in the bottom half of the screen44 * right: true if component in the left half of the screen45 */46export function getPreferredDirectionFor(node) {47 const windowRect = getWindowRect();48 const elementRect = getRectFor(node);49 const rightOffset = windowRect.width - elementRect.right;50 const bottomOffset = windowRect.height - elementRect.bottom;51 return {52 top: bottomOffset < elementRect.top,53 left: rightOffset < elementRect.left,54 right: rightOffset >= elementRect.left,55 bottom: bottomOffset >= elementRect.top56 };57}58/**59 * Gets coordinates and dimensions in pixels for the provided react component60 * @param {React Node} node61 * @return {Shape}62 */63export function getPositionFor(node, { fixed = false } = {}) {64 const windowRect = getWindowRect();65 const elementRect = getRectFor(node);66 return {67 top: elementRect.top + (fixed ? 0 : windowRect.top),68 left: elementRect.left + (fixed ? 0 : windowRect.left),69 width: elementRect.width,70 height: elementRect.height71 };72}73/**74 * Linearly interpolates and clamps between two values75 * @param {number} min76 * @param {number} max77 * @param {number} n78 * @return {number}...
index.js
Source:index.js
1'use strict';2const ffi = require('ffi');3const ref = require('ref');4const sharp = require('sharp');5const StructType = require('ref-struct');6const { spawnSync } = require('child_process');7const stringType = ref.types.CString;8const stringPtr = ref.refType(stringType);9const longType = ref.types.long;10const hwndType = longType;11const takeScreenshotPath = `${__dirname}/bin/nircmd.exe`;12const lib = new ffi.Library('user32', {13 GetForegroundWindow: ['long', []],14 GetWindowRect: [15 'long', [hwndType, stringPtr],16 ],17 FindWindowA: [18 'long', ["string", "string"]19 ]20});21var windStruct = StructType({22 left: longType,23 top: longType,24 right: longType,25 bottom: longType26}) 27const hwnd = lib.FindWindowA(null, 'Diablo II');28const windRect = new windStruct;29console.log('hwnd ', hwnd);30console.log('GetWindowRect ', lib.GetWindowRect(hwnd, windRect.ref()));31console.log('WindStruct ', windRect.left, windRect.top, windRect.right, windRect.bottom);32const width = windRect.right - windRect.left;33const height = windRect.bottom - windRect.top;34console.log('Window size', width, height);35while (lib.GetForegroundWindow() !== hwnd) {36 console.log('Waiting for game to be active', new Date);37}38const screenshotPath = `${__dirname}/images/output/full-screen.png`;39const screenshotPathOutput = `${__dirname}/images/output/diablo-2.png`;40spawnSync(takeScreenshotPath, ['savescreenshotfull', screenshotPath]);41sharp(screenshotPath)42 .extract(getGameClientRect())43 .toFile(screenshotPathOutput, function(err) {44 console.log('Image extracted!', screenshotPathOutput);45 });46function getGameClientRect() {47 const VERTICAL_OFFSET = 30;48 const HORIZONTAL_OFFSET = 5;49 return { left: windRect.left + HORIZONTAL_OFFSET, top: windRect.top + VERTICAL_OFFSET, width: width - HORIZONTAL_OFFSET, height: height - VERTICAL_OFFSET }...
getWindowRect.js
Source:getWindowRect.js
...11 * All attributes are in in CSS pixels. To change the window react, you can either specify `width` and `height`, `x` and `y` or all properties together.12 *13 * @example14 * module.exports = {15 * 'demo test .getWindowRect()': function(browser) {16 * // Retrieve the attributes17 * browser.getWindowRect(function(value) {18 * console.log(value);19 * });20 * },21 *22 * 'getWindowRect ES6 demo test': async function(browser) {23 * const resultValue = await browser.getWindowRect();24 * console.log('result value', resultValue);25 * }26 * }27 *28 * @w3c29 * @method getWindowRect30 * @link /#dfn-get-window-rect31 * @param {function} callback Callback function to be called when the command finishes.32 * @see windowRect33 * @api protocol.contexts34 */35class GetWindowRect extends ClientCommand {36 get returnsFullResultObject() {37 return false;...
get_rect.js
Source:get_rect.js
1// @require ../init.js2(function ( $ ) {3 /* RECT */4 $.getRect = function ( node ) {5 return node === window ? $.getWindowRect () : node.getBoundingClientRect ();6 };7 $.fn.getRect = function () {8 return this.length ? $.getRect ( this[0] ) : undefined;9 };10 /* WINDOW RECT */11 $.getWindowRect = function () {12 let rect = {};13 rect.left = 0;14 rect.top = 0;15 rect.width = window.innerWidth;16 rect.height = window.innerHeight;17 rect.right = rect.width;18 rect.bottom = rect.height;19 return rect;20 };...
commands.js
Source:commands.js
1export default {2 newSession: 'WebDriver:NewSession',3 executeScript: 'WebDriver:ExecuteScript',4 takeScreenshot: 'WebDriver:TakeScreenshot',5 getWindowRect: 'WebDriver:GetWindowRect',6 setWindowRect: 'WebDriver:SetWindowRect',7 quit: 'Marionette:Quit'...
Using AI Code Generation
1var webdriverio = require('webdriverio');2var options = {3 desiredCapabilities: {4 }5};6 .remote(options)7 .init()8 .getWindowRect().then(function(result) {9 console.log(result);10 })11 .end();
Using AI Code Generation
1var webdriverio = require('webdriverio');2var options = {3 desiredCapabilities: {4 }5};6 .remote(options)7 .init()8 .getTitle().then(function(title) {9 console.log('Title was: ' + title);10 })11 .end()12 .then(function() {13 console.log('done');14 })15 .catch(function(err) {16 console.log(err);17 });
Using AI Code Generation
1var webdriverio = require('webdriverio');2var options = { desiredCapabilities: { browserName: 'chrome' } };3var client = webdriverio.remote(options);4 .init()5 .windowHandleSize({width: 500, height: 600})6 .end();
Using AI Code Generation
1browser.getWindowRect().then(function(rect) {2 console.log(rect);3});4browser.setWindowRect(0, 0, 800, 600).then(function() {5 return browser.getWindowRect();6}).then(function(rect) {7 console.log(rect);8});9browser.maximizeWindow().then(function() {10 return browser.getWindowRect();11}).then(function(rect) {12 console.log(rect);13});14browser.fullscreenWindow().then(function() {15 return browser.getWindowRect();16}).then(function(rect) {17 console.log(rect);18});19browser.minimizeWindow().then(function() {20 return browser.getWindowRect();21}).then(function(rect) {22 console.log(rect);23});24browser.closeWindow().then(function() {25});26browser.deleteWindow().then(function() {27});28});29browser.switchWindow('WebdriverIO window').then(function() {30});31browser.getTabIds().then(function(tabs) {32 console.log(tabs);33});34browser.switchTab('WebdriverIO window').then(function() {35});36browser.setViewportSize({37}).then(function(size) {38 console.log(size);39});40browser.getViewportSize().then(function(size) {41 console.log(size);42});43browser.getOrientation().then(function(
Using AI Code Generation
1const { remote } = require('webdriverio');2(async () => {3 const browser = await remote({4 capabilities: {5 }6 })7 const windowRect = await browser.getWindowRect()8 console.log(windowRect)9 await browser.deleteSession()10})().catch((e) => console.error(e))11{ x: 0, y: 0, width: 1920, height: 1080 }
Using AI Code Generation
1it('should get the window size', function () {2 var windowSize = browser.getWindowSize();3 console.log(windowSize);4 expect(windowSize.width).to.equal(800);5 expect(windowSize.height).to.equal(600);6});7it('should set the window size', function () {8 browser.setWindowSize(800, 600);9 var windowSize = browser.getWindowSize();10 console.log(windowSize);11 expect(windowSize.width).to.equal(800);12 expect(windowSize.height).to.equal(600);13});14it('should maximize the window', function () {15 browser.maximizeWindow();16 var windowSize = browser.getWindowSize();17 console.log(windowSize);18 expect(windowSize.width).to.equal(800);19 expect(windowSize.height).to.equal(600);20});21it('should set the window size', function () {22 browser.setWindowSize(800, 600);23 var windowSize = browser.getWindowSize();24 console.log(windowSize);25 expect(windowSize.width).to.equal(800);26 expect(windowSize.height).to.equal(600);27});28it('should fullscreen the window', function () {29 browser.fullscreenWindow();30 var windowSize = browser.getWindowSize();31 console.log(windowSize);32 expect(windowSize.width).to.equal(800);33 expect(windowSize.height).to.equal(600);34});35it('should refresh the browser', function () {36 browser.refresh();37 expect(browser.getTitle()).to.equal('Google');38});39it('should switch the window', function () {40 expect(browser.getUrl()).to.contain('github');41});
Wondering what could be a next-gen browser and mobile test automation framework that is also simple and concise? Yes, that’s right, it's WebdriverIO. Since the setup is very easy to follow compared to Selenium testing configuration, you can configure the features manually thereby being the center of attraction for automation testing. Therefore the testers adopt WedriverIO to fulfill their needs of browser testing.
Learn to run automation testing with WebdriverIO tutorial. Go from a beginner to a professional automation test expert with LambdaTest WebdriverIO tutorial.
Running Your First Automation Script - Learn the steps involved to execute your first Test Automation Script using WebdriverIO since the setup is very easy to follow and the features can be configured manually.
Selenium Automation With WebdriverIO - Read more about automation testing with WebdriverIO and how it supports both browsers and mobile devices.
Browser Commands For Selenium Testing - Understand more about the barriers faced while working on your Selenium Automation Scripts in WebdriverIO, the ‘browser’ object and how to use them?
Handling Alerts & Overlay In Selenium - Learn different types of alerts faced during automation, how to handle these alerts and pops and also overlay modal in WebdriverIO.
How To Use Selenium Locators? - Understand how Webdriver uses selenium locators in a most unique way since having to choose web elements very carefully for script execution is very important to get stable test results.
Deep Selectors In Selenium WebdriverIO - The most popular automation testing framework that is extensively adopted by all the testers at a global level is WebdriverIO. Learn how you can use Deep Selectors in Selenium WebdriverIO.
Handling Dropdown In Selenium - Learn more about handling dropdowns and how it's important while performing automated browser testing.
Automated Monkey Testing with Selenium & WebdriverIO - Understand how you can leverage the amazing quality of WebdriverIO along with selenium framework to automate monkey testing of your website or web applications.
JavaScript Testing with Selenium and WebdriverIO - Speed up your Javascript testing with Selenium and WebdriverIO.
Cross Browser Testing With WebdriverIO - Learn more with this step-by-step tutorial about WebdriverIO framework and how cross-browser testing is done with WebdriverIO.
Get 100 minutes of automation test minutes FREE!!