Best JavaScript code snippet using appium
test.js
Source:test.js  
...30    this.timeout(99999999999);31    //Accept Popup32    await driver.execute('mobile:alert', { action: 'accept' });33    //Enter Name34    let name = await driver.waitForElementByAccessibilityId("name");35    await name.sendKeys('Shibobrota');36    //Enter Email37    let email = await driver.waitForElementByAccessibilityId("email");38    await email.sendKeys('das7@divami.com');39    //Enter Phone Number40    let phone = await driver.waitForElementByAccessibilityId("phone_number");41    await phone.clear();42    await phone.sendKeys('+917008244473');43    //Enter Password44    let Password = await driver.waitForElementByAccessibilityId("password");45    await Password.sendKeys('Divami@123');46    //Click Send Verification Code47    let button = await driver.waitForElementByAccessibilityId("signup_submit");48    await button.click();49    //Enter code50    let verificationCode = await driver.waitForElementByAccessibilityId("verificationcode");51    await verificationCode.sendKeys('1234sdv123');52    //Click Send Verification Code53    let verifyButton = await driver.waitForElementByAccessibilityId("verify");54    await verifyButton.click();55  });56  it('Should Login', async function () {57    this.timeout(999999999);58    //Accept Popup59    await driver.execute('mobile:alert', { action: 'accept' });60    //Click on login61    const login = await driver.waitForElementByAccessibilityId("login");62    await login.click();63    //Enter Email64    let email = await driver.waitForElementByAccessibilityId("email");65    await email.sendKeys('divami@docs.duke-ai.com');66    //Enter Password67    let Password = await driver.waitForElementByAccessibilityId("password");68    await Password.sendKeys('Divami@3');69    //Click Login70    let loginButton = await driver.waitForElementByAccessibilityId("submit");71    await loginButton.click();72   73  });74  it('Should click forgot password and return.', async function () {75    this.timeout(99999999);76    //Accept Popup77    await driver.execute('mobile:alert', { action: 'accept' });78    //Click on login79    const login = await driver.waitForElementByAccessibilityId("login");80    await login.click();81    //Click on Forgot Password82    const forgotPwd = await driver.waitForElementByAccessibilityId("forgotPassword");83    await forgotPwd.click();84    //Click Login85    let loginButton = await driver.waitForElementByAccessibilityId("REMEMBER ? GO BACK TO LOGIN");86    await loginButton.click();87  });88  it('Should click forgot password and enter wrong email to check popup.', async function () {89    this.timeout(99999999999);90    //Accept Popup91    await driver.execute('mobile:alert', { action: 'accept' });92    //Click on login93    const login = await driver.waitForElementByAccessibilityId("login");94    await login.click();95    //Click on Forgot Password96    const forgotPwd = await driver.waitForElementByAccessibilityId("forgotPassword");97    await forgotPwd.click();98    await driver.setImplicitWaitTimeout(50);99    //Enter Email100    let email = await driver.waitForElementByAccessibilityId("email");101    await email.sendKeys('das@divami.com');102    //Click Login103    let sendVerificationButton = await driver.waitForElementByAccessibilityId("sendVerificationCode");104    await sendVerificationButton.click();105    await driver.setImplicitWaitTimeout(500);106    //Check alert Title107    let alertTitleElement = await driver.elements("xpath","//XCUIElementTypeStaticText[@name=\"Error!\"]");108    // Check the text109    const alertTitle = await alertTitleElement.text();110    assert.equal(alertTitle, `Error!`);111    //Check alert message112    let alertmessageElement = await driver.waitForElementByAccessibilityId("This email is not registered with us. Would you like to register\?");113    // Check the text114    const alertmessage = await alertmessageElement.text();115    assert.equal(alertmessage, `This email is not registered with us. Would you like to register?`);116    //Click Close117    let closeButton = await driver.waitForElementByAccessibilityId("Close");118    await closeButton.click();119  });...local_test_native_android.js
Source:local_test_native_android.js  
...26    console.log('quitting');27  });28  it('LOGIN_AND_ASK_QUESTION', async () => {29    console.log('LOGIN_SCREEN_LOADED CALLED');30    const elementUserName = await driver.waitForElementByAccessibilityId('LoginScreen_UserNameInput', 10000, 100);31    expect(elementUserName).to.exist;32    elementUserName.sendKeys('Alp Burak');33    const elementPassword = await driver.waitForElementByAccessibilityId('LoginScreen_PasswordInput', 10000, 100);34    elementPassword.sendKeys('pass123');35    const elementSubmitButton = await driver.waitForElementByAccessibilityId('LoginScreen_SubmitButton', 10000, 100);36    elementSubmitButton.click();37    const elementNextPage = await driver.waitForElementByAccessibilityId('AskQuestionsScreen_NextPage', 10000, 100);38    expect(elementNextPage).to.exist;39    console.log('ASK QUESTION PAGE IS CLOSE TO SHOWN');40    elementNextPage.click();41    const elementSelectTeacher = await driver.waitForElementByAccessibilityId('AskQuestionsScreen_SelectTeacher', 10000, 100);42    const elementSelectLesson = await driver.waitForElementByAccessibilityId('AskQuestionsScreen_SelectLesson', 10000, 100);43    const elementSelectBaslik = await driver.waitForElementByAccessibilityId('AskQuestionScreen_Baslik', 10000, 100);44    const elementSelectAciklama = await driver.waitForElementByAccessibilityId('AskQuestionScreen_Aciklama', 10000, 100);45    const elementButtonAskSubmit = await driver.waitForElementByAccessibilityId('AskQuestionScreen_Ask', 10000, 100);46    expect(elementSelectTeacher).to.exist;47    expect(elementSelectLesson).to.exist;48    expect(elementSelectBaslik).to.exist;49    expect(elementSelectAciklama).to.exist;50    expect(elementButtonAskSubmit).to.exist;51    console.log('ASK QUESTION PAGE IS SHOWN');52    console.log('QUESTION is close to be asked');53    elementSelectTeacher.sendKeys('Musa Fehmi Orakci');54    elementSelectLesson.sendKeys('Matematik');55    elementSelectBaslik.sendKeys('DenemeSoru1');56    elementSelectAciklama.sendKeys('DenemeSoru1Aciklamaaa');57    elementButtonAskSubmit.click();58    console.log('QUESTION has been asked');59  });60  it('SEE_STUDENTS_ACTIVE_QUESTIONS', async () => {61    console.log('LOGIN_SCREEN_LOADED CALLED');62    const elementUserName = await driver.waitForElementByAccessibilityId('LoginScreen_UserNameInput', 10000, 100);63    expect(elementUserName).to.exist;64    elementUserName.sendKeys('Alp Burak');65    const elementPassword = await driver.waitForElementByAccessibilityId('LoginScreen_PasswordInput', 10000, 100);66    elementPassword.sendKeys('pass123');67    const elementSubmitButton = await driver.waitForElementByAccessibilityId('LoginScreen_SubmitButton', 10000, 100);68    elementSubmitButton.click();69    const elementNextPage = await driver.waitForElementByAccessibilityId('AskQuestionsScreen_NextPage', 10000, 100);70    expect(elementNextPage).to.exist;71  });72  it('SEE_TEACHERS_ACTIVE_QUESTIONS', async () => {73  });74  it('SEE_STUDENTS_PASIVE_QUESTIONS', async () => {75  });76  it('SEE_STUDENTS_ACTIVE_QUESTIONS', async () => {77  });78  it('SOLVE_A_QUESTION', async () => {79  });...GeneralAppOperations.js
Source:GeneralAppOperations.js  
1module.exports = class AppOperations {2    // * Click element3    clickByAccessibilityId(ele, time) {4        return driver.waitForElementByAccessibilityId(ele, time, `${ele} is not clickable`).elementByAccessibilityId(ele).click();5    }6    clickById(ele, time) {7        return driver.waitForElementById(ele, time, `${ele} is not clickable`).elementById(ele).click();8    }9    clickByXPath(ele, time) {10        return driver.waitForElementByXPath(ele, time, `${ele} is not clickable`).elementByXPath(ele).click();11    }12    // * Element displayed13    elementDisplayedById(ele, time) {14        return driver.waitForElementById(ele, time, `${ele} is not displayed`).elementById(ele).isDisplayed();15    }16    elementDisplayedByAccessibilityId(ele, time) {17        return driver.waitForElementByAccessibilityId(ele, time, `${ele} is not displayed`).elementByAccessibilityId(ele).isDisplayed();18    }19    elementDisplayedByXPath(ele, time) {20        return driver.waitForElementByXPath(ele, time, `${ele} is not displayed`).elementByXPath(ele).isDisplayed();21    }22    // Move to down23    moveTo(ele, dir) {24        return driver.moveToElement(ele, dir);25    }26    moveToExecuteUP() {27        return driver.execute('mobile: scroll', { direction: 'up' });28    }29    moveToExecuteDown() {30        return driver.execute('mobile: scroll', { direction: 'down' });31    }32    // Get attribute33    getAttributeByXPath(ele, att, time) {34        return driver.waitForElementByXPath(ele, time).elementByXPath(ele).getAttribute(att);35    }36    getAttributeById(ele, att, time) {37        return driver.waitForElementById(ele, time).elementById(ele).getAttribute(att);38    }39    getAttributeByAccessibilityId(ele, att, time) {40        return driver.waitForElementByAccessibilityId(ele, time).elementByAccessibilityId(ele).getAttribute(att);41    }42    // Fill text43    fillTextByAccessibilityId(ele, text, time) {44        return driver.waitForElementByAccessibilityId(ele, time).elementByAccessibilityId(ele).click().sendKeys(text)45            .hideDeviceKeyboard();46    }47    fillTextById(ele, text, time) {48        return driver.waitForElementById(ele, time).elementById(ele).click().sendKeys(text)49            .hideDeviceKeyboard();50    }51    async fillTextByXPath(ele, text, time) {52        await driver.waitForElementByXPath(ele, time).elementByXPath(ele).click().sendKeys(text);53        return driver.hideDeviceKeyboard();54    }55    // Element Exist56    exist(ele, css) {57        return driver.elementExist(ele, css);58    }...PageObjectApp.js
Source:PageObjectApp.js  
...3    // Do not need params here.4  }5  // * Click element6  clickByAccessibilityId(ele) {7    return driver.waitForElementByAccessibilityId(ele, 20000, `${ele} is not clickable`).elementByAccessibilityId(ele).click();8  }9  clickById(ele) {10    return driver.waitForElementById(ele, 20000, `${ele} is not clickable`).elementById(ele).click();11  }12  clickByXPath(ele) {13    return driver.waitForElementByXPath(ele, 20000, `${ele} is not clickable`).elementByXPath(ele).click();14  }15  // * Element displayed16  elementDisplayedById(ele) {17    return driver.waitForElementById(ele, 20000, `${ele} is not displayed`).elementById(ele).isDisplayed();18  }19  elementDisplayedByAccessibilityId(ele) {20    return driver.waitForElementByAccessibilityId(ele, 20000, `${ele} is not displayed`).elementByAccessibilityId(ele).isDisplayed();21  }22  elementDisplayedByXPath(ele) {23    return driver.waitForElementByXPath(ele, 20000, `${ele} is not displayed`).elementByXPath(ele).isDisplayed();24  }25  // Move to down26  moveTo(ele, dir) {27    return driver.moveToElement(ele, dir);28  }29  moveToExecuteUP() {30    return driver.execute('mobile: scroll', { direction: 'up' });31  }32  moveToExecuteDown() {33    return driver.execute('mobile: scroll', { direction: 'down' });34  }35  // Get attribute36  getAttributeByXPath(ele, att) {37    return driver.waitForElementByXPath(ele, 10000).elementByXPath(ele).getAttribute(att);38  }39  getAttributeById(ele, att) {40    return driver.waitForElementById(ele, 20000).elementById(ele).getAttribute(att);41  }42  getAttributeByAccessibilityId(ele, att) {43    return driver.waitForElementByAccessibilityId(ele, 20000).elementByAccessibilityId(ele).getAttribute(att);44  }45  // Fill text46  fillTextByAccessibilityId(ele, text) {47    return driver.waitForElementByAccessibilityId(ele, 20000).elementByAccessibilityId(ele).click().clean(ele).sendKeys(text).hideKeyboardTouch();48  }49  fillTextById(ele, text) {50    return driver.waitForElementById(ele, 40000).elementById(ele).click().clean(ele).sendKeys(text).hideKeyboardTouch();51  }52  async fillTextByXPath(ele, text) {53    await driver.waitForElementByXPath(ele, 10000).elementByXPath(ele).click().clean(ele).sendKeys(text);54    return driver.hideKeyboardTouch();55  }56  //Element Exist57  exist(ele, css) {58    return driver.elementExist(ele, css);59  }60  //Get elements61  getElementsByXPath(ele) {...attribute-e2e-specs.js
Source:attribute-e2e-specs.js  
...8let animationEl;9describe('apidemo - attributes', function () {10  before(async function () {11    driver = await initDriver(APIDEMOS_CAPS);12    animationEl = await driver.waitForElementByAccessibilityId('Animation');13  });14  after(async function () {15    await driver.quit();16  });17  it('should be able to find resourceId attribute', async function () {18    await animationEl.getAttribute('resourceId').should.eventually.become('android:id/text1');19  });20  it('should be able to find text attribute', async function () {21    await animationEl.getAttribute('text').should.eventually.become('Animation');22  });23  it('should be able to find name attribute', async function () {24    await animationEl.getAttribute('name').should.eventually.become('Animation');25  });26  it('should be able to find name attribute, falling back to text', async function () {27    await animationEl.click();28    await driver.waitForElementByAccessibilityId('Bouncing Balls');29    let textViewEl = await driver.elementByAccessibilityId('Bouncing Balls');30    await textViewEl.getAttribute('name');31    await driver.back();32    await driver.waitForElementByAccessibilityId('Animation');33  });34  it('should be able to find content description attribute', async function () {35    await animationEl.getAttribute('contentDescription').should.eventually.become("Animation");36  });37  it('should be able to find displayed attribute', async function () {38    await animationEl.getAttribute('displayed').should.eventually.become('true');39  });40  it('should be able to find enabled attribute', async function () {41    await animationEl.getAttribute('enabled').should.eventually.become('true');42  });43  it('should be able to find displayed attribute through normal func', async function () {44    const displayed = await animationEl.isDisplayed();45    (displayed + '').should.equal('true');46  });...ios-basic-interactions.test.js
Source:ios-basic-interactions.test.js  
...40  });41  it('should send keys to inputs', async function () {42    // Find TextField input element43    const textInputId = `TextField1`;44    const textViewsEl = await driver.waitForElementByAccessibilityId(textInputId);45    // Check that it doesn't have a value46    let value = await textViewsEl.getValue();47    assert.isNull(value, 'Input should have no value');48    // Send keys to that input49    await textViewsEl.sendKeys('Hello World!');50    // Check that the input has new value51    value = await textViewsEl.getValue();52    assert.equal(value, 'Hello World!', 'Input should have newly input value');53  });54  it('should click a button that opens an alert', async function () {55    // Find Button element and click on it56    const buttonElementId = `show alert`;57    const buttonElement = await driver.waitForElementByAccessibilityId(buttonElementId);58    await buttonElement.click();59    // Wait for the alert to show up60    const alertTitleId = `Cool title`;61    const alertTitleElement = await driver.waitForElementByAccessibilityId(alertTitleId);62    // Check the text63    const alertTitle = await alertTitleElement.text();64    assert.equal(alertTitle, `Cool title`);65  });...helper.js
Source:helper.js  
1const WAIT_TO_LOAD = 10_0002export async function fillName(name, driver, wait = true) {3    if(wait) {4        try {5            await driver.waitForElementByAccessibilityId("name_input", {timeout: WAIT_TO_LOAD});6        } catch (e) {7            console.log(e);8        }9    }10    const nameInput = await driver.elementByAccessibilityId("name_input");11    await nameInput.type(name);12}13export async function fillSurname(surname, driver, wait = true) {14    if(wait)15        await driver.waitForElementByAccessibilityId("surname_input", {timeout: WAIT_TO_LOAD});16    const surnameInput = await driver.elementByAccessibilityId("surname_input");17    await surnameInput.type(surname);18}19export async function fillBirthDate(date, driver, wait = true){20    if(wait)21        await driver.waitForElementByAccessibilityId("birthdate_input", {timeout: WAIT_TO_LOAD});22    const birthDateInput = await driver.elementByAccessibilityId("birthdate_input");23    await birthDateInput.type(date);24}25export async function fillCityName(cityName, driver, wait = true){26    if(wait)27        await driver.waitForElementByAccessibilityId("cityname_input", {timeout: WAIT_TO_LOAD});28    const birthDateInput = await driver.elementByAccessibilityId("cityname_input");29    await birthDateInput.type(cityName);30}31export async function fillSideEffects(sideEffects, driver, wait = true){32    if(wait)33        await driver.waitForElementByAccessibilityId("sideEffect_input", {timeout: WAIT_TO_LOAD});34    const birthDateInput = await driver.elementByAccessibilityId("sideEffect_input");35    await birthDateInput.type(sideEffects);...firstTest.appium.spec.js
Source:firstTest.appium.spec.js  
...8    await driver.init(config());9  });  10test('Test first screen', async () => {11    //Button12    await driver.waitForElementByAccessibilityId('firstBtn', 2000);13    const firstBtn = await driver.elementByAccessibilityId('firstBtn');14    await firstBtn.click();15    //Switch16    await driver.waitForElementByAccessibilityId('firstSwitch', 2000);17    const firstSwitch = await driver.elementByAccessibilityId('firstSwitch');18    await firstSwitch.click();19    //Text Input20    await driver.waitForElementByAccessibilityId('firstTxtInput', 2000);21    const firstTxtInput = await driver.elementByAccessibilityId('firstTxtInput');22    await firstTxtInput.sendKeys('Hello');23    //Text24    await driver.waitForElementByAccessibilityId('firstTxt', 2000);25    const txt = await driver.elementByAccessibilityId('firstTxt').text();26    expect(txt).toBe(27        `firstTxt`,28        );   29    //Finishing the test30    await driver.waitForElementByAccessibilityId('flow', 2000);31    const flow = await driver.elementByAccessibilityId('flow');32    await flow.click();33    await flow.click();34    await driver.waitForElementByAccessibilityId('congrats', 2000);35    const congrats = await driver.elementByAccessibilityId('congrats');36    await congrats.sendKeys('Congratulations ððð');37    await driver.quit();38        ...Using AI Code Generation
1driver.waitForElementByAccessibilityId(accessibilityId, timeout, interval);2driver.waitForElementByClassName(className, timeout, interval);3driver.waitForElementByCss(css, timeout, interval);4driver.waitForElementById(id, timeout, interval);5driver.waitForElementByLinkText(linkText, timeout, interval);6driver.waitForElementByName(name, timeout, interval);7driver.waitForElementByPartialLinkText(partialLinkText, timeout, interval);8driver.waitForElementByTagName(tagName, timeout, interval);9driver.waitForElementByXPath(xpath, timeout, interval);10driver.waitForElementNotByAccessibilityId(accessibilityId, timeout, interval);11driver.waitForElementNotByClassName(className, timeout, interval);12driver.waitForElementNotByCss(css, timeout, interval);13driver.waitForElementNotById(id, timeout, interval);14driver.waitForElementNotByLinkText(linkText, timeout, interval);15driver.waitForElementNotByName(name, timeout, interval);Using AI Code Generation
1var wd = require('wd');2driver.init({3}).then(function() {4  return driver.waitForElementByAccessibilityId('test', 5000);5}).then(function() {6  return driver.quit();7});8var wd = require('wd');9driver.init({10}).then(function() {11}).then(function() {12  return driver.quit();13});14var wd = require('wd');15driver.init({16}).then(function() {17  return driver.waitForElementByClassName('UIAButton', 5000);18}).then(function() {19  return driver.quit();20});21var wd = require('wd');22driver.init({23}).then(function() {24  return driver.waitForElementByCssSelector('UIAApplication[1]/UIAWindow[1]/UIAButton[1]', 5000);25}).then(function() {26  return driver.quit();27});28var wd = require('wd');29var driver = wd.promiseChainRemote("Using AI Code Generation
1var webdriverio = require('webdriverio');2var options = {3    desiredCapabilities: {4    }5};6    .remote(options)7    .init()8    .waitForElementByAccessibilityId('gbqfbb', 2000)9    .setValue('#gbqfbb', 'Hello World!')10    .end();112015-03-10T14:10:55.606Z - info: [REMOTE] Welcome to Appium v1.3.4 (REV 0f6c1b6f2b8d0c6e9a2f9e4b4d4c8a4a4c0f4f0e)Using AI Code Generation
1var wd = require('wd');2require('colors');3var desired = {4};5var driver = wd.promiseChainRemote('localhost', 4723);6  .init(desired)7  .then(function() {8    return driver.waitForElementByAccessibilityId('buttonTest', 10000);9  })10  .then(function() {11    console.log('Element Found');12  })13  .fin(function() {14    return driver.quit();15  })16  .done();17var wd = require('wd');18require('colors');19var desired = {20};21var driver = wd.promiseChainRemote('localhost', 4723);22  .init(desired)23  .then(function() {24  })25  .then(function() {26    console.log('Element Found');27  })28  .fin(function() {29    return driver.quit();30  })31  .done();32var wd = require('wd');33require('colors');34var desired = {35};36var driver = wd.promiseChainRemote('localhost', 4723);Using AI Code Generation
1var wd = require('wd'),2  assert = require('assert'),3  Q = require('q'),4  _ = require('underscore'),5  fs = require('fs'),6  path = require('path');7var desiredCaps = {8};9var driver = wd.promiseChainRemote('localhost', 4723);10  .init(desiredCaps)11  .setImplicitWaitTimeout(5000)12  .waitForElementByAccessibilityId('my_text_fieldCD', 10000)13  .then(function (el) {14    console.log('found element with id my_text_fieldCD');15  }, function (err) {16    console.log('did not find element with id my_text_fieldCD');17  })18  .fin(function () {19    return driver.quit();20  })21  .done();Using AI Code Generation
1driver.waitForElementByAccessibilityId('myId', 10000, function(err, el) {2  if (err) throw err;3  el.click();4});5  if (err) throw err;6  el.click();7});8driver.waitForElementByClassName('UIAButton', 10000, function(err, el) {9  if (err) throw err;10  el.click();11});12driver.waitForElementByIosUIAutomation('.elements().withName('myId')', 10000, function(err, el) {13  if (err) throw err;14  el.click();15});16driver.waitForElementByAndroidUIAutomator('new UiSelector().className('android.widget.Button')', 10000, function(err, el) {17  if (err) throw err;18  el.click();19});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!!
