Best JavaScript code snippet using appium
basics-specs.js
Source:basics-specs.js
...10 driver.currentContext().should.eventually.equal("NATIVE_APP")11 .nodeify(done);12 });13 it('getting list should work after webview open', function (done) {14 driver.contexts().should.eventually.have.length.above(0)15 .nodeify(done);16 });17 it('getting list multiple times should not crash appium', function (done) {18 driver19 .contexts().should.eventually.have.length.above(0)20 .contexts().should.eventually.have.length.above(0)21 .contexts().should.eventually.have.length.above(0)22 .contexts().should.eventually.have.length.above(0)23 .contexts().should.eventually.have.length.above(0)24 .contexts().should.eventually.have.length.above(0)25 .contexts().should.eventually.have.length.above(0)26 .nodeify(done);27 });28 it('contexts should be strings', function (done) {29 driver.contexts().then(function (ctxs) {30 ctxs.length.should.be.above(0);31 _.each(ctxs, function (ctx) {32 (typeof ctx).should.equal("string");33 });34 }).nodeify(done);35 });36 it('setting context without getting contexts should work', function (done) {37 driver38 .context("WEBVIEW_1")39 .sleep(500)40 .get(env.GUINEA_TEST_END_POINT)41 .sleep(500)42 .title()43 .should.eventually.equal("I am a page title")44 .nodeify(done);45 });46 it('setting context to \'WEBVIEW_X\' should work', function (done) {47 driver.contexts().then(function (contexts) {48 contexts.should.have.length.above(0);49 driver50 .context(contexts[1])51 .sleep(500)52 .get(env.GUINEA_TEST_END_POINT)53 .sleep(1000)54 .title()55 .should.eventually.equal("I am a page title")56 .nodeify(done);57 });58 });59 it('setting context to \'WEBVIEW\' should work', function (done) {60 driver61 .context("WEBVIEW")62 .sleep(500)63 .get(env.GUINEA_TEST_END_POINT)64 .sleep(500)65 .title()66 .should.eventually.equal("I am a page title")67 .nodeify(done);68 });69 it('setting context to \'null\' should work', function (done) {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])99 .get(env.GUINEA_TEST_END_POINT)100 .sleep(3000)101 .title()102 .should.eventually.equal("I am a page title")103 .context("NATIVE_APP")104 .context(contexts[1])105 .get(env.GUINEA_TEST_END_POINT)106 .sleep(3000)107 .elementByLinkText('i am a link').click()108 .elementById('only_on_page_2').should.eventually.exist109 .nodeify(done);110 });...
tests.js
Source:tests.js
...21 app: appPath22};23driver.init(androidCapabilities)24.then(function() {25 return driver.contexts();26})27.then(function(contexts) {28 return switchToWebViewContext();29})30.then(function() {31 return test();32})33.catch(function(err) {34 console.log('catch', err);35});36function test() {37 return driver.elementById('ziv-take-photo-btn').click()38 .then(function() {39 return switchToNativeContext();40 })41 .then(function() {42 //click take picture43 var resId = 'com.android.camera2:id/shutter_button';44 return clickAndroidElementByResId(resId);45 })46 .then(function() {47 //click picture done48 var resId = 'com.android.camera2:id/btn_done';49 return clickAndroidElementByResId(resId);50 })51 .then(function() {52 return switchToWebViewContext();53 })54 .then(function() {55 return getCropBtn();56 })57 .then(function(btn) {58 btn.click();59 })60 .then(function() {61 return driver.elementsByClassName('ziv-slider-item');62 })63 .then(function(elements) {64 return elements[0].click();65 })66 .then(function() {67 return driver.elementById('ziv-edit-photo-continue-btn').click();68 })69 .then(function() {70 return isPictureSave();71 })72 .then(function(isPictureSave) {73 console.log(isPictureSave);74 })75 .catch(function(err) {76 console.log('TEST ERR', err)77 });78}79function isPictureSave() {80 var url = 'http://192.168.1.104:3000/photos/image.jpg';81 return request(url)82 .spread(function(response) {83 console.log(response.statusCode);84 return Promise.resolve(response.statusCode == 200);85 })86 .catch(function(err) {87 return Promise.resolve(false);88 });89}90function getCropBtn() {91 return driver.elementsByClassName('jr-crop')92 .then(function(elements) {93 return elements[0].elementsByTagName('button');94 })95 .then(function(elements) {96 return elements[1];97 });98}99function clickAndroidElementByResId(resId) {100 return getAndroidElementByResId(resId)101 .then(function(element) {102 return element.click();103 });104}105function getAndroidElementByResId(resId) {106 return driver.elementsByAndroidUIAutomator('new UiSelector().resourceId("' + resId + '")')107 .then(function(elements) {108 return Promise.resolve(elements[0]);109 });110}111function switchToWebViewContext() {112 return driver.contexts()113 .then(function(contexts) {114 return getContext(contexts, 'WEBVIEW')115 })116 .then(function(context) {117 return driver.context(context);118 });119}120function switchToNativeContext() {121 return driver.contexts()122 .then(function(contexts) {123 return getContext(contexts, 'NATIVE')124 })125 .then(function(context) {126 return driver.context(context);127 });128}129function getContext(contexts, phrase) {130 var context = _.find(contexts, function(context) {131 return context.indexOf(phrase) != -1;132 });133 if (context) {134 return context;135 } else {...
web-e2e-specs.js
Source:web-e2e-specs.js
...23 it('should get the title of a webview page', async function () {24 await driver.title().should.eventually.equal('I am a page title');25 });26 it('should find one native and one web context', async function () {27 let contexts = await driver.contexts();28 contexts.length.should.equal(2);29 contexts[0].should.match(/^native/i);30 contexts[1].should.match(/^webview/i);31 });32 it('should send text to html text inputs', async function () {33 const html = await driver.source();34 html.should.match(/Selenium/);35 const textbox = await driver.elementById('i_am_a_textbox');36 await textbox.clear();37 await textbox.type('Text contents');38 await textbox.getAttribute('value').should.eventually.equal('Text contents');39 await textbox.clear();40 await textbox.text().should.eventually.equal('');41 });42 it('should navigate between webview pages', async function () {43 const anchorLink = await driver.elementById('i am a link');44 await anchorLink.click();45 const bodyEl = await driver.elementByTagName('body');46 bodyEl.getAttribute('value').should.eventually.equal(/I am some other page content/);47 await driver.back();48 await driver.elementById('i am a link').should.eventually.exist;49 });50 it('should be able to switch from webview back to native, navigate to a different webview and then switch back to web context', async function () {51 // Switch to webview52 let contexts = await driver.contexts();53 await driver.context(contexts[1]);54 await driver.title().should.eventually.equal('I am a page title');55 // Switch to native and go to different activity56 await driver.context(contexts[0]);57 await driver.startActivity({appActivity: 'io.appium.android.apis.view.WebView3', appPackage: 'io.appium.android.apis'});58 contexts = await driver.contexts();59 await driver.elementById('android:id/content').should.eventually.exist;60 // Switch to webview again61 await driver.context(contexts[1]);62 await driver.title().should.eventually.equal('I am a page title');63 });64 });65 describe('Chrome Browser', function () {66 it('should reject "browserName=Chrome" sessions', async function () {67 await initSession({68 ...GENERIC_CAPS,69 browserName: 'Chrome',70 }).should.eventually.be.rejectedWith(/doesn't have permission/);71 await deleteSession();72 });...
context-e2e-specs.js
Source:context-e2e-specs.js
...23 after(async function () {24 await driver.quit();25 });26 it('should find webview context', async function () {27 let contexts = await driver.contexts();28 contexts.length.should.be.at.least(2);29 // make sure the process was found, otherwise it comes out as "undefined"30 contexts.join('').should.not.include('undefined');31 contexts.join('').should.include(WEBVIEW);32 });33 it('should go into the webview', async function () {34 // TODO: Fix this on TestObject. Chromedriver does not exist error35 if (process.env.TESTOBJECT_E2E_TESTS) {36 this.skip();37 }38 let contexts = await driver.contexts();39 await driver.context(contexts[1]);40 });41 it('should be able to go into native context and interact with it after restarting app', async function () {42 await driver.closeApp();43 await driver.launchApp();44 await driver.context(NATIVE);45 await driver.elementByXPath(NATIVE_LOCATOR);46 });47 it('should be able to go into native context and interact with it after resetting app', async function () {48 await driver.resetApp();49 await driver.context(NATIVE);50 await driver.elementByXPath(NATIVE_LOCATOR);51 });52 it('should be able to go into webview context and interact with it after restarting app', async function () {...
macaca-test-sample.test.js
Source:macaca-test-sample.test.js
1/* ================================================================2 * macaca-test-sample by xdf(xudafeng[at]126.com)3 *4 * first created at : Thu Mar 10 2016 14:21:49 GMT+0800 (CST)5 *6 * ================================================================7 * Copyright xdf8 *9 * Licensed under the MIT License10 * You may not use this file except in compliance with the License.11 *12 * ================================================================ */13'use strict';14var path = require('path');15var wd = require('webdriver-client')({16 platformName: 'iOS',17 app: path.join(__dirname, '..', 'app', 'ios-app-bootstrap.zip')18});19describe('macaca test sample', function() {20 this.timeout(5 * 60 * 1000);21 var driver = wd.initPromiseChain();22 before(function() {23 return driver.initDriver();24 });25 after(function() {26 return driver27 .sleep(1000)28 .quit();29 });30 it('#1 should login success', function() {31 return driver32 .login('12345678', '111111')33 .sleep(1000);34 });35 it('#2 should display home', function() {36 return driver37 .takeScreenshot();38 });39 it('#3 should display webview', function() {40 return driver41 .elementByName('Webview')42 .click()43 .sleep(3000)44 .takeScreenshot();45 });46 it('#4 should go into webview', function() {47 return driver48 .contexts()49 .then(function(arr) {50 return driver51 .context(arr[arr.length - 1]);52 })53 .elementById('pushView')54 .click()55 .sleep(1000)56 .elementById('popView')57 .click()58 .sleep(1000)59 .takeScreenshot();60 });61 it('#5 should go into test', function() {62 return driver63 .contexts()64 .then(function(arr) {65 return driver66 .context(arr[0]);67 })68 .elementByName('Baidu')69 .click()70 .sleep(5000)71 .takeScreenshot();72 });73 it('#6 should works with web', function() {74 return driver75 .contexts()76 .then(function(arr) {77 return driver78 .context(arr[arr.length - 1]);79 })80 .elementById('index-kw')81 .sendKeys('TesterHome')82 .elementById('index-bn')83 .click()84 .sleep(5000)85 .source()86 .then(function(html) {87 html.should.containEql('TesterHome');88 })89 .takeScreenshot();90 });91 it('#7 should logout success', function() {92 return driver93 .contexts()94 .then(function(arr) {95 return driver96 .context(arr[0]);97 })98 .elementByName('PERSONAL')99 .click()100 .sleep(1000)101 .takeScreenshot()102 .elementByName('Logout')103 .click()104 .sleep(1000)105 .takeScreenshot();106 });...
background.js
Source:background.js
1(function(info, tab) {2 var regexp = {3 "BaiduPan": {4 reg: /^1[a-z]+/i,5 site: "http://pan.baidu.com/s/"6 },7 "Bilibili": {8 reg: /^av\d+$/i,9 site: "http://www.bilibili.com/video/"10 },11 "Acfun": {12 reg: /^ac\d+$/i,13 site: "http://www.acfun.tv/v/"14 }15 };16 var Pixiv = {17 "pic": "http://www.pixiv.net/member_illust.php?mode=medium&illust_id=",18 "auth": "http://www.pixiv.net/member.php?id="19 };20 //add an item21 // chrome.runtime.onInstalled.addListener(function() {22 var id = chrome.contextMenus.create({23 "title": "PixivLink",24 "contexts": ["selection"],25 "id": "oldDriver"26 // "onclick": onClickHandler27 });28 console.log("item:" + id);29 chrome.contextMenus.create({30 "title": "å¾çid",31 "parentId": "oldDriver",32 "contexts": ["selection"],33 "id": "picId",34 "onclick": function(info, tab) {35 var value = info.selectionText.trim();36 window.open(Pixiv["pic"] + value, "_blank");37 }38 });39 chrome.contextMenus.create({40 "title": "ä½è
id",41 "parentId": "oldDriver",42 "contexts": ["selection"],43 "id": "authid",44 "onclick": function(info, tab) {45 var value = info.selectionText.trim();46 window.open(Pixiv["auth"] + value, "_blank");47 }48 });49 chrome.contextMenus.create({50 "title": "å
¶ä»",51 "parentId": "oldDriver",52 "contexts": ["selection"],53 "id": "others",54 "onclick": onClickHandler55 });56 // });57 // chrome.contextMenus.onClicked.addListener(onClickHandler);58 function onClickHandler(info, tab) {59 var value = info.selectionText.trim();60 console.log(value);61 if (value != "") {62 for (var i in regexp) {63 if (regexp.hasOwnProperty(i)) {64 if (regexp[i].reg.test(value)) {65 console.log(i);66 window.open(regexp[i].site + value, "_blank");67 }68 }69 }70 }71 }...
context-specs.js
Source:context-specs.js
...16 .contexts().should.eventually.have.length(1)17 .nodeify(done);18 });19 it('setting context to \'null\' should work', function (done) {20 driver.contexts().then(function (ctxs) {21 ctxs.length.should.be.equal(1);22 return ctxs[0];23 }).then(function (ctx) {24 return driver.context(ctx);25 })26 .context(null)27 .nodeify(done);28 });29 it('setting context to \'NATIVE_APP\' should work', function (done) {30 driver.contexts().then(function (ctxs) {31 ctxs.length.should.be.above(0);32 return ctxs[0];33 }).then(function (ctx) {34 return driver.context(ctx);35 })36 .context('NATIVE_APP')37 .nodeify(done);38 });39 it('setting context to non-existent context should return \'NoSuchContext\' (status: 35)', function (done) {40 driver41 .context("WEBVIEW_42")42 .should.be.rejectedWith(/status: 35/)43 .nodeify(done);44 });...
window-specs.js
Source:window-specs.js
1"use strict";2var setup = require("../../common/setup-base"),3 desired = require('./desired');4describe('uicatalog - contexts @skip-ios6', function () {5 var driver;6 setup(this, desired).then(function (d) { driver = d; });7 it('getting contexts should do nothing when no webview open', function (done) {8 driver9 .contexts().should.eventually.have.length(1)10 .nodeify(done);11 });...
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('btnK')).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('btnK')).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('btnK')).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('btnK')).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');43driver.findElement(By.name('btnK')).click();
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!!