Best JavaScript code snippet using root
expect.test.js
Source:expect.test.js
...44 await e.expect(e.element(e.by.id('test').and(e.by.type('type')))).toBeVisible();45 await e.expect(e.element(e.by.id('test').not())).toBeVisible();46 });47 it(`expect with wrong parameters should throw`, async () => {48 await expectToThrow(() => e.expect('notAnElement'));49 await expectToThrow(() => e.expect(e.element('notAMatcher')));50 });51 it(`matchers with wrong parameters should throw`, async () => {52 await expectToThrow(() => e.element(e.by.label(5)));53 await expectToThrow(() => e.element(e.by.id(5)));54 await expectToThrow(() => e.element(e.by.type(0)));55 await expectToThrow(() => e.by.traits(1));56 await expectToThrow(() => e.by.traits(['nonExistentTrait']));57 await expectToThrow(() => e.element(e.by.value(0)));58 await expectToThrow(() => e.element(e.by.text(0)));59 await expectToThrow(() => e.element(e.by.id('test').withAncestor('notAMatcher')));60 await expectToThrow(() => e.element(e.by.id('test').withDescendant('notAMatcher')));61 await expectToThrow(() => e.element(e.by.id('test').and('notAMatcher')));62 });63 it(`waitFor (element)`, async () => {64 await e.waitFor(e.element(e.by.id('id'))).toExist().withTimeout(0);65 await e.waitFor(e.element(e.by.id('id'))).toBeVisible();66 await e.waitFor(e.element(e.by.id('id'))).toBeNotVisible();67 await e.waitFor(e.element(e.by.id('id'))).toExist();68 await e.waitFor(e.element(e.by.id('id'))).toExist().withTimeout(0);69 await e.waitFor(e.element(e.by.id('id'))).toNotExist().withTimeout(0);70 await e.waitFor(e.element(e.by.id('id'))).toHaveText('text');71 await e.waitFor(e.element(e.by.id('id'))).toHaveValue('value');72 await e.waitFor(e.element(e.by.id('id'))).toNotHaveValue('value');73 await e.waitFor(e.element(e.by.id('id'))).toBeVisible().whileElement(e.by.id('id2')).scroll(50, 'down');74 await e.waitFor(e.element(e.by.id('id'))).toBeVisible().whileElement(e.by.id('id2')).scroll(50);75 });76 it(`waitFor (element) with wrong parameters should throw`, async () => {77 await expectToThrow(() => e.waitFor('notAnElement'));78 await expectToThrow(() => e.waitFor(e.element(e.by.id('id'))).toExist().withTimeout('notANumber'));79 await expectToThrow(() => e.waitFor(e.element(e.by.id('id'))).toExist().withTimeout(-1));80 await expectToThrow(() => e.waitFor(e.element(e.by.id('id'))).toBeVisible().whileElement('notAnElement'));81 });82 it(`waitFor (element) with non-elements should throw`, async () => {83 await expectToThrow(() => e.waitFor('notAnElement').toBeVisible());84 });85 it(`interactions`, async () => {86 await e.element(e.by.label('Tap Me')).tap();87 await e.element(e.by.label('Tap Me')).tapAtPoint({x: 10, y:10});88 await e.element(e.by.label('Tap Me')).longPress();89 await e.element(e.by.label('Tap Me')).longPress(2000);90 await e.element(e.by.id('UniqueId819')).multiTap(3);91 await e.element(e.by.id('UniqueId937')).typeText('passcode');92 await e.element(e.by.id('UniqueId937')).tapBackspaceKey();93 await e.element(e.by.id('UniqueId937')).tapReturnKey();94 await e.element(e.by.id('UniqueId005')).clearText();95 await e.element(e.by.id('UniqueId005')).replaceText('replaceTo');96 await e.element(e.by.id('UniqueId005')).pinchWithAngle('outward', 'fast', 0);97 await e.element(e.by.id('UniqueId005')).pinchWithAngle('outward');98 await e.element(e.by.id('ScrollView161')).scroll(100);99 await e.element(e.by.id('ScrollView161')).scroll(100, 'down');100 await e.element(e.by.id('ScrollView161')).scroll(100, 'up');101 await e.element(e.by.id('ScrollView161')).scroll(100, 'right');102 await e.element(e.by.id('ScrollView161')).scroll(100, 'left');103 await e.element(e.by.id('ScrollView161')).scrollTo('bottom');104 await e.element(e.by.id('ScrollView161')).scrollTo('top');105 await e.element(e.by.id('ScrollView161')).scrollTo('left');106 await e.element(e.by.id('ScrollView161')).scrollTo('right');107 await e.element(e.by.id('ScrollView799')).swipe('down');108 await e.element(e.by.id('ScrollView799')).swipe('down', 'fast');109 await e.element(e.by.id('ScrollView799')).swipe('up', 'slow');110 await e.element(e.by.id('ScrollView799')).swipe('left', 'fast');111 await e.element(e.by.id('ScrollView799')).swipe('right', 'slow');112 await e.element(e.by.id('ScrollView799')).swipe('down', 'fast', 0.9);113 await e.element(e.by.id('ScrollView799')).swipe('up', 'slow', 0.9);114 await e.element(e.by.id('ScrollView799')).swipe('left', 'fast', 0.9);115 await e.element(e.by.id('ScrollView799')).swipe('right', 'slow', 0.9);116 await e.element(e.by.id('ScrollView799')).atIndex(1);117 });118 it(`interactions with wrong parameters should throw`, async () => {119 await expectToThrow(() => e.element(e.by.id('UniqueId819')).multiTap('NaN'));120 await expectToThrow(() => e.element(e.by.id('UniqueId937')).typeText(0));121 await expectToThrow(() => e.element(e.by.id('UniqueId005')).replaceText(3));122 await expectToThrow(() => e.element(e.by.id('UniqueId005')).pinchWithAngle('noDirection', 'slow', 0));123 await expectToThrow(() => e.element(e.by.id('UniqueId005')).pinchWithAngle(1, 'slow', 0));124 await expectToThrow(() => e.element(e.by.id('UniqueId005')).pinchWithAngle('outward', 1, 0));125 await expectToThrow(() => e.element(e.by.id('UniqueId005')).pinchWithAngle('outward', 'noDirection', 0));126 await expectToThrow(() => e.element(e.by.id('UniqueId005')).pinchWithAngle('outward', 'slow', 'NaN'));127 await expectToThrow(() => e.element(e.by.id('UniqueId005')).replaceText(3));128 await expectToThrow(() => e.element(e.by.id('ScrollView161')).scroll('NaN', 'down'));129 await expectToThrow(() => e.element(e.by.id('ScrollView161')).scroll(100, 'noDirection'));130 await expectToThrow(() => e.element(e.by.id('ScrollView161')).scroll(100, 0));131 await expectToThrow(() => e.element(e.by.id('ScrollView161')).scrollTo(0));132 await expectToThrow(() => e.element(e.by.id('ScrollView161')).scrollTo('noDirection'));133 await expectToThrow(() => e.element(e.by.id('ScrollView799')).swipe(4, 'fast'));134 await expectToThrow(() => e.element(e.by.id('ScrollView799')).swipe('noDirection', 0));135 await expectToThrow(() => e.element(e.by.id('ScrollView799')).swipe('noDirection', 'fast'));136 await expectToThrow(() => e.element(e.by.id('ScrollView799')).swipe('down', 'NotFastNorSlow'));137 await expectToThrow(() => e.element(e.by.id('ScrollView799')).swipe('down', 'NotFastNorSlow', 0.9));138 await expectToThrow(() => e.element(e.by.id('ScrollView799')).atIndex('NaN'));139 await expectToThrow(() => e.element(e.by.type('UIPickerView')).setDatePickerDate(0, 'mm'));140 await expectToThrow(() => e.element(e.by.type('UIPickerView')).setDatePickerDate('something', 0));141 });142 it(`exportGlobals() should export api functions`, async () => {143 const originalExpect = expect;144 e.exportGlobals();145 const newExpect = expect;146 global.expect = originalExpect;147 expect(newExpect).not.toEqual(originalExpect);148 expect(element).toBeDefined();149 expect(waitFor).toBeDefined();150 expect(by).toBeDefined();151 });152});153async function expectToThrow(func) {154 try {155 await func();156 } catch (ex) {157 expect(ex).toBeDefined();158 }159}160class MockExecutor {161 async execute(invocation) {162 if (typeof invocation === 'function') {163 invocation = invocation();164 }165 expect(invocation.target).toBeDefined();166 expect(invocation.target.type).toBeDefined();167 expect(invocation.target.value).toBeDefined();...
validate-schema.spec.js
Source:validate-schema.spec.js
2var validateSchema = require('../validate-schema')3describe('validate-schema', function() {4 describe('unsupported', function() {5 it('should only support objects', function() {6 expectToThrow('', /schema must be an object/)7 })8 it('should not support `not`', function() {9 expectToThrow(10 {11 not: validSchema(),12 },13 /is not supported/14 )15 })16 it('should not support `anyOf`', function() {17 expectToThrow(18 {19 anyOf: [validSchema()],20 },21 /is not supported/22 )23 })24 it('should not support `items: []`', function() {25 expectToThrow(26 {27 type: 'array',28 items: [],29 },30 /is not supported/31 )32 })33 })34 describe('allOf', function() {35 it('should validate that allOf has at least one item', function() {36 expectToThrow(37 {38 allOf: [],39 },40 /at least one item/41 )42 })43 it('should merge and validte nested allOf', function() {44 expectToNotThrow({45 allOf: [46 {47 format: 'bogus',48 },49 {50 allOf: [51 {52 type: 'integer',53 description: 'foo',54 },55 {56 // this overrides format and integer type57 type: 'string',58 title: 'foo',59 format: 'uri',60 },61 ],62 },63 ],64 })65 })66 })67 describe('oneOf', function() {68 it('should validate oneOf has at least one item', function() {69 expectToThrow(70 {71 oneOf: [],72 },73 /at least one item/74 )75 })76 it('should validate that type is not an array', function() {77 expectToThrow(78 {79 oneOf: [{ type: ['null', 'string'] }],80 },81 /not supported directly inside oneOf/,82 ['oneOf', 0]83 )84 })85 it('should validate that there is no nesting of oneOf, allOf', function() {86 expectToThrow(87 {88 oneOf: [89 {90 oneOf: [validSchema()],91 },92 ],93 },94 /not supported directly inside oneOf/,95 ['oneOf', 0]96 )97 expectToThrow(98 {99 oneOf: [100 {101 allOf: [validSchema()],102 },103 ],104 },105 /not supported directly inside oneOf/,106 ['oneOf', 0]107 )108 })109 it('should validate that a type is present in all oneOf', function() {110 expectToThrow(111 {112 oneOf: [validSchema(), { description: '' }],113 },114 /type is required/,115 ['oneOf', 1]116 )117 expectToNotThrow({118 oneOf: [{ type: 'string' }, { type: 'null' }],119 })120 })121 it('should validate title and example for polymorphic objects', function() {122 expectToThrow(123 {124 oneOf: [125 { type: 'object' },126 { type: 'object', title: 'bar' },127 ],128 },129 /title is required/,130 ['oneOf', 0]131 )132 expectToThrow(133 {134 oneOf: [135 { type: 'object', title: 'foo' },136 { type: 'object', title: 'bar', example: 'bar' },137 ],138 },139 /example is required/,140 ['oneOf', 0]141 )142 expectToNotThrow({143 oneOf: [144 { type: 'object', title: 'foo', example: {} },145 { type: 'object', title: 'bar', example: {} },146 ],147 })148 expectToNotThrow({149 oneOf: [{ type: 'object' }, { type: 'null' }],150 })151 })152 it('should validate a well formed object', function() {153 expectToNotThrow({154 oneOf: [155 { type: 'object', title: 'foo', example: {} },156 { type: 'object', title: 'bar', example: {} },157 ],158 })159 })160 it('should merge oneOfs with keys in the root', function() {161 expectToNotThrow({162 type: 'object',163 title: 'foo',164 oneOf: [{ example: {} }, { example: {} }],165 })166 })167 })168 describe('object', function() {169 it('should validate types', function() {170 expectToThrow(171 {172 type: 'object',173 properties: {174 bogus: { type: 'bogus' },175 },176 },177 /invalid type/,178 ['properties', 'bogus']179 )180 expectToNotThrow({181 type: 'object',182 properties: {183 array: { type: 'array', items: validSchema() },184 boolean: { type: 'boolean' },185 integer: { type: 'integer' },186 null: { type: 'null' },187 number: { type: 'number' },188 object: { type: 'object' },189 string: { type: 'string' },190 },191 })192 })193 it('should validate pattern properties', function() {194 expectToThrow(195 {196 type: 'object',197 patternProperties: {198 'd+': { type: 'bogus' },199 },200 },201 /invalid type/,202 ['patternProperties', 'd+']203 )204 })205 it('should validate format', function() {206 expectToThrow(207 {208 type: 'object',209 format: 'uri',210 },211 /invalid type/212 )213 expectToThrow(214 {215 type: 'string',216 format: 'bogus',217 },218 /invalid format/219 )220 expectToNotThrow({221 type: ['null', 'string'],222 format: 'uri',223 })224 expectToNotThrow({225 type: 'string',226 format: 'uri',227 })228 })229 it('should validate enum', function() {230 expectToNotThrow({231 type: [232 'object',233 'null',234 'string',235 'number',236 'integer',237 'boolean',238 ],239 enum: [null, 'foo', 1, 0.01, true, false],240 })241 expectToNotThrow({242 type: 'string',243 enum: ['foo', 'bar'],244 })245 expectToThrow(246 {247 type: 'string',248 enum: [null],249 },250 /does not match/251 )252 expectToThrow(253 {254 type: 'string',255 enum: [],256 },257 /at least one item/258 )259 })260 it('should validate examples', function() {261 expectToThrow(262 {263 type: 'string',264 example: 1,265 },266 /invalid example/267 )268 expectToNotThrow({269 type: 'string',270 title: 'no example',271 })272 expectToNotThrow({273 type: 'object',274 example: {275 foo: 'bar',276 hello: 1,277 },278 properties: {279 foo: {280 type: 'string',281 enum: ['bar'],282 example: 'bar',283 },284 hello: {285 type: 'integer',286 example: 2,287 },288 },289 })290 })291 })292 describe('array', function() {293 it('should validate that items is defined', function() {294 expectToThrow(295 {296 type: 'array',297 },298 /items must be defined/299 )300 expectToThrow(301 {302 type: 'array',303 items: {},304 },305 /items must be defined/306 )307 })308 it('should validate items', function() {309 expectToThrow(310 {311 type: 'array',312 items: { type: 'bogus' },313 },314 /invalid type/,315 ['items']316 )317 expectToNotThrow({318 type: 'array',319 items: validSchema(),320 })321 })322 })323 function validSchema() {324 return { type: 'null' }325 }326 function expectToThrow(schema, message, path) {327 if (!path) path = []328 var threw = false329 try {330 validateSchema(schema)331 } catch (e) {332 threw = true333 expect(e.name).to.equal('SchemaValidationError')334 expect(e.message).to.match(message)335 expect(e.path).to.have.members(path)336 }337 expect(threw).to.equal(true, 'Should have thrown an exception')338 }339 function expectToNotThrow(schema) {340 expect(validateSchema.bind(undefined, schema)).to.not.throw()...
Using AI Code Generation
1var expectToThrow = require("../index").expectToThrow;2expectToThrow(function () {3 throw new Error("test error");4}, "test error");5expectToThrow(function () {6 throw new Error("test error");7}, "test error", "test error message");8expectToThrow(function () {9 throw new Error("test error");10}, "test error", "test error message", "test error stack");11expectToThrow(function () {12 throw new Error("test error");13}, "test error", "test error message", "test error stack", "test error name");14expectToThrow(function () {15 throw new Error("test error");16}, "test error", "test error message", "test error stack", "test error name", "test error fileName");17expectToThrow(function () {18 throw new Error("test error");19}, "test error", "test error message", "test error stack", "test error name", "test error fileName", "test error lineNumber");20expectToThrow(function () {21 throw new Error("test error");22}, "test error", "test error message", "test error stack", "test error name", "test error fileName", "test error lineNumber", "test error columnNumber");23expectToThrow(function () {24 throw new Error("test error");25}, "test error", "test error message", "test error stack", "test error name", "test error fileName", "test error lineNumber", "test error columnNumber", "test error description");26expectToThrow(function () {27 throw new Error("test error");28}, "test error", "test error message", "test error stack", "test error name", "test error fileName", "test error lineNumber", "test error columnNumber", "test error description", "test error number");29expectToThrow(function () {30 throw new Error("test error");31}, "test error", "test error message", "test error stack", "test error name", "test error fileName", "test error lineNumber", "test error columnNumber", "test error description", "test error number", "test error code");32var expect = require("../index").expect;33expect(function () {34 throw new Error("test error");35}).to.throw("test error");36expect(function () {37 throw new Error("test error");38}).to.throw("test error", "test error message");
Using AI Code Generation
1var expect = require('chai').expect;2var add = require('../src/add');3describe('add', function() {4 it('should add two numbers', function() {5 expect(add(1, 2)).to.equal(3);6 });7 it('should throw an error if arguments are not numbers', function() {8 expect(function() {9 add('a', 'b');10 }).to.throw(Error);11 });12});
Using AI Code Generation
1var root = require('..');2var expect = require('chai').expect;3root.expectToThrow(4 function() {5 throw new Error('test');6 },7 function(err) {8 expect(err).to.be.an.instanceof(Error);9 }10);
Using AI Code Generation
1var expect = require('../index.js').expectToThrow;2var assert = require('assert');3function TestError(message) {4 this.message = message;5 this.name = "TestError";6}7TestError.prototype = Object.create(Error.prototype);8TestError.prototype.constructor = TestError;9expect(function() {10 throw new TestError('Test Error');11}, 'Test Error', TestError);12expect(function() {13 console.log('no error thrown');14}, 'Test Error', TestError);15expect(function() {16 throw new TestError('Wrong Message');17}, 'Test Error', TestError);18expect(function() {19 throw new Error('Test Error');20}, 'Test Error', TestError);21expect(function() {22 console.log('no error thrown');23}, 'Wrong Message', TestError);24expect(function() {25 console.log('no error thrown');26}, 'Test Error', Error);
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!!