Best JavaScript code snippet using appium-xcuitest-driver
ios-complex.js
Source:ios-complex.js
1"use strict";2require("./helpers/setup");3var wd = require("wd"),4 _ = require('underscore'),5 Q = require('q'),6 serverConfigs = require('./helpers/appium-servers'),7 _p = require('./helpers/promise-utils'),8 fs = require('fs');9describe("ios complex", function () {10 this.timeout(300000);11 var driver;12 var allPassed = true;13 before(function () {14 var serverConfig = process.env.npm_package_config_sauce ?15 serverConfigs.sauce : serverConfigs.local;16 driver = wd.promiseChainRemote(serverConfig);17 require("./helpers/logging").configure(driver);18 var desired = _.clone(require("./helpers/caps").ios81);19 desired.app = require("./helpers/apps").iosUICatalogApp;20 if (process.env.npm_package_config_sauce) {21 desired.name = 'ios - complex';22 desired.tags = ['sample'];23 }24 return driver.init(desired);25 });26 after(function () {27 return driver28 .quit()29 .finally(function () {30 if (process.env.npm_package_config_sauce) {31 return driver.sauceJobStatus(allPassed);32 }33 });34 });35 afterEach(function () {36 allPassed = allPassed && this.currentTest.state === 'passed';37 });38 function clickMenuItem(name) {39 return driver40 .elementByName(name)41 .catch(function () {42 return driver43 .elementByClassName('XCUIElementTypeTable')44 .elementsByClassName('>','XCUIElementTypeCell')45 .then(_p.filterWithName(name)).first();46 }).click();47 }48 function goBack() {49 return driver50 .elementByName('Back')51 .click();52 }53 it("should print every menu item", function () {54 return driver55 .elementByClassName('XCUIElementTypeTable')56 .elementsByClassName('>','XCUIElementTypeCell')57 .then(_p.printNames);58 });59 it("should find an element", function () {60 return driver61 // first view in UICatalog is a table62 .elementByClassName('XCUIElementTypeTable')63 .should.eventually.exist64 // check the number of cells/rows inside the table65 .elementsByClassName('XCUIElementTypeCell')66 .then(_p.filterDisplayed)67 .then(function (els) {68 els.should.have.length.above(6);69 return els;70 })71 .first()72 .elementsByClassName('>', 'XCUIElementTypeStaticText')73 // various checks74 .first().getAttribute('value')75 .should.become('Action Sheets')76 .waitForElementByClassName('XCUIElementTypeNavigationBar')77 .should.eventually.exist;78 });79 it("should switch context", function () {80 return clickMenuItem('Web View')81 .sleep(1000)82 // get the contexts83 .contexts()84 .then(function(contexts){85 // switch to webview86 return driver.context(contexts[1]); 87 })88 .sleep(1000)89 90 // Wait for an element from apple.com homepage to be present91 .waitForElementById('ac-globalnav')92 .should.eventually.exist93 // leave the webview 94 .context('NATIVE_APP').sleep(1000)95 96 //Verify we are out of the webview97 .waitForElementByClassName('XCUIElementTypeWindow')98 .should.eventually.exist99 .then(goBack)100 });101 it("should get an element location", function () {102 return driver.elementsByClassName("XCUIElementTypeCell")103 .then(_p.filterDisplayed)104 .at(2)105 .getLocation()106 .then(function (loc) {107 loc.x.should.equal(0);108 loc.y.should.be.above(100);109 });110 });111 it("should take screenshots", function () {112 return driver113 // base64 screeshot114 .takeScreenshot()115 .should.eventually.exist116 // save screenshot to local file117 .then(function () {118 try {119 fs.unlinkSync('/tmp/foo.png');120 } catch (ign) {}121 fs.existsSync('/tmp/foo.png').should.not.be.ok;122 })123 .saveScreenshot('/tmp/foo.png')124 .then(function () {125 fs.existsSync('/tmp/foo.png').should.be.ok;126 });127 });128 it("should edit a text field", function () {129 var el, defaultValue;130 return clickMenuItem('Text Fields')131 // get the field and the default/empty text132 .elementByClassName('XCUIElementTypeTextField')133 .then(function (_el) {134 el = _el;135 return el.getValue(); })136 .then(function (val) { defaultValue = val; })137 // type something138 .then(function () {139 return el140 .sendKeys('1234 appium')141 .getValue().should.become('1234 appium')142 .elementByName('Done').click().sleep(1000); // dismissing keyboard143 })144 // clear the field145 .then(function () { return el.clear(); })146 .then(function () { el.getValue().should.become(defaultValue); })147 // back to main menu148 .then(goBack);149 });150 it("should trigger/accept/dismiss an alert", function () {151 return clickMenuItem('Alert Views')152 // trigger simple alert153 .elementByName('Simple').click()154 .alertText().should.eventually.include('A Short Title Is Best')155 .dismissAlert()156 // trigger modal alert with cancel & ok buttons157 .sleep(1000)158 .elementByName('Okay / Cancel').click()159 .alertText().should.eventually.include('A Short Title Is Best')160 .acceptAlert()161 .sleep(1000)162 // back to main menu163 .then(goBack);164 });165 // TODO: Waiting to hear back from Facebook to see if WebDriverAgent supports programatically setting sliders.166 // (see issue: https://github.com/facebook/WebDriverAgent/issues/339)167 /*it("should set a slider value", function () {168 var slider;169 return clickMenuItem('Sliders')170 // retrieve slider, check initial value171 .elementByClassName("UIASlider")172 .then(function (_slider) { slider = _slider; })173 .then(function () {174 return slider.getValue().should.become('42%');175 })176 // change value177 .then(function () { return slider.sendKeys("0%"); })178 .then(function () {179 return slider.getValue().should.become('0%');180 })181 // back to main menu182 .then(goBack);183 });*/184 if (!process.env.npm_package_config_sauce) {185 it("should retrieve the session list", function () {186 driver.sessions()187 .then(function (sessions) {188 JSON.stringify(sessions).should.include(driver.getSessionId());189 });190 });191 }192 it("should retrieve an element size", function () {193 return Q.all([194 driver.elementByClassName('XCUIElementTypeTable').getSize(),195 driver.elementByClassName('XCUIElementTypeCell').getSize(),196 ]).then(function (sizes) {197 sizes[0].width.should.equal(sizes[1].width);198 sizes[0].height.should.not.equal(sizes[1].height);199 });200 });201 it("should get the source", function () {202 var mainMenuSource;203 // main menu source204 return driver205 .source().then(function (source) {206 mainMenuSource = source;207 mainMenuSource.should.include('XCUIElementTypeStaticText');208 mainMenuSource.should.include('Text Fields');209 })210 // text fields section source211 .then(function () {212 return clickMenuItem("Text Fields");213 }).source().then(function (textFieldSectionSource) {214 textFieldSectionSource.should.include('XCUIElementTypeStaticText');215 textFieldSectionSource.should.include('Text Fields');216 })217 // back to main menu218 .then(goBack);219 });...
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var chai = require('chai');4var chaiAsPromised = require('chai-as-promised');5chai.use(chaiAsPromised);6var expect = chai.expect;7var should = chai.should();8var browser = wd.promiseChainRemote({9});10var desired = {11};12 .init(desired)13 .then(function() {14 return browser.setImplicitWaitTimeout(6000);15 })16 .then(function() {17 })18 .then(function(table) {19 return table.elementByClassName('XCUIElementTypeCell');20 })21 .then(function(cell) {22 })23 .then(function(text) {24 return text.getAttribute('value');25 })26 .then(function(textValue) {27 console.log('text value is: ', textValue);28 })29 .fin(function() {30 return browser.quit();31 })32 .done();33var wd = require('wd');34var assert = require('assert');35var chai = require('chai');36var chaiAsPromised = require('chai-as-promised');37chai.use(chaiAsPromised);38var expect = chai.expect;39var should = chai.should();40var browser = wd.promiseChainRemote({41});42var desired = {43};44 .init(desired)45 .then(function() {46 return browser.setImplicitWaitTimeout(6000);47 })48 .then(function() {49 })50 .then(function(table) {51 return table.elementByClassName('XCUIElementTypeCell');52 })
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var fs = require('fs');4var path = require('path');5var desired = {6 app: path.resolve('/Users/username/Downloads/iossimulator.app'),7};8var driver = wd.promiseChainRemote('localhost', 4723);9 .init(desired)10 .waitForElementByClassName('XCUIElementTypeTable', 5000)11 .then(function(table) {12 table.elementByClassName('XCUIElementTypeCell', function(err, cell) {13 console.log(cell);14 });15 })16 .fin(function() { return driver.quit(); })17 .done();18table.elementByClassName('XCUIElementTypeTable', function(err, table) {19 console.log(table);20});21table.elementByClassName('XCUIElementTypeCell', function(err, cell) {22 console.log(cell);23});
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desired = {4};5 .init(desired)6 .then(function() {7 console.log("Session started");8 return driver.waitForElementByAccessibilityId("Table", 10000);9 })10 .then(function(table) {11 return table.elementByClassName("XCUIElementTypeCell");12 })13 .then(function(element) {14 console.log("Found element");15 })16 .catch(function(err) {17 console.log("Error: " + err);18 });19Error: Error response status: 13, UnknownError - An unknown server-side error occurred while processing the command. Original error: Error Domain=com.facebook.WebDriverAgent Code=1 "Failed to find matching element" UserInfo={NSLocalizedDescription=Failed to find matching element, NSLocalizedRecoverySuggestion=Error kAXErrorCannotComplete performing AXUIElementCopyAttributeValues on element AX element pid: 1228, elementOrHash.elementID: 140000000000000, attribute: kAXChildrenAttribute, value: (null), parameterizedAttribute: (null), error: Error kAXErrorCannotComplete performing AXUIElementCopyAttributeValues on element AX element pid: 1228, elementOrHash.elementID: 140000000000000, attribute: kAXChildrenAttribute, value: (null), parameterizedAttribute: (null), error: Error kAXErrorCannotComplete performing AXUIElementCopyAttributeValues on element AX element pid: 1228, elementOrHash.elementID: 140000000000000, attribute: kAXChildrenAttribute, value: (null), parameterizedAttribute: (null), error: Error kAXErrorCannotComplete performing AXUIElementCopyAttributeValues on element AX element pid: 1228, elementOrHash.elementID: 140000000000000, attribute: kAXChildrenAttribute, value: (null), parameterizedAttribute: (null), error: Error kAXErrorCannotComplete performing AXUIElementCopyAttributeValues
Using AI Code Generation
1var wd = require('wd');2var assert = require('assert');3var desired = {4};5var driver = wd.promiseChainRemote('localhost', 4723);6 .init(desired)7 .then(function() {8 return driver.elementByClassName("UIATableView");9 })10 .then(function(table) {11 return table.elementByClassName("UIATableCell");12 })13 .then(function(cell) {14 return cell.elementByClassName("UIAStaticText");15 })16 .then(function(text) {17 return text.text();18 })19 .then(function(text) {20 assert.equal(text, "This is a test");21 })22 .fin(function() { return driver.quit(); })23 .done();24var wd = require('wd');25var assert = require('assert');26var desired = {27};28var driver = wd.promiseChainRemote('localhost', 4723);29 .init(desired)30 .then(function() {31 return driver.elementByClassName("UIATableView");32 })33 .then(function(table) {34 return table.elementByClassName("UIATableCell");35 })36 .then(function(cell) {37 return cell.elementByClassName("UIAStaticText");38 })39 .then(function(text) {40 return text.text();41 })42 .then(function(text) {43 assert.equal(text, "This is a test");44 })45 .fin(function() { return driver.quit(); })46 .done();47var wd = require('wd');48var assert = require('assert');49var desired = {
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!!