Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0243b1b

Browse files
[java] BiDi code examples (SeleniumHQ#1502)
* [java] update bidi tests * update variable for consistency --------- Co-authored-by: Titus Fortner <[email protected]>
1 parent 2b8ec83 commit 0243b1b

File tree

6 files changed

+90
-191
lines changed

6 files changed

+90
-191
lines changed

examples/java/src/test/java/dev/selenium/bidirectional/BidiApi.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package dev.selenium.bidirectional;
2+
3+
import static org.openqa.selenium.remote.http.Contents.utf8String;
4+
5+
import com.google.common.net.MediaType;
6+
import dev.selenium.BaseChromeTest;
7+
import java.net.URI;
8+
import java.util.concurrent.CountDownLatch;
9+
import java.util.concurrent.TimeUnit;
10+
import java.util.concurrent.atomic.AtomicReference;
11+
import java.util.function.Predicate;
12+
import org.junit.jupiter.api.Assertions;
13+
import org.junit.jupiter.api.Test;
14+
import org.openqa.selenium.By;
15+
import org.openqa.selenium.HasAuthentication;
16+
import org.openqa.selenium.JavascriptExecutor;
17+
import org.openqa.selenium.UsernameAndPassword;
18+
import org.openqa.selenium.WebElement;
19+
import org.openqa.selenium.devtools.NetworkInterceptor;
20+
import org.openqa.selenium.devtools.events.CdpEventTypes;
21+
import org.openqa.selenium.devtools.events.DomMutationEvent;
22+
import org.openqa.selenium.logging.HasLogEvents;
23+
import org.openqa.selenium.remote.http.HttpResponse;
24+
import org.openqa.selenium.remote.http.Route;
25+
26+
public class BidiApiTest extends BaseChromeTest {
27+
@Test
28+
public void registerBasicAuth() {
29+
Predicate<URI> uriPredicate = uri -> uri.getHost().contains("the-internet.herokuapp.com");
30+
((HasAuthentication) driver).register(uriPredicate, UsernameAndPassword.of("admin", "admin"));
31+
32+
driver.get("https://the-internet.herokuapp.com/basic_auth");
33+
34+
Assertions.assertTrue(driver.getPageSource().contains("Basic Auth"));
35+
}
36+
37+
@Test
38+
public void domMutation() throws InterruptedException {
39+
driver.get("https://www.google.com");
40+
WebElement span = driver.findElement(By.cssSelector("span"));
41+
42+
AtomicReference<DomMutationEvent> seen = new AtomicReference<>();
43+
CountDownLatch latch = new CountDownLatch(1);
44+
((HasLogEvents) driver).onLogEvent(CdpEventTypes.domMutation(mutation -> {
45+
seen.set(mutation);
46+
latch.countDown();
47+
}));
48+
49+
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('cheese', 'gouda');",
50+
span);
51+
52+
Assertions.assertTrue(latch.await(10, TimeUnit.SECONDS));
53+
Assertions.assertEquals("cheese", seen.get().getAttributeName());
54+
Assertions.assertEquals("gouda", seen.get().getCurrentValue());
55+
}
56+
57+
@Test
58+
public void interceptNetworkResponse() {
59+
NetworkInterceptor interceptor = new NetworkInterceptor(
60+
driver, Route.matching(req -> true)
61+
.to(() -> req -> new HttpResponse()
62+
.setStatus(200)
63+
.addHeader("Content-Type", MediaType.HTML_UTF_8.toString())
64+
.setContent(utf8String("Creamy, delicious cheese!"))));
65+
driver.get("https://www.selenium.dev");
66+
String source = driver.getPageSource();
67+
68+
Assertions.assertTrue(source.contains("delicious cheese"));
69+
}
70+
}

website_and_docs/content/documentation/webdriver/bidirectional/bidi_api.en.md

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,7 @@ With Selenium, you can automate the input of basic auth credentials whenever the
1717
{{< tabpane langEqualsHeader=true >}}
1818
{{< badge-examples >}}
1919
{{< tab header="Java" >}}
20-
Predicate<URI> uriPredicate = uri -> uri.getHost().contains("your-domain.com");
21-
22-
((HasAuthentication) driver).register(uriPredicate, UsernameAndPassword.of("admin", "password"));
23-
driver.get("https://your-domain.com/login");
20+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/BidiApiTest.java/#L29-L32" >}}
2421
{{< /tab >}}
2522
{{< tab header="Python" text=true >}}
2623
{{< badge-code >}}
@@ -76,27 +73,9 @@ element in the DOM.
7673

7774
{{< tabpane langEqualsHeader=true >}}
7875
{{< badge-examples >}}
79-
{{< tab header="Java" >}}
80-
ChromeDriver driver = new ChromeDriver();
81-
82-
AtomicReference<DomMutationEvent> seen = new AtomicReference<>();
83-
CountDownLatch latch = new CountDownLatch(1);
84-
((HasLogEvents) driver).onLogEvent(domMutation(mutation -> {
85-
seen.set(mutation);
86-
latch.countDown();
87-
}));
88-
89-
driver.get("https://www.google.com");
90-
WebElement span = driver.findElement(By.cssSelector("span"));
91-
92-
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('cheese', 'gouda');", span);
93-
94-
assertThat(latch.await(10, SECONDS), is(true));
95-
assertThat(seen.get().getAttributeName(), is("cheese"));
96-
assertThat(seen.get().getCurrentValue(), is("gouda"));
97-
98-
driver.quit();
99-
{{< /tab >}}
76+
{{< tab header="Java" >}}
77+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/BidiApiTest.java/#L39-L50" >}}
78+
{{< /tab >}}
10079
{{< tab header="Python" >}}
10180
from selenium import webdriver
10281
from selenium.webdriver.common.by import By
@@ -444,27 +423,7 @@ it with the following examples.
444423
{{< tabpane langEqualsHeader=true >}}
445424
{{< badge-examples >}}
446425
{{< tab header="Java" >}}
447-
import org.openqa.selenium.WebDriver;
448-
import org.openqa.selenium.devtools.HasDevTools;
449-
import org.openqa.selenium.devtools.NetworkInterceptor;
450-
import org.openqa.selenium.remote.http.Contents;
451-
import org.openqa.selenium.remote.http.Filter;
452-
import org.openqa.selenium.remote.http.HttpResponse;
453-
import org.openqa.selenium.remote.http.Route;
454-
455-
NetworkInterceptor interceptor = new NetworkInterceptor(
456-
driver,
457-
Route.matching(req -> true)
458-
.to(() -> req -> new HttpResponse()
459-
.setStatus(200)
460-
.addHeader("Content-Type", MediaType.HTML_UTF_8.toString())
461-
.setContent(utf8String("Creamy, delicious cheese!"))));
462-
463-
driver.get("https://example-sausages-site.com");
464-
465-
String source = driver.getPageSource();
466-
467-
assertThat(source).contains("delicious cheese!");
426+
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Bidirectional/BidiApiTest.java#L59-L66" >}}
468427
{{< /tab >}}
469428
{{< tab header="Python" >}}
470429
Currently unavailable in python due the inability to mix certain async and sync commands

website_and_docs/content/documentation/webdriver/bidirectional/bidi_api.ja.md

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ With Selenium, you can automate the input of basic auth credentials whenever the
2525

2626
{{< tabpane langEqualsHeader=true >}}
2727
{{< tab header="Java" >}}
28-
Predicate<URI> uriPredicate = uri -> uri.getHost().contains("your-domain.com");
29-
30-
((HasAuthentication) driver).register(uriPredicate, UsernameAndPassword.of("admin", "password"));
31-
driver.get("https://your-domain.com/login");
28+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/BidiApiTest.java/#L29-L32" >}}
3229
{{< /tab >}}
3330
{{< tab header="Python" text=true >}}
3431
{{< badge-code >}}
@@ -83,27 +80,9 @@ WebDriver BiDi when there are DOM mutations on a specific
8380
element in the DOM.
8481

8582
{{< tabpane langEqualsHeader=true >}}
86-
{{< tab header="Java" >}}
87-
ChromeDriver driver = new ChromeDriver();
88-
89-
AtomicReference<DomMutationEvent> seen = new AtomicReference<>();
90-
CountDownLatch latch = new CountDownLatch(1);
91-
((HasLogEvents) driver).onLogEvent(domMutation(mutation -> {
92-
seen.set(mutation);
93-
latch.countDown();
94-
}));
95-
96-
driver.get("https://www.google.com");
97-
WebElement span = driver.findElement(By.cssSelector("span"));
98-
99-
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('cheese', 'gouda');", span);
100-
101-
assertThat(latch.await(10, SECONDS), is(true));
102-
assertThat(seen.get().getAttributeName(), is("cheese"));
103-
assertThat(seen.get().getCurrentValue(), is("gouda"));
104-
105-
driver.quit();
106-
{{< /tab >}}
83+
{{< tab header="Java" >}}
84+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/BidiApiTest.java/#L39-L50" >}}
85+
{{< /tab >}}
10786
{{< tab header="Python" >}}
10887
from selenium import webdriver
10988
from selenium.webdriver.common.by import By
@@ -448,27 +427,7 @@ it with the following examples.
448427

449428
{{< tabpane langEqualsHeader=true >}}
450429
{{< tab header="Java" >}}
451-
import org.openqa.selenium.WebDriver;
452-
import org.openqa.selenium.devtools.HasDevTools;
453-
import org.openqa.selenium.devtools.NetworkInterceptor;
454-
import org.openqa.selenium.remote.http.Contents;
455-
import org.openqa.selenium.remote.http.Filter;
456-
import org.openqa.selenium.remote.http.HttpResponse;
457-
import org.openqa.selenium.remote.http.Route;
458-
459-
NetworkInterceptor interceptor = new NetworkInterceptor(
460-
driver,
461-
Route.matching(req -> true)
462-
.to(() -> req -> new HttpResponse()
463-
.setStatus(200)
464-
.addHeader("Content-Type", MediaType.HTML_UTF_8.toString())
465-
.setContent(utf8String("Creamy, delicious cheese!"))));
466-
467-
driver.get("https://example-sausages-site.com");
468-
469-
String source = driver.getPageSource();
470-
471-
assertThat(source).contains("delicious cheese!");
430+
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Bidirectional/BidiApiTest.java#L59-L66" >}}
472431
{{< /tab >}}
473432
{{< tab header="Python" >}}
474433
Currently unavailable in python due the inability to mix certain async and sync commands

website_and_docs/content/documentation/webdriver/bidirectional/bidi_api.pt-br.md

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ Alguns aplicativos fazem o uso da autenticação do navegador para proteger suas
1313

1414
{{< tabpane langEqualsHeader=true >}}
1515
{{< tab header="Java" >}}
16-
Predicate<URI> uriPredicate = uri -> uri.getHost().contains("your-domain.com");
17-
18-
((HasAuthentication) driver).register(uriPredicate, UsernameAndPassword.of("admin", "password"));
19-
driver.get("https://your-domain.com/login");
16+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/BidiApiTest.java/#L29-L32" >}}
2017
{{< /tab >}}
2118
{{< tab header="Python" text=true >}}
2219
{{< badge-code >}}
@@ -69,27 +66,9 @@ driver.get("https://your-domain.com/login")
6966
Mutation Observation(Observação de Mutação) é a capacidade de capturar eventos via WebDriver BiDi quando há mutações DOM em um elemento específico no DOM.
7067

7168
{{< tabpane langEqualsHeader=true >}}
72-
{{< tab header="Java" >}}
73-
ChromeDriver driver = new ChromeDriver();
74-
75-
AtomicReference<DomMutationEvent> seen = new AtomicReference<>();
76-
CountDownLatch latch = new CountDownLatch(1);
77-
((HasLogEvents) driver).onLogEvent(domMutation(mutation -> {
78-
seen.set(mutation);
79-
latch.countDown();
80-
}));
81-
82-
driver.get("https://www.google.com");
83-
WebElement span = driver.findElement(By.cssSelector("span"));
84-
85-
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('cheese', 'gouda');", span);
86-
87-
assertThat(latch.await(10, SECONDS), is(true));
88-
assertThat(seen.get().getAttributeName(), is("cheese"));
89-
assertThat(seen.get().getCurrentValue(), is("gouda"));
90-
91-
driver.quit();
92-
{{< /tab >}}
69+
{{< tab header="Java" >}}
70+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/BidiApiTest.java/#L39-L50" >}}
71+
{{< /tab >}}
9372
{{< tab header="Python" >}}
9473
from selenium import webdriver
9574
from selenium.webdriver.common.by import By
@@ -433,27 +412,7 @@ com os exemplos a seguir.
433412

434413
{{< tabpane langEqualsHeader=true >}}
435414
{{< tab header="Java" >}}
436-
import org.openqa.selenium.WebDriver;
437-
import org.openqa.selenium.devtools.HasDevTools;
438-
import org.openqa.selenium.devtools.NetworkInterceptor;
439-
import org.openqa.selenium.remote.http.Contents;
440-
import org.openqa.selenium.remote.http.Filter;
441-
import org.openqa.selenium.remote.http.HttpResponse;
442-
import org.openqa.selenium.remote.http.Route;
443-
444-
NetworkInterceptor interceptor = new NetworkInterceptor(
445-
driver,
446-
Route.matching(req -> true)
447-
.to(() -> req -> new HttpResponse()
448-
.setStatus(200)
449-
.addHeader("Content-Type", MediaType.HTML_UTF_8.toString())
450-
.setContent(utf8String("Creamy, delicious cheese!"))));
451-
452-
driver.get("https://example-sausages-site.com");
453-
454-
String source = driver.getPageSource();
455-
456-
assertThat(source).contains("delicious cheese!");
415+
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Bidirectional/BidiApiTest.java#L59-L66" >}}
457416
{{< /tab >}}
458417
{{< tab header="Python" >}}
459418
Currently unavailable in python due the inability to mix certain async and sync commands

website_and_docs/content/documentation/webdriver/bidirectional/bidi_api.zh-cn.md

Lines changed: 5 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,7 @@ With Selenium, you can automate the input of basic auth credentials whenever the
2727

2828
{{< tabpane langEqualsHeader=true >}}
2929
{{< tab header="Java" >}}
30-
Predicate<URI> uriPredicate = uri -> uri.getHost().contains("your-domain.com");
31-
32-
((HasAuthentication) driver).register(uriPredicate, UsernameAndPassword.of("admin", "password"));
33-
driver.get("https://your-domain.com/login");
30+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/BidiApiTest.java/#L29-L32" >}}
3431
{{< /tab >}}
3532
{{< tab header="Python" text=true >}}
3633
{{< badge-code >}}
@@ -85,27 +82,9 @@ WebDriver BiDi when there are DOM mutations on a specific
8582
element in the DOM.
8683

8784
{{< tabpane langEqualsHeader=true >}}
88-
{{< tab header="Java" >}}
89-
ChromeDriver driver = new ChromeDriver();
90-
91-
AtomicReference<DomMutationEvent> seen = new AtomicReference<>();
92-
CountDownLatch latch = new CountDownLatch(1);
93-
((HasLogEvents) driver).onLogEvent(domMutation(mutation -> {
94-
seen.set(mutation);
95-
latch.countDown();
96-
}));
97-
98-
driver.get("https://www.google.com");
99-
WebElement span = driver.findElement(By.cssSelector("span"));
100-
101-
((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('cheese', 'gouda');", span);
102-
103-
assertThat(latch.await(10, SECONDS), is(true));
104-
assertThat(seen.get().getAttributeName(), is("cheese"));
105-
assertThat(seen.get().getCurrentValue(), is("gouda"));
106-
107-
driver.quit();
108-
{{< /tab >}}
85+
{{< tab header="Java" >}}
86+
{{< gh-codeblock path="/examples/java/src/test/java/dev/selenium/bidirectional/BidiApiTest.java/#L39-L50" >}}
87+
{{< /tab >}}
10988
{{< tab header="Python" >}}
11089
from selenium import webdriver
11190
from selenium.webdriver.common.by import By
@@ -450,27 +429,7 @@ it with the following examples.
450429

451430
{{< tabpane langEqualsHeader=true >}}
452431
{{< tab header="Java" >}}
453-
import org.openqa.selenium.WebDriver;
454-
import org.openqa.selenium.devtools.HasDevTools;
455-
import org.openqa.selenium.devtools.NetworkInterceptor;
456-
import org.openqa.selenium.remote.http.Contents;
457-
import org.openqa.selenium.remote.http.Filter;
458-
import org.openqa.selenium.remote.http.HttpResponse;
459-
import org.openqa.selenium.remote.http.Route;
460-
461-
NetworkInterceptor interceptor = new NetworkInterceptor(
462-
driver,
463-
Route.matching(req -> true)
464-
.to(() -> req -> new HttpResponse()
465-
.setStatus(200)
466-
.addHeader("Content-Type", MediaType.HTML_UTF_8.toString())
467-
.setContent(utf8String("Creamy, delicious cheese!"))));
468-
469-
driver.get("https://example-sausages-site.com");
470-
471-
String source = driver.getPageSource();
472-
473-
assertThat(source).contains("delicious cheese!");
432+
{{< gh-codeblock path="/examples/dotnet/SeleniumDocs/Bidirectional/BidiApiTest.java#L59-L66" >}}
474433
{{< /tab >}}
475434
{{< tab header="Python" >}}
476435
Currently unavailable in python due the inability to mix certain async and sync commands

0 commit comments

Comments
 (0)