Best JavaScript code snippet using appium-base-driver
events.test.js
Source:events.test.js
1/* eslint-env qunit */2import * as Events from '../../src/js/utils/events.js';3import document from 'global/document';4import log from '../../src/js/utils/log.js';5QUnit.module('Events');6QUnit.test('should add and remove an event listener to an element', function(assert) {7 assert.expect(1);8 const el = document.createElement('div');9 const listener = function() {10 assert.ok(true, 'Click Triggered');11 };12 Events.on(el, 'click', listener);13 // 1 click14 Events.trigger(el, 'click');15 Events.off(el, 'click', listener);16 // No click should happen.17 Events.trigger(el, 'click');18});19QUnit.test('should add and remove multiple event listeners to an element with a single call', function(assert) {20 assert.expect(6);21 const el = document.createElement('div');22 const listener = function() {23 assert.ok(true, 'Callback triggered');24 };25 Events.on(el, ['click', 'event1', 'event2'], listener);26 Events.trigger(el, 'click');27 Events.trigger(el, 'click');28 Events.off(el, 'click', listener);29 // No click should happen.30 Events.trigger(el, 'click');31 Events.trigger(el, 'event1');32 Events.trigger(el, 'event1');33 Events.off(el, 'event1', listener);34 // No event1 should happen.35 Events.trigger(el, 'event1');36 Events.trigger(el, 'event2');37 Events.trigger(el, 'event2');38 Events.off(el, 'event2', listener);39 // No event2 should happen.40 Events.trigger(el, 'event2');41 Events.off(el, ['click', 'event1', 'event2'], listener);42});43QUnit.test('should be possible to pass data when you trigger an event', function(assert) {44 assert.expect(6);45 const el = document.createElement('div');46 const fakeData1 = 'Fake Data 1';47 const fakeData2 = {txt: 'Fake Data 2'};48 const listener = function(evt, hash) {49 assert.ok(true, 'Callback triggered');50 assert.deepEqual(fakeData1, hash.d1, 'Shoulbe be passed to the handler');51 assert.deepEqual(fakeData2, hash.d2, 'Shoulbe be passed to the handler');52 };53 Events.on(el, ['event1', 'event2'], listener);54 Events.trigger(el, 'event1', { d1: fakeData1, d2: fakeData2});55 Events.trigger(el, 'event2', { d1: fakeData1, d2: fakeData2});56 Events.off(el, ['event1', 'event2'], listener);57});58QUnit.test('should remove all listeners of a type', function(assert) {59 const el = document.createElement('div');60 let clicks = 0;61 const listener = function() {62 clicks++;63 };64 const listener2 = function() {65 clicks++;66 };67 Events.on(el, 'click', listener);68 Events.on(el, 'click', listener2);69 // 2 clicks70 Events.trigger(el, 'click');71 assert.ok(clicks === 2, 'both click listeners fired');72 Events.off(el, 'click');73 // No click should happen.74 Events.trigger(el, 'click');75 assert.ok(clicks === 2, 'no click listeners fired');76});77QUnit.test('should remove all listeners of an array of types', function(assert) {78 const el = document.createElement('div');79 let calls = 0;80 const listener = function() {81 calls++;82 };83 const listener2 = function() {84 calls++;85 };86 Events.on(el, ['click', 'event1'], listener);87 Events.on(el, ['click', 'event1'], listener2);88 // 2 calls89 Events.trigger(el, 'click');90 // 2 calls91 Events.trigger(el, 'event1');92 assert.ok(calls === 4, 'both click listeners fired');93 Events.off(el, ['click', 'event1']);94 // No click should happen.95 Events.trigger(el, 'click');96 // No event1 should happen.97 Events.trigger(el, 'event1');98 assert.ok(calls === 4, 'no event listeners fired');99});100QUnit.test('should remove all listeners from an element', function(assert) {101 assert.expect(2);102 const el = document.createElement('div');103 const listener = function() {104 assert.ok(true, 'Fake1 Triggered');105 };106 const listener2 = function() {107 assert.ok(true, 'Fake2 Triggered');108 };109 Events.on(el, 'fake1', listener);110 Events.on(el, 'fake2', listener2);111 Events.trigger(el, 'fake1');112 Events.trigger(el, 'fake2');113 Events.off(el);114 // No listener should happen.115 Events.trigger(el, 'fake1');116 Events.trigger(el, 'fake2');117 Events.off(el, 'fake1', listener);118 Events.off(el, 'fake2', listener2);119});120QUnit.test('should listen only once', function(assert) {121 assert.expect(1);122 const el = document.createElement('div');123 const listener = function() {124 assert.ok(true, 'Click Triggered');125 };126 Events.one(el, 'click', listener);127 // 1 click128 Events.trigger(el, 'click');129 // No click should happen.130 Events.trigger(el, 'click');131});132QUnit.test('should listen only once in multiple events from a single call', function(assert) {133 assert.expect(3);134 const el = document.createElement('div');135 const listener = function() {136 assert.ok(true, 'Callback Triggered');137 };138 Events.one(el, ['click', 'event1', 'event2'], listener);139 // 1 click140 Events.trigger(el, 'click');141 // No click should happen.142 Events.trigger(el, 'click');143 // event1 must be handled.144 Events.trigger(el, 'event1');145 // No event1 should be handled.146 Events.trigger(el, 'event1');147 // event2 must be handled.148 Events.trigger(el, 'event2');149 // No event2 should be handled.150 Events.trigger(el, 'event2');151});152QUnit.test('should stop immediate propagtion', function(assert) {153 assert.expect(1);154 const el = document.createElement('div');155 Events.on(el, 'test', function(e) {156 assert.ok(true, 'First listener fired');157 e.stopImmediatePropagation();158 });159 Events.on(el, 'test', function(e) {160 assert.ok(false, 'Second listener fired');161 });162 Events.trigger(el, 'test');163 Events.off(el, 'test');164});165QUnit.test('should bubble up DOM unless bubbles == false', function(assert) {166 assert.expect(3);167 const outer = document.createElement('div');168 const inner = outer.appendChild(document.createElement('div'));169 // Verify that if bubbles === true, event bubbles up dom.170 Events.on(inner, 'bubbles', function(e) {171 assert.ok(true, 'Inner listener fired');172 });173 Events.on(outer, 'bubbles', function(e) {174 assert.ok(true, 'Outer listener fired');175 });176 Events.trigger(inner, { type: 'bubbles', target: inner, bubbles: true });177 // Only change 'bubbles' to false, and verify only inner handler is called.178 Events.on(inner, 'nobub', function(e) {179 assert.ok(true, 'Inner listener fired');180 });181 Events.on(outer, 'nobub', function(e) {182 assert.ok(false, 'Outer listener fired');183 });184 Events.trigger(inner, { type: 'nobub', target: inner, bubbles: false });185 Events.off(inner, 'bubbles');186 Events.off(outer, 'bubbles');187 Events.off(inner, 'nobub');188 Events.off(outer, 'nobub');189});190QUnit.test('should have a defaultPrevented property on an event that was prevent from doing default action', function(assert) {191 assert.expect(2);192 const el = document.createElement('div');193 Events.on(el, 'test', function(e) {194 assert.ok(true, 'First listener fired');195 e.preventDefault();196 });197 Events.on(el, 'test', function(e) {198 assert.ok(e.defaultPrevented, 'Should have `defaultPrevented` to signify preventDefault being called');199 });200 Events.trigger(el, 'test');201 Events.off(el, 'test');202});203QUnit.test('should have relatedTarget correctly set on the event', function(assert) {204 assert.expect(2);205 const el1 = document.createElement('div');206 const el2 = document.createElement('div');207 const relatedEl = document.createElement('div');208 Events.on(el1, 'click', function(e) {209 assert.equal(e.relatedTarget, relatedEl, 'relatedTarget is set for all browsers when related element is set on the event');210 });211 Events.trigger(el1, { type: 'click', relatedTarget: relatedEl });212 Events.on(el2, 'click', function(e) {213 assert.equal(e.relatedTarget, null, 'relatedTarget is null when none is provided');214 });215 Events.trigger(el2, { type: 'click', relatedTarget: undefined });216 Events.off(el1, 'click');217 Events.off(el2, 'click');218});219QUnit.test('should execute remaining handlers after an exception in an event handler', function(assert) {220 assert.expect(1);221 const oldLogError = log.error;222 log.error = function() {};223 const el = document.createElement('div');224 const listener1 = function() {225 throw new Error('GURU MEDITATION ERROR');226 };227 const listener2 = function() {228 assert.ok(true, 'Click Triggered');229 };230 Events.on(el, 'click', listener1);231 Events.on(el, 'click', listener2);232 // 1 click233 Events.trigger(el, 'click');234 log.error = oldLogError;235 Events.off(el, 'click');236});237QUnit.test('trigger with an object should set the correct target property', function(assert) {238 const el = document.createElement('div');239 Events.on(el, 'click', function(e) {240 assert.equal(e.target, el, 'the event object target should be our element');241 });242 Events.trigger(el, { type: 'click'});243 Events.off(el, 'click');244});245QUnit.test('retrigger with a string should use the new element as target', function(assert) {246 const el1 = document.createElement('div');247 const el2 = document.createElement('div');248 Events.on(el2, 'click', function(e) {249 assert.equal(e.target, el2, 'the event object target should be the new element');250 });251 Events.on(el1, 'click', function(e) {252 Events.trigger(el2, 'click');253 });254 Events.trigger(el1, 'click');255 Events.trigger(el1, {type: 'click'});256 Events.off(el1, 'click');257 Events.off(el2, 'click');258});259QUnit.test('retrigger with an object should use the old element as target', function(assert) {260 const el1 = document.createElement('div');261 const el2 = document.createElement('div');262 Events.on(el2, 'click', function(e) {263 assert.equal(e.target, el1, 'the event object target should be the old element');264 });265 Events.on(el1, 'click', function(e) {266 Events.trigger(el2, e);267 });268 Events.trigger(el1, 'click');269 Events.trigger(el1, {type: 'click'});270 Events.off(el1, 'click');271 Events.off(el2, 'click');272});273QUnit.test('should listen only once for any', function(assert) {274 const el = document.createElement('div');275 let triggered = 0;276 const listener = () => triggered++;277 Events.any(el, 'click', listener);278 assert.equal(triggered, 0, 'listener was not yet triggered');279 // 1 click280 Events.trigger(el, 'click');281 assert.equal(triggered, 1, 'listener was triggered');282 // No click should happen.283 Events.trigger(el, 'click');284 assert.equal(triggered, 1, 'listener was not triggered again');285});286QUnit.test('only the first event should call listener via any', function(assert) {287 const el = document.createElement('div');288 let triggered = 0;289 const listener = () => triggered++;290 Events.any(el, ['click', 'event1', 'event2'], listener);291 assert.equal(triggered, 0, 'listener was not yet triggered');292 // 1 click293 Events.trigger(el, 'click');294 assert.equal(triggered, 1, 'listener was triggered');295 // nothing below here should trigger the Callback296 Events.trigger(el, 'click');297 Events.trigger(el, 'event1');298 Events.trigger(el, 'event1');299 Events.trigger(el, 'event2');300 Events.trigger(el, 'event2');301 assert.equal(triggered, 1, 'listener was not triggered again');...
index.spec.js
Source:index.spec.js
1import spies from 'chai-spies';2import chai, { expect } from 'chai';3import triggerEvent from './triggerEvent.js';4import Events from '../src/index.js';5chai.use(spies);6function injectHTML() {7 document.body.innerHTML = `8 <div id="event-test">9 <ul>10 <li class='green'>hello</li>11 <li class='green'>world</li>12 <li class='red'>again</li>13 <li class='red'>from</li>14 <li class='green'>cam</li>15 </ul>16 </div>17 `;18}19function uninjectHTML() {20 const el = document.querySelector('#event-test');21 el.parentNode.removeChild(el);22}23describe('Events', () => {24 describe('.on and .off', () => {25 beforeEach(() => {26 injectHTML();27 });28 afterEach(() => {29 uninjectHTML();30 });31 it('on should invoke callback when event fired', () => {32 const callback = chai.spy();33 const el = document.querySelector('#event-test ul');34 Events.on(el, 'click', callback);35 triggerEvent(el, 'click');36 expect(callback).to.have.been.called.once;37 });38 it('on bind same callback twice will only invoke once', () => {39 const callback = chai.spy();40 const el = document.querySelector('#event-test ul');41 Events.on(el, 'click', callback);42 Events.on(el, 'click', callback);43 triggerEvent(el, 'click');44 expect(callback).to.have.been.called.once;45 });46 it('on can bind two events', () => {47 const callback1 = chai.spy();48 const callback2 = chai.spy();49 const callback3 = chai.spy();50 const el = document.querySelector('#event-test ul');51 Events.on(el, 'click', callback1);52 Events.on(el, 'click', callback2);53 triggerEvent(el, 'click');54 expect(callback1).to.have.been.called.once;55 expect(callback2).to.have.been.called.once;56 expect(callback3).to.have.not.been.called;57 });58 it('off can remove on event', () => {59 const callback = chai.spy();60 const el = document.querySelector('#event-test ul');61 Events.on(el, 'click', callback);62 Events.off(el, 'click', callback);63 triggerEvent(el, 'click');64 expect(callback).to.have.not.been.called;65 });66 });67 describe('once', () => {68 beforeEach(() => {69 injectHTML();70 });71 afterEach(() => {72 uninjectHTML();73 });74 it('once will only invoke callback once', () => {75 const callback = chai.spy();76 const el = document.querySelector('#event-test ul');77 Events.once(el, 'click', callback);78 triggerEvent(el, 'click');79 triggerEvent(el, 'click');80 expect(callback).to.have.been.called.once;81 });82 it('on will invoke callback many times as you trigger', () => {83 const callback = chai.spy();84 const el = document.querySelector('#event-test ul');85 Events.on(el, 'click', callback);86 triggerEvent(el, 'click');87 triggerEvent(el, 'click');88 triggerEvent(el, 'click');89 expect(callback).to.have.been.called.exactly(3);90 });91 it('off can unbind once', () => {92 const callback = chai.spy();93 const el = document.querySelector('#event-test ul');94 Events.once(el, 'click', callback);95 Events.off(el, 'click');96 triggerEvent(el, 'click');97 expect(callback).to.have.not.been.called;98 });99 });100 describe('with namespace', () => {101 beforeEach(() => {102 injectHTML();103 });104 afterEach(() => {105 uninjectHTML();106 });107 it('on can bind with namespace', () => {108 const callback = chai.spy();109 const el = document.querySelector('#event-test ul');110 Events.on(el, 'click.testns', callback);111 triggerEvent(el, 'click');112 expect(callback).to.have.been.called.once;113 });114 it('off can remove event with namespace', () => {115 const callback = chai.spy();116 const el = document.querySelector('#event-test ul');117 Events.on(el, 'click.testns', callback);118 Events.off(el, 'click', callback);119 triggerEvent(el, 'click');120 expect(callback).to.have.not.been.called;121 });122 it('off with namespace only remove that namespace', () => {123 const callback1 = chai.spy();124 const callback2 = chai.spy();125 const el = document.querySelector('#event-test ul');126 Events.on(el, 'click.testns', callback1);127 Events.on(el, 'click.anotherns', callback2);128 Events.off(el, 'click.anotherns');129 triggerEvent(el, 'click');130 expect(callback1).to.have.been.called.once;131 expect(callback2).to.have.not.been.called;132 });133 it('off without namespace will remove all events', () => {134 const callback1 = chai.spy();135 const callback2 = chai.spy();136 const el = document.querySelector('#event-test ul');137 Events.on(el, 'click.testns', callback1);138 Events.on(el, 'click.anotherns', callback2);139 Events.off(el, 'click');140 triggerEvent(el, 'click');141 expect(callback1).to.have.not.been.called;142 expect(callback2).to.have.not.been.called;143 });144 });145 describe('event delegation', () => {146 beforeEach(() => {147 injectHTML();148 });149 afterEach(() => {150 uninjectHTML();151 });152 it('delegate can bind event', () => {153 const callback = chai.spy();154 const el = document.querySelector('#event-test ul');155 Events.delegate(el, 'li.red', 'click', callback);156 triggerEvent(document.querySelector('#event-test ul li.red'), 'click');157 expect(callback).to.have.been.called.once;158 triggerEvent(document.querySelector('#event-test ul li.green'), 'click');159 expect(callback).to.have.not.been.called;160 });161 it('undelegate can remove event delegation', () => {162 const el = document.querySelector('#event-test ul');163 const callback1 = chai.spy();164 const callback2 = chai.spy(function(event) {165 expect(event.type).to.eq('click');166 });167 Events.delegate(el, 'li.red', 'click', callback1);168 Events.delegate(el, 'li.green', 'click', callback2);169 Events.undelegate(el, 'li.red', 'click');170 triggerEvent(document.querySelector('#event-test ul li.red'), 'click');171 triggerEvent(document.querySelector('#event-test ul li.green'), 'click');172 expect(callback1).to.have.not.been.called;173 expect(callback2).to.have.been.called.once;174 });175 it('off can also remove event delegation', () => {176 const callback = chai.spy();177 const el = document.querySelector('#event-test ul');178 Events.delegate(el, 'li.red', 'click', callback);179 Events.off(el, 'li.red', 'click');180 triggerEvent(el, 'click');181 expect(callback).to.have.not.been.called;182 });183 it('off with namespace only remove that namespace', () => {184 const callback1 = chai.spy();185 const callback2 = chai.spy();186 const el = document.querySelector('#event-test ul');187 Events.delegate(el, 'li.red', 'click.testns', callback1);188 Events.delegate(el, 'li.red', 'click.anotherns', callback2);189 Events.off(el, 'click.anotherns');190 triggerEvent(el.querySelector('li.red'), 'click');191 expect(callback1).to.have.been.called.once;192 expect(callback2).to.have.not.been.called;193 });194 it('off without namespace will remove all events', () => {195 const callback1 = chai.spy();196 const callback2 = chai.spy();197 const el = document.querySelector('#event-test ul');198 Events.delegate(el, 'li.red', 'click.testns', callback1);199 Events.delegate(el, 'li.red', 'click.anotherns', callback2);200 Events.off(el, 'click.anotherns');201 triggerEvent(el.querySelector('li.red'), 'click');202 expect(callback1).to.have.not.been.called;203 expect(callback2).to.have.not.been.called;204 });205 });206 describe('trigger', () => {207 beforeEach(() => {208 injectHTML();209 });210 afterEach(() => {211 uninjectHTML();212 });213 it('can trigger events', () => {214 const callback = chai.spy();215 const el = document.querySelector('#event-test ul');216 Events.on(el, 'click', callback);217 Events.trigger(el, 'click');218 expect(callback).to.have.been.called.once;219 });220 it('can trigger scroll events', () => {221 const callback = chai.spy();222 const el = document.querySelector('#event-test ul');223 Events.on(el, 'scroll', callback);224 Events.trigger(el, 'scroll');225 expect(callback).to.have.been.called.once;226 });227 it('can trigger resize events', () => {228 const callback = chai.spy();229 const el = document.querySelector('#event-test ul');230 Events.on(el, 'resize', callback);231 Events.trigger(el, 'resize');232 expect(callback).to.have.been.called.once;233 });234 it('can trigger with params', () => {235 const callback = chai.spy();236 const el = document.querySelector('#event-test ul');237 Events.on(el, 'resize', callback);238 Events.trigger(el, 'resize', {foo: 'bar'});239 expect(callback).to.have.been.called.once;240 });241 });...
remove-test.js
Source:remove-test.js
1/*global bean:true, buster:true, Syn:true, assert:true, defer:true, features:true, globalSetUp:true, globalTearDown:true*/2buster.testCase('remove', {3 'setUp': globalSetUp4 , 'tearDown': globalTearDown5 , 'should return the element passed in': function () {6 var el = this.byId('foo')7 , handler = function () {}8 , returned9 bean.add(el, 'click', handler)10 returned = bean.remove(el, 'click', handler)11 assert.same(el, returned, 'returns the element passed in')12 }13 , 'should be able to remove a single event': function (done) {14 var el = this.byId('foo')15 , calls = 016 , trigger = this.trigger()17 , handler = trigger.wrap(function () {18 calls++19 bean.remove(el, 'click', handler)20 Syn.click(el)21 })22 trigger.after(function () {23 assert.equals(calls, 1, 'remove a single event')24 done()25 }, 50)26 bean.add(el, 'click', handler)27 Syn.click(el)28 }29 , 'should be able to remove mulitple events with an object literal': function (done) {30 var el = this.byId('input')31 , calls = 032 , trigger = this.trigger()33 , handler1 = function () {34 calls++35 bean.remove(el, {36 click : trigger.wrapped(handler1)37 , keydown : trigger.wrapped(handler2)38 })39 Syn.click(el)40 Syn.key('j', el)41 }42 , handler2 = this.spy()43 , handler3 = this.spy()44 trigger.after(function () {45 assert.equals(calls, 1, 'remove a events with object literal')46 refute(handler2.called, 'correct handler properly removed')47 assert.equals(handler3.callCount, 1, 'non-matching handler should not be removed')48 done()49 }, 50)50 bean.add(el, 'click' , trigger.wrap(handler1))51 bean.add(el, 'keydown', trigger.wrap(handler2))52 bean.add(el, 'keydown', trigger.wrap(handler3))53 Syn.click(el)54 }55 , 'should be able to remove all events of a specific type': function (done) {56 var el = this.byId('input')57 , calls = 058 , trigger = this.trigger()59 , handler1 = this.spy()60 , handler2 = function () {61 calls++62 bean.remove(el, 'click')63 Syn.click(el)64 }65 trigger.after(function () {66 assert.equals(calls, 1, 'removes all events of a specific type')67 assert.equals(handler1.callCount, 1, 'removes all events of a specific type')68 done()69 }, 50)70 bean.add(el, 'click', trigger.wrap(handler1))71 bean.add(el, 'click', trigger.wrap(handler2))72 Syn.click(el)73 }74 , 'should be able to remove all events of a specific type (multiple)': function (done) {75 var el = this.byId('input')76 , calls = 077 , trigger = this.trigger()78 , handler1 = this.spy()79 , handler2 = function () {80 calls++81 bean.remove(el, 'mousedown mouseup')82 Syn.click(el)83 }84 trigger.after(function () {85 assert.equals(calls, 1, 'removes all events of a specific type')86 assert.equals(handler1.callCount, 1, 'removes all events of a specific type')87 done()88 }, 50)89 bean.add(el, 'mousedown', trigger.wrap(handler1))90 bean.add(el, 'mouseup', trigger.wrap(handler2))91 Syn.click(el)92 }93 , 'should be able to remove all events': function (done) {94 var el = this.byId('input')95 , calls = 096 , trigger = this.trigger()97 , handler1 = function () {98 calls++99 bean.remove(el)100 Syn.click(el)101 Syn.key('j', el)102 }103 , handler2 = this.spy()104 trigger.after(function () {105 assert.equals(calls, 1, 'removes all events')106 assert.equals(handler2.callCount, 0, 'removes all events')107 done()108 }, 50)109 bean.add(el, 'click', trigger.wrap(handler1))110 bean.add(el, 'keydown', trigger.wrap(handler2))111 112 Syn.click(el)113 }114 , 'should only remove events of specified type': function (done) {115 // testing that bean.remove(el, type) removes *only* of that type and no others116 var el = this.byId('input')117 , calls = 0118 , trigger = this.trigger()119 , handler1 = this.spy()120 , handler2 = function (e) {121 calls++122 bean.remove(el, e.type)123 }124 trigger.after(function () {125 assert.equals(calls, 2, 'removes all events of a specific type')126 assert.equals(handler1.callCount, 2, 'removes all events of a specific type')127 done()128 }, 50)129 bean.add(el, 'click', trigger.wrap(handler1))130 bean.add(el, 'keyup', trigger.wrap(handler1))131 bean.add(el, 'click', trigger.wrap(handler2))132 bean.add(el, 'keyup', trigger.wrap(handler2))133 Syn.click(el)134 Syn.key(el, 'f')135 Syn.click(el)136 Syn.key(el, 'f')137 }138 , 'should only remove events for specified handler': function (done) {139 // testing that bean.remove(el, fn) removes *only* that handler and no others140 var el = this.byId('input')141 , trigger = this.trigger()142 , handler1 = this.spy()143 , handler2 = this.spy()144 trigger.after(function () {145 assert.equals(handler1.callCount, 0, 'removes all events of a specific handler')146 assert.equals(handler2.callCount, 2, 'removes all events of a specific handler')147 done()148 }, 50)149 bean.add(el, 'click', trigger.wrap(handler1))150 bean.add(el, 'keyup', trigger.wrap(handler1))151 bean.add(el, 'click', trigger.wrap(handler2))152 bean.add(el, 'keyup', trigger.wrap(handler2))153 bean.remove(el, trigger.wrapped(handler1))154 Syn.click(el)155 Syn.key(el, 'f')156 }157 , 'should remove all events, including namespaced': function (done) {158 // testing that bean.remove(el, fn) removes *only* that handler and no others159 var el = this.byId('input')160 , handler1 = this.spy()161 , handler2 = this.spy()162 bean.add(el, 'click.foo', handler1)163 bean.add(el, 'click', handler1)164 bean.add(el, 'keyup.bar', handler2)165 bean.add(el, 'keyup', handler2)166 bean.remove(el)167 Syn.click(el)168 Syn.key(el, 'f')169 defer(function () {170 assert.equals(handler1.callCount, 0, 'removes all events')171 assert.equals(handler2.callCount, 0, 'removes all events')172 done()173 }, 100)174 }175 , 'should be able to remove all events of a certain namespace': function (done) {176 var el = this.byId('input')177 , calls = 0178 , trigger = this.trigger()179 , handler1 = function () {180 calls++181 bean.remove(el, '.foo')182 Syn.click(el)183 Syn.key('j', el)184 }185 , handler2 = this.spy()186 , handler3 = this.spy()187 trigger.after(function () {188 assert.equals(calls, 1, 'removes all events of a certain namespace')189 assert.equals(handler2.callCount, 0, 'removes all events of a certain namespace')190 assert.equals(handler3.callCount, 2, 'removes all events of a certain namespace')191 done()192 }, 50)193 bean.add(el, 'click.foo', trigger.wrap(handler1))194 bean.add(el, 'keydown.foo', trigger.wrap(handler2))195 bean.add(el, 'click.bar', trigger.wrap(handler3))196 Syn.click(el)197 }198 , 'should only remove event if the remove namespaces is within the event namespace or if the event namespace is within the remove namespace': function (done) {199 var el = this.byId('foo')200 , trigger = this.trigger()201 , spy = this.spy()202 trigger.after(function () {203 assert.equals(spy.callCount, 4, 'calls on appropriate namespaces')204 done()205 })206 bean.remove(el)207 bean.add(el, 'fat.test1.foo.ded fat.test2.foo fat.test1.foo', trigger.wrap(spy))208 bean.fire(el, 'fat.test1.ded', ['1'])209 bean.fire(el, 'fat.test2', ['2'])210 bean.remove(el, '.foo.ded')211 bean.fire(el, 'fat.foo', ['3'])212 }...
events.js
Source:events.js
1var tape = require("tape");2var on = require("../../lib/events/on");3var off = require("../../lib/events/off");4var trigger = require("../../lib/events/trigger");5var el = document.createElement("div");6var el2 = document.createElement("span");7var els = [el, el2];8var classCb = function() {9 this.className += "on";10};11var attrCb = function() {12 this.setAttribute("data-state", this.getAttribute("data-state") + "ON");13};14tape("test events on/off/trigger for one element, one event", function(t) {15 el.className = "";16 on(el, "click", classCb);17 trigger(el, "click");18 t.equal(el.className, "on", "attached callback has been fired properly");19 el.className = "off";20 off(el, "click", classCb);21 trigger(el, "click");22 t.equal(el.className, "off", "triggered event didn't fire detached callback");23 t.end();24});25tape("test events on/off/trigger for multiple elements, one event", function(26 t27) {28 el.className = "";29 el2.className = "";30 on(els, "click", classCb);31 trigger(els, "click");32 t.equal(33 el.className,34 "on",35 "attached callback has been fired properly on the first element"36 );37 t.equal(38 el2.className,39 "on",40 "attached callback has been fired properly on the second element"41 );42 el.className = "off";43 el2.className = "off";44 off(els, "click", classCb);45 trigger(els, "click");46 t.equal(47 el.className,48 "off",49 "triggered event didn't fire detached callback on the first element"50 );51 t.equal(52 el2.className,53 "off",54 "triggered event didn't fire detached callback on the second element"55 );56 t.end();57});58tape("test events on/off/trigger for one element, multiple events", function(59 t60) {61 el.className = "";62 on(el, "click mouseover", classCb);63 trigger(el, "click mouseover");64 t.equal(el.className, "onon", "attached callbacks have been fired properly");65 el.className = "off";66 off(el, "click mouseover", classCb);67 trigger(el, "click mouseover");68 t.equal(69 el.className,70 "off",71 "triggered events didn't fire detached callback"72 );73 t.end();74});75tape(76 "test events on/off/trigger for multiple elements, multiple events",77 function(t) {78 el.className = "";79 el2.className = "";80 el.setAttribute("data-state", "");81 el2.setAttribute("data-state", "");82 on(els, "click mouseover", classCb);83 on(els, "resize scroll", attrCb);84 trigger(els, "click mouseover resize scroll");85 t.equal(86 el.className,87 "onon",88 "attached callbacks has been fired properly on the first element"89 );90 t.equal(91 el.getAttribute("data-state"),92 "ONON",93 "attached callbacks has been fired properly on the first element"94 );95 t.equal(96 el2.className,97 "onon",98 "attached callbacks has been fired properly on the second element"99 );100 t.equal(101 el2.getAttribute("data-state"),102 "ONON",103 "attached callbacks has been fired properly on the second element"104 );105 el.className = "off";106 el2.className = "off";107 el.setAttribute("data-state", "off");108 el2.setAttribute("data-state", "off");109 off(els, "click mouseover", classCb);110 off(els, "resize scroll", attrCb);111 trigger(els, "click mouseover resize scroll");112 t.equal(113 el.className,114 "off",115 "triggered events didn't fire detached callbacks on the first element"116 );117 t.equal(118 el.getAttribute("data-state"),119 "off",120 "triggered events didn't fire detached callbacks on the first element"121 );122 t.equal(123 el2.className,124 "off",125 "triggered events didn't fire detached callbacks on the first element"126 );127 t.equal(128 el2.getAttribute("data-state"),129 "off",130 "triggered events didn't fire detached callbacks on the first element"131 );132 t.end();133 }134);135tape("test events on top level elements", function(t) {136 var el = document;137 el.className = "";138 on(el, "click", classCb);139 trigger(el, "click");140 t.equal(141 el.className,142 "on",143 "attached callback has been fired properly on document"144 );145 el = window;146 el.className = "";147 // With jsdom, the default this is global, not window, so we need to explicitly bind to window.148 on(el, "click", classCb.bind(window));149 trigger(el, "click");150 t.equal(151 el.className,152 "on",153 "attached callback has been fired properly on window"154 );155 t.end();...
demo.js
Source:demo.js
...20 // Run tests21 await page.goto(`${URL}/signup`);22 await percySnapshot(page, 'Signup Page', {widths: TEST_WIDTHS});23 await page.$eval('#signup-email', el => el.setAttribute('value',"test@percy.io"));24 await page.$eval('#signup-submit', el => el.click());25 await percySnapshot(page, 'Signup Page Validation', {widths: TEST_WIDTHS});26 await page.goto(`${URL}/login`);27 await percySnapshot(page, 'Login Page', {widths: TEST_WIDTHS});28 await page.$eval('#login-email', el => el.setAttribute('value',"test-login@percy.io"));29 await page.$eval('#login-submit', el => el.click());30 await percySnapshot(page, 'Login Page Validation', {widths: TEST_WIDTHS});31 await page.goto(`${URL}/`);32 await percySnapshot(page, 'Index Page', {widths: TEST_WIDTHS});33 await page.$eval('#menu-toggle', el => el.click());34 await percySnapshot(page, 'Index Page Menu Closed', {widths: TEST_WIDTHS,percyCSS:'.card-body { visibility: hidden; }'});35 // reset menu36 await page.$eval('#menu-toggle', el => el.click());37 await page.$eval('#messages-toggle', el => el.click());38 await page.$eval('#todo-toggle', el => el.click());39 await page.$eval('#calendar-toggle', el => el.click());40 await percySnapshot(page, 'Index Top Row Collapsed', {widths: TEST_WIDTHS,percyCSS:'.card-body { visibility: hidden; }'});41 // reset page42 await page.$eval('#messages-toggle', el => el.click());43 await page.$eval('#todo-toggle', el => el.click());44 await page.$eval('#calendar-toggle', el => el.click());45 await page.click('#nav-profile-menu');46 await percySnapshot(page, 'Profile Menu', {widths: TEST_WIDTHS,percyCSS:'.card-body { visibility: hidden; }'});47 // reset Page48 await page.click('#nav-profile-menu');49 await page.click('#nav-messages-menu');50 await percySnapshot(page, 'Messages Menu', {widths: TEST_WIDTHS,percyCSS:'.card-body { visibility: hidden; }'});51 // reset page52 await page.click('#nav-messages-menu');53 await page.click('#nav-tasks-menu');54 await percySnapshot(page, 'Tasks Menu', {widths: TEST_WIDTHS});55 await page.goto(`${URL}/404_error`);56 await percySnapshot(page, '404 Page', {widths: TEST_WIDTHS});57 await page.goto(`${URL}/500_error`);58 await percySnapshot(page, '500 Page', {widths: TEST_WIDTHS});...
DomEventSpec.js
Source:DomEventSpec.js
1describe('DomEvent', function () {2 var el;3 function simulateClick(el) {4 if (document.createEvent) {5 var e = document.createEvent('MouseEvents');6 e.initMouseEvent('click', true, true, window,7 0, 0, 0, 0, 0, false, false, false, false, 0, null);8 return el.dispatchEvent(e);9 } else if (el.fireEvent) {10 return el.fireEvent('onclick');11 }12 }13 beforeEach(function () {14 el = document.createElement('div');15 el.style.position = 'absolute';16 el.style.top = el.style.left = '-10000px';17 document.body.appendChild(el);18 });19 afterEach(function () {20 document.body.removeChild(el);21 });22 describe('#addListener', function () {23 it('adds a listener and calls it on event', function () {24 var listener1 = sinon.spy(),25 listener2 = sinon.spy();26 L.DomEvent.addListener(el, 'click', listener1);27 L.DomEvent.addListener(el, 'click', listener2);28 simulateClick(el);29 expect(listener1.called).to.be.ok();30 expect(listener2.called).to.be.ok();31 });32 it('binds "this" to the given context', function () {33 var obj = {foo: 'bar'},34 result;35 L.DomEvent.addListener(el, 'click', function () {36 result = this;37 }, obj);38 simulateClick(el);39 expect(result).to.eql(obj);40 });41 it('passes an event object to the listener', function () {42 var type;43 L.DomEvent.addListener(el, 'click', function (e) {44 type = e && e.type;45 });46 simulateClick(el);47 expect(type).to.eql('click');48 });49 it('is chainable', function () {50 var res = L.DomEvent.addListener(el, 'click', function () {});51 expect(res.addListener).to.be.a('function');52 });53 });54 describe('#removeListener', function () {55 it('removes a previously added listener', function () {56 var listener = sinon.spy();57 L.DomEvent.addListener(el, 'click', listener);58 L.DomEvent.removeListener(el, 'click', listener);59 simulateClick(el);60 expect(listener.called).to.not.be.ok();61 });62 it('is chainable', function () {63 var res = L.DomEvent.removeListener(el, 'click', function () {});64 expect(res.removeListener).to.be.a('function');65 });66 });67 describe('#stopPropagation', function () {68 it('stops propagation of the given event', function () {69 var child = document.createElement('div'),70 listener = sinon.spy();71 el.appendChild(child);72 L.DomEvent.addListener(child, 'click', L.DomEvent.stopPropagation);73 L.DomEvent.addListener(el, 'click', listener);74 simulateClick(child);75 expect(listener.called).to.not.be.ok();76 el.removeChild(child);77 });78 });79 describe('#preventDefault', function () {80 it('prevents the default action of event', function () {81 L.DomEvent.addListener(el, 'click', L.DomEvent.preventDefault);82 expect(simulateClick(el)).to.be(false);83 });84 });...
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2var By = webdriver.By;3var until = webdriver.until;4var driver = new webdriver.Builder()5 .forBrowser('chrome')6 .build();7driver.findElement(By.name('q')).sendKeys('webdriver');8driver.findElement(By.name('btnG')).click();9driver.wait(until.titleIs('webdriver - Google Search'), 1000);10driver.quit();11var webdriver = require('selenium-webdriver');12var By = webdriver.By;13var until = webdriver.until;14var driver = new webdriver.Builder()
Using AI Code Generation
1var webdriver = require('selenium-webdriver');2driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');3driver.findElement(webdriver.By.name('btnG')).click();4driver.wait(function() {5 return driver.getTitle().then(function(title) {6 return title === 'webdriver - Google Search';7 });8}, 1000);9driver.quit();
Using AI Code Generation
1describe('Test', function() {2 it('should click on element', function() {3 var el = element(by.id('btn1'));4 el.click();5 });6});7describe('Test', function() {8 it('should click on element', function() {9 var el = element(by.id('btn1'));10 browser.driver.executeScript('mobile: click', {element: el});11 });12});13describe('Test', function() {14 it('should click on element', function() {15 var el = element(by.id('btn1'));16 browser.touchActions().tap(el).perform();17 });18});19describe('Test', function() {20 it('should click on element', function() {21 var el = element(by.id('btn1'));22 browser.driver.touchActions().tap(el).perform();23 });24});25describe('Test', function() {26 it('should click on element', function() {27 var el = element(by.id('btn1'));28 browser.actions().click(el).perform();29 });30});31describe('Test', function() {32 it('should click on element', function() {33 var el = element(by.id('btn1'));34 browser.driver.actions().click(el).perform();35 });36});37I have also tried using the browser.actions().mouseMove(el).click().perform() method. It does not work either. I have tried using the following code:38describe('Test', function() {39 it('should click on element', function() {
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!!