Skip to content

Commit 8617cf3

Browse files
innazhdiemol
andauthored
Innazh/add browser options examples java (#1865)
* add browser options test examples in java added java examples/tests for browserName, browserVersion,platformName, scriptTimeout, pageLoadTimeout, implicitWaitTimeout, unhandledPromptBehaviour, setWindowRect, strictFileInteractability * add java examples to /drivers/options.en.md added all mising examples for java for driver options page, except for the ones that require moving code * rename variables in the added to OptionsTest file functions * update the line nums in options.en.md based on the changes I just made to the example file * update options.ja.md file with java examples for driver options * update options.pt-br.md file with java examples * update options.zh-cn.md file with java examples * remove toString() method from a string var I was using toString() on a string variable by accident, removing it here. * addressed the feedback and now using Assertions library using assertions library, fixed spacing to match the rest of the code, added the missing imports. All of this threw off the line numbers for the options docs, so will have to edit it in the next commit. * Update all line num in the docs Since I had added imports, the line numbers fro the examples had to be updated. * Fixing nightly version retrieval --------- Co-authored-by: Diego Molina <[email protected]> Co-authored-by: Diego Molina <[email protected]>
1 parent f449648 commit 8617cf3

File tree

6 files changed

+163
-54
lines changed

6 files changed

+163
-54
lines changed

.github/workflows/java-examples.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
command: |
6868
pip install yq
6969
xml_content=$(curl -sf https://oss.sonatype.org/service/local/repositories/snapshots/content/org/seleniumhq/selenium/selenium-java/)
70-
latest_snapshot=$(echo $xml_content | xq '.content.data."content-item"' | jq -r 'sort_by(.lastModified) | reverse | .[0] | .text')
70+
latest_snapshot=$(echo $xml_content | xq '.content.data."content-item"' | jq -r .text)
7171
echo $latest_snapshot
7272
cd examples/java
7373
mvn -B -U test -Dselenium.version="$latest_snapshot"
@@ -81,7 +81,7 @@ jobs:
8181
command: |
8282
pip install yq
8383
$xml_content = Invoke-WebRequest -Uri "https://oss.sonatype.org/service/local/repositories/snapshots/content/org/seleniumhq/selenium/selenium-java/"
84-
$latest_snapshot = $xml_content.Content | xq '.content.data.\"content-item\"' | jq -r 'sort_by(.lastModified) | reverse | .[0] | .text'
84+
$latest_snapshot = $xml_content.Content | xq '.content.data.\"content-item\"' | jq -r .text
8585
Write-Output $latest_snapshot
8686
cd examples/java
8787
mvn -B -U test "-Dselenium.version=$latest_snapshot"

examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package dev.selenium.drivers;
22

33
import dev.selenium.BaseTest;
4+
5+
import java.time.Duration;
6+
import java.time.temporal.ChronoUnit;
7+
48
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.Assertions;
510
import org.openqa.selenium.PageLoadStrategy;
11+
import org.openqa.selenium.UnexpectedAlertBehaviour;
612
import org.openqa.selenium.WebDriver;
713
import org.openqa.selenium.chrome.ChromeOptions;
14+
import org.openqa.selenium.remote.CapabilityType;
815
import org.openqa.selenium.chrome.ChromeDriver;
916

1017
public class OptionsTest extends BaseTest {
@@ -60,5 +67,107 @@ public void setAcceptInsecureCerts() {
6067
driver.quit();
6168
}
6269
}
70+
71+
@Test
72+
public void getBrowserName() {
73+
ChromeOptions chromeOptions = new ChromeOptions();
74+
String name = chromeOptions.getBrowserName();
75+
Assertions.assertFalse(name.isEmpty(), "Browser name should not be empty");
76+
}
77+
78+
@Test
79+
public void setBrowserVersion() {
80+
ChromeOptions chromeOptions = new ChromeOptions();
81+
String version = "latest";
82+
chromeOptions.setBrowserVersion(version);
83+
Assertions.assertEquals(version, chromeOptions.getBrowserVersion());
84+
}
85+
86+
@Test
87+
public void setPlatformName() {
88+
ChromeOptions chromeOptions = new ChromeOptions();
89+
String platform = "OS X 10.6";
90+
chromeOptions.setPlatformName(platform);
91+
Assertions.assertEquals(platform, chromeOptions.getPlatformName().toString());
92+
}
93+
94+
@Test
95+
public void setScriptTimeout() {
96+
ChromeOptions chromeOptions = new ChromeOptions();
97+
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
98+
chromeOptions.setScriptTimeout(duration);
99+
100+
WebDriver driver = new ChromeDriver(chromeOptions);
101+
try {
102+
Duration timeout = driver.manage().timeouts().getScriptTimeout();
103+
Assertions.assertEquals(timeout, duration, "The script timeout should be set to 5 seconds.");
104+
} finally {
105+
driver.quit();
106+
}
107+
}
108+
109+
@Test
110+
public void setPageLoadTimeout() {
111+
ChromeOptions chromeOptions = new ChromeOptions();
112+
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
113+
chromeOptions.setPageLoadTimeout(duration);
114+
115+
WebDriver driver = new ChromeDriver(chromeOptions);
116+
try {
117+
Duration timeout = driver.manage().timeouts().getPageLoadTimeout();
118+
Assertions.assertEquals(timeout, duration, "The page load timeout should be set to 5 seconds.");
119+
} finally {
120+
driver.quit();
121+
}
122+
}
123+
124+
@Test
125+
public void setImplicitWaitTimeout() {
126+
ChromeOptions chromeOptions = new ChromeOptions();
127+
Duration duration = Duration.of(5, ChronoUnit.SECONDS);
128+
chromeOptions.setImplicitWaitTimeout(duration);
129+
130+
WebDriver driver = new ChromeDriver(chromeOptions);
131+
try {
132+
Duration timeout = driver.manage().timeouts().getImplicitWaitTimeout();
133+
Assertions.assertEquals(timeout, duration, "The implicit wait timeout should be set to 5 seconds.");
134+
} finally {
135+
driver.quit();
136+
}
137+
}
138+
139+
@Test
140+
public void setUnhandledPromptBehaviour() {
141+
ChromeOptions chromeOptions = new ChromeOptions();
142+
chromeOptions.setUnhandledPromptBehaviour(UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY);
143+
//verify the capability object is not null
144+
Object capabilityObject = chromeOptions.getCapability(CapabilityType.UNHANDLED_PROMPT_BEHAVIOUR);
145+
Assertions.assertNotNull(capabilityObject, "Capability UNHANDLED_PROMPT_BEHAVIOUR should not be null.");
146+
Assertions.assertEquals(capabilityObject.toString(), UnexpectedAlertBehaviour.DISMISS_AND_NOTIFY.toString());
147+
}
148+
149+
@Test
150+
public void setWindowRect() {
151+
ChromeOptions chromeOptions = new ChromeOptions();
152+
chromeOptions.setCapability(CapabilityType.SET_WINDOW_RECT, true);
153+
//verify the capability object is not null
154+
Object capabilityObject = chromeOptions.getCapability(CapabilityType.SET_WINDOW_RECT);
155+
Assertions.assertNotNull(capabilityObject, "Capability SET_WINDOW_RECT should not be null.");
156+
157+
Boolean capability = (Boolean) capabilityObject;
158+
Assertions.assertTrue(capability, "The capability SET_WINDOW_RECT should be set to true.");
159+
}
160+
161+
@Test
162+
public void setStrictFileInteractability() {
163+
ChromeOptions chromeOptions = new ChromeOptions();
164+
chromeOptions.setCapability(CapabilityType.STRICT_FILE_INTERACTABILITY, true);
165+
//verify the capability object is not null
166+
Object capabilityObject = chromeOptions.getCapability(CapabilityType.STRICT_FILE_INTERACTABILITY);
167+
Assertions.assertNotNull(capabilityObject, "Capability STRICT_FILE_INTERACTABILITY should not be null.");
168+
169+
Boolean capability = (Boolean) capabilityObject;
170+
Assertions.assertTrue(capability, "The capability STRICT_FILE_INTERACTABILITY should be set to true.");
171+
}
63172
}
64173

website_and_docs/content/documentation/webdriver/drivers/options.en.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Browser name is set by default when using an Options class instance.
3131

3232
{{< tabpane text=true >}}
3333
{{< tab header="Java" >}}
34-
{{< badge-code >}}
34+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L73-74" >}}
3535
{{< /tab >}}
3636
{{% tab header="Python" %}}
3737
{{< gh-codeblock path="examples/python/tests/drivers/test_options.py#L36" >}}
@@ -58,7 +58,7 @@ it will be automatically downloaded by [Selenium Manager]({{< ref "../../seleniu
5858

5959
{{< tabpane text=true >}}
6060
{{< tab header="Java" >}}
61-
{{< badge-code >}}
61+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L80-82" >}}
6262
{{< /tab >}}
6363
{{% tab header="Python" %}}
6464
{{< gh-codeblock path="examples/python/tests/drivers/test_options.py#L37" >}}
@@ -114,7 +114,7 @@ event fire is returned.
114114
{{< tabpane langEqualsHeader=true >}}
115115
{{< badge-examples >}}
116116
{{< tab header="Java" text=true >}}
117-
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L14-L16">}}
117+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L21-L23">}}
118118
{{< /tab >}}
119119
{{< tab header="Python" text=true >}}
120120
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L9-L10">}}
@@ -171,7 +171,7 @@ event fire is returned.
171171
{{< tabpane langEqualsHeader=true >}}
172172
{{< badge-examples >}}
173173
{{< tab header="Java" text=true >}}
174-
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L27-L29">}}
174+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L34-L36">}}
175175
{{< /tab >}}
176176
{{< tab header="Python" text=true >}}
177177
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L19-L20">}}
@@ -227,7 +227,7 @@ WebDriver only waits until the initial page is downloaded.
227227
{{< tabpane langEqualsHeader=true >}}
228228
{{< badge-examples >}}
229229
{{< tab header="Java" text=true >}}
230-
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L40-L42">}}
230+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L47-L49">}}
231231
{{< /tab >}}
232232
{{< tab header="Python" text=true >}}
233233
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L29-L30">}}
@@ -287,7 +287,7 @@ setting `platformName` sets the OS at the remote-end.
287287

288288
{{< tabpane text=true >}}
289289
{{< tab header="Java" >}}
290-
{{< badge-code >}}
290+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L88-L90">}}
291291
{{< /tab >}}
292292
{{% tab header="Python" %}}
293293
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L38">}}
@@ -325,7 +325,7 @@ effect for the entire session.
325325

326326
{{< tabpane text=true >}}
327327
{{< tab header="Java" >}}
328-
{{< badge-code >}}
328+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L60-L61">}}
329329
{{< /tab >}}
330330
{{% tab header="Python" %}}
331331
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L39-40">}}
@@ -360,7 +360,7 @@ is imposed when a new session is created by WebDriver.
360360

361361
{{< tabpane text=true >}}
362362
{{< tab header="Java" >}}
363-
{{< badge-code >}}
363+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L96-L98">}}
364364
{{< /tab >}}
365365
{{% tab header="Python" %}}
366366
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L47-48">}}
@@ -389,7 +389,7 @@ _TimeoutException_.
389389

390390
{{< tabpane text=true >}}
391391
{{< tab header="Java" >}}
392-
{{< badge-code >}}
392+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L111-L113">}}
393393
{{< /tab >}}
394394
{{% tab header="Python" %}}
395395
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L55-56">}}
@@ -416,7 +416,7 @@ is imposed when a new session is created by WebDriver.
416416

417417
{{< tabpane text=true >}}
418418
{{< tab header="Java" >}}
419-
{{< badge-code >}}
419+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L126-L128">}}
420420
{{< /tab >}}
421421
{{% tab header="Python" %}}
422422
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L63-64">}}
@@ -454,7 +454,7 @@ user prompt encounters at the remote-end. This is defined by
454454

455455
{{< tabpane text=true >}}
456456
{{< tab header="Java" >}}
457-
{{< badge-code >}}
457+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L141-L142">}}
458458
{{< /tab >}}
459459
{{% tab header="Python" %}}
460460
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L71-72">}}
@@ -479,7 +479,7 @@ Indicates whether the remote end supports all of the [resizing and repositioning
479479

480480
{{< tabpane text=true >}}
481481
{{< tab header="Java" >}}
482-
{{< badge-code >}}
482+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L151-L152">}}
483483
{{< /tab >}}
484484
{{% tab header="Python" %}}
485485
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L79-80">}}
@@ -507,7 +507,7 @@ when using _Element Send Keys_ with hidden file upload controls.
507507

508508
{{< tabpane text=true >}}
509509
{{< tab header="Java" >}}
510-
{{< badge-code >}}
510+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L163-L164">}}
511511
{{< /tab >}}
512512
{{% tab header="Python" %}}
513513
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L87-88">}}

website_and_docs/content/documentation/webdriver/drivers/options.ja.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Browser name is set by default when using an Options class instance.
3131

3232
{{< tabpane text=true >}}
3333
{{< tab header="Java" >}}
34-
{{< badge-code >}}
34+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L73-74" >}}
3535
{{< /tab >}}
3636
{{% tab header="Python" %}}
3737
{{< gh-codeblock path="examples/python/tests/drivers/test_options.py#L36" >}}
@@ -59,7 +59,7 @@ it will be automatically downloaded by [Selenium Manager]({{< ref "../../seleniu
5959

6060
{{< tabpane text=true >}}
6161
{{< tab header="Java" >}}
62-
{{< badge-code >}}
62+
{{< gh-codeblock path="examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L80-82" >}}
6363
{{< /tab >}}
6464
{{% tab header="Python" %}}
6565
{{< gh-codeblock path="examples/python/tests/drivers/test_options.py#L37" >}}
@@ -112,7 +112,7 @@ WebDriver は [load](https://developer.mozilla.org/ja/docs/Web/API/Window/load_e
112112

113113
{{< tabpane langEqualsHeader=true >}}
114114
{{< tab header="Java" text=true >}}
115-
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L14-L16">}}
115+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L21-L23">}}
116116
{{< /tab >}}
117117
{{< tab header="Python" text=true >}}
118118
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L9-L10">}}
@@ -168,7 +168,7 @@ WebDriver は、[DOMContentLoaded](https://developer.mozilla.org/ja/docs/Web/API
168168

169169
{{< tabpane langEqualsHeader=true >}}
170170
{{< tab header="Java" text=true >}}
171-
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L27-L29">}}
171+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L34-L36">}}
172172
{{< /tab >}}
173173
{{< tab header="Python" text=true >}}
174174
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L19-L20">}}
@@ -223,7 +223,7 @@ WebDriver は、最初のページがダウンロードされるまで待機し
223223

224224
{{< tabpane langEqualsHeader=true >}}
225225
{{< tab header="Java" text=true >}}
226-
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L40-L42">}}
226+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L47-L49">}}
227227
{{< /tab >}}
228228
{{< tab header="Python" text=true >}}
229229
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L29-L30">}}
@@ -281,7 +281,7 @@ fun main() {
281281

282282
{{< tabpane text=true >}}
283283
{{< tab header="Java" >}}
284-
{{< badge-code >}}
284+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L88-L90">}}
285285
{{< /tab >}}
286286
{{% tab header="Python" %}}
287287
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L38">}}
@@ -313,7 +313,7 @@ fun main() {
313313

314314
{{< tabpane text=true >}}
315315
{{< tab header="Java" >}}
316-
{{< badge-code >}}
316+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L60-L61">}}
317317
{{< /tab >}}
318318
{{% tab header="Python" %}}
319319
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L39-40">}}
@@ -345,7 +345,7 @@ WebDriverの `セッション` には特定の `セッションタイムアウ
345345

346346
{{< tabpane text=true >}}
347347
{{< tab header="Java" >}}
348-
{{< badge-code >}}
348+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L96-L98">}}
349349
{{< /tab >}}
350350
{{% tab header="Python" %}}
351351
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L47-48">}}
@@ -371,7 +371,7 @@ WebDriverの `セッション` には特定の `セッションタイムアウ
371371

372372
{{< tabpane text=true >}}
373373
{{< tab header="Java" >}}
374-
{{< badge-code >}}
374+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L111-L113">}}
375375
{{< /tab >}}
376376
{{% tab header="Python" %}}
377377
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L55-56">}}
@@ -396,7 +396,7 @@ WebDriverの `セッション` には特定の `セッションタイムアウ
396396

397397
{{< tabpane text=true >}}
398398
{{< tab header="Java" >}}
399-
{{< badge-code >}}
399+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L126-L128">}}
400400
{{< /tab >}}
401401
{{% tab header="Python" %}}
402402
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L63-64">}}
@@ -433,7 +433,7 @@ WebDriverの `セッション` には特定の `セッションタイムアウ
433433

434434
{{< tabpane text=true >}}
435435
{{< tab header="Java" >}}
436-
{{< badge-code >}}
436+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L141-L142">}}
437437
{{< /tab >}}
438438
{{% tab header="Python" %}}
439439
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L71-72">}}
@@ -459,7 +459,7 @@ WebDriverの `セッション` には特定の `セッションタイムアウ
459459

460460
{{< tabpane text=true >}}
461461
{{< tab header="Java" >}}
462-
{{< badge-code >}}
462+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L151-L152">}}
463463
{{< /tab >}}
464464
{{% tab header="Python" %}}
465465
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L79-80">}}
@@ -486,7 +486,7 @@ WebDriverの `セッション` には特定の `セッションタイムアウ
486486

487487
{{< tabpane text=true >}}
488488
{{< tab header="Java" >}}
489-
{{< badge-code >}}
489+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/drivers/OptionsTest.java#L163-L164">}}
490490
{{< /tab >}}
491491
{{% tab header="Python" %}}
492492
{{< gh-codeblock path="/examples/python/tests/drivers/test_options.py#L87-88">}}

0 commit comments

Comments
 (0)