Best JavaScript code snippet using appium-xcuitest-driver
navigation-guards.js
Source:navigation-guards.js
...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)90 .assert.containsText('.view', 'home')91 .click('li:nth-child(4) a')92 .assert.urlEquals('http://localhost:8080/navigation-guards/baz')93 .assert.containsText('.view', 'baz (not saved)')94 .click('button')95 .assert.containsText('.view', 'baz (saved)')...
directweb.spec.js
Source:directweb.spec.js
...19 await directweb.addVirtualAuthenticator();20 await directweb.open(URL);21 });22 afterEach(async () => {23 await directweb.acceptAlert();24 });25 it("Should not register an empty username", async () => {26 await directweb.register("");27 const result = await directweb.getAlertText();28 expect(result).to.eq("missing username");29 });30 it("Should not authenticate a non-email username", async () => {31 await directweb.register(jabber.createFullName());32 const result = await directweb.getAlertText();33 expect(result).to.eq("Username Invalid");34 });35 it("Should not login a non-registered username", async () => {36 await directweb.login(username);37 const result = await directweb.getAlertText();38 expect(result).to.eq("User not found");39 });40 after(async () => {41 await directweb.close();42 });43});44describe("Register", () => {45 before(async () => {46 directweb = new DirectWeb();47 await directweb.addVirtualAuthenticator();48 await directweb.open(URL);49 });50 it("Should successfully register a user", async () => {51 await directweb.register(username);52 const result = await directweb.getAlertText();53 expect(result).to.eq(`${username} successfully registered!`);54 });55 after(async () => {56 await directweb.acceptAlert();57 });58});59describe("Login", () => {60 it("Should successfully login", async () => {61 await directweb.login(username);62 const result = await directweb.getAlertText();63 expect(result).to.eq(`${username} successfully logged in!`);64 });65 after(async () => {66 await directweb.acceptAlert();67 });68});69describe("Cross Validations", async () => {70 before(async () => {71 directweb1 = new DirectWeb();72 await directweb1.addVirtualAuthenticator();73 await directweb1.open(URL);74 await directweb1.register(username1);75 await directweb1.acceptAlert();76 });77 it("Authenticator A should not be able to login with username of Authenticator B", async () => {78 await directweb.login(username1);79 const result = await directweb.getAlertText();80 expect(result).to.eq("Your Identity could not be verified");81 });82 it("Authenticator B should not be able to login with username of Authenticator A", async () => {83 await directweb1.login(username);84 const result = await directweb1.getAlertText();85 expect(result).to.eq("Your Identity could not be verified");86 });87 after(async () => {88 await directweb.acceptAlert();89 await directweb1.acceptAlert();90 });91});92describe("Push Auth", async () => {93 afterEach(async () => {94 await directweb.acceptAlert();95 await directweb1.acceptAlert();96 });97 it("Should successfully request for push authentication", async () => {98 await directweb.pushAddAuth(directweb1, username1, emailService);99 const requestResult = await directweb.getAlertText();100 const grantResult = await directweb1.getAlertText();101 expect(requestResult).to.eq("Push auth successful!");102 expect(grantResult).to.eq("Successfully authenticated!");103 });104 it("Should successfully grant a push request", async () => {105 await directweb1.pushAddAuth(directweb, username, emailService);106 const requestResult = await directweb1.getAlertText();107 const grantResult = await directweb.getAlertText();108 expect(requestResult).to.eq("Push auth successful!");109 expect(grantResult).to.eq("Successfully authenticated!");110 });111});112describe("Add Authenticator", () => {113 afterEach(async () => {114 await directweb.acceptAlert();115 await directweb1.acceptAlert();116 });117 it("Should successfully request add authenticator", async () => {118 await directweb.pushAddAuth(directweb1, username1, emailService, 1);119 const requestResult = await directweb.getAlertText();120 const grantResult = await directweb1.getAlertText();121 expect(requestResult).to.eq("Add auth successful!");122 expect(grantResult).to.eq("Successfully authenticated!");123 });124 it("Should successfully grant to add authenticator", async () => {125 await directweb1.pushAddAuth(directweb, username, emailService, 1);126 const requestResult = await directweb1.getAlertText();127 const grantResult = await directweb.getAlertText();128 expect(requestResult).to.eq("Add auth successful!");129 expect(grantResult).to.eq("Successfully authenticated!");...
accept.spec.js
Source:accept.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 'acceptAlert' 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, "acceptAlert").mockResolvedValue(null);13 jest.spyOn(appiumService, "getAlertVisible").mockResolvedValue(false);14 await alert.accept();15 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);16 expect(appiumService.acceptAlert).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, "acceptAlert").mockResolvedValue(null);24 jest.spyOn(appiumService, "getAlertVisible")25 .mockResolvedValueOnce(true)26 .mockResolvedValueOnce(true)27 .mockResolvedValueOnce(true)28 .mockResolvedValueOnce(false);29 await alert.accept();30 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);31 expect(appiumService.acceptAlert).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.accept();41 } catch (err) {42 expect(err).toBeInstanceOf(ActionError);43 expect(err).toHaveProperty("message", "Failed to accept alert. No alert present.");44 }45 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);46 expect(appiumService.acceptAlert).toHaveBeenCalledTimes(0);47 expect(appiumService.getAlertVisible).toHaveBeenCalledTimes(0);48});49it("throws an ActionError if accepting 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, "acceptAlert").mockRejectedValue(error);54 expect.assertions(5);55 try {56 await alert.accept();57 } catch (err) {58 expect(err).toBeInstanceOf(ActionError);59 expect(err).toHaveProperty("message", "Failed to accept alert. Alert still present.");60 }61 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);62 expect(appiumService.acceptAlert).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, "acceptAlert").mockResolvedValue(null);70 jest.spyOn(appiumService, "getAlertVisible").mockResolvedValue(true);71 expect.assertions(5);72 try {73 await alert.accept();74 } catch (err) {75 expect(err).toBeInstanceOf(ActionError);76 expect(err).toHaveProperty("message", "Failed to accept alert. Alert still visible after 5000ms.");77 }78 expect(appiumService.getAlertText).toHaveBeenCalledTimes(1);79 expect(appiumService.acceptAlert).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, "acceptAlert").mockRejectedValue(error);87 jest.spyOn(appiumService, "getAlertVisible").mockResolvedValue(true);88 expect.assertions(5);89 try {90 await alert.accept();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.acceptAlert).toHaveBeenCalledTimes(1);97 expect(appiumService.getAlertVisible).toHaveBeenCalledTimes(0);...
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', () => {11 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', () => {17 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', () => {23 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', () => {29 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 () => {37 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', () => {44 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 });...
test.js
Source:test.js
...31 .dismissAlert()32 .assert.elementCount('.pending-task', 2)33 .click('.pending-task .delete')34 .pause(1000)35 .acceptAlert()36 .assert.elementCount('.pending-task', 1)37 .click('.completed-task .undo')38 .pause(1000)39 .assert.elementCount('.pending-task', 2)40 .assert.elementCount('.completed-task', 2)41 .click('.completed-task .delete')42 .pause(1000)43 .dismissAlert()44 .assert.elementCount('.completed-task', 2)45 .click('.completed-task .delete')46 .pause(1000)47 .acceptAlert()48 .assert.elementCount('.completed-task', 1)49 .setValue('input[type=text]', '')50 .click('button[name=addTask]')51 .acceptAlert()52 .assert.elementCount('.pending-task', 2)53 .click('.completed-task .delete')54 .acceptAlert()55 .assert.elementNotPresent('.completed')56 .click('.pending-task .delete')57 .acceptAlert()58 .click('.pending-task .delete')59 .acceptAlert()60 .assert.elementNotPresent('.pending')61 .end()62 }...
script.js
Source:script.js
1(function ($) {2 var acceptAlertCookieName = 'Composite.Web.Html.AcceptAlert';34 $(document).ready(function () {56 $(".accept-alert").each(function () {7 var acceptAlert = $(this).appendTo($("body"));8 var cookiesName = acceptAlertCookieName + acceptAlert.data('cookieskey');9 var acceptAlertCookies = getCookies(cookiesName);1011 if (acceptAlertCookies == "OK") {12 acceptAlert.remove();13 return;14 }1516 var okBtn = acceptAlert.find("button");17 okBtn.on("click", function () {18 setCookies(cookiesName, "OK");19 acceptAlert.animate({ height: '0' }, 500);20 });2122 setTimeout(function () { acceptAlert.animate({ height: acceptAlert.find(".container").innerHeight() }, 1000); }, 1000);23 });2425 $(window).on("resize", function () {26 $(".accept-alert").each(function () {27 var acceptAlert = $(this);28 var cookiesName = acceptAlertCookieName + acceptAlert.data('cookieskey');29 var acceptAlertCookie = getCookies(cookiesName);30 if (acceptAlertCookie != "OK") {31 acceptAlert.css("height", acceptAlert.find(".container").innerHeight());32 }33 });34 });3536 function getCookies(cookiesName) {37 if (typeof (Storage) !== "undefined") {38 return localStorage.getItem(cookiesName);39 }40 return "false";41 }4243 function setCookies(cookiesName, value) {44 if (typeof (Storage) !== "undefined") {45 localStorage.setItem(cookiesName, value);46 }47 }48 });
...
alert.test.js
Source:alert.test.js
...5 alertPage.clickOnAlertButton(1)6 const alertText = browser.getAlertText()7 console.log('Alert text is ', alertText)8 assert.equal('I am a JS Alert', alertText) //assert.equal() , expect - equals()9 browser.acceptAlert()10 console.log(alertPage.getResultText)11 assert.equal('You successfuly clicked an alert',alertPage.getResultText())12 browser.pause(3000)13 })14 it('dismiss alert', () =>{15 browser.url("https://the-internet.herokuapp.com/javascript_alerts")16 alertPage.clickOnAlertButton(2)17 browser.dismissAlert()18 console.log(alertPage.getResultText)19 assert.equal('You clicked: Cancel',alertPage.getResultText())20 browser.pause(3000)21 })22 it('enter text on alert and accept alert', () =>{23 browser.url("https://the-internet.herokuapp.com/javascript_alerts")24 alertPage.clickOnAlertButton(3)25 browser.sendAlertText('Viji')26 browser.acceptAlert()27 console.log(alertPage.getResultText)28 assert.equal('You entered: Viji',alertPage.getResultText())29 browser.pause(3000)30 })31 it('rediffmail accept alert', () =>{32 browser.url("https://mail.rediff.com/cgi-bin/login.cgi")33 $(`//input[@type='submit']`).waitForDisplayed();34 $(`//input[@type='submit']`).click();35 const alerttext = browser.getAlertText()36 console.log(alerttext)37 assert.equal('Please enter a valid user name',alerttext)38 browser.acceptAlert()39 browser.pause(3000)40 })...
acceptAlert.test.js
Source:acceptAlert.test.js
...3 test('acceptAlert function should accept Alert', async () => {4 const state = {5 browser: { acceptAlert: jest.fn() },6 };7 await acceptAlert(state);8 expect(state.browser.acceptAlert.mock.calls.length).toBe(1);9 });...
Using AI Code Generation
1const {remote} = require('webdriverio');2(async () => {3 const browser = await remote({4 capabilities: {5 }6 });7 await browser.acceptAlert();8 await browser.deleteSession();9})();10const {remote} = require('webdriverio');11(async () => {12 const browser = await remote({13 capabilities: {14 }15 });16 await browser.dismissAlert();17 await browser.deleteSession();18})();19const {remote} = require('webdriverio');20(async () => {21 const browser = await remote({22 capabilities: {23 }24 });25 await browser.getAlertText();26 await browser.deleteSession();27})();28const {remote} = require('webdriverio');29(async () => {30 const browser = await remote({31 capabilities: {32 }33 });34 await browser.sendAlertText('test');35 await browser.deleteSession();36})();37const {remote} = require('webdriverio');38(async () => {39 const browser = await remote({
Using AI Code Generation
1const wdio = require("webdriverio");2const opts = {3 capabilities: {4 }5};6async function main() {7 const client = await wdio.remote(opts);8 await client.pause(5000);9 const alert = await client.$("~Alert");10 await alert.click();11 await client.pause(5000);12 await client.acceptAlert();13 await client.pause(5000);14 await client.deleteSession();15}16main();17[HTTP] {"using":"accessibility id","value":"Alert"}18[WD Proxy] Got response with status 200: {"value":{},"sessionId":"6E0F6D8B-2F2F-46B0-8B6C-9C6B45C6F1E6","status":0}
Using AI Code Generation
1const wdio = require('webdriverio');2const opts = {3 capabilities: {4 }5};6async function main() {7 const client = await wdio.remote(opts);8 await client.pause(5000);9 await client.acceptAlert();10 await client.deleteSession();11}12main();13const wdio = require('webdriverio');14const { acceptAlert } = require('wdio-sync');15const opts = {16 capabilities: {17 }18};19async function main() {20 const client = await wdio.remote(opts);21 await client.pause(5000);22 await acceptAlert();23 await client.deleteSession();24}25main();
Using AI Code Generation
1const wdio = require("webdriverio");2const opts = {3 capabilities: {4 }5};6async function main() {7 const client = await wdio.remote(opts);8 await client.pause(5000);9 await client.acceptAlert();10}11main();12const wdio = require("webdriverio");13const opts = {14 capabilities: {15 }16};17async function main() {18 const client = await wdio.remote(opts);19 await client.pause(5000);20 await client.click("~show alert");21 await client.acceptAlert();22}23main();
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.acceptAlert();9 })10 .fin(function () { return driver.quit(); })11 .done();12driver.dismissAlert()13var wd = require('wd');14var assert = require('assert');15var desired = {16};17var driver = wd.promiseChainRemote('localhost', 4723);18 .init(desired)19 .then(function () {20 return driver.dismissAlert();21 })22 .fin(function () { return driver.quit(); })23 .done();24driver.getAlertText()25var wd = require('wd');26var assert = require('assert');27var desired = {
Using AI Code Generation
1var assert = require('assert');2var wd = require('wd');3var caps = {4};5var driver = wd.promiseChainRemote('localhost', 4723);6 .init(caps)7 .elementByAccessibilityId('Alerts')8 .click()9 .elementByAccessibilityId('Simple')10 .click()11 .elementByAccessibilityId('OK')12 .click()13 .acceptAlert()14 .nodeify(done);15var assert = require('assert');16var wd = require('wd');17var caps = {18};19var driver = wd.promiseChainRemote('localhost', 4723);20 .init(caps)21 .elementByAccessibilityId('Alerts')22 .click()23 .elementByAccessibilityId('Simple')24 .click()25 .elementByAccessibilityId('OK')26 .click()27 .dismissAlert()28 .nodeify(done);29var assert = require('assert');30var wd = require('wd');31var caps = {32};33var driver = wd.promiseChainRemote('localhost', 4723);34 .init(caps)35 .elementByAccessibilityId('Alerts')36 .click()37 .elementByAccessibilityId('Simple')38 .click()39 .elementByAccessibilityId('OK')40 .click()41 .getAlertText()42 .then(function (text) {43 console.log(text);44 })45 .nodeify(done);46var assert = require('assert');47var wd = require('wd');48var caps = {
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!!