Best JavaScript code snippet using testcafe
actions.js
Source:actions.js
1import TYPE from './type';2import SelectorBuilder from '../../client-functions/selectors/selector-builder';3import ClientFunctionBuilder from '../../client-functions/client-function-builder';4import functionBuilderSymbol from '../../client-functions/builder-symbol';5import CommandBase from './base';6import { ActionOptions, ClickOptions, MouseOptions, TypeOptions, DragToElementOptions } from './options';7import { initSelector, initUploadSelector } from './validations/initializers';8import executeJsExpression from '../execute-js-expression';9import { isJSExpression } from './utils';10import {11 actionOptions,12 integerArgument,13 positiveIntegerArgument,14 nonEmptyStringArgument,15 nullableStringArgument,16 urlArgument,17 stringOrStringArrayArgument,18 setSpeedArgument,19 actionRoleArgument,20 booleanArgument21} from './validations/argument';22import { SetNativeDialogHandlerCodeWrongTypeError } from '../../errors/test-run';23import { ExecuteClientFunctionCommand } from './observation';24// Initializers25function initActionOptions (name, val) {26 return new ActionOptions(val, true);27}28function initClickOptions (name, val) {29 return new ClickOptions(val, true);30}31function initMouseOptions (name, val) {32 return new MouseOptions(val, true);33}34function initTypeOptions (name, val) {35 return new TypeOptions(val, true);36}37function initDragToElementOptions (name, val) {38 return new DragToElementOptions(val, true);39}40function initDialogHandler (name, val, { skipVisibilityCheck, testRun }) {41 let fn;42 if (isJSExpression(val))43 fn = executeJsExpression(val.value, testRun, { skipVisibilityCheck });44 else45 fn = val.fn;46 if (fn === null || fn instanceof ExecuteClientFunctionCommand)47 return fn;48 const options = val.options;49 const methodName = 'setNativeDialogHandler';50 let builder = fn && fn[functionBuilderSymbol];51 const isSelector = builder instanceof SelectorBuilder;52 const functionType = typeof fn;53 if (functionType !== 'function' || isSelector)54 throw new SetNativeDialogHandlerCodeWrongTypeError(isSelector ? 'Selector' : functionType);55 builder = builder instanceof ClientFunctionBuilder ?56 fn.with(options)[functionBuilderSymbol] :57 new ClientFunctionBuilder(fn, options, { instantiation: methodName, execution: methodName });58 return builder.getCommand([]);59}60// Commands61export class ClickCommand extends CommandBase {62 constructor (obj, testRun) {63 super(obj, testRun, TYPE.click);64 }65 _getAssignableProperties () {66 return [67 { name: 'selector', init: initSelector, required: true },68 { name: 'options', type: actionOptions, init: initClickOptions, required: true }69 ];70 }71}72export class RightClickCommand extends CommandBase {73 constructor (obj, testRun) {74 super(obj, testRun, TYPE.rightClick);75 }76 _getAssignableProperties () {77 return [78 { name: 'selector', init: initSelector, required: true },79 { name: 'options', type: actionOptions, init: initClickOptions, required: true }80 ];81 }82}83export class ExecuteExpressionCommand extends CommandBase {84 constructor (obj, testRun) {85 super(obj, testRun, TYPE.executeExpression);86 }87 _getAssignableProperties () {88 return [89 { name: 'expression', type: nonEmptyStringArgument, required: true },90 { name: 'resultVariableName', type: nonEmptyStringArgument, defaultValue: null },91 { name: 'isAsyncExpression', type: booleanArgument, defaultValue: false }92 ];93 }94}95export class DoubleClickCommand extends CommandBase {96 constructor (obj, testRun) {97 super(obj, testRun, TYPE.doubleClick);98 }99 _getAssignableProperties () {100 return [101 { name: 'selector', init: initSelector, required: true },102 { name: 'options', type: actionOptions, init: initClickOptions, required: true }103 ];104 }105}106export class HoverCommand extends CommandBase {107 constructor (obj, testRun) {108 super(obj, testRun, TYPE.hover);109 }110 _getAssignableProperties () {111 return [112 { name: 'selector', init: initSelector, required: true },113 { name: 'options', type: actionOptions, init: initMouseOptions, required: true }114 ];115 }116}117export class TypeTextCommand extends CommandBase {118 constructor (obj, testRun) {119 super(obj, testRun, TYPE.typeText);120 }121 _getAssignableProperties () {122 return [123 { name: 'selector', init: initSelector, required: true },124 { name: 'text', type: nonEmptyStringArgument, required: true },125 { name: 'options', type: actionOptions, init: initTypeOptions, required: true }126 ];127 }128}129export class DragCommand extends CommandBase {130 constructor (obj, testRun) {131 super(obj, testRun, TYPE.drag);132 }133 _getAssignableProperties () {134 return [135 { name: 'selector', init: initSelector, required: true },136 { name: 'dragOffsetX', type: integerArgument, required: true },137 { name: 'dragOffsetY', type: integerArgument, required: true },138 { name: 'options', type: actionOptions, init: initMouseOptions, required: true }139 ];140 }141}142export class DragToElementCommand extends CommandBase {143 constructor (obj, testRun) {144 super(obj, testRun, TYPE.dragToElement);145 }146 _getAssignableProperties () {147 return [148 { name: 'selector', init: initSelector, required: true },149 { name: 'destinationSelector', init: initSelector, required: true },150 { name: 'options', type: actionOptions, init: initDragToElementOptions, required: true }151 ];152 }153}154export class SelectTextCommand extends CommandBase {155 constructor (obj, testRun) {156 super(obj, testRun, TYPE.selectText);157 }158 _getAssignableProperties () {159 return [160 { name: 'selector', init: initSelector, required: true },161 { name: 'startPos', type: positiveIntegerArgument, defaultValue: null },162 { name: 'endPos', type: positiveIntegerArgument, defaultValue: null },163 { name: 'options', type: actionOptions, init: initActionOptions, required: true }164 ];165 }166}167export class SelectEditableContentCommand extends CommandBase {168 constructor (obj, testRun) {169 super(obj, testRun, TYPE.selectEditableContent);170 }171 _getAssignableProperties () {172 return [173 { name: 'startSelector', init: initSelector, required: true },174 { name: 'endSelector', init: initSelector, defaultValue: null },175 { name: 'options', type: actionOptions, init: initActionOptions, required: true }176 ];177 }178}179export class SelectTextAreaContentCommand extends CommandBase {180 constructor (obj, testRun) {181 super(obj, testRun, TYPE.selectTextAreaContent);182 }183 _getAssignableProperties () {184 return [185 { name: 'selector', init: initSelector, required: true },186 { name: 'startLine', type: positiveIntegerArgument, defaultValue: null },187 { name: 'startPos', type: positiveIntegerArgument, defaultValue: null },188 { name: 'endLine', type: positiveIntegerArgument, defaultValue: null },189 { name: 'endPos', type: positiveIntegerArgument, defaultValue: null },190 { name: 'options', type: actionOptions, init: initActionOptions, required: true }191 ];192 }193}194export class PressKeyCommand extends CommandBase {195 constructor (obj, testRun) {196 super(obj, testRun, TYPE.pressKey);197 }198 _getAssignableProperties () {199 return [200 { name: 'keys', type: nonEmptyStringArgument, required: true },201 { name: 'options', type: actionOptions, init: initActionOptions, required: true }202 ];203 }204}205export class NavigateToCommand extends CommandBase {206 constructor (obj, testRun) {207 super(obj, testRun, TYPE.navigateTo);208 }209 _getAssignableProperties () {210 return [211 { name: 'url', type: urlArgument, required: true },212 { name: 'stateSnapshot', type: nullableStringArgument, defaultValue: null },213 { name: 'forceReload', type: booleanArgument, defaultValue: false }214 ];215 }216}217export class SetFilesToUploadCommand extends CommandBase {218 constructor (obj, testRun) {219 super(obj, testRun, TYPE.setFilesToUpload);220 }221 _getAssignableProperties () {222 return [223 { name: 'selector', init: initUploadSelector, required: true },224 { name: 'filePath', type: stringOrStringArrayArgument, required: true }225 ];226 }227}228export class ClearUploadCommand extends CommandBase {229 constructor (obj, testRun) {230 super(obj, testRun, TYPE.clearUpload);231 }232 _getAssignableProperties () {233 return [234 { name: 'selector', init: initUploadSelector, required: true }235 ];236 }237}238export class SwitchToIframeCommand extends CommandBase {239 constructor (obj, testRun) {240 super(obj, testRun, TYPE.switchToIframe);241 }242 _getAssignableProperties () {243 return [244 { name: 'selector', init: initSelector, required: true }245 ];246 }247}248export class SwitchToMainWindowCommand {249 constructor () {250 this.type = TYPE.switchToMainWindow;251 }252}253export class SetNativeDialogHandlerCommand extends CommandBase {254 constructor (obj, testRun) {255 super(obj, testRun, TYPE.setNativeDialogHandler);256 }257 _getAssignableProperties () {258 return [259 { name: 'dialogHandler', init: initDialogHandler, required: true }260 ];261 }262}263export class GetNativeDialogHistoryCommand {264 constructor () {265 this.type = TYPE.getNativeDialogHistory;266 }267}268export class GetBrowserConsoleMessagesCommand {269 constructor () {270 this.type = TYPE.getBrowserConsoleMessages;271 }272}273export class SetTestSpeedCommand extends CommandBase {274 constructor (obj, testRun) {275 super(obj, testRun, TYPE.setTestSpeed);276 }277 _getAssignableProperties () {278 return [279 { name: 'speed', type: setSpeedArgument, required: true }280 ];281 }282}283export class SetPageLoadTimeoutCommand extends CommandBase {284 constructor (obj, testRun) {285 super(obj, testRun, TYPE.setPageLoadTimeout);286 }287 _getAssignableProperties () {288 return [289 { name: 'duration', type: positiveIntegerArgument, required: true }290 ];291 }292}293export class UseRoleCommand extends CommandBase {294 constructor (obj, testRun) {295 super(obj, testRun, TYPE.useRole);296 }297 _getAssignableProperties () {298 return [299 { name: 'role', type: actionRoleArgument, required: true }300 ];301 }...
Using AI Code Generation
1import { initDragToElementOptions } from 'testcafe-browser-provider-electron';2import { initDragToElementOptions } from 'testcafe-browser-provider-electron';3import { initDragToElementOptions } from 'testcafe-browser-provider-electron';4import { initDragToElementOptions } from 'testcafe-browser-provider-electron';5import { initDragToElementOptions } from 'testcafe-browser-provider-electron';6import { initDragToElementOptions } from 'testcafe-browser-provider-electron';7import { initDragToElementOptions } from 'testcafe-browser-provider-electron';8import { initDragToElementOptions } from 'testcafe-browser-provider-electron';9import { initDragToElementOptions } from 'testcafe-browser-provider-electron';10import { initDragToElementOptions } from 'testcafe-browser-provider-electron';11import { initDragToElementOptions } from 'testcafe-browser-provider-electron';12import { initDragToElementOptions } from 'testcafe-browser-provider-electron';13import { initDragToElementOptions } from 'testcafe-browser-provider-electron';14import { initDragToElementOptions } from 'testcafe-browser-provider-electron';15import { initDragToElementOptions } from 'testcafe-browser-provider-electron';16import { init
Using AI Code Generation
1import { initDragToElementOptions } from 'testcafe';2test('My Test', async t => {3 .dragToElement('#draggable', '#droppable', initDragToElementOptions()4 .offsetX(10)5 .offsetY(10)6 .speed(1)7 );8});9import { dragToElement } from 'testcafe';10test('My Test', async t => {11 .dragToElement('#draggable', '#droppable', {12 });13});14import { Selector } from 'testcafe';15test('My Test', async t => {16 const draggable = Selector('#draggable');17 const droppable = Selector('#droppable');18 .dragToElement(draggable, droppable, {19 });20});21import { Selector } from 'testcafe';22test('My Test', async t => {23 const draggable = Selector('#draggable');24 const droppable = Selector('#droppable');25 .dragToElement(draggable, droppable);26});27t.dragToElement( selector [, dragOffsetX] [, dragOffsetY] [, options] )
Using AI Code Generation
1import { initDragToElementOptions } from 'testcafe';2import { Selector } from 'testcafe';3test('Drag and Drop', async t => {4 .dragToElement(Selector('#slider'), Selector('#slider'), initDragToElementOptions({ offsetX: 10, offsetY: 10 }));5});6{7 "scripts": {8 },9 "devDependencies": {10 }11}
Using AI Code Generation
1import { initDragToElementOptions } from 'testcafe';2import { Selector } from 'testcafe';3test('Drag and Drop', async t => {4 .dragToElement(Selector('#slider'), Selector('#slider'), initDragToElementOptions({ offsetX: 10, offsetY: 10 }));5});6{7 "scripts": {8 },9 "devDependencies": {10 }11}
Using AI Code Generation
1import { initDragToElementOptions } from 'testcafe';2import { Selector } from 'testcafe';3test('Drag an element', async t => {4 const draggable = Selector('#draggable');5 const droppable = Selector('#droppable');6 .dragToElement(draggable, droppable, initDragToElementOptions({ destinationOffsetX: 10, destinationOffsetY: 10 }));7});8import { ClientFunction } from 'testcafe';9import { t } from 'testcafe';10export function initDragToElementOptions (options) {11 return ClientFunction(() => {12 return {13 };14 }, { dependencies: { options } })();15}16import { Selector } from 'testcafe';17declare module 'testcafe' {18 interface TestController {19 dragToElement(sourceSelector: Selector, destinationSelector: Selector, dragToElementOptions: object): Promise<void>;20 }21}22import { Selector } from 'testcafe';23declare module 'testcafe' {24 interface Selector {25 dragToElement(destinationSelector: Selector, dragToElementOptions: object): Promise<void>;26 }27}28import { Selector } from 'testcafe';29Selector.prototype.dragToElement = async function (destinationSelector, dragToElementOptions) {30 await t.dragToElement(this, destinationSelector, dragToElementOptions);31};32import { Selector } from 'testcafe';33Selector.prototype.dragToElement = async function (destinationSelector, dragToElementOptions) {34 await t.dragToElement(this, destinationSelector, dragToElementOptions);35};
Using AI Code Generation
1import { Selector } from 'testcafe';2test('My first test', async t => {3 .dragToElement('#draggable', '#droppable')4 .expect(Selector('#droppable').innerText).contains('Dropped!');5});6import { Selector } from 'testcafe';7test('My first test', async t => {8 .dragToElement('#draggable', '#droppable')9 .expect(Selector('#droppable').innerText).contains('Dropped!');10});11import { initDragToElementOptions } from 'testcafe-browser-provider-electron';12test('My test', async t => {13 .dragToElement(Selector('#draggable'), Selector('#droppable'), initDragToElementOptions);14});15omport { initDragToElementOptions } from 'testcafe-browser-provider-electron';16import { Selector } from 'testcafe';17test('My test', async t => {18 .dragToElement(Selector('#draggable'), Selector('#droppable'), initDragToElementOptions);19});
Using AI Code Generation
1import { initDragToElementOptions } from 'testcafe-browser-provider-electron';2import { Selector } from 'testcafe';3test('My test', async t => {4 .dragToElement(Selector('#draggable'), Selector('#droppable'), initDragToElementOptions);5});6import { initDragToElementOptions } from 'testcafe-browser-provider-electron';7import { Selector } from 'testcafe';8test('My test', async t => {9 .dragToElement(Selector('#draggable'), Selector('#droppable'), initDragToElementOptions);10});
Using AI Code Generation
1import { Selector } from 'testcafe';2import { initDragToElementOptions } from './dragToElementOptions';3test('test', async t => {4 const dragElement = Selector('#slider');5 const dropElement = Selector('#slider-label');6 .dragToElement(dragElement, dropElement, initDragToElementOptions());7});8import { DragToElementOptions } from 'testcafe';9export const initDragToElementOptions = () => {10 const dragToElementOptions = new DragToElementOptions();11 dragToElementOptions.destinationOffsetX = 5;12 dragToElementOptions.destinationOffsetY = 5;13 return dragToElementOptions;14}
Using AI Code Generation
1import { initDragToElementOptions } from 'testcafe-browser-tools';2import { Selector } from 'testcafe';3test('Drag and Drop', async t => {4 .dragToElement(Selector('#draggable'), Selector('#droppable'), initDragToElementOptions)5 .expect(Selector('#droppable').innerText).eql('Dropped!');6});7import { DragToElementOptions } from 'testcafe-browser-tools/dist/api/typings';8export const initDragToElementOptions: DragToElementOptions = {9 modifiers: {10 }11};
Using AI Code Generation
1import { Selector, t } from 'testcafe';2import { initDragToElementOptions } from 'testcafe-browser-provider-electron';3test('Drag and drop', async t => {4 .dragToElement(Selector('#drag'), Selector('#drop'), initDragToElementOptions);5});6import { Selector, t } from 'testcafe';7test('Drag and drop', async t => {8 .dragToElement(Selector('#drag'), Selector('#drop'));9});10import { Selector, t } from 'testcafe';11import { initDragToElementOptions } from 'testcafe-browser-provider-electron';12test('Drag and drop', async t => {13 .dragToElement(Selector('#drag'), Selector('#drop'), initDragToElementOptions(10, 10, 0.5));14});
Using AI Code Generation
1import { initDragToElementOptions } from 'testcafe-browser-tools';2import { Selector } from 'testcafe';3test('Drag and Drop', async t => {4 .dragToElement(Selector('#draggable'), Selector('#droppable'), initDragToElementOptions)5 .expect(Selector('#droppable').innerText).eql('Dropped!');6});7import { DragToElementOptions } from 'testcafe-browser-tools/dist/api/typings';8export const initDragToElementOptions: DragToElementOptions = {9 modifiers: {10 }11};
Using AI Code Generation
1import { Selector } from 'testcafe';2test('Drag slider', async t => {3 .dragToElement('#slider', '#tried-test-cafe')4 .expect(Selector('#tried-test-cafe').checked).ok();5});6import { Selector } from 'testcafe';7test('Drag slider', async t => {8 .dragToElement('#slider', '#tried-test-cafe')9 .expect(Selector('#tried-test-cafe').checked).ok();10});11 .dragToElement('#slider', '#tried-test-cafe', { offsetX: 10, offsetY: 10 })12 .expect(Selector('#tried-test-cafe').checked).ok();13 .drag('#slider', 10, 10)14 .expect(Selector('#tried-test-cafe').checked).ok();15 .drag('#slider', 10, 10)16 .expect(Selector('#tried-test-cafe').checked).ok();17 .drag('#slider', 10, 10)18 .expect(Selector('#tried-test-cafe').checked).ok();
Using AI Code Generation
1import { Selector } from 'testcafe';2import { initDragToElementOptions } from './dragToElementOptions';3test('test', async t => {4 const dragElement = Selector('#slider');5 const dropElement = Selector('#slider-label');6 .dragToElement(dragElement, dropElement, initDragToElementOptions());7});8import { DragToElementOptions } from 'testcafe';9export const initDragToElementOptions = () => {10 const dragToElementOptions = new DragToElementOptions();11 dragToElementOptions.destinationOffsetX = 5;12 dragToElementOptions.destinationOffsetY = 5;13 return dragToElementOptions;14}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!