Skip to content

Commit

Permalink
chore: Update web e2e tests for WdIO compatibility (#1412)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach authored Jun 6, 2022
1 parent b4e2d43 commit ea3a636
Show file tree
Hide file tree
Showing 8 changed files with 624 additions and 605 deletions.
2 changes: 1 addition & 1 deletion ci-jobs/templates/xcuitest-e2e-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
deviceName: ${{ parameters.deviceName }}
vmImage: ${{ parameters.vmImage }}
script: |
npx mocha --timeout 480000 --reporter mocha-multi-reporters --reporter-options configFile=$(Build.SourcesDirectory)/ci-jobs/mocha-config.json build/test/functional/web/safari-basic-e2e-specs.js -g @skip-ci -i --exit
npx mocha --timeout 480000 --reporter mocha-multi-reporters --reporter-options configFile=$(Build.SourcesDirectory)/ci-jobs/mocha-config.json --recursive build/test/functional/web -g @skip-ci -i --exit
# - template: ./ios-e2e-template.yml
# parameters:
Expand Down
24 changes: 23 additions & 1 deletion test/functional/web/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ const GUINEA_PIG_IFRAME_PAGE = `${TEST_END_POINT}/iframes.html`;
// http://testsafebrowsing.appspot.com/ for alternatives
const PHISHING_END_POINT = 'http://testsafebrowsing.appspot.com/s/phishing.html';
const APPIUM_IMAGE = `${BASE_END_POINT}/appium.png`;
const newCookie = {
name: 'newcookie',
value: 'i am new here'
};
const oldCookie1 = {
name: 'guineacookie1',
value: 'i am a cookie value'
};
const oldCookie2 = {
name: 'guineacookie2',
value: 'cookié2'
};

function doesIncludeCookie (cookies, cookie) {
cookies.map((c) => c.name).should.include(cookie.name);
cookies.map((c) => c.value).should.include(cookie.value);
}
function doesNotIncludeCookie (cookies, cookie) {
cookies.map((c) => c.name).should.not.include(cookie.name);
cookies.map((c) => c.value).should.not.include(cookie.value);
}

async function spinTitle (driver) {
return await retry(10, async function () {
Expand Down Expand Up @@ -61,5 +82,6 @@ async function openPage (driver, url, tries = 10, interval = 500) {
export {
spinTitle, spinTitleEquals, spinWait, openPage, GUINEA_PIG_PAGE,
GUINEA_PIG_FRAME_PAGE, GUINEA_PIG_IFRAME_PAGE, PHISHING_END_POINT,
APPIUM_IMAGE, GUINEA_PIG_SCROLLABLE_PAGE, GUINEA_PIG_APP_BANNER_PAGE
APPIUM_IMAGE, GUINEA_PIG_SCROLLABLE_PAGE, GUINEA_PIG_APP_BANNER_PAGE,
doesIncludeCookie, doesNotIncludeCookie, newCookie, oldCookie1, oldCookie2,
};
57 changes: 32 additions & 25 deletions test/functional/web/safari-alerts-e2e-specs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import _ from 'lodash';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { retryInterval } from 'asyncbox';
import { SAFARI_CAPS } from '../desired';
import { SAFARI_CAPS, amendCapabilities } from '../desired';
import { initSession, deleteSession, MOCHA_TIMEOUT } from '../helpers/session';
import { GUINEA_PIG_PAGE } from './helpers';

Expand All @@ -15,10 +14,10 @@ describe('safari - alerts', function () {

let driver;
before(async function () {
const caps = _.defaults({
safariInitialUrl: GUINEA_PIG_PAGE,
safariAllowPopups: true,
}, SAFARI_CAPS);
const caps = amendCapabilities(SAFARI_CAPS, {
'appium:safariInitialUrl': GUINEA_PIG_PAGE,
'appium:safariAllowPopups': true,
});
driver = await initSession(caps);
});
after(async function () {
Expand All @@ -33,40 +32,48 @@ describe('safari - alerts', function () {
await retryInterval(5, 500, driver.dismissAlert.bind(driver));
}

it('should accept alert', async function () {
await driver.elementById('alert1').click();
// All tests below are skipped until https://github.com/appium/appium/issues/17013 is resolved

it.skip('should accept alert', async function () {
const alert = await driver.$('#alert1');
await alert.click();
await acceptAlert(driver);
(await driver.title()).should.include('I am a page title');
(await driver.getTitle()).should.include('I am a page title');
});

it('should dismiss alert', async function () {
await driver.elementById('alert1').click();
it.skip('should dismiss alert', async function () {
const alert = await driver.$('#alert1');
await alert.click();
await dismissAlert(driver);
(await driver.title()).should.include('I am a page title');
(await driver.getTitle()).should.include('I am a page title');
});

it('should get text of alert', async function () {
await driver.elementById('alert1').click();
(await driver.alertText()).should.include('I am an alert');
it.skip('should get text of alert', async function () {
const alert = await driver.$('#alert1');
await alert.click();
(await driver.getAlertText()).should.include('I am an alert');
await dismissAlert(driver);
});
it('should not get text of alert that closed', async function () {
await driver.elementById('alert1').click();
it.skip('should not get text of alert that closed', async function () {
const alert = await driver.$('#alert1');
await alert.click();
await acceptAlert(driver);
await driver.alertText()
await driver.getAlertText()
.should.eventually.be.rejectedWith(/An attempt was made to operate on a modal dialog when one was not open/);
});
it('should set text of prompt', async function () {
await driver.elementById('prompt1').click();
await driver.alertKeys('of course!');
it.skip('should set text of prompt', async function () {
const alert = await driver.$('#prompt1');
await alert.click();
await driver.sendAlertText('of course!');
await acceptAlert(driver);

const el = await driver.elementById('promptVal');
const el = await driver.$('#promptVal');
(await el.getAttribute('value')).should.eql('of course!');
});
it('should fail to set text of alert', async function () {
await driver.elementById('alert1').click();
await driver.alertKeys('yes I do!')
it.skip('should fail to set text of alert', async function () {
const alert = await driver.$('#alert1');
await alert.click();
await driver.sendAlertText('yes I do!')
.should.eventually.be.rejectedWith(/no input fields/);
});
});
27 changes: 2 additions & 25 deletions test/functional/web/safari-basic-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { SAFARI_CAPS, amendCapabilities } from '../desired';
import {
spinTitle, spinTitleEquals, spinWait, openPage, GUINEA_PIG_PAGE,
// GUINEA_PIG_SCROLLABLE_PAGE,
PHISHING_END_POINT, GUINEA_PIG_IFRAME_PAGE
PHISHING_END_POINT, GUINEA_PIG_IFRAME_PAGE,
doesIncludeCookie, doesNotIncludeCookie, newCookie, oldCookie1, oldCookie2
} from './helpers';
import { util } from '@appium/support';
import { retryInterval } from 'asyncbox';
Expand All @@ -16,28 +17,6 @@ chai.should();
chai.use(chaiAsPromised);
const expect = chai.expect;

function doesIncludeCookie (cookies, cookie) {
cookies.map((c) => c.name).should.include(cookie.name);
cookies.map((c) => c.value).should.include(cookie.value);
}
function doesNotIncludeCookie (cookies, cookie) {
cookies.map((c) => c.name).should.not.include(cookie.name);
cookies.map((c) => c.value).should.not.include(cookie.value);
}

const newCookie = {
name: 'newcookie',
value: 'i am new here'
};
const oldCookie1 = {
name: 'guineacookie1',
value: 'i am a cookie value'
};
const oldCookie2 = {
name: 'guineacookie2',
value: 'cookié2'
};

const DEFAULT_CAPS = amendCapabilities(SAFARI_CAPS, {
'appium:safariInitialUrl': GUINEA_PIG_PAGE,
// 'appium:safariLogAllCommunication': true,
Expand Down Expand Up @@ -547,5 +526,3 @@ describe('Safari - basics -', function () {
});
});
});

export { doesIncludeCookie, doesNotIncludeCookie, newCookie, oldCookie1, oldCookie2 };
134 changes: 69 additions & 65 deletions test/functional/web/safari-execute-e2e-specs.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import _ from 'lodash';
// import _ from 'lodash';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import http from 'http';
import { SAFARI_CAPS } from '../desired';
// import http from 'http';
import { SAFARI_CAPS, amendCapabilities } from '../desired';
import { initSession, deleteSession, MOCHA_TIMEOUT } from '../helpers/session';
import { openPage, GUINEA_PIG_PAGE } from './helpers';

Expand All @@ -21,10 +21,10 @@ describe('safari - execute -', function () {

let driver;
before(async function () {
const caps = _.defaults({
safariInitialUrl: GUINEA_PIG_PAGE,
showSafariConsoleLog: true,
}, SAFARI_CAPS);
const caps = amendCapabilities(SAFARI_CAPS, {
'appium:safariInitialUrl': GUINEA_PIG_PAGE,
'appium:showSafariConsoleLog': true,
});
driver = await initSession(caps);
});
after(async function () {
Expand All @@ -34,112 +34,116 @@ describe('safari - execute -', function () {
async function runTests (secure = false) { // eslint-disable-line require-await
describe('mobile: x methods', function () {
it('should run in native context', async function () {
await driver.execute('mobile: scroll', {direction: 'down'}).should.not.be.rejected;
await driver.executeScript('mobile: scroll', [{direction: 'down'}]).should.not.be.rejected;
});
});

describe('synchronous', function () {
it('should bubble up javascript errors', async function () {
await driver.execute(`'nan'--`).should.eventually.be.rejected;
await driver.executeScript(`'nan'--`, []).should.eventually.be.rejected;
});

it('should eval javascript', async function () {
await driver.execute('return 1 + 1').should.eventually.equal(2);
await driver.executeScript('return 1 + 1', []).should.eventually.equal(2);
});

it('should not be returning hardcoded results', async function () {
await driver.execute('return 1+1').should.eventually.equal(2);
await driver.executeScript('return 1+1', []).should.eventually.equal(2);
});

it(`should return nothing when you don't explicitly return`, async function () {
expect(await driver.execute('1+1')).to.not.exist;
expect(await driver.executeScript('1+1', [])).to.not.exist;
});

if (!secure) {
it('should execute code inside the web view', async function () {
await driver.execute(GET_RIGHT_INNERHTML).should.eventually.be.ok;
await driver.execute(GET_WRONG_INNERHTML).should.eventually.not.be.ok;
await driver.executeScript(GET_RIGHT_INNERHTML, []).should.eventually.be.ok;
await driver.executeScript(GET_WRONG_INNERHTML, []).should.eventually.not.be.ok;
});

it('should convert selenium element arg to webview element', async function () {
const el = await driver.elementById('useragent');
await driver.execute(SCROLL_INTO_VIEW, [el]);
const el = await driver.findElement('id', 'useragent');
await driver.executeScript(SCROLL_INTO_VIEW, [el]);
});

it('should catch stale or undefined element as arg', async function () {
const el = await driver.elementById('useragent');
return driver.execute(SCROLL_INTO_VIEW, [{'ELEMENT': (el.value + 1)}])
.should.eventually.be.rejectedWith(/Error converting element ID/);
const el = await driver.findElement('id', 'useragent');
return driver.executeScript(SCROLL_INTO_VIEW, [{'ELEMENT': (el.value + 1)}])
.should.eventually.be.rejected;
});

it('should be able to return multiple elements from javascript', async function () {
await driver.execute(GET_ELEM_BY_TAGNAME)
await driver.executeScript(GET_ELEM_BY_TAGNAME, [])
.should.eventually.have.length.above(0);
});
}

it('should pass along non-element arguments', async function () {
const arg = 'non-element-argument';
await driver.execute('var args = Array.prototype.slice.call(arguments, 0); return args[0];', [arg])
await driver.executeScript('var args = Array.prototype.slice.call(arguments, 0); return args[0];', [arg])
.should.eventually.equal(arg);
});

it('should handle return values correctly', async function () {
const arg = ['one', 'two', 'three'];
await driver.execute('var args = Array.prototype.slice.call(arguments, 0); return args;', arg)
await driver.executeScript('var args = Array.prototype.slice.call(arguments, 0); return args;', arg)
.should.eventually.eql(arg);
});
});

describe('asynchronous', function () {
it('should execute async javascript', async function () {
await driver.setAsyncScriptTimeout(1000);
await driver.executeAsync(`arguments[arguments.length - 1](123);`)
.should.eventually.equal(123);
});

it('should bubble up errors', async function () {
await driver.executeAsync(`arguments[arguments.length - 1]('nan'--);`)
.should.eventually.be.rejectedWith(/operator applied to value that is not a reference/);
});

it('should timeout when callback is not invoked', async function () {
await driver.setAsyncScriptTimeout(1000);
await driver.executeAsync(`return 1 + 2`)
.should.eventually.be.rejectedWith(/Timed out waiting for/);
});
});
// TODO: Update for WdIO compatibility
// describe('asynchronous', function () {
// it('should execute async javascript', async function () {
// await driver.setAsyncScriptTimeout(1000);
// await driver.executeAsync(`arguments[arguments.length - 1](123);`)
// .should.eventually.equal(123);
// });

// it('should bubble up errors', async function () {
// await driver.executeAsync(`arguments[arguments.length - 1]('nan'--);`)
// .should.eventually.be.rejectedWith(/operator applied to value that is not a reference/);
// });

// it('should timeout when callback is not invoked', async function () {
// await driver.setAsyncScriptTimeout(1000);
// await driver.executeAsync(`return 1 + 2`)
// .should.eventually.be.rejectedWith(/Timed out waiting for/);
// });
// });
}

describe('http', function () {
runTests();
describe('cors', function () {
let server;
const host = '127.0.0.1';
const port = 8080;
before(function () {
// create an http server so we can test CORS handling without
// going to an external site
server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write('appium-xcuitest-driver async execute tests');
res.end();
}).listen({host, port});
});
after(function () {
if (server) {
server.close();
}
});

it('should execute async javascript from a different site', async function () {
await driver.get(`http://${host}:${port}`);
await driver.setAsyncScriptTimeout(1000);
await driver.executeAsync(`arguments[arguments.length - 1](123);`)
.should.eventually.equal(123);
});
});
// TODO: Update for WdIO compatibility
// describe('cors', function () {
// let server;
// const host = '127.0.0.1';
// const port = 8080;
// before(function () {
// // create an http server so we can test CORS handling without
// // going to an external site
// server = http.createServer(function (req, res) {
// res.writeHead(200, {'Content-Type': 'text/html'});
// res.write('appium-xcuitest-driver async execute tests');
// res.end();
// }).listen({host, port});
// });
// after(function () {
// if (server) {
// server.close();
// }
// });

// it('should execute async javascript from a different site', async function () {
// await driver.navigateTo(`http://${host}:${port}`);
// await driver.setAsyncScriptTimeout(1000);
// await driver.executeAsync(`arguments[arguments.length - 1](123);`)
// .should.eventually.equal(123);
// });
// });
});

describe('https', function () {
before(async function () {
await openPage(driver, 'https://google.com');
Expand Down
Loading

0 comments on commit ea3a636

Please sign in to comment.