Best JavaScript code snippet using appium-xcuitest-driver
navigation-guards.js
Source:navigation-guards.js
...11 // alert commands not available in phantom12 if (process.env.PHANTOMJS) return13 browser14 .click('li:nth-child(2) a')15 .dismissAlert()16 .waitFor(100)17 .dismissAlert()18 .assert.urlEquals('http://localhost:8080/navigation-guards/')19 .assert.containsText('.view', 'home')20 .click('li:nth-child(2) a')21 .acceptAlert()22 .assert.urlEquals('http://localhost:8080/navigation-guards/foo')23 .assert.containsText('.view', 'foo')24 .click('li:nth-child(3) a')25 .dismissAlert()26 .waitFor(100)27 .dismissAlert()28 .assert.urlEquals('http://localhost:8080/navigation-guards/foo')29 .assert.containsText('.view', 'foo')30 .click('li:nth-child(3) a')31 .acceptAlert()32 .assert.urlEquals('http://localhost:8080/navigation-guards/bar')33 .assert.containsText('.view', 'bar')34 .click('li:nth-child(2) a')35 .dismissAlert()36 .waitFor(100)37 .acceptAlert() // redirect to baz38 .assert.urlEquals('http://localhost:8080/navigation-guards/baz')39 .assert.containsText('.view', 'baz (not saved)')40 .click('li:nth-child(2) a')41 .dismissAlert() // not saved42 .assert.urlEquals('http://localhost:8080/navigation-guards/baz')43 .assert.containsText('.view', 'baz (not saved)')44 .click('li:nth-child(2) a')45 .acceptAlert() // not saved, force leave46 .waitFor(100)47 .dismissAlert() // should trigger foo's guard48 .waitFor(100)49 .dismissAlert()50 .assert.urlEquals('http://localhost:8080/navigation-guards/baz')51 .assert.containsText('.view', 'baz')52 .click('li:nth-child(2) a')53 .acceptAlert()54 .waitFor(100)55 .acceptAlert()56 .assert.urlEquals('http://localhost:8080/navigation-guards/foo')57 .assert.containsText('.view', 'foo')58 // test initial visit59 browser60 .url('http://localhost:8080/navigation-guards/foo')61 .dismissAlert()62 .waitFor(100)63 .dismissAlert()64 // should redirect to root65 .assert.urlEquals('http://localhost:8080/navigation-guards/')66 // and should not render anything67 .assert.elementNotPresent('.view')68 .url('http://localhost:8080/navigation-guards/foo')69 .acceptAlert()70 .assert.urlEquals('http://localhost:8080/navigation-guards/foo')71 .assert.containsText('.view', 'foo')72 .url('http://localhost:8080/navigation-guards/bar')73 .dismissAlert()74 .waitFor(100)75 .dismissAlert()76 // should redirect to root77 .assert.urlEquals('http://localhost:8080/navigation-guards/')78 // and should not render anything79 .assert.elementNotPresent('.view')80 .url('http://localhost:8080/navigation-guards/bar')81 .acceptAlert()82 .assert.urlEquals('http://localhost:8080/navigation-guards/bar')83 .assert.containsText('.view', 'bar')84 },85 'navigation guards': function (browser) {86 browser87 // back to home88 .url('http://localhost:8080/navigation-guards/')89 .waitForElementVisible('#app', 1000)...
dismiss.spec.js
Source:dismiss.spec.js
1jest.mock("../../src/worker/services/appiumService");2const { appiumService } = require("../../src/worker/services/appiumService");3const { AppiumError, ActionError } = require("../../src/worker/errors");4const { alert } = require("../../main");5afterEach(() => {6 jest.resetAllMocks();7 jest.restoreAllMocks();8});9it("executes the 'dismissAlert' method on the Appium Service", async () => {10 const text = [ "Alert", "Hello World!" ].join("\n");11 jest.spyOn(appiumService, "getAlertText").mockResolvedValue(text);12 jest.spyOn(appiumService, "dismissAlert").mockResolvedValue(null);13 jest.spyOn(appiumService, "getAlertVisible").mockResolvedValue(false);14 await alert.dismiss();15 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);16 expect(appiumService.dismissAlert).toHaveBeenCalledTimes(1);17 expect(appiumService.getAlertVisible).toHaveBeenCalledTimes(1);18 expect(appiumService.getAlertVisible).toHaveBeenCalledWith({ text });19});20it("polls the 'getAlertVisible' method on the Appium Service until the alert is hidden", async () => {21 const text = [ "Alert", "Hello World!" ].join("\n");22 jest.spyOn(appiumService, "getAlertText").mockResolvedValue(text);23 jest.spyOn(appiumService, "dismissAlert").mockResolvedValue(null);24 jest.spyOn(appiumService, "getAlertVisible")25 .mockResolvedValueOnce(true)26 .mockResolvedValueOnce(true)27 .mockResolvedValueOnce(true)28 .mockResolvedValueOnce(false);29 await alert.dismiss();30 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);31 expect(appiumService.dismissAlert).toHaveBeenCalledTimes(1);32 expect(appiumService.getAlertVisible).toHaveBeenCalledTimes(4);33 expect(appiumService.getAlertVisible).toHaveBeenCalledWith({ text });34});35it("throws an ActionError if no alert is present", async () => {36 const error = new AppiumError("No alert present", 27);37 jest.spyOn(appiumService, "getAlertText").mockRejectedValue(error);38 expect.assertions(5);39 try {40 await alert.dismiss();41 } catch (err) {42 expect(err).toBeInstanceOf(ActionError);43 expect(err).toHaveProperty("message", "Failed to dismiss alert. No alert present.");44 }45 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);46 expect(appiumService.dismissAlert).toHaveBeenCalledTimes(0);47 expect(appiumService.getAlertVisible).toHaveBeenCalledTimes(0);48});49it("throws an ActionError if dismiss the alert fails", async () => {50 const error = new AppiumError("Request error.", 3);51 const text = [ "Alert", "Hello World!" ].join("\n");52 jest.spyOn(appiumService, "getAlertText").mockResolvedValue(text);53 jest.spyOn(appiumService, "dismissAlert").mockRejectedValue(error);54 expect.assertions(5);55 try {56 await alert.dismiss();57 } catch (err) {58 expect(err).toBeInstanceOf(ActionError);59 expect(err).toHaveProperty("message", "Failed to dismiss alert. Alert still present.");60 }61 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);62 expect(appiumService.dismissAlert).toHaveBeenCalledTimes(1);63 expect(appiumService.getAlertVisible).toHaveBeenCalledTimes(0);64});65it("throws an ActionError if alert is still visible after polling", async () => {66 jest.setTimeout(6000);67 const text = [ "Alert", "Hello World!" ].join("\n");68 jest.spyOn(appiumService, "getAlertText").mockResolvedValue(text);69 jest.spyOn(appiumService, "dismissAlert").mockResolvedValue(null);70 jest.spyOn(appiumService, "getAlertVisible").mockResolvedValue(true);71 expect.assertions(5);72 try {73 await alert.dismiss();74 } catch (err) {75 expect(err).toBeInstanceOf(ActionError);76 expect(err).toHaveProperty("message", "Failed to dismiss alert. Alert still visible after 5000ms.");77 }78 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);79 expect(appiumService.dismissAlert).toHaveBeenCalledTimes(1);80 expect(appiumService.getAlertVisible).toHaveBeenCalled();81});82it("propagates other types of errors", async () => {83 const error = new Error("Something went wrong.");84 const text = [ "Alert", "Hello World!" ].join("\n");85 jest.spyOn(appiumService, "getAlertText").mockResolvedValue(text);86 jest.spyOn(appiumService, "dismissAlert").mockRejectedValue(error);87 jest.spyOn(appiumService, "getAlertVisible").mockResolvedValue(true);88 expect.assertions(5);89 try {90 await alert.dismiss();91 } catch (err) {92 expect(err).toBeInstanceOf(error.constructor);93 expect(err).toHaveProperty("message", error.message);94 }95 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);96 expect(appiumService.dismissAlert).toHaveBeenCalledTimes(1);97 expect(appiumService.getAlertVisible).toHaveBeenCalledTimes(0);...
px-alert-message-dismiss-tests.js
Source:px-alert-message-dismiss-tests.js
1describe('Alert Dismiss', function() {2 it('`visible` is initially set to true when the alert message attaches', function() {3 var dismissAlert = fixture('PxDismissAlertMessage');4 flush(function() {5 expect(dismissAlert.visible).to.equal(true);6 });7 });8 it('`visible` is initially set to false when the alert message attaches if `disableAutoShow` is set', function() {9 var dismissAlert = fixture('PxDisableAutoShowAlertMessage');10 flush(function() {11 expect(dismissAlert.visible).to.equal(false);12 });13 });14 it('`visible` is set to false when the alert message is dismissed', function(done) {15 var dismissAlert = fixture('PxDismissAlertMessage');16 flush(function() {17 var closeBtn = Polymer.dom(dismissAlert.root).querySelector('#dismissButton');18 closeBtn.click();19 expect(dismissAlert.visible).to.equal(false);20 done();21 });22 });23 it('message is shown and hidden when `visible` is set via JavaScript', function(done) {24 var dismissAlert = fixture('PxDismissAlertMessage');25 flush(function() {26 dismissAlert.visible = false;27 var alert = Polymer.dom(dismissAlert.root).querySelector('#alert');28 // Wait 500ms for each animation to finish29 setTimeout(function() {30 expect(window.getComputedStyle(alert).display).to.equal('none');31 dismissAlert.visible = true;32 }, 500);33 setTimeout(function() {34 expect(window.getComputedStyle(alert).display).to.equal('flex');35 done();36 }, 1000);37 });38 });39 it('alert message is automatically dismissed after the `autoDismiss` timeout', function(done) {40 var dismissAlert = fixture('PxAutoDismissAlertShort');41 flush(function() {42 var alert = Polymer.dom(dismissAlert.root).querySelector('#alert');43 expect(window.getComputedStyle(alert).display).to.equal('flex');44 setTimeout(function() {45 expect(window.getComputedStyle(alert).display).to.equal('none');46 done();47 }, 1200); // wait long enough for autoDismiss timeout AND animation to finish48 });49 });50 it('alert message is automatically dismissed after the `autoDismiss` timeout when it is re-opened', function(done) {51 var dismissAlert = fixture('PxAutoDismissAlertShort');52 flush(function() {53 var alert = Polymer.dom(dismissAlert.root).querySelector('#alert');54 dismissAlert.visible = false;55 setTimeout(function() {56 dismissAlert.visible = true;57 }, 100);58 setTimeout(function() {59 expect(window.getComputedStyle(alert).display).to.equal('none');60 done();61 }, 1000); // wait long enough for autoDismiss timeout AND animation to finish62 });63 });64 it('alert message is hidden after px-alert-message-hidden event is fired when dismiss X is tapped', function(done) {65 var closeBtnAlert = fixture('PxBasicAlertMessage');66 var handler = closeBtnAlert.addEventListener('px-alert-message-hidden', function(evt) {67 expect(closeBtnAlert.querySelector('#alert').classList.contains('alert-message--visible')).equal(false);68 assert.equal(evt.type, 'px-alert-message-hidden', 'event name should be px-alert-message-hidden');69 closeBtnAlert.removeEventListener('px-alert-message-hidden', handler);70 done();71 });72 closeBtnAlert.action = "dismiss";73 flush(function(){74 closeBtnAlert.querySelector('.action .dismiss').click();75 });76 });77 it('alert message is hidden after px-alert-message-hidden event is fired when OK button is tapped', function(done) {78 var okBtnAlert = fixture('PxBasicAlertMessage');79 var handler = okBtnAlert.addEventListener('px-alert-message-hidden', function(evt) {80 expect(okBtnAlert.querySelector('#alert').classList.contains('alert-message--visible')).equal(false);81 assert.equal(evt.type, 'px-alert-message-hidden', 'event name should be px-alert-message-hidden');82 okBtnAlert.removeEventListener('px-alert-message-hidden', handler);83 done();84 });85 okBtnAlert.action = "acknowledge";86 flush(function(){87 okBtnAlert.querySelector('.action button').click();88 });89 });...
alerts.js
Source:alerts.js
...14 function ($rootScope, $timeout) {15 return {16 restrict: "E",17 replace: true,18 template: "<div ng-show='alert' class='alert alert-main alert-{{alert.type}} fade in push-x-10' ng-click='dismissAlert()'>"+19 "<button type='button' class='close' ng-click='dismissAlert()'>Ã</button>"+20 "<strong>{{ alert.title }}:</strong> {{ alert.msg }}</div>",21 link: function($scope, $element){ 22 $scope.dismissAlert = function(){23 $scope.alert = false;24 };25 $rootScope.$on("showAlert", function (event, alert) {26 $scope.alert = alert;27 event.stopPropagation();28 $timeout($scope.dismissAlert, 6000);29 });30 }31 };32 }33 ])34 /**35 * This directive will abstract display of transactional alerts.36 * Place it right above the <div class="content-body"></div> tag on the page where you37 * want your alert to appear38 * Usage: <mammoth-alerts></mammoth-alerts>39 */40 .directive("viewAlert",[function () {41 return {42 restrict: "E",43 replace: true,44 template: "<div ng-show='view_alert' class='alert alert-{{view_alert.type}} fade in push-y-10' ng-click='dismissAlert()'>"+45 "<button type='button' class='close' ng-click='dismissAlert()'>Ã</button>"+46 "<strong>{{ view_alert.title }}:</strong> {{ view_alert.msg }}</div>", 47 link: function($scope){48 $scope.dismissAlert = function(){49 $scope.view_alert = false;50 };51 }52 };53 }])54 /**55 * These are alerts ment to display messages for the system user as they do their56 * tasks. e.g. a success message to inform the user that an action was successful or not57 * Usage:58 * In your controller:59 * Call the service with the following params:...
handleModal.spec.js
Source:handleModal.spec.js
1import handleModal from 'src/support/action/handleModal';2describe('handleModal', () => {3 beforeEach(() => {4 global.browser = {5 acceptAlert: jest.fn(),6 dismissAlert: jest.fn(),7 getAlertText: jest.fn(),8 };9 });10 it('should call acceptAlert on the browser to close a alertbox', async () => {11 await handleModal('accept', 'alertbox');12 expect(global.browser.acceptAlert).toHaveBeenCalledTimes(1);13 expect(global.browser.dismissAlert).not.toHaveBeenCalled();14 expect(global.browser.getAlertText).not.toHaveBeenCalled();15 });16 it('should call acceptAlert on the browser to close a confirmbox', async () => {17 await handleModal('accept', 'confirmbox');18 expect(global.browser.acceptAlert).toHaveBeenCalledTimes(1);19 expect(global.browser.dismissAlert).not.toHaveBeenCalled();20 expect(global.browser.getAlertText).not.toHaveBeenCalled();21 });22 it('should call acceptAlert on the browser to close a prompt', async () => {23 await handleModal('accept', 'prompt');24 expect(global.browser.acceptAlert).toHaveBeenCalledTimes(1);25 expect(global.browser.dismissAlert).not.toHaveBeenCalled();26 expect(global.browser.getAlertText).not.toHaveBeenCalled();27 });28 it('should call acceptAlert on the browser to dismiss a alertbox', async () => {29 await handleModal('dismiss', 'alertbox');30 expect(global.browser.acceptAlert).toHaveBeenCalledTimes(1);31 expect(global.browser.dismissAlert).not.toHaveBeenCalled();32 expect(global.browser.getAlertText).not.toHaveBeenCalled();33 });34 it(35 'should call dismissAlert on the browser to dismiss a confirmbox',36 async () => {37 await handleModal('dismiss', 'confirmbox');38 expect(global.browser.dismissAlert).toHaveBeenCalledTimes(1);39 expect(global.browser.acceptAlert).not.toHaveBeenCalled();40 expect(global.browser.getAlertText).not.toHaveBeenCalled();41 }42 );43 it('should call dismissAlert on the browser to dismiss a prompt', async () => {44 await handleModal('dismiss', 'prompt');45 expect(global.browser.dismissAlert).toHaveBeenCalledTimes(1);46 expect(global.browser.acceptAlert).not.toHaveBeenCalled();47 expect(global.browser.getAlertText).not.toHaveBeenCalled();48 });...
Using AI Code Generation
1const wdio = require('webdriverio');2const options = {3 capabilities: {4 },5};6(async () => {7 const client = await wdio.remote(options);8 await client.pause(3000);9 await client.dismissAlert();10 await client.deleteSession();11})();12const wdio = require('webdriverio');13const options = {14 capabilities: {15 },16};17(async () => {18 const client = await wdio.remote(options);19 await client.pause(3000);20 await client.acceptAlert();21 await client.deleteSession();22})();23const wdio = require('webdriverio');24const options = {25 capabilities: {26 },27};28(async () => {29 const client = await wdio.remote(options);30 await client.pause(3000);31 const alertText = await client.getAlertText();32 console.log(alertText);33 await client.deleteSession();34})();35const wdio = require('webdriverio');36const options = {
Using AI Code Generation
1(async function test() {2 const { remote } = require('webdriverio');3 const opts = {4 capabilities: {5 },6 };7 const client = await remote(opts);8 const alertButton = await client.$('~Alert Views');9 await alertButton.click();10 const simpleAlertButton = await client.$('~Simple');11 await simpleAlertButton.click();12 const dismissButton = await client.$('~OK');13 await dismissButton.click();14 const alert = await client.$('~OK');15 await alert.dismissAlert();16 await client.deleteSession();17})();18(async function test() {19 const { remote } = require('webdriverio');20 const opts = {21 capabilities: {22 },23 };24 const client = await remote(opts);25 const alertButton = await client.$('~Alert Views');26 await alertButton.click();27 const simpleAlertButton = await client.$('~Simple');28 await simpleAlertButton.click();29 const dismissButton = await client.$('~OK');30 await dismissButton.click();31 const alert = await client.$('~OK');32 await alert.acceptAlert();33 await client.deleteSession();34})();35(async function test() {36 const { remote } = require('webdriverio');37 const opts = {38 capabilities: {39 },40 };41 const client = await remote(opts);
Using AI Code Generation
1driver.dismissAlert();2driver.acceptAlert();3driver.getAlertText();4driver.sendAlertText("Hello World");5driver.getDeviceTime();6driver.getGeoLocation();7driver.setGeoLocation();8driver.getPerformanceData();9driver.getPerformanceDataTypes();10driver.isAppInstalled();11driver.installApp();
Using AI Code Generation
1describe('Test', () => {2 it('should dismiss alert', async () => {3 await browser.execute('mobile: dismissAlert', {});4 });5});6describe('Test', () => {7 it('should dismiss alert', async () => {8 await browser.execute('mobile: dismissAlert', {});9 });10});11describe('Test', () => {12 it('should dismiss alert', async () => {13 await browser.execute('mobile: dismissAlert', {});14 });15});16describe('Test', () => {17 it('should dismiss alert', async () => {18 await browser.execute('mobile: dismissAlert', {});19 });20});21describe('Test', () => {22 it('should dismiss alert', async () => {23 await browser.execute('mobile: dismissAlert', {});24 });25});26describe('Test', () => {27 it('should dismiss alert', async () => {28 await browser.execute('mobile: dismissAlert', {});29 });30});31describe('Test', () => {32 it('should dismiss alert', async () => {33 await browser.execute('mobile: dismissAlert', {});34 });35});36describe('Test', () => {37 it('should dismiss alert', async () => {38 await browser.execute('mobile: dismissAlert', {});39 });40});41describe('Test', () => {42 it('should dismiss alert', async () => {43 await browser.execute('mobile: dismissAlert', {});44 });45});46describe('Test', () => {47 it('should dismiss alert', async () => {48 await browser.execute('mobile: dismissAlert', {});49 });50});51describe('Test', () => {52 it('should dismiss alert', async () => {
Using AI Code Generation
1await driver.dismissAlert();21) await driver.execute('mobile: dismissAlert', {});32) await driver.execute('mobile: dismissAlert', {'name': 'OK'});43) await driver.execute('mobile: dismissAlert', {'name': 'OK', 'buttonLabel': 'OK'});5await driver.execute('mobile: dismissAlert', {});6await driver.execute('mobile: dismissAlert', {'name': 'OK'});7await driver.execute('mobile: dismissAlert', {'name': 'OK', 'buttonLabel': 'OK'});8await driver.execute('mobile: dismissAlert', {'name': 'OK', 'buttonLabel': 'OK', 'buttonName': 'OK'});9await driver.execute('mobile: dismissAlert', {'name': 'OK', 'buttonLabel': 'OK', 'buttonName': 'OK', 'buttonType': 'XCUIElementTypeButton'});10await driver.execute('mobile: dismissAlert', {'buttonLabel': 'OK'});11await driver.execute('mobile: dismissAlert', {'buttonLabel': 'OK', 'buttonName': 'OK'});12await driver.execute('mobile: dismissAlert', {'buttonLabel': 'OK', 'buttonName': 'OK', 'buttonType': 'XCUIElementTypeButton'});13await driver.execute('mobile: dismissAlert', {'buttonLabel': 'OK', 'buttonName': 'OK', 'button
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!!