Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated broken examples in nightwatch-v2 #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,6 @@ dist

# TernJS port file
.tern-port

# Mac files
.DS_Store
5,281 changes: 539 additions & 4,742 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
},
"devDependencies": {
"@nightwatch/selenium-server": "^4.5.0",
"chromedriver": "^111.0.0"
"chromedriver": "latest"
}
}
6 changes: 4 additions & 2 deletions tests/api/acceptAlert.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
test('acceptAlert() example test', function (browser) {
const buttonSelector = 'button[onclick="jsAlert()"]';

browser
.navigateTo('https://nightwatchjs.org/__e2e/window/alerts.html')
.click('#show-alert')
.navigateTo('https://the-internet.herokuapp.com/javascript_alerts')
.click(buttonSelector)
.ensure.alertIsPresent('> there is an alert open')
.pause(500)
.acceptAlert()
Expand Down
26 changes: 19 additions & 7 deletions tests/api/captureBrowserExceptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,31 @@ test('captureBrowserExceptions() example test', async function (browser) {
console.log('>>> Exception:', event);
});

const buttonEl = await browser.navigateTo('https://nightwatchjs.org/__e2e/window/').findElement('#openWindowBttn');
await browser.navigateTo('https://the-internet.herokuapp.com/windows');

const buttonIdAttr = await browser.executeScript(function () {
// Create a new button
const button = document.createElement('button');
button.innerText = 'Test Button';
button.id = 'testButton';
button.setAttribute('onclick', 'throw new Error("Test error.")');

// Append the button to the body
document.body.appendChild(button);

// Return the button's `id` attribute so we can use it later
return button.id;
});

await browser
.executeScript(function (el) {
el.setAttribute('onclick', 'throw new Error("Test error.")');
}, [buttonEl])
.click(buttonEl)
.waitForElementVisible(`#${buttonIdAttr}`, 1000) // wait for the button to be visible
.click(`#${buttonIdAttr}`)
.perform(function () {
expect(eventData).is.an('object').and.not.null;
expect(eventData).to.have.own.property('exceptionDetails');

const {exceptionDetails} = eventData;
const { exceptionDetails } = eventData;
expect(exceptionDetails.exception.description).to.include('Test error');
});

});
});
2 changes: 1 addition & 1 deletion tests/api/click.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ describe('.click() example test', function () {
pageLoadStrategy: 'eager'
};

const githubButton = 'a[aria-label="Nightwatch on Github"]';
const githubButton = 'a[href="https://github.com/nightwatchjs/"]';

before(browser => browser.navigateTo('https://nightwatchjs.org/'));

Expand Down
9 changes: 6 additions & 3 deletions tests/api/closeWindow.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
describe('.closeWindow() example test', function () {

beforeEach(browser => browser.navigateTo('https://nightwatchjs.org/__e2e/window/'));
beforeEach(browser => browser.navigateTo('https://the-internet.herokuapp.com/windows'));
afterEach(browser => browser.end());

it('basic window test', function (browser) {
browser
.windowHandles(result => {
expect(result.value).to.have.length(1);
})
.click('#openWindowBttn')
.waitForElementVisible('a[target="_blank"]', 1000)
.click('a[target="_blank"]')
.windowHandles(result => {
expect(result.value).to.have.length(2);
})
Expand All @@ -22,7 +23,9 @@ describe('.closeWindow() example test', function () {
});

it('async window test', async function (browser) {
await browser.click('#openWindowBttn');
await browser
.waitForElementVisible('a[target="_blank"]', 1000)
.click('a[target="_blank"]')

let windowHandles = await browser.windowHandles();
expect(windowHandles).to.have.length(2);
Expand Down
6 changes: 4 additions & 2 deletions tests/api/dismissAlert.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
test('dismissAlert() example test', function (browser) {
const buttonSelector = 'button[onclick="jsAlert()"]';

browser
.navigateTo('https://nightwatchjs.org/__e2e/window/alerts.html')
.click('#show-alert')
.navigateTo('https://the-internet.herokuapp.com/javascript_alerts')
.click(buttonSelector)
.ensure.alertIsPresent('> there is an alert open')
.pause(500)
.dismissAlert(function (result) {
Expand Down
4 changes: 2 additions & 2 deletions tests/api/dragAndDrop.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
describe('dragAndDrop() example test', function () {

// Should remove this example as dragAndDrop is available from V 3.0.0
it('demo test', function (browser) {
browser
.navigateTo('https://mdn.github.io/dom-examples/drag-and-drop/copy-move-DataTransfer.html')
.waitForElementVisible('#src_copy')
.pause(1000)
.assert.visible('#dest_copy')
.dragAndDrop('#src_copy', '#dest_copy')
.assert.elementPresent('#src_copy #newId');
// .assert.elementPresent('#src_copy #newId');
});
});

2 changes: 1 addition & 1 deletion tests/api/findElement.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ describe('findElement() example test', function () {
it('demo test async', async function (browser) {
await browser.navigateTo('https://nightwatchjs.org/');

const resultElement = await browser.findElement('#index-container');
const resultElement = await browser.findElement('#header');
browser.assert.ok('element-6066-11e4-a52e-4f735466cecf' in resultElement);
console.log('Element ID:', resultElement.getId());
});
Expand Down
5 changes: 3 additions & 2 deletions tests/api/findElements.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
describe('findElements() example test', function () {
it('demo test async', async function (browser) {
await browser.navigateTo('https://nightwatchjs.org/');
const resultElements = await browser.findElements('.nav-links-left .nav-item');
const resultElements = await browser.findElements('.navigation .navigation-list');

await expect(resultElements).to.have.length(4);
await expect(resultElements).to.have.length(2);

resultElements.forEach((item) => console.log('Element Id:', item.getId()));
});
});

1 change: 0 additions & 1 deletion tests/api/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ describe('frame() example test', function () {

browser
.navigateTo('https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe')
.ensure.ableToSwitchToFrame(frameId)
.frame(frameId, function (result) {
console.log(result);
})
Expand Down
9 changes: 5 additions & 4 deletions tests/api/getAlertText.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
test('getAlertText() example test', function (browser) {

test('getAlertText() example test', function (browser) {
const buttonSelector = 'button[onclick="jsAlert()"]';
browser
.navigateTo('https://nightwatchjs.org/__e2e/window/alerts.html')
.click('#show-confirm')
.navigateTo('https://the-internet.herokuapp.com/javascript_alerts')
.click(buttonSelector)
.ensure.alertIsPresent('> there is an alert open')
.pause(500)
.getAlertText(function (res) {
browser.assert.strictEqual(res.value, 'Are you sure?');
browser.assert.strictEqual(res.value, 'I am a JS Alert');
});

});
2 changes: 1 addition & 1 deletion tests/api/getTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ describe('getTitle() example test', function () {

it('demo test', async function (browser) {
const title = await browser.getTitle();
await browser.assert.equal(title, 'Nightwatch.js | Node.js powered End-to-End testing framework');
await browser.assert.equal(title, 'Nightwatch V3 | Node.js powered End-to-End testing framework');
});
});
1 change: 1 addition & 0 deletions tests/api/isSelected.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ describe('isSelected() example test', function () {
it('demo test', function (browser) {
browser
.navigateTo('https://www.w3docs.com/learn-html/html-select-tag.html')
.waitForElementVisible('select option[value=books]', 2000) // Time added because it is behaving is flaky test
.isSelected('select option[value=books]', function (result) {
this.assert.equal(result.value, true);
});
Expand Down
4 changes: 2 additions & 2 deletions tests/api/registerBasicAuth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ describe('registerBasicAuth() example test', function () {

it('registerBasicAuth test', function (browser) {
browser
.registerBasicAuth('test', 'test')
.navigateTo('http://browserspy.dk/password-ok.php')
.registerBasicAuth('admin', 'admin')
.navigateTo('https://the-internet.herokuapp.com/basic_auth')
.waitForElementVisible('#content');
});
});
17 changes: 12 additions & 5 deletions tests/api/switchToWindow.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
describe('.switchToWindow() example test', function () {

beforeEach(browser => browser.navigateTo('https://nightwatchjs.org/__e2e/window/'));
beforeEach(browser => browser.navigateTo('https://example.com'));
afterEach(browser => browser.end());

it('async window test', async function (browser) {
await browser.pause(2000);
await browser.click('#openWindowBttn');
await browser
.pause(2000)
.execute(function () {
document.querySelector('a[href="https://www.iana.org/domains/example"]').target = '_blank';
})
.click('a[href="https://www.iana.org/domains/example"]')


let windowHandles = await browser.windowHandles();
browser.assert.equal(windowHandles.length, 2);

await browser.switchWindow(windowHandles[1]);
await browser.waitForElementVisible('form');

await browser.pause(3000);
let url = await browser.url();
browser.assert.equal(url, 'https://www.iana.org/help/example-domains');

await browser.pause(3000);
});
});
46 changes: 30 additions & 16 deletions tests/api/windowHandles.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
describe('.windowHandles() example test', function () {

beforeEach(browser => browser.navigateTo('https://nightwatchjs.org/__e2e/window/'));
beforeEach(browser => browser.url('https://example.com'));

afterEach(browser => browser.end());

it('basic window test', function (browser) {
browser
.windowHandles(res => {
browser.assert.equal(res.value.length, 1);
it('opens a new window when clicking a link', async function (browser) {

await browser
.windowHandles(result => {
browser.assert.equal(result.value.length, 1);
})
.click('#openWindowBttn')
.windowHandles(res => {
browser.assert.equal(res.value.length, 2);
.execute(function () {
// modify <a> element to open link in a new tab
document.querySelector('a[href="https://www.iana.org/domains/example"]').target = '_blank';
})
.click('a[href="https://www.iana.org/domains/example"]')
.windowHandles(result => {
browser.assert.equal(result.value.length, 2);
})
.windowHandles(function (result) {
console.log('result:', result);
});
});

it('async window test', async function (browser) {
await browser.click('#openWindowBttn');
it('can interact with elements in the new window', async function (browser) {
nothuman2718 marked this conversation as resolved.
Show resolved Hide resolved

let windowHandles = await browser.windowHandles();
browser.assert.equal(windowHandles.length, 2);
await browser.execute(function () {
// modify <a> element to open link in a new tab
document.querySelector('a[href="https://www.iana.org/domains/example"]').target = '_blank'; // Link to another page
});

await browser.switchWindow(windowHandles[1]);
await browser.waitForElementVisible('form');
});
});
await browser.click('a[href="https://www.iana.org/domains/example"]');

let result = await browser.windowHandles();
browser.assert.equal(result.length, 2);

console.log('result:', result);
await browser.switchWindow(result[1]);

await browser.waitForElementVisible('#header');
browser.assert.visible('#header');
});
});
17 changes: 9 additions & 8 deletions tests/guide/duckDuckGo.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
describe('duckduckgo example', function() {
it('Search Nightwatch.js and check results', function(browser) {
describe('duckduckgo example', function () {
it('Search Nightwatch.js and check results', function (browser) {
browser
.navigateTo('https://duckduckgo.com')
.waitForElementVisible('#search_form_input_homepage')
.sendKeys('#search_form_input_homepage', ['Nightwatch.js'])
.click('#search_button_homepage')
.assert.visible('.results--main')
.assert.textContains('.results--main', 'Nightwatch.js');
});
.waitForElementVisible('input[name="q"]')
.sendKeys('input[name="q"]', ['Nightwatch.js'])
.submitForm('form')
.assert.visible('.react-results--main')
.assert.textContains('.react-results--main', 'Nightwatch.js')
.end();
});
});
9 changes: 4 additions & 5 deletions tests/guide/shadowRootExample.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
describe('Shadow Root example test', function() {
describe('Shadow Root example test', function () {

// Doesn't work in Firefox due to a serialization issue in Geckodriver
this.disabled = this.settings.desiredCapabilities.browserName === 'firefox';

it('retrieve the shadowRoot', async function(browser) {
it('retrieve the shadowRoot', async function (browser) {
await browser
.navigateTo('https://mdn.github.io/web-components-examples/popup-info-box-web-component/')
.waitForElementVisible('form');
.waitForElementVisible('form')

const shadowRootEl = await browser.getShadowRoot('popup-info');
const infoElement = await shadowRootEl.find('.info');

await expect(infoElement.property('innerHTML'))
.to.be.a('string')
.and.to.include('card validation code');

const iconElement = await shadowRootEl.find('.icon');
const firstElement = await browser.getFirstElementChild(iconElement);
const firstElement = await browser.getFirstElementChild(iconElement)

await expect.element(firstElement).to.be.an('img');

Expand Down