Skip to content

Commit

Permalink
[js] switchTo().window() should use a 'handle' parameter rather than …
Browse files Browse the repository at this point in the history
…`name` (#2718)

This is to match the W3C Webdriver spec.
  • Loading branch information
Standard8 authored and jleyba committed Sep 7, 2016
1 parent 4d79679 commit be7ff6f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 15 deletions.
5 changes: 4 additions & 1 deletion javascript/node/selenium-webdriver/lib/webdriver.js
Original file line number Diff line number Diff line change
Expand Up @@ -1656,7 +1656,10 @@ class TargetLocator {
window(nameOrHandle) {
return this.driver_.schedule(
new command.Command(command.Name.SWITCH_TO_WINDOW).
setParameter('name', nameOrHandle),
// "name" supports the legacy drivers. "handle" is the W3C
// compliant parameter.
setParameter('name', nameOrHandle).
setParameter('handle', nameOrHandle),
'WebDriver.switchTo().window(' + nameOrHandle + ')');
}

Expand Down
94 changes: 80 additions & 14 deletions javascript/node/selenium-webdriver/test/lib/webdriver_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,10 @@ describe('WebDriver', function() {
let e = new error.NoSuchWindowError('window not found');
let executor = new FakeExecutor().
expect(CName.SWITCH_TO_WINDOW).
withParameters({'name': 'foo'}).
withParameters({
'name': 'foo',
'handle': 'foo'
}).
andReturnError(e).
end();

Expand All @@ -439,7 +442,10 @@ describe('WebDriver', function() {
let e = new error.NoSuchWindowError('window not found');
let executor = new FakeExecutor().
expect(CName.SWITCH_TO_WINDOW).
withParameters({'name': 'foo'}).
withParameters({
'name': 'foo',
'handle': 'foo'
}).
andReturnError(e).
expect(CName.GET_TITLE).
andReturnSuccess('Google Search').
Expand All @@ -456,7 +462,10 @@ describe('WebDriver', function() {
let e = new error.NoSuchWindowError('window not found');
let executor = new FakeExecutor().
expect(CName.SWITCH_TO_WINDOW).
withParameters({'name':'foo'}).
withParameters({
'name': 'foo',
'handle': 'foo'
}).
andReturnError(e).
end();

Expand All @@ -467,7 +476,10 @@ describe('WebDriver', function() {
it('testErrbacksThatReturnErrorsStillSwitchToCallbackChain', function() {
let executor = new FakeExecutor().
expect(CName.SWITCH_TO_WINDOW).
withParameters({'name':'foo'}).
withParameters({
'name': 'foo',
'handle': 'foo'
}).
andReturnError(new error.NoSuchWindowError('window not found')).
end();

Expand All @@ -479,7 +491,10 @@ describe('WebDriver', function() {

it('testErrbacksThrownCanOverrideOriginalError', function() {
let executor = new FakeExecutor().
expect(CName.SWITCH_TO_WINDOW, {'name': 'foo'}).
expect(CName.SWITCH_TO_WINDOW, {
'name': 'foo',
'handle': 'foo'
}).
andReturnError(new error.NoSuchWindowError('window not found')).
end();

Expand Down Expand Up @@ -572,7 +587,10 @@ describe('WebDriver', function() {
let e = new error.NoSuchWindowError('window not found');
let executor = new FakeExecutor().
expect(CName.GET_TITLE).
expect(CName.SWITCH_TO_WINDOW, {'name': 'foo'}).
expect(CName.SWITCH_TO_WINDOW, {
'name': 'foo',
'handle': 'foo'
}).
andReturnError(e).
end();

Expand All @@ -587,8 +605,11 @@ describe('WebDriver', function() {
it('canBeSuppressWhenTheyOccur', function() {
let executor = new FakeExecutor().
expect(CName.GET_TITLE).
expect(CName.SWITCH_TO_WINDOW, {'name':'foo'}).
andReturnError(new error.NoSuchWindowError('window not found')).
expect(CName.SWITCH_TO_WINDOW, {
'name': 'foo',
'handle': 'foo'
}).
andReturnError(new error.NoSuchWindowError('window not found')).
expect(CName.CLOSE).
end();

Expand All @@ -605,7 +626,10 @@ describe('WebDriver', function() {
let e = new error.NoSuchWindowError('window not found');
let executor = new FakeExecutor().
expect(CName.GET_TITLE).
expect(CName.SWITCH_TO_WINDOW, {'name':'foo'}).
expect(CName.SWITCH_TO_WINDOW, {
'name': 'foo',
'handle': 'foo'
}).
andReturnError(e).
end();

Expand All @@ -623,8 +647,11 @@ describe('WebDriver', function() {
let executor = new FakeExecutor().
expect(CName.GET_TITLE).
expect(CName.GET_CURRENT_URL).
expect(CName.SWITCH_TO_WINDOW, {'name':'foo'}).
andReturnError(new StubError()).
expect(CName.SWITCH_TO_WINDOW, {
'name': 'foo',
'handle': 'foo'
}).
andReturnError(new StubError()).
expect(CName.CLOSE).
end();

Expand Down Expand Up @@ -664,7 +691,10 @@ describe('WebDriver', function() {
it('fromAnErrbackSuppressesTheError', function() {
var count = 0;
let executor = new FakeExecutor().
expect(CName.SWITCH_TO_WINDOW, {'name':'foo'}).
expect(CName.SWITCH_TO_WINDOW, {
'name': 'foo',
'handle': 'foo'
}).
andReturnError(new StubError()).
expect(CName.GET_CURRENT_URL).
andReturnSuccess('http://www.google.com').
Expand Down Expand Up @@ -791,8 +821,11 @@ describe('WebDriver', function() {

it('hasANestedCommandThatFails', function() {
let executor = new FakeExecutor().
expect(CName.SWITCH_TO_WINDOW, {'name': 'foo'}).
andReturnError(new StubError()).
expect(CName.SWITCH_TO_WINDOW, {
'name': 'foo',
'handle': 'foo'
}).
andReturnError(new StubError()).
end();

var driver = executor.createDriver();
Expand Down Expand Up @@ -1505,6 +1538,39 @@ describe('WebDriver', function() {
});
});

describe("switchTo()", function() {
describe("window", function() {
it('should return a resolved promise when the window is found', function() {
let executor = new FakeExecutor().
expect(CName.SWITCH_TO_WINDOW).
withParameters({
'name': 'foo',
'handle': 'foo'
}).
andReturnSuccess().
end();

executor.createDriver().switchTo().window('foo');
return waitForIdle();
});

it('should propogate exceptions', function() {
let e = new error.NoSuchWindowError('window not found');
let executor = new FakeExecutor().
expect(CName.SWITCH_TO_WINDOW).
withParameters({
'name': 'foo',
'handle': 'foo'
}).
andReturnError(e).
end();

executor.createDriver().switchTo().window('foo');
return waitForAbort().then(v => assert.strictEqual(v, e));
});
});
});

describe('elementEquality', function() {
it('isReflexive', function() {
var a = new WebElement({}, 'foo');
Expand Down
38 changes: 38 additions & 0 deletions javascript/node/selenium-webdriver/test/window_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'use strict';

var Browser = require('..').Browser,
By = require('..').By,
assert = require('../testing/assert'),
test = require('../lib/test');

Expand Down Expand Up @@ -53,6 +54,26 @@ test.suite(function(env) {
changeSizeBy(-20, -20);
});

test.it('can switch to a new window', function() {
driver.get(test.Pages.xhtmlTestPage);

driver.getWindowHandle().then(function(handle) {
driver.getAllWindowHandles().then(function(originalHandles) {
driver.findElement(By.linkText("Open new window")).click();

driver.wait(forNewWindowToBeOpened(originalHandles), 2000);

assert(driver.getTitle()).equalTo("XHTML Test Page");

getNewWindowHandle(originalHandles).then(function(newHandle) {
driver.switchTo().window(newHandle);

assert(driver.getTitle()).equalTo("We Arrive Here")
});
});
});
});

// See https://github.com/mozilla/geckodriver/issues/113
test.ignore(env.browsers(Browser.FIREFOX)).
it('can set the window position of the current window', function() {
Expand Down Expand Up @@ -134,4 +155,21 @@ test.suite(function(env) {
});
};
}

function forNewWindowToBeOpened(originalHandles) {
return function() {
return driver.getAllWindowHandles().then(function(currentHandles) {
return currentHandles.length > originalHandles.length;
});
};
}

function getNewWindowHandle(originalHandles) {
// Note: this assumes there's just one new window.
return driver.getAllWindowHandles().then(function(currentHandles) {
return currentHandles.filter(function(i) {
return originalHandles.indexOf(i) < 0;
})[0];
});
}
});

0 comments on commit be7ff6f

Please sign in to comment.