Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/java/src/org/openqa/selenium/Platform.java
1865 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License. You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
package org.openqa.selenium;
19
20
import java.util.Arrays;
21
import java.util.Locale;
22
import java.util.regex.Matcher;
23
import java.util.regex.Pattern;
24
import org.jspecify.annotations.NullMarked;
25
import org.jspecify.annotations.Nullable;
26
27
/**
28
* Represents the known and supported Platforms that WebDriver runs on. This is pretty close to the
29
* Operating System, but differs slightly, because this class is used to extract information such as
30
* program locations and line endings.
31
*/
32
// Useful URLs:
33
// http://hg.openjdk.java.net/jdk7/modules/jdk/file/a37326fa7f95/src/windows/native/java/lang/java_props_md.c
34
@NullMarked
35
public enum Platform {
36
37
/** Never returned, but can be used to request a browser running on any version of Windows. */
38
WINDOWS("windows") {
39
@Override
40
public @Nullable Platform family() {
41
return null;
42
}
43
44
@Override
45
public String toString() {
46
return "windows";
47
}
48
},
49
50
/**
51
* For versions of Windows that "feel like" Windows XP. These are ones that store files in
52
* "\Program Files\" and documents under "\\documents and settings\\username"
53
*/
54
XP("Windows Server 2003", "xp", "windows", "winnt", "windows_nt", "windows nt") {
55
@Override
56
public @Nullable Platform family() {
57
return WINDOWS;
58
}
59
60
@Override
61
public String toString() {
62
return "Windows XP";
63
}
64
},
65
66
/** For versions of Windows that "feel like" Windows Vista. */
67
VISTA("windows vista", "Windows Server 2008") {
68
@Override
69
public @Nullable Platform family() {
70
return WINDOWS;
71
}
72
73
@Override
74
public String toString() {
75
return "Windows Vista";
76
}
77
},
78
79
WIN7("windows 7", "win7") {
80
@Override
81
public @Nullable Platform family() {
82
return WINDOWS;
83
}
84
85
@Override
86
public String toString() {
87
return "Windows 7";
88
}
89
},
90
91
/** For versions of Windows that "feel like" Windows 8. */
92
WIN8("Windows Server 2012", "windows 8", "win8") {
93
@Override
94
public @Nullable Platform family() {
95
return WINDOWS;
96
}
97
98
@Override
99
public String toString() {
100
return "Windows 8";
101
}
102
},
103
104
WIN8_1("windows 8.1", "win8.1") {
105
@Override
106
public @Nullable Platform family() {
107
return WINDOWS;
108
}
109
110
@Override
111
public String toString() {
112
return "Windows 8.1";
113
}
114
},
115
116
WIN10("windows 10", "win10") {
117
@Override
118
public @Nullable Platform family() {
119
return WINDOWS;
120
}
121
122
@Override
123
public String toString() {
124
return "Windows 10";
125
}
126
},
127
128
WIN11("windows 11", "win11") {
129
@Override
130
public @Nullable Platform family() {
131
return WINDOWS;
132
}
133
134
@Override
135
public String toString() {
136
return "Windows 11";
137
}
138
},
139
140
MAC("mac", "darwin", "macOS", "mac os x", "os x") {
141
@Override
142
public @Nullable Platform family() {
143
return null;
144
}
145
146
@Override
147
public String toString() {
148
return "mac";
149
}
150
},
151
152
SNOW_LEOPARD("snow leopard", "os x 10.6", "macos 10.6") {
153
@Override
154
public @Nullable Platform family() {
155
return MAC;
156
}
157
158
@Override
159
public String toString() {
160
return "OS X 10.6";
161
}
162
},
163
164
MOUNTAIN_LION("mountain lion", "os x 10.8", "macos 10.8") {
165
@Override
166
public @Nullable Platform family() {
167
return MAC;
168
}
169
170
@Override
171
public String toString() {
172
return "OS X 10.8";
173
}
174
},
175
176
MAVERICKS("mavericks", "os x 10.9", "macos 10.9") {
177
@Override
178
public @Nullable Platform family() {
179
return MAC;
180
}
181
182
@Override
183
public String toString() {
184
return "OS X 10.9";
185
}
186
},
187
188
YOSEMITE("yosemite", "os x 10.10", "macos 10.10") {
189
@Override
190
public @Nullable Platform family() {
191
return MAC;
192
}
193
194
@Override
195
public String toString() {
196
return "OS X 10.10";
197
}
198
},
199
200
EL_CAPITAN("el capitan", "os x 10.11", "macos 10.11") {
201
@Override
202
public @Nullable Platform family() {
203
return MAC;
204
}
205
206
@Override
207
public String toString() {
208
return "OS X 10.11";
209
}
210
},
211
212
SIERRA("sierra", "os x 10.12", "macos 10.12") {
213
@Override
214
public @Nullable Platform family() {
215
return MAC;
216
}
217
218
@Override
219
public String toString() {
220
return "macOS 10.12";
221
}
222
},
223
224
HIGH_SIERRA("high sierra", "os x 10.13", "macos 10.13") {
225
@Override
226
public @Nullable Platform family() {
227
return MAC;
228
}
229
230
@Override
231
public String toString() {
232
return "macOS 10.13";
233
}
234
},
235
236
MOJAVE("mojave", "os x 10.14", "macos 10.14") {
237
@Override
238
public @Nullable Platform family() {
239
return MAC;
240
}
241
242
@Override
243
public String toString() {
244
return "macOS 10.14";
245
}
246
},
247
248
CATALINA("catalina", "os x 10.15", "macos 10.15") {
249
@Override
250
public @Nullable Platform family() {
251
return MAC;
252
}
253
254
@Override
255
public String toString() {
256
return "macOS 10.15";
257
}
258
},
259
260
BIG_SUR("big sur", "os x 11.0", "macos 11.0") {
261
@Override
262
public @Nullable Platform family() {
263
return MAC;
264
}
265
266
@Override
267
public String toString() {
268
return "macOS 11.0";
269
}
270
},
271
272
MONTEREY("monterey", "os x 12.0", "macos 12.0") {
273
@Override
274
public @Nullable Platform family() {
275
return MAC;
276
}
277
278
@Override
279
public String toString() {
280
return "macOS 12.0";
281
}
282
},
283
284
VENTURA("ventura", "os x 13.0", "macos 13.0") {
285
@Override
286
public @Nullable Platform family() {
287
return MAC;
288
}
289
290
@Override
291
public String toString() {
292
return "macOS 13.0";
293
}
294
},
295
296
SONOMA("sonoma", "os x 14.0", "macos 14.0") {
297
@Override
298
public @Nullable Platform family() {
299
return MAC;
300
}
301
302
@Override
303
public String toString() {
304
return "macOS 14.0";
305
}
306
},
307
308
SEQUOIA("sequoia", "os x 15.0", "macos 15.0") {
309
@Override
310
public @Nullable Platform family() {
311
return MAC;
312
}
313
314
@Override
315
public String toString() {
316
return "macOS 15.0";
317
}
318
},
319
320
/** Many platforms have UNIX traits, amongst them LINUX, Solaris and BSD. */
321
UNIX("solaris", "bsd") {
322
@Override
323
public @Nullable Platform family() {
324
return null;
325
}
326
},
327
328
LINUX("linux") {
329
@Override
330
public @Nullable Platform family() {
331
return UNIX;
332
}
333
334
@Override
335
public String toString() {
336
return "linux";
337
}
338
},
339
340
ANDROID("android", "dalvik") {
341
@Override
342
public @Nullable Platform family() {
343
return null;
344
}
345
},
346
347
IOS("iOS") {
348
@Override
349
public @Nullable Platform family() {
350
return null;
351
}
352
},
353
354
/** Never returned, but can be used to request a browser running on any operating system. */
355
ANY("") {
356
@Override
357
public @Nullable Platform family() {
358
return ANY;
359
}
360
361
@Override
362
public boolean is(Platform compareWith) {
363
return this == compareWith;
364
}
365
366
@Override
367
public String toString() {
368
return "any";
369
}
370
};
371
372
private static final Pattern VERSION_PATTERN = Pattern.compile("^(\\d+)\\.(\\d+).*");
373
private static @Nullable Platform current;
374
private final String[] partOfOsName;
375
private int minorVersion = 0;
376
private int majorVersion = 0;
377
378
Platform(String... partOfOsName) {
379
this.partOfOsName = partOfOsName;
380
}
381
382
/**
383
* Get current platform (not necessarily the same as operating system).
384
*
385
* @return current platform
386
*/
387
public static Platform getCurrent() {
388
if (current == null) {
389
current = extractFromSysProperty(System.getProperty("os.name"));
390
391
String version = System.getProperty("os.version", "0.0.0");
392
int major = 0;
393
int minor = 0;
394
395
final Matcher matcher = VERSION_PATTERN.matcher(version);
396
if (matcher.matches()) {
397
try {
398
major = Integer.parseInt(matcher.group(1));
399
minor = Integer.parseInt(matcher.group(2));
400
} catch (NumberFormatException e) {
401
// These things happen
402
}
403
}
404
405
current.majorVersion = major;
406
current.minorVersion = minor;
407
}
408
return current;
409
}
410
411
/**
412
* Extracts platforms based on system properties in Java and uses a heuristic to determine the
413
* most likely operating system. If unable to determine the operating system, it will default to
414
* UNIX.
415
*
416
* @param osName the operating system name to determine the platform of
417
* @return the most likely platform based on given operating system name
418
*/
419
public static Platform extractFromSysProperty(String osName) {
420
return extractFromSysProperty(osName, System.getProperty("os.version"));
421
}
422
423
/**
424
* Extracts platforms based on system properties in Java and uses a heuristic to determine the
425
* most likely operating system. If unable to determine the operating system, it will default to
426
* UNIX.
427
*
428
* @param osName the operating system name to determine the platform of
429
* @param osVersion the operating system version to determine the platform of
430
* @return the most likely platform based on given operating system name and version
431
*/
432
public static Platform extractFromSysProperty(String osName, String osVersion) {
433
osName = osName.toLowerCase(Locale.ENGLISH);
434
// os.name for android is linux
435
if ("dalvik".equalsIgnoreCase(System.getProperty("java.vm.name"))) {
436
return Platform.ANDROID;
437
}
438
// Windows 8 can't be detected by osName alone
439
if (osVersion.equals("6.2") && osName.startsWith("windows nt")) {
440
return WIN8;
441
}
442
// Windows 8 can't be detected by osName alone
443
if (osVersion.equals("6.3") && osName.startsWith("windows nt")) {
444
return WIN8_1;
445
}
446
Platform mostLikely = UNIX;
447
String previousMatch = null;
448
for (Platform os : Platform.values()) {
449
for (String matcher : os.partOfOsName) {
450
if ("".equals(matcher)) {
451
continue;
452
}
453
matcher = matcher.toLowerCase(Locale.ENGLISH);
454
if (os.isExactMatch(osName, matcher)) {
455
return os;
456
}
457
if (os.isCurrentPlatform(osName, matcher) && isBetterMatch(previousMatch, matcher)) {
458
previousMatch = matcher;
459
mostLikely = os;
460
}
461
}
462
}
463
464
// Default to assuming we're on a UNIX variant (including LINUX)
465
return mostLikely;
466
}
467
468
/**
469
* Gets a platform with the name matching the parameter.
470
*
471
* @param name the platform name
472
* @return the Platform enum value matching the parameter
473
*/
474
public static Platform fromString(String name) {
475
for (Platform platform : values()) {
476
if (platform.toString().equalsIgnoreCase(name)) {
477
return platform;
478
}
479
}
480
481
for (Platform os : Platform.values()) {
482
for (String matcher : os.partOfOsName) {
483
if (name.equalsIgnoreCase(matcher)) {
484
return os;
485
}
486
}
487
}
488
throw new WebDriverException("Unrecognized platform: " + name);
489
}
490
491
/**
492
* Decides whether the previous match is better or not than the current match. If previous match
493
* is null, the newer match is always better.
494
*
495
* @param previous the previous match
496
* @param matcher the newer match
497
* @return true if newer match is better, false otherwise
498
*/
499
private static boolean isBetterMatch(@Nullable String previous, String matcher) {
500
return previous == null || matcher.length() >= previous.length();
501
}
502
503
public String[] getPartOfOsName() {
504
return Arrays.copyOf(partOfOsName, partOfOsName.length);
505
}
506
507
/**
508
* Heuristic for comparing two platforms. If platforms (which is not the same thing as operating
509
* systems) are found to be approximately similar in nature, this will return true. For instance
510
* the LINUX platform is similar to UNIX, and will give a positive result if compared.
511
*
512
* @param compareWith the platform to compare with
513
* @return true if platforms are approximately similar, false otherwise
514
*/
515
public boolean is(Platform compareWith) {
516
return
517
// Any platform is itself
518
this == compareWith
519
||
520
// Any platform is also ANY platform
521
compareWith == ANY
522
||
523
// And any Platform which is not a platform type belongs to the same family
524
(this.family() != null && this.family().is(compareWith));
525
}
526
527
/**
528
* Returns a platform that represents a family for the current platform. For instance the LINUX if
529
* a part of the UNIX family, the XP is a part of the WINDOWS family.
530
*
531
* @return the family platform for the current one, or {@code null} if this {@code Platform}
532
* represents a platform family (such as Windows, or MacOS)
533
*/
534
public abstract @Nullable Platform family();
535
536
private boolean isCurrentPlatform(String osName, String matchAgainst) {
537
return osName.contains(matchAgainst);
538
}
539
540
private boolean isExactMatch(String osName, String matchAgainst) {
541
return matchAgainst.equals(osName);
542
}
543
544
/**
545
* Returns the major version of this platform.
546
*
547
* @return the major version of specified platform
548
*/
549
public int getMajorVersion() {
550
return majorVersion;
551
}
552
553
/**
554
* Returns the minor version of this platform.
555
*
556
* @return the minor version of specified platform
557
*/
558
public int getMinorVersion() {
559
return minorVersion;
560
}
561
}
562
563