Best JavaScript code snippet using appium
basics-specs.js
Source:basics-specs.js
...70 driver.contexts().then(function (ctxs) {71 ctxs.length.should.be.above(0);72 return ctxs[0];73 }).then(function (ctx) {74 return driver.context(ctx);75 })76 .context(null)77 .nodeify(done);78 });79 it('returning to \'NATIVE_APP\' should work', function (done) {80 driver.contexts().then(function (ctxs) {81 ctxs.length.should.be.above(0);82 return ctxs[0];83 }).then(function (ctx) {84 return driver.context(ctx);85 })86 .context('NATIVE_APP')87 .nodeify(done);88 });89 it('setting context to non-existent context should return \'NoSuchContext\' (status: 35)', function (done) {90 driver91 .context("WEBVIEW_420")92 .should.be.rejectedWith(/status: 35/)93 .nodeify(done);94 });95 it('switching back and forth between native and webview contexts should work @skip-ios6', function (done) {96 driver.contexts().then(function (contexts) {97 driver98 .context(contexts[1])...
ContextConsumers.js
Source:ContextConsumers.js
1import React from 'react';2import AppContext, {AppProvider} from "../AppContext/AppContext";3import DriverContext, {DriverProvider} from "../DriverContext/DriverContext";4import TripContext, {TripProvider} from "../TripContext/TripContext";5import MapContext, {MapProvider} from "../MapContext/MapContext";6import NotificationContext, {NotificationProvider} from "../NotificationContext/NotificationContext";7export default class ContextConsumers extends React.Component{8 render(){9 return (10 <DriverProvider>11 <DriverContext.Consumer>12 { driverContext => (13 <TripProvider14 driverContext={driverContext}15 >16 <TripContext.Consumer>17 {18 tripContext => (19 <NotificationProvider20 driverContext={driverContext}21 tripContext={tripContext}22 >23 <NotificationContext.Consumer>24 { notificationContext => (25 <MapProvider26 driverContext={driverContext}27 >28 <MapContext.Consumer>29 { mapContext => (30 <AppProvider 31 driverContext={driverContext}32 tripContext={tripContext}33 notificationContext={notificationContext}34 mapContext={mapContext}35 >36 {this.props.children}37 </AppProvider>38 )}39 </MapContext.Consumer>40 </MapProvider>41 )}42 </NotificationContext.Consumer>43 </NotificationProvider>44 )45 }46 </TripContext.Consumer>47 </TripProvider>48 )}49 </DriverContext.Consumer>50 </DriverProvider>51 );52 };...
actions.js
Source:actions.js
1import { baseURL } from '../../../base-url.js';2export default {3 async loadDrivers(context) {4 const response = await fetch(baseURL + '/driver/getAll', {5 method: 'GET',6 headers: {7 'Authorization': "Bearer " + localStorage.getItem("jwt")8 },9 });10 const responseData = await response.json();11 if (!response.ok) {12 // error13 }14 const drivers = [];15 for (const key in responseData) {16 const driver = {17 id: responseData[key].id,18 name: responseData[key].name,19 surname: responseData[key].surname,20 plates: responseData[key].plates,21 phoneNumbers: responseData[key].phoneNumbers,22 };23 drivers.push(driver);24 }25 context.commit('setDrivers', drivers);26 },27 async loadDriver(context, data) {28 let url = new URL(baseURL + '/driver/getDriver');29 url.search = new URLSearchParams({30 id: data31 })32 const response = await fetch(url, {33 method: 'GET',34 headers: {35 'Authorization': "Bearer " + localStorage.getItem("jwt")36 }37 });38 const responseData = await response.json();39 if (!response.ok) {40 // error41 }42 context.commit('setDriver', responseData);43 },44 unloadDriver(context) {45 context.commit('unloadDriver');46 },47 async addDriver(context, data) {48 const response = await fetch(baseURL + '/driver/create', {49 method: 'POST',50 headers: {51 'Authorization': "Bearer " + localStorage.getItem("jwt"),52 'Content-Type': 'application/json'53 },54 body: JSON.stringify(data)55 });56 if (!response.ok) {57 alert("Nie dodano kierowcy")58 }59 context.commit('addDriver', data);60 },61 async deleteDriver(context, data) {62 let url = new URL(baseURL + '/driver/deleteDriver');63 url.search = new URLSearchParams({64 id: data65 })66 const response = await fetch(url, {67 method: 'DELETE',68 headers: {69 'Authorization': "Bearer " + localStorage.getItem("jwt")70 }71 });72 if (!response.ok) {73 // error74 } 75 },76 async updateDriver(context, data) {77 const response = await fetch(baseURL + '/driver/update', {78 method: 'PUT',79 headers: {80 'Authorization': "Bearer " + localStorage.getItem("jwt"),81 'Content-Type': 'application/json'82 },83 body: JSON.stringify(data)84 });85 if (!response.ok) {86 // error 87 }88 context.commit('updateDriver', data);89 },90 async setDriver(context, data) {91 context.commit('setDriver', data);92 }...
server.js
Source:server.js
1/*2Copyright 2007-2009 WebDriver committers3Copyright 2007-2009 Google Inc.4Portions copyright 2007 ThoughtWorks, Inc5Licensed under the Apache License, Version 2.0 (the "License");6you may not use this file except in compliance with the License.7You may obtain a copy of the License at8 http://www.apache.org/licenses/LICENSE-2.09Unless required by applicable law or agreed to in writing, software10distributed under the License is distributed on an "AS IS" BASIS,11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12See the License for the specific language governing permissions and13limitations under the License.14*/15var driver = false;16window.addEventListener("load", function(e) {17 handle = Components.classes["@googlecode.com/webdriver/fxdriver;1"].createInstance(Components.interfaces.nsISupports);18 var server = handle.wrappedJSObject;19 server.startListening();20 if (!driver) {21 driver = server.newDriver(window);22 } else {23 if (window.content)24 var frames = window.content.frames;25 // If we are already focused on a frame, try and stay focused26 if (driver.context.frameId !== undefined && frames) {27 if (frames && frames.length > driver.context.frameId) {28 // do nothing29 } else {30 if (frames && frames.length && "FRAME" == frames[0].frameElement.tagName) {31 if (!frames[driver.context.frameId]) {32 driver.context.frameId = 0;33 }34 } else {35 driver.context.frameId = undefined;36 }37 }38 } else {39 // Other use a sensible default40 if (frames && frames.length && "FRAME" == frames[0].frameElement.tagName) {41 if (!frames[driver.context.frameId]) {42 driver.context.frameId = 0;43 }44 } else {45 driver.context.frameId = undefined;46 }47 }48 }49}, true);50//window.addEventListener("focus", function(e) {51// var active = e.originalTarget;52// var doc = gBrowser.selectedBrowser.contentDocument;53// if (active.ownerDocument == doc) {54// driver.activeElement = active;55// }...
UseContextConsumer.js
Source:UseContextConsumer.js
1import React, {useState, useEffect, useContext} from 'react'2const driverContext = React.createContext();3export default function App(){4 const [employee, setEmployee]=useState({Name:'james', DriverLoc:'London W3', Status:'On Hire'})5 return(<div>6 <driverContext.Provider value={{data:employee, updateDriver:setEmployee}}>7 <Employee></Employee>8 </driverContext.Provider>9 </div>)10}11const Employee=()=>{12 const ctx = useContext(driverContext)13 return(<div>14 <h2>Employee...</h2>15 <p>16 Name: {ctx.data.Name}17 </p>18 <p>19 Status: {ctx.data.Status}20 </p>21 <DriverLoc></DriverLoc>22 </div>)23}24const DriverLoc=()=>{25 const ctx = useContext(driverContext)26 const [diffLoc, setLoc]=useState({loc:''})27 const locs = ["Twickenham T11","East Ham E2","Southhall UB2","Croydon CR1"]28 let locId = 0;29 useEffect(() => {30 setInterval(() => {31 //ctx.updateDriver({...ctx.data,Name:ctx.data.Name,Status:ctx.data.Status, DriverLoc:locs[locId]})32 ctx.updateDriver({...ctx.data, DriverLoc:locs[locId]})33 setLoc({...diffLoc,loc:locs[locId]})34 locId++35 if(locId == 4)36 locId = 0;37 }, 2000);38 39 }, [])40 function updateStatus(e){41 //ctx.updateDriver({...ctx.data,Name:ctx.data.Name,Status:e.target.name, DriverLoc:ctx.data.DriverLoc})42 //ctx.updateDriver({...ctx, Status:e.target.name})43 ctx.updateDriver({...ctx.data, Status:e.target.name})44 }45 return (<div>46 <h2>Driver Current Location: {ctx.data.DriverLoc}</h2>47 <h2>Driver new Location: {diffLoc.loc}</h2>48 <button onClick={updateStatus} name="Picked Passenger">Picked Passenger</button>49 <button onClick={updateStatus} name="On Hire">On Hire</button>50 </div>)...
UseContext1.js
Source:UseContext1.js
1import React, {useState, useEffect, useContext} from 'react'2const driverContext = React.createContext();3export default function TheBase(){4 const [drivers, setDriver]=useState({DriverId: 101, DriverName: 'James', DriverLoc: 'Central London W3'})5 return(<div>6 <h2>This is The base...</h2>7 <driverContext.Provider value={drivers}>8 <DriverInfo></DriverInfo>9 </driverContext.Provider>10 </div>)11}12const DriverInfo=()=>{13 const ctx = useContext(driverContext)14 return (15 <div>16 <h2>Driver information..</h2>17 <p>18 Id: {ctx.DriverId}19 </p>20 <p>21 Name: {ctx.DriverName}22 </p> 23 <DriverLocation></DriverLocation>24 </div>25 )26}27function DriverLocation(){28 const ctx = useContext(driverContext)29 const [diffLoc, setLoc]=useState({loc:"htis is "})30 const locs = ["Twickenham T11","East Ham E2","Southhall UB2","Croydon CR1"]31 let locId = 0;32 useEffect(() => {33 setInterval(() => {34 setLoc({...diffLoc,loc:locs[locId]})35 console.log(locs[locId], diffLoc.loc, locId)36 locId++37 if(locId == 4)38 locId = 0;39 }, 2000);40 41 }, [])42 return(43 <div>44 <h2>Driver Location</h2>45 <p>46 Current Location: {ctx.DriverLoc}47 </p>48 </div>)...
UseStateFuncs2.js
Source:UseStateFuncs2.js
1import React, {useState, useEffect, useContext} from 'react'2const driverContext = React.createContext();3export default function App(){4 const [employee, setEmployee]=useState({Name:'james', DriverLoc:'London W3'})5 return(<div>6 <driverContext.Provider value={{data:employee, updateLocation:setEmployee}}>7 <Employee></Employee>8 </driverContext.Provider>9 </div>)10}11const Employee=()=>{12 const ctx = useContext(driverContext)13 return(<div>14 <h2>Employee...</h2>15 <p>16 Name: {ctx.data.Name}17 </p>18 <DriverLoc></DriverLoc>19 </div>)20}21const DriverLoc=()=>{22 const ctx = useContext(driverContext)23 const [diffLoc, setLoc]=useState({loc:''})24 const locs = ["Twickenham T11","East Ham E2","Southhall UB2","Croydon CR1"]25 let locId = 0;26 useEffect(() => {27 setInterval(() => {28 ctx.updateLocation({...ctx.data, DriverLoc:locs[locId]})29 setLoc({...diffLoc,loc:locs[locId]})30 locId++31 if(locId == 4)32 locId = 0;33 }, 2000);34 35 }, [])36 return (<div>37 <h2>Driver Current Location: {ctx.data.DriverLoc}</h2>38 <h2>Driver new Location: {diffLoc.loc}</h2>39 </div>)...
DriverContext.js
Source:DriverContext.js
1import React, { createContext, useContext, useState } from 'react';2const DriverContext = createContext({3 phones: [],4 setPhones: () => { }5});6export const useDriverContext = () => {7 return useContext(DriverContext)8}9export const DriverDataProvider = ({ children }) => {10 const [phones, setPhones] = useState();11 return (12 <DriverContext.Provider value={ {13 phones,14 setPhones15 } }>16 {children }17 </DriverContext.Provider>18 )...
Using AI Code Generation
1var webdriver = require('selenium-webdriver'),2 until = webdriver.until;3var driver = new webdriver.Builder()4 .forBrowser('chrome')5 .build();6driver.findElement(By.name('q')).sendKeys('webdriver');7driver.findElement(By.name('btnG')).click();8driver.wait(until.titleIs('webdriver - Google Search'), 1000);9driver.quit();10var webdriver = require('selenium-webdriver'),11 until = webdriver.until;12var driver = new webdriver.Builder()13 .forBrowser('chrome')14 .build();15driver.findElement(By.name('q')).sendKeys('webdriver');16driver.findElement(By.name('btnG')).click();17driver.wait(until.titleIs('webdriver - Google Search'), 1000);18driver.quit();19var webdriver = require('selenium-webdriver'),20 until = webdriver.until;21var driver = new webdriver.Builder()22 .forBrowser('chrome')23 .build();24driver.findElement(By.name('q')).sendKeys('webdriver');25driver.findElement(By.name('btnG')).click();26driver.wait(until.titleIs('webdriver - Google Search'), 1000);27driver.quit();28var webdriver = require('selenium-webdriver'),29 until = webdriver.until;30var driver = new webdriver.Builder()31 .forBrowser('chrome')32 .build();33driver.findElement(By.name('q')).sendKeys('webdriver');34driver.findElement(By.name('btnG')).click();35driver.wait(until.titleIs('webdriver - Google Search'), 1000);36driver.quit();37var webdriver = require('selenium-webdriver'),38 until = webdriver.until;39var driver = new webdriver.Builder()40 .forBrowser('chrome')41 .build();42driver.findElement(By.name('q')).sendKeys('webdriver');
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var chai = require('chai');4var expect = chai.expect;5var should = chai.should();6var asserters = wd.asserters;7var desiredCaps = {8};9var driver = wd.promiseChainRemote('localhost', 4723);10driver.init(desiredCaps)11 .then(function () {12 return driver.context('WEBVIEW_1');13 })14 .then(function () {15 return driver.elementById('buttonTestCD');16 })17 .then(function (el) {18 return el.click();19 })20 .then(function () {21 return driver.elementById('visibleButtonTest');22 })23 .then(function (el) {24 return el.click();25 })26 .then(function () {27 return driver.elementById('my_text_field');28 })29 .then(function (el) {30 return el.sendKeys('Hello World!');31 })32 .then(function () {33 return driver.elementById('showToastButton');34 })35 .then(function (el) {36 return el.click();37 })38 .then(function () {39 return driver.elementById('visibleTextView');40 })41 .then(function (el) {42 return el.text();43 })44 .then(function (text) {45 console.log(text);46 })47 .fin(function () {48 return driver.quit();49 })50 .done();
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!!