How to use driver.elementsByTagName method in Appium Xcuitest Driver

Best JavaScript code snippet using appium-xcuitest-driver

safari-basic-e2e-specs.js

Source:safari-basic-e2e-specs.js Github

copy

Full Screen

...120      it('should find a web element in the web view', async () => {121        (await driver.elementById('i_am_an_id')).should.exist;122      });123      it('should find multiple web elements in the web view', async () => {124        (await driver.elementsByTagName('a')).should.have.length.at.least(5);125      });126      it('should fail gracefully to find multiple missing web elements in the web view', async () => {127        (await driver.elementsByTagName('blar')).should.have.length(0);128      });129      it('should find element from another element', async () => {130        let el = await driver.elementByClassName('border');131        (await el.elementByXPath('./form')).should.exist;132      });133      it('should be able to click links', async () => {134        let el = await driver.elementByLinkText('i am a link');135        await el.click();136        await spinTitleEquals(driver, 'I am another page title');137      });138      it('should retrieve an element attribute', async () => {139        let el = await driver.elementById('i_am_an_id');140        (await el.getAttribute('id')).should.be.equal('i_am_an_id');141        expect(await el.getAttribute('blar')).to.be.null;142      });143      it('should retrieve implicit attributes', async () => {144        let els = await driver.elementsByTagName('option');145        els.should.have.length(3);146        (await els[2].getAttribute('index')).should.be.equal('2');147      });148      it('should retrieve an element text', async () => {149        let el = await driver.elementById('i_am_an_id');150        (await el.text()).should.be.equal('I am a div');151      });152      // TODO: figure out what equality means here153      it.skip('should check if two elements are equal', async () => {154        let el1 = await driver.elementById('i_am_an_id');155        let el2 = await driver.elementByCss('#i_am_an_id');156        el1.should.be.equal(el2);157      });158      it('should return the page source', async () => {...

Full Screen

Full Screen

tests.js

Source:tests.js Github

copy

Full Screen

...104      } catch (ign) {}105    }106    fs = await driver.elementsByClassName('UIATextField');107  } else {108    fs = await driver.elementsByTagName('textField');109  }110  // some early versions of appium didn't filter out the extra text fields111  // that UIAutomation started putting in, so make the test sensitive112  // to that113  let firstField = fs[0], secondField;114  if (fs.length === 2) {115    secondField = fs[1];116  } else if (fs.length === 4) {117    secondField = fs[2];118  } else {119    throw new Error('Got strange number of fields in testapp: ' + fs.length);120  }121  await firstField.sendKeys('4');122  await secondField.sendKeys('5');123  if (appium1) {124    await driver.elementByClassName('UIAButton').click();125  } else {126    await driver.elementByTagName('button').click();127  }128  let text;129  if (appium1) {130    text = await driver.elementByClassName('UIAStaticText').text();131  } else {132    text = await driver.elementByTagName('staticText').text();133  }134  text.should.equal('9');135};136async function iosCycle (driver, caps) {137  let appium1 = isAppium1(caps);138  let el;139  if (appium1) {140    el = await driver.elementByClassName('UIATextField');141  } else {142    el = await driver.elementByTagName('textField');143  }144  await el.sendKeys('123');145  await el.clear();146}147tests.iosSendKeysStressTest = async function (driver, caps) {148  try {149    await driver.elementByAccessibilityId('OK').click();150  } catch (ign) {}151  for (let i = 0; i < (process.env.STRESS_TIMES || 50); i++) {152    try {153      await iosCycle(driver, caps);154    } catch (e) {155      throw new Error(`Failure on stress run ${i}: ${e}`);156    }157  }158};159tests.iosHybridTest = async function (driver, caps) {160  if (!isAppium1(caps)) {161    throw new Error('Hybrid test only works with Appium 1 caps');162  }163  await selectWebview(driver);164  await driver.get('https://google.com');165  await driver.waitFor(titleToMatch('Google'), 10000, 1000);166  await driver.context('NATIVE_APP');167  (await driver.source()).should.containEql('<AppiumAUT>');168};169tests.iosLocServTest = async function (driver) {170  await retryInterval(5, 1000, async () => {171    let uiSwitch = await driver.elementByClassName('UIASwitch');172    let res = await uiSwitch.getAttribute('value');173    [1, true].should.containEql(res);174  });175};176tests.iosLocServTest.extraCaps = {177  locationServicesAuthorized: true,178  locationServicesEnabled: true,179  bundleId: 'io.appium.TestApp'180};181tests.iosIwd = async function (driver, caps) {182  let appium1 = isAppium1(caps);183  let times = 30, timings = [];184  const maxTimeMs = 900;185  const loop = async () => {186    let start = Date.now();187    if (appium1) {188      await driver.elementsByClassName('UIATextField');189    } else {190      await driver.elementsByTagName('textField');191    }192    return Date.now() - start;193  };194  for (let i = 0; i < times; i++) {195    timings.push(await loop());196  }197  const avgTime = _.sum(timings) / times;198  if (avgTime > maxTimeMs) {199    throw new Error(`Expected average command time to be no greater than ` +200                    `${maxTimeMs}ms but it was ${avgTime}ms`);201  }202};203tests.androidTest = async function (driver, caps) {204  await androidCycle(driver, caps);205};206tests.androidLongTest = async function (driver, caps) {207  for (let i = 0; i < 15; i++) {208    await androidCycle(driver, caps);209  }210};211tests.androidLoadTest = async function (driver) {212  const iterations = 20; // 10 minute runtime213  const intervalMs = 30000;214  let args = await driver.elementById('textOptions');215  let goBtn = await driver.elementById('go');216  await args.clear();217  await args.sendKeys(`-M 500 -v 20 -s ${Math.floor(iterations * intervalMs / 1000)}`);218  for (let i = 0; i < iterations; i++) {219    // Only the first click triggers the test, the rest of the clicks are ignored220    // and only to keep the session open whilst the test is running221    await goBtn.click();222    await driver.sleep(30000);223  }224};225async function androidCycle (driver, caps) {226  let appium1 = isAppium1(caps);227  if (appium1) {228    await driver.elementByAccessibilityId('Add Contact').click();229  } else {230    await driver.elementByName('Add Contact').click();231  }232  let fs;233  if (appium1) {234    fs = await driver.elementsByClassName('android.widget.EditText');235  } else {236    fs = await driver.elementsByTagName('textfield');237  }238  await fs[0].sendKeys('My Name');239  await fs[2].sendKeys('someone@somewhere.com');240  // do contains search since RDC adds weird extra edit text241  (await fs[0].text()).should.containEql('My Name');242  (await fs[2].text()).should.containEql('someone@somewhere.com');243  await driver.back();244  await driver.sleep(2);245  let text;246  if (appium1) {247    text = await driver.elementByClassName('android.widget.Button').text();248  } else {249    text = await driver.elementByTagName('button').text();250  }...

Full Screen

Full Screen

basics-base.js

Source:basics-base.js Github

copy

Full Screen

1"use strict";2var setup = require("../setup-base"),3    webviewHelper = require("../../../helpers/webview"),4    loadWebView = webviewHelper.loadWebView,5    isChrome = webviewHelper.isChrome,6    Q = require('q'),7    spinTitle = webviewHelper.spinTitle,8    spinWait = require('../../../helpers/spin.js').spinWait,9    skip = webviewHelper.skip;10module.exports = function (desired) {11  describe('basics', function () {12    var driver;13    setup(this, desired, {'no-reset': true}).then(function (d) { driver = d; });14    beforeEach(function (done) {15      loadWebView(desired, driver).nodeify(done);16    });17    it('should find a web element in the web view', function (done) {18      driver19        .elementById('i_am_an_id').should.eventually.exist20        .nodeify(done);21    });22    it('should find multiple web elements in the web view', function (done) {23      driver24        .elementsByTagName('a').should.eventually.have.length.above(0)25        .nodeify(done);26    });27    it('should fail gracefully to find multiple missing web elements in the web view', function (done) {28      driver29        .elementsByTagName('blar').should.eventually.have.length(0)30        .nodeify(done);31    });32    it('should find element from another element', function (done) {33      driver34        .elementByClassName('border')35        .elementByXPath('>', './form').should.eventually.exist36        .nodeify(done);37    });38    it('should be able to click links', function (done) {39      driver40        .elementByLinkText('i am a link').click()41        .then(function () { return spinTitle('I am another page title', driver); })42        .nodeify(done);43    });44    it('should retrieve an element attribute', function (done) {45      driver46        .elementById('i_am_an_id')47          .getAttribute("id").should.become('i_am_an_id')48        .elementById('i_am_an_id')49          .getAttribute("blar").should.not.eventually.exist50        .nodeify(done);51    });52    it('should retrieve implicit attributes', function (done) {53      driver54        .elementsByTagName('option')55        .then(function (els) {56          els.should.have.length(3);57          return els[2].getAttribute('index').should.become('2');58        }).nodeify(done);59    });60    it('should retrieve an element text', function (done) {61      driver62        .elementById('i_am_an_id').text().should.become('I am a div')63        .nodeify(done);64    });65    it('should check if two elements are equals', function (done) {66      Q.all([67        driver.elementById('i_am_an_id'),68        driver.elementByTagName('div')69      ]).then(function (els) {70        return els[0].equals(els[1]).should.be.ok;71      }).nodeify(done);72    });73    it('should return the page source', function (done) {74      driver75        .source()76        .then(function (source) {77          source.should.include('<html');78          source.should.include('I am a page title');79          source.should.include('i appear 3 times');80          source.should.include('</html>');81        }).nodeify(done);82    });83    it('should get current url', function (done) {84      driver85        .url().should.eventually.include("test/guinea-pig")86        .nodeify(done);87    });88    it('should send keystrokes to specific element', function (done) {89      driver90        .elementById('comments')91          .clear()92          .sendKeys("hello world")93          .getValue().should.become("hello world")94        .nodeify(done);95    });96    it('should send keystrokes to active element', function (done) {97      driver98        .elementById('comments')99          .clear()100          .click()101          .keys("hello world")102        .elementById('comments')103          .getValue().should.become("hello world")104        .nodeify(done);105    });106    it('should clear element', function (done) {107      driver108        .elementById('comments')109          .sendKeys("hello world")110          .getValue().should.eventually.have.length.above(0)111        .elementById('comments')112          .clear()113          .getValue().should.become("")114        .nodeify(done);115    });116    it('should say whether an input is selected', function (done) {117      driver118        .elementById('unchecked_checkbox')119          .selected().should.not.eventually.be.ok120        .elementById('unchecked_checkbox')121          .click()122          .selected().should.eventually.be.ok123        .nodeify(done);124    });125    it('should be able to retrieve css properties', function (done) {126      driver127        .elementById('fbemail').getComputedCss('background-color')128          .should.become("rgba(255, 255, 255, 1)")129        .nodeify(done);130    });131    it('should retrieve an element size', function (done) {132      driver133        .elementById('i_am_an_id').getSize()134        .then(function (size) {135          size.width.should.be.above(0);136          size.height.should.be.above(0);137        }).nodeify(done);138    });139    it('should get location of an element', function (done) {140      driver141        .elementById('fbemail')142          .getLocation()143        .then(function (loc) {144          loc.x.should.be.above(0);145          loc.y.should.be.above(0);146        }).nodeify(done);147    });148    it('should retrieve tag name of an element', function (done) {149      driver150        .elementById('fbemail').getTagName().should.become("input")151        .elementByCss("a").getTagName().should.become("a")152        .nodeify(done);153    });154    it('should retrieve a window size @skip-chrome', function (done) {155      driver156        .getWindowSize()157        .then(158          function (size) {159            size.height.should.be.above(0);160            size.width.should.be.above(0);161          }).nodeify(done);162    });163    it('should move to an arbitrary x-y element and click on it', function (done) {164      driver.elementByLinkText('i am a link')165        .moveTo(5, 15)166        .click()167      .then(function () { return spinTitle("I am another page title", driver); })168      .nodeify(done);169    });170    it('should submit a form', function (done) {171      driver172        .elementById('comments')173          .sendKeys('This is a comment')174          .submit()175        .then(function () {176          return spinWait(function () {177            return driver178              .elementById('your_comments')179              .text()180              .should.become('Your comments: This is a comment');181          });182        }183      ).nodeify(done);184    });185    it('should return true when the element is displayed', function (done) {186      driver187        .elementByLinkText('i am a link')188          .isDisplayed().should.eventually.be.ok189        .nodeify(done);190    });191    it('should return false when the element is not displayed', function (done) {192      driver193        .elementById('invisible div')194          .isDisplayed().should.not.eventually.be.ok195        .nodeify(done);196    });197    it('should return true when the element is enabled', function (done) {198      driver199        .elementByLinkText('i am a link')200          .isEnabled().should.eventually.be.ok201        .nodeify(done);202    });203    it('should return false when the element is not enabled', function (done) {204      driver205        .execute("$('#fbemail').attr('disabled', 'disabled');")206        .elementById('fbemail').isEnabled().should.not.eventually.be.ok207        .nodeify(done);208    });209    it("should return the active element", function (done) {210      var testText = "hi there";211      driver212        .elementById('i_am_a_textbox').sendKeys(testText)213        .active().getValue().should.become(testText)214        .nodeify(done);215    });216    it('should properly navigate to anchor', function (done) {217      driver218        .url().then(function (curl) {219          return driver.get(curl);220        }).nodeify(done);221    });222    it('should be able to refresh', function (done) {223      driver.refresh()224      .nodeify(done);225    });226    it('should be able to get performance logs', function (done) {227      if (!isChrome(desired)) return skip(228        "Performance logs aren't available except in Chrome", done);229      driver230        .logTypes()231          .should.eventually.include('performance')232        .log('performance').then(function (logs) {233          logs.length.should.be.above(0);234        }).nodeify(done);235    });236  });...

Full Screen

Full Screen

safari-window-e2e-specs.js

Source:safari-window-e2e-specs.js Github

copy

Full Screen

...163      (await driver.title()).should.be.equal('Iframe guinea pig');164      let h1 = await driver.elementByTagName('h1');165      (await h1.text()).should.be.equal('Sub frame 1');166      await driver.frame(null);167      (await driver.elementsByTagName('iframe')).should.have.length(3);168    });169  });...

Full Screen

Full Screen

App.e2e.js

Source:App.e2e.js Github

copy

Full Screen

1test('should pass', async () => {2  const button = await driver.elementsByTagName('button')3  await driver.tapElement(button[0])4  const contexts = await driver.contexts()5  console.log(contexts)6  await driver.context(contexts[0])7  await driver.openNotifications()8  await sleep(2000)9  const notifications = await driver.elementByXPath('//*[@text="It\'s a notification!"]')10  console.log(notifications)...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2const chai = require('chai');3const chaiAsPromised = require('chai-as-promised');4chai.use(chaiAsPromised);5const should = chai.should();6const assert = chai.assert;7const expect = chai.expect;8const {

Full Screen

Using AI Code Generation

copy

Full Screen

1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder()3    .withCapabilities({4    })5    .build();6    .elementsByTagName('button')7    .then(function (els) {8        return els[1].click();9    })10    .then(function () {11        return driver.quit();12    })13    .done();14var webdriver = require('selenium

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2async function main() {3  await driver.init({4  });5  const elements = await driver.elementsByTagName('input');6  console.log(`Found ${elements.length} input elements`);7}8main();9driver.elementsByCssSelector('input[

Full Screen

Using AI Code Generation

copy

Full Screen

1var webdriver = require('selenium-webdriver');2var driver = new webdriver.Builder()3  .forBrowser('selenium-webdriver')4  .build();5driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');6driver.findElement(webdriver.By.name('btnG')).click();7driver.wait(function() {8  return driver.getTitle().then(function(title) {9    return title === 'webdriver - Google Search';10  });11}, 1000);12driver.quit();13var webdriver = require('selenium-webdriver');14var driver = new webdriver.Builder()15  .forBrowser('selenium-webdriver')16  .build();17driver.findElement(webdriver.By.name('q')).sendKeys('webdriver');18driver.findElement(webdriver.By.name('btnG')).click();19driver.wait(function() {20  return driver.getTitle().then(function(title) {21    return title === 'webdriver - Google Search';22  });23}, 1000);24driver.quit();25Appium version (or git revision) that exhibits the issue: 1.7.126Last Appium version that did not exhibit the issue (if applicable): 1.6.527Node.js version (unless using Appium.app|exe): 8.9.1

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2const chai = require('chai');3const chaiAsPromised = require('chai-as-promised');4chai.use(chaiAsPromised);5const should = chai.should();6const desiredCaps = {7};8driver.init(desiredCaps)9  .then(() => {10    return driver.elementsByTagName('XCUIElementTypeCell');11  })12  .then((cells) => {13    cells.length.should.be.above(0);14  })15  .catch((err) => {16    console.log(err);17  });18const wd = require('wd');19const chai = require('chai');20const chaiAsPromised = require('chai-as-promised');21chai.use(chaiAsPromised);22const should = chai.should();23const desiredCaps = {24};25driver.init(desiredCaps)26  .then(() => {27    return driver.elementsByTagName('XCUIElementTypeCell');28  })29  .then((cells) => {30    cells.length.should.be.above(0);31  })32  .catch((err) => {33    console.log(err);34  });35const wd = require('wd');36const chai = require('chai');37const chaiAsPromised = require('chai-as-promised');38chai.use(chaiAsPromised);39const should = chai.should();40const desiredCaps = {

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3var desired = require('./desired');4var caps = desired.caps;5var serverConfig = desired.serverConfig;6var driver = wd.promiseChainRemote(serverConfig);7driver.init(caps)8  .then(function () {9    return driver.elementsByTagName('XCUIElementTypeButton');10  })11  .then(function (els) {12    console.log(els.length);13  })14  .fin(function () { return driver.quit(); })15  .done();16var serverConfig = {

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3var asserters = wd.asserters;4var browser = wd.promiseChainRemote('localhost', 4723);5var desired = {6};7  .init(desired)8  .sleep(5000)9  .click()10  .sleep(5000)11  .elementsByTagName('XCUIElementTypeStaticText')12  .then(function(elements) {13    elements[0].text().then(function(text) {14      console.log(text);15    });16  });

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var assert = require('assert');3var desiredCaps = {4};5driver.init(desiredCaps).then(function () {6}).then(function () {7    return driver.elementsByTagName('input');8}).then(function (elements) {9    console.log('Number of elements found: ' + elements.length);10    assert.equal(elements.length, 2);11    console.log('Test passed');12}).fin(function () {13    return driver.quit();14}).done();15var wd = require('wd');16var assert = require('assert');17var desiredCaps = {18};19driver.init(desiredCaps).then(function () {20}).then(function () {21    return driver.elementsByTagName('input');22}).then(function (elements) {23    console.log('Number of elements found: ' + elements.length);24    assert.equal(elements.length, 2);25    console.log('Test passed');26}).fin(function () {27    return driver.quit();28}).done();

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium Xcuitest Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Sign up Free
_

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful