Best JavaScript code snippet using appium
common.spec.js
Source:common.spec.js
1/*jshint node: true, jasmine: true, browser: true */2/*global ContactFindOptions, ContactName, Q*/3/*4 *5 * Licensed to the Apache Software Foundation (ASF) under one6 * or more contributor license agreements. See the NOTICE file7 * distributed with this work for additional information8 * regarding copyright ownership. The ASF licenses this file9 * to you under the Apache License, Version 2.0 (the10 * "License"); you may not use this file except in compliance11 * with the License. You may obtain a copy of the License at12 *13 * http://www.apache.org/licenses/LICENSE-2.014 *15 * Unless required by applicable law or agreed to in writing,16 * software distributed under the License is distributed on an17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY18 * KIND, either express or implied. See the License for the19 * specific language governing permissions and limitations20 * under the License.21 *22*/23// these tests are meant to be executed by Cordova Medic Appium runner24// you can find it here: https://github.com/apache/cordova-medic/25// it is not necessary to do a full CI setup to run these tests, just run:26// node cordova-medic/medic/medic.js appium --platform android --plugins cordova-plugin-contacts27'use strict';28var wdHelper = global.WD_HELPER;29var screenshotHelper = global.SCREENSHOT_HELPER;30var contactsHelper = require('../helpers/contactsHelper');31var MINUTE = 60 * 1000;32var PLATFORM = global.PLATFORM;33var UNORM = global.UNORM;34describe('Contacts Android', function () {35 var driver;36 var webviewContext;37 var promiseCount = 0;38 // going to set this to false if session is created successfully39 var failedToStart = true;40 function getNextPromiseId() {41 return 'appium_promise_' + promiseCount++;42 }43 function saveScreenshotAndFail(error) {44 fail(error);45 return screenshotHelper46 .saveScreenshot(driver)47 .quit()48 .then(function () {49 return getDriver();50 });51 }52 function getDriver() {53 driver = wdHelper.getDriver(PLATFORM);54 return driver.getWebviewContext()55 .then(function(context) {56 webviewContext = context;57 return driver.context(webviewContext);58 })59 .waitForDeviceReady()60 .injectLibraries();61 }62 function addContact(firstName, lastName, bday) {63 var bdayString = bday ? bday.toDateString() : undefined;64 var contactName = contactsHelper.getContactName(firstName, lastName);65 return driver66 .context(webviewContext)67 .setAsyncScriptTimeout(MINUTE)68 .executeAsync(function(contactname, bday, callback) {69 navigator.contacts.create({70 'displayName': contactname.formatted,71 'name': contactname,72 'note': 'DeleteMe',73 'birthday': new Date(bday)74 }).save(callback, callback);75 }, [contactName, bdayString])76 .then(function(result) {77 if (result && result.hasOwnProperty('code')) {78 throw result;79 }80 return result;81 });82 }83 function pickContact(name) {84 var promiseId = getNextPromiseId();85 return driver86 .context(webviewContext)87 .execute(function (pID) {88 navigator._appiumPromises[pID] = Q.defer();89 navigator.contacts.pickContact(function (contact) {90 navigator._appiumPromises[pID].resolve(contact);91 }, function (err) {92 navigator._appiumPromises[pID].reject(err);93 });94 }, [promiseId])95 .context('NATIVE_APP')96 .then(function () {97 switch (PLATFORM) {98 case 'ios':99 return driver100 .waitForElementByXPath(UNORM.nfd('//UIAStaticText[@label="' + name + '"]'), 20000)101 .elementByXPath(UNORM.nfd('//UIAStaticText[@label="' + name + '"]'))102 .elementByXPath(UNORM.nfd('//UIAStaticText[@label="' + name + '"]'));103 case 'android':104 return driver105 .waitForElementByXPath('//android.widget.TextView[@text="' + name + '"]', MINUTE);106 }107 })108 .click()109 .context(webviewContext)110 .executeAsync(function (pID, cb) {111 navigator._appiumPromises[pID].promise112 .then(function (contact) {113 // for some reason Appium cannot get Date object114 // let's make birthday a string then115 contact.birthday = contact.birthday.toDateString();116 cb(contact);117 }, function (err) {118 cb('ERROR: ' + err);119 });120 }, [promiseId])121 .then(function (result) {122 if (typeof result === 'string' && result.indexOf('ERROR:') === 0) {123 throw result;124 }125 return result;126 });127 }128 function renameContact(oldName, newGivenName, newFamilyName) {129 return driver130 .context(webviewContext)131 .setAsyncScriptTimeout(7 * MINUTE)132 .executeAsync(function (oldname, newgivenname, newfamilyname, callback) {133 var obj = new ContactFindOptions();134 obj.filter = oldname;135 obj.multiple = false;136 navigator.contacts.find(['displayName', 'name'], function(contacts) {137 if (contacts.length === 0) {138 callback({ 'code': -35142 });139 return;140 }141 var contact = contacts[0];142 contact.displayName = newgivenname + ' ' + newfamilyname;143 var name = new ContactName();144 name.givenName = newgivenname;145 name.familyName = newfamilyname;146 contact.name = name;147 contact.save(callback, callback);148 }, callback, obj);149 }, [oldName, newGivenName, newFamilyName])150 .then(function(result) {151 if (result && result.hasOwnProperty('code')) {152 if (result.code === -35142) {153 throw 'Couldn\'t find the contact "' + oldName + '"';154 }155 throw result;156 }157 return result;158 });159 }160 function removeTestContacts() {161 return driver162 .context(webviewContext)163 .setAsyncScriptTimeout(MINUTE)164 .executeAsync(function (callback) {165 var obj = new ContactFindOptions();166 obj.filter = 'DeleteMe';167 obj.multiple = true;168 navigator.contacts.find(['note'], function(contacts) {169 var removes = [];170 contacts.forEach(function(contact) {171 removes.push(contact);172 });173 if (removes.length === 0) {174 return;175 }176 var nextToRemove;177 if (removes.length > 0) {178 nextToRemove = removes.shift();179 }180 function removeNext(item) {181 if (typeof item === 'undefined') {182 callback();183 return;184 }185 if (removes.length > 0) {186 nextToRemove = removes.shift();187 } else {188 nextToRemove = undefined;189 }190 item.remove(function removeSucceeded() {191 removeNext(nextToRemove);192 }, function removeFailed() {193 removeNext(nextToRemove);194 });195 }196 removeNext(nextToRemove);197 }, callback, obj);198 }, [])199 .then(function(result) {200 if (typeof result !== 'undefined') {201 throw result;202 }203 });204 }205 function checkSession(done) {206 if (failedToStart) {207 fail('Failed to start a session');208 done();209 }210 }211 it('contacts.ui.util configuring driver and starting a session', function (done) {212 getDriver()213 .then(function () {214 failedToStart = false;215 }, fail)216 .done(done);217 }, 10 * MINUTE);218 describe('Picking contacts', function () {219 afterEach(function (done) {220 checkSession(done);221 removeTestContacts()222 .finally(done);223 }, MINUTE);224 it('contacts.ui.spec.1 Pick a contact', function (done) {225 checkSession(done);226 var bday = new Date(1991, 1, 1);227 driver228 .then(function () {229 return addContact('Test', 'Contact', bday);230 })231 .then(function () {232 return pickContact('Test Contact');233 })234 .then(function (contact) {235 expect(contact.name.givenName).toBe('Test');236 expect(contact.name.familyName).toBe('Contact');237 expect(contact.birthday).toBe(bday.toDateString());238 })239 .fail(saveScreenshotAndFail)240 .done(done);241 }, 5 * MINUTE);242 it('contacts.ui.spec.2 Update an existing contact', function (done) {243 checkSession(done);244 driver245 .then(function () {246 return addContact('Dooney', 'Evans');247 })248 .then(function () {249 return renameContact('Dooney Evans', 'Urist', 'McContact');250 })251 .then(function (contact) {252 expect(contact.name.givenName).toBe('Urist');253 expect(contact.name.familyName).toBe('McContact');254 })255 .then(function () {256 return pickContact('Urist McContact');257 })258 .then(function (contact) {259 expect(contact.name.givenName).toBe('Urist');260 expect(contact.name.familyName).toBe('McContact');261 })262 .fail(saveScreenshotAndFail)263 .done(done);264 }, 10 * MINUTE);265 it('contacts.ui.spec.3 Create a contact with no name', function (done) {266 checkSession(done);267 driver268 .then(function () {269 return addContact();270 })271 .then(function () {272 switch (PLATFORM) {273 case 'android':274 return pickContact('(No name)');275 case 'ios':276 return pickContact('No Name');277 }278 })279 .then(function (contact) {280 if (contact.name) {281 expect(contact.name.givenName).toBeFalsy();282 expect(contact.name.middleName).toBeFalsy();283 expect(contact.name.familyName).toBeFalsy();284 expect(contact.name.formatted).toBeFalsy();285 } else {286 expect(contact.name).toBeFalsy();287 }288 })289 .fail(saveScreenshotAndFail)290 .done(done);291 }, 5 * MINUTE);292 it('contacts.ui.spec.4 Create a contact with Unicode characters in name', function (done) {293 checkSession(done);294 driver295 .then(function () {296 return addContact('Ðâ¬Ð¹ÑомонаÑ
', 'ФеоÑаÐ');297 })298 .then(function () {299 return pickContact('Ðâ¬Ð¹ÑомонаÑ
ФеоÑаÐ');300 })301 .then(function (contact) {302 expect(contact.name.givenName).toBe('Ðâ¬Ð¹ÑомонаÑ
');303 expect(contact.name.familyName).toBe('ФеоÑаÐ');304 })305 .fail(saveScreenshotAndFail)306 .done(done);307 }, 5 * MINUTE);308 });309 it('contacts.ui.util Destroy the session', function (done) {310 checkSession(done);311 driver312 .quit()313 .done(done);314 }, 5 * MINUTE);...
appliance.js
Source:appliance.js
...17 result = true18 }19 })20 } else {21 const normInp = unorm.nfd(token.word)22 template.cht.forEach(word => {23 const normTemp = unorm.nfd(word)24 if (normInp.includes(normTemp)) {25 result = true26 }27 })28 }29 })30 return result31}32const templateAll = {33 cht: ['å
¨', 'é½'],34 eng: ['all', 'all'],35 thre: 0.936}37const templateLight = {...
benchmark.js
Source:benchmark.js
...32 benchmarkFunction(name + ": nfc -> nfc", function (unorm) {33 unorm.nfc(nfc);34 });35 benchmarkFunction(name + ": nfc -> nfd", function (unorm) {36 unorm.nfd(nfc);37 });38 benchmarkFunction(name + ": nfd -> nfc", function (unorm) {39 unorm.nfc(nfd);40 });41 benchmarkFunction(name + ": nfd -> nfd", function (unorm) {42 unorm.nfd(nfd);43 });44}45runBenchmarks("kalevala finnish", kalevalaFinnish);...
unorm.sample.js
Source:unorm.sample.js
...6 + '10\u207B\u00B9\u2070 m.';7const combining = /[\u0300-\u036F]/g; // Use XRegExp('\\p{M}', 'g'); see example.js.8console.log(`Regular: ${text}`);9console.log(`NFC: ${unorm.nfc(text)}`);10console.log(`NFD: ${unorm.nfd(text)}`);11console.log(`NFKC: ${unorm.nfkc(text)}`);12console.log(`NFKD: * ${unorm.nfkd(text).replace(combining, '')}`);13console.log(' * = Combining characters removed from decomposed form.');14const orgText = 'íê¸1';15const breakedText = unorm.nfd(orgText);16console.log(`Regular: ${orgText}`);17console.log(`NFD: ${breakedText}`);18console.log(`NFC: ${unorm.nfc(breakedText)}`);...
index.js
Source:index.js
1(function(){2 var unorm;3 unorm = require('unorm');4 function toFullwidth(it){5 return unorm.nfd(it + "").replace(/ /g, '\u3000').replace(/[\u0020-\u007E]/g, function(it){6 return String.fromCharCode(it.charCodeAt(0) + 0xFEE0);7 });8 }9 function toFullwidthNFC(it){10 return unorm.nfc(toFullwidth(it));11 }12 function toHalfwidth(it){13 return unorm.nfd(it + "").replace(/\u3000/g, ' ').replace(/[\uFF01-\uFF5E]/g, function(it){14 return String.fromCharCode(it.charCodeAt(0) - 0xFEE0);15 });16 }17 function toHalfwidthNFC(it){18 return unorm.nfc(toHalfwidth(it));19 }20 module.exports = function(str, cb){21 return cb(toFullwidth(str));22 };23 module.exports.toFullwidth = toFullwidthNFC;24 module.exports.toHalfwidth = toHalfwidthNFC;25 module.exports.toFullwidthNFC = toFullwidthNFC;26 module.exports.toHalfwidthNFC = toFullwidthNFC;27}).call(this);
keyword.helper.js
Source:keyword.helper.js
...8 result = true9 }10 })11 } else {12 const normInp = unorm.nfd(token.word)13 template.cht.forEach(word => {14 const normTemp = unorm.nfd(word)15 if (normInp.includes(normTemp)) {16 result = true17 }18 })19 }20 })21 return result...
Normalize.js
Source:Normalize.js
1"use strict";2var unorm = require('unorm');3exports.nfd = function nfd (x) {4 return unorm.nfd(x);5};6exports.nfc = function nfc (x) {7 return unorm.nfc(x);8};9exports.nfkd = function nfkd (x) {10 return unorm.nfkd(x);11};12exports.nfkc = function nfkc (x) {13 return unorm.nfkc(x);...
unaccented.js
Source:unaccented.js
2module.exports = function(str) {3 var composed = unorm.nfc(str).toLowerCase()4 var unaccented = []5 composed.split('').forEach(function(char) {6 unaccented.push(unorm.nfd(char)[0])7 })8 return unaccented.join('')...
Using AI Code Generation
1var unorm = require('unorm');2var str = "é";3console.log(unorm.nfd(str));4var unorm = require('unorm');5var str = "é";6console.log(unorm.nfd(str));7var unorm = require('unorm');8var str = "é";9console.log(unorm.nfd(str));10var unorm = require('unorm');11var str = "é";12console.log(unorm.nfd(str));13var unorm = require('unorm');14var str = "é";15console.log(unorm.nfd(str));16var unorm = require('unorm');17var str = "é";18console.log(unorm.nfd(str));19var unorm = require('unorm');20var str = "é";21console.log(unorm.nfd(str));22var unorm = require('unorm');23var str = "é";24console.log(unorm.nfd(str));25var unorm = require('unorm');26var str = "é";27console.log(unorm.nfd(str));28var unorm = require('unorm');29var str = "é";30console.log(unorm.nfd(str));31var unorm = require('unorm');32var str = "é";33console.log(unorm.nfd(str));34var unorm = require('unorm');35var str = "é";36console.log(unorm.nfd(str));37var unorm = require('unorm');38var str = "é";39console.log(unorm.nfd(str));40var unorm = require('unorm');41var str = "é";42console.log(unorm.nfd(str));
Using AI Code Generation
1var unorm = require('unorm');2var str = "NFD";3var str1 = "NFC";4var str2 = "NFKD";5var str3 = "NFKC";6console.log(unorm.nfd(str));7console.log(unorm.nfc(str1));8console.log(unorm.nfkd(str2));9console.log(unorm.nfkc(str3));10var unorm = require('unorm');11var str = "NFD";12var str1 = "NFC";13var str2 = "NFKD";14var str3 = "NFKC";15console.log(unorm.nfd(str));16console.log(unorm.nfc(str1));17console.log(unorm.nfkd(str2));18console.log(unorm.nfkc(str3));19var unorm = require('unorm');20var str = "NFD";21var str1 = "NFC";22var str2 = "NFKD";23var str3 = "NFKC";24console.log(unorm.nfd(str));25console.log(unorm.nfc(str1));26console.log(unorm.nfkd(str2));27console.log(unorm.nfkc(str3));28var unorm = require('unorm');29var str = "NFD";30var str1 = "NFC";31var str2 = "NFKD";32var str3 = "NFKC";33console.log(unorm.nfd(str));34console.log(unorm.nfc(str1));35console.log(unorm.nfkd(str2));36console.log(unorm.nfkc(str3));37var unorm = require('unorm');38var str = "NFD";39var str1 = "NFC";40var str2 = "NFKD";41var str3 = "NFKC";42console.log(unorm.nfd(str));43console.log(unorm.nfc(str1));44console.log(unorm.nfkd(str2));45console.log(unorm.nfkc(str3));46var unorm = require('unorm');47var str = "NFD";48var str1 = "NFC";49var str2 = "NFKD";
Using AI Code Generation
1var unorm = require('unorm');2var text = 'Hello World';3var textNFD = unorm.nfd(text);4console.log(textNFD);5var textNFC = unorm.nfc(text);6console.log(textNFC);7var unorm = require('unorm');
Using AI Code Generation
1var unorm = require('unorm');2var str = unorm.nfd("sometext");3console.log(str);4var unorm = require('unorm');5var str = unorm.nfd("sometext");6console.log(str);7var unorm = require('unorm');8var str = unorm.nfd("sometext");9console.log(str);10var unorm = require('unorm');11var str = unorm.nfd("sometext");12console.log(str);13var unorm = require('unorm');14var str = unorm.nfd("sometext");15console.log(str);16var unorm = require('unorm');17var str = unorm.nfd("sometext");18console.log(str);19var unorm = require('unorm');20var str = unorm.nfd("sometext");21console.log(str);22var unorm = require('unorm');23var str = unorm.nfd("sometext");24console.log(str);25var unorm = require('unorm');26var str = unorm.nfd("sometext");27console.log(str);28var unorm = require('unorm');29var str = unorm.nfd("sometext");30console.log(str);31var unorm = require('unorm');32var str = unorm.nfd("sometext");33console.log(str);34var unorm = require('unorm');35var str = unorm.nfd("sometext");36console.log(str);37var unorm = require('unorm');38var str = unorm.nfd("sometext");39console.log(str);
Using AI Code Generation
1var unorm = require('unorm');2var str = "h\u00E9llo";3var str1 = unorm.nfd(str);4console.log(str1);5var unorm = require('unorm');6var str = "h\u00E9llo";7var str1 = unorm.nfd(str);8console.log(str1);9Your name to display (optional):10Your name to display (optional):
Using AI Code Generation
1var unorm = require('unorm');2var normalized = unorm.nfd("Ã");3console.log(normalized);4var unorm = require('unorm');5var normalized = unorm.nfd("Ã");6console.log(normalized);7var unorm = require('unorm');8var normalized = unorm.nfd("Ã");9console.log(normalized);10var unorm = require('unorm');11var normalized = unorm.nfd("Ã");12console.log(normalized);13var unorm = require('unorm');14var normalized = unorm.nfd("Ã");15console.log(normalized);16var unorm = require('unorm');17var normalized = unorm.nfd("Ã");18console.log(normalized);19var unorm = require('unorm');20var normalized = unorm.nfd("Ã");21console.log(normalized);22var unorm = require('unorm');23var normalized = unorm.nfd("Ã");24console.log(normalized);25var unorm = require('unorm');26var normalized = unorm.nfd("Ã");27console.log(normalized);28var unorm = require('unorm');29var normalized = unorm.nfd("Ã");30console.log(normalized);
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!!