Skip to content

Commit 17240b3

Browse files
committed
[js] Replace WebElement.getSize/getLocation with a single getRect
1 parent 2e91397 commit 17240b3

File tree

6 files changed

+39
-43
lines changed

6 files changed

+39
-43
lines changed

javascript/node/selenium-webdriver/CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ mode.
122122
- Dropped support for "requiredCapabilities" from WebDriver.createSession
123123
- actions now returns the new `lib/input.Actions` class
124124
- removed touchActions
125+
* Changes to `lib/webdriver.WebElement`:
126+
- Replaced getSize & getLocation with getRect
125127
* Changes to `lib/webdriver.Alert`:
126128
- Removed authenticateAs
127129
* Changes to `lib/webdriver.Options` (`driver.manage()`):

javascript/node/selenium-webdriver/lib/command.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ const Name = {
145145
IS_ELEMENT_DISPLAYED: 'isElementDisplayed',
146146
GET_ELEMENT_LOCATION: 'getElementLocation',
147147
GET_ELEMENT_LOCATION_IN_VIEW: 'getElementLocationOnceScrolledIntoView',
148+
GET_ELEMENT_RECT: 'getElementRect',
148149
GET_ELEMENT_SIZE: 'getElementSize',
149150
GET_ELEMENT_ATTRIBUTE: 'getElementAttribute',
150151
GET_ELEMENT_VALUE_OF_CSS_PROPERTY: 'getElementValueOfCssProperty',

javascript/node/selenium-webdriver/lib/http.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,7 @@ const W3C_COMMAND_MAP = new Map([
292292
// Element interaction.
293293
[cmd.Name.GET_ELEMENT_TAG_NAME, get('/session/:sessionId/element/:id/name')],
294294
[cmd.Name.GET_ELEMENT_VALUE_OF_CSS_PROPERTY, get('/session/:sessionId/element/:id/css/:propertyName')],
295-
[cmd.Name.GET_ELEMENT_LOCATION, get('/session/:sessionId/element/:id/rect')],
296-
[cmd.Name.GET_ELEMENT_SIZE, get('/session/:sessionId/element/:id/rect')],
295+
[cmd.Name.GET_ELEMENT_RECT, get('/session/:sessionId/element/:id/rect')],
297296
[cmd.Name.CLEAR_ELEMENT, post('/session/:sessionId/element/:id/clear')],
298297
[cmd.Name.CLICK_ELEMENT, post('/session/:sessionId/element/:id/click')],
299298
[cmd.Name.SEND_KEYS_TO_ELEMENT, post('/session/:sessionId/element/:id/value')],

javascript/node/selenium-webdriver/lib/webdriver.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2082,26 +2082,27 @@ class WebElement {
20822082
}
20832083

20842084
/**
2085-
* Computes this element's bounding box, in pixels.
2085+
* Returns an object describing an element's location, in pixels relative to
2086+
* the document element, and the element's size in pixels.
20862087
*
2087-
* @return {!Promise<{width: number, height: number}>} A
2088-
* promise that will be resolved with the element's size as a
2089-
* {@code {width:number, height:number}} object.
2088+
* @return {!Promise<{width: number, height: number, x: number, y: number}>}
2089+
* A promise that will resolve with the element's rect.
20902090
*/
2091-
getSize() {
2092-
return this.execute_(new command.Command(command.Name.GET_ELEMENT_SIZE));
2093-
}
2094-
2095-
/**
2096-
* Computes the location of this element in page space.
2097-
*
2098-
* @return {!Promise<{x: number, y: number}>} A promise that
2099-
* will be resolved to the element's location as a
2100-
* {@code {x:number, y:number}} object.
2101-
*/
2102-
getLocation() {
2103-
return this.execute_(
2104-
new command.Command(command.Name.GET_ELEMENT_LOCATION));
2091+
async getRect() {
2092+
try {
2093+
return await this.execute_(
2094+
new command.Command(command.Name.GET_ELEMENT_RECT));
2095+
} catch (err) {
2096+
if (err instanceof error.UnknownCommandError) {
2097+
const {width, height} =
2098+
await this.execute_(
2099+
new command.Command(command.Name.GET_ELEMENT_SIZE));
2100+
const {x, y} =
2101+
await this.execute_(
2102+
new command.Command(command.Name.GET_ELEMENT_LOCATION));
2103+
return {x, y, width, height};
2104+
}
2105+
}
21052106
}
21062107

21072108
/**

javascript/node/selenium-webdriver/test/actions_test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ test.suite(function(env) {
6666
await driver.get(fileServer.whereIs('/data/actions/record_click.html'));
6767

6868
const div = await driver.findElement(By.css('div'));
69+
const rect = await div.getRect();
70+
assert.deepEqual(rect, {width: 500, height: 500, x: 0, y: 0});
6971

7072
await driver.actions({bridge: true}).click(div).perform();
7173

@@ -77,6 +79,8 @@ test.suite(function(env) {
7779
await driver.get(fileServer.whereIs('/data/actions/record_click.html'));
7880

7981
const div = await driver.findElement(By.css('div'));
82+
const rect = await div.getRect();
83+
assert.deepEqual(rect, {width: 500, height: 500, x: 0, y: 0});
8084

8185
await driver.actions({bridge: true})
8286
.move({x: 10, y: 10, origin: div})

javascript/node/selenium-webdriver/test/rect_test.js

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,27 @@ const {By} = require('..');
2323
const test = require('../lib/test');
2424

2525
test.suite(function(env) {
26-
describe('rect commands', function() {
26+
describe('WebElement', function() {
2727
let driver;
28-
let el;
2928

3029
before(async function() {
3130
driver = await env.builder().build();
32-
await driver.get(
33-
'data:text/html,<!DOCTYPE html><style>'
34-
+ '*{padding:0; margin:0}'
35-
+ 'div{position: absolute; top: 50px; left: 50px;'
36-
+ 'height: 50px;width:50px;background: green;}'
37-
+ '</style><div>Hello</div>');
38-
el = await driver.findElement(By.css('div'));
3931
});
4032

4133
after(function() {
42-
if (driver) {
43-
return driver.quit();
44-
}
45-
});
46-
47-
it('WebElement.getLocation()', async function() {
48-
let location = await el.getLocation();
49-
assert.equal(location.x, 50);
50-
assert.equal(location.y, 50);
34+
return driver.quit();
5135
});
5236

53-
it('WebElement.getSize()', async function() {
54-
let size = await el.getSize();
55-
assert.equal(size.width, 50);
56-
assert.equal(size.height, 50);
37+
it('getRect()', async function() {
38+
await driver.get(
39+
'data:text/html,<!DOCTYPE html><style>'
40+
+ '*{padding:0; margin:0}'
41+
+ 'div{position: absolute; top: 50px; left: 40px;'
42+
+ 'height: 25px;width:35px;background: green;}'
43+
+ '</style><div>Hello</div>');
44+
const el = await driver.findElement(By.css('div'));
45+
const rect = await el.getRect();
46+
assert.deepEqual(rect, {width: 35, height: 25, x: 40, y: 50});
5747
});
58-
5948
});
6049
});

0 commit comments

Comments
 (0)