Skip to content

Commit 486d06c

Browse files
committed
reorganizing code and adding unit test
1 parent 91aae73 commit 486d06c

File tree

4 files changed

+117
-28
lines changed

4 files changed

+117
-28
lines changed
Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,15 @@
1-
package com.baeldung.selenium.avoidbotDetectionSelenium;
1+
package com.baeldung.selenium.avoidbot;
22

3-
import org.openqa.selenium.By;
4-
import org.openqa.selenium.Keys;
5-
import org.openqa.selenium.WebElement;
3+
import com.baeldung.selenium.avoidbot.GoogleSearchService;
64
import org.openqa.selenium.chrome.ChromeDriver;
7-
import org.openqa.selenium.chrome.ChromeOptions;
8-
import java.util.HashMap;
9-
import java.util.Map;
105

116
public class AvoidBotDetectionSelenium {
127

138
public static void main(String[] args) {
14-
ChromeOptions options = new ChromeOptions();
15-
options.addArguments("--disable-blink-features=AutomationControlled");
16-
17-
ChromeDriver driver = new ChromeDriver(options);
18-
Map<String, Object> params = new HashMap<String, Object>();
19-
params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })");
20-
driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params);
21-
driver.get("https://www.google.com");
22-
System.out.println("Navigated to Google's homepage.");
23-
24-
WebElement searchBox = driver.findElement(By.name("q"));
25-
System.out.println("Found the search box.");
26-
27-
searchBox.sendKeys("baeldung");
28-
System.out.println("Entered 'baeldung' into the search box.");
29-
30-
searchBox.sendKeys(Keys.ENTER);
31-
System.out.println("Submitted the search query.");
32-
System.out.println("Page title is: " + driver.getTitle());
33-
34-
driver.quit();
9+
ChromeDriver driver = WebDriverFactory.createDriver();
10+
GoogleSearchService googleService = new GoogleSearchService(driver);
11+
googleService.navigateToGoogle();
12+
googleService.search("baeldung");
13+
googleService.quit();
3514
}
3615
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.selenium.avoidbot;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.Keys;
5+
import org.openqa.selenium.WebDriver;
6+
import org.openqa.selenium.WebElement;
7+
8+
public class GoogleSearchService {
9+
10+
private final WebDriver driver;
11+
12+
public GoogleSearchService(WebDriver driver) {
13+
this.driver = driver;
14+
}
15+
16+
public void navigateToGoogle() {
17+
driver.get("https://www.google.com");
18+
}
19+
20+
public void search(String query) {
21+
WebElement searchBox = driver.findElement(By.name("q"));
22+
searchBox.sendKeys(query);
23+
searchBox.sendKeys(Keys.ENTER);
24+
}
25+
26+
public String getPageTitle() {
27+
return driver.getTitle();
28+
}
29+
30+
public void quit() {
31+
driver.quit();
32+
}
33+
}
34+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.selenium.avoidbot;
2+
3+
import org.openqa.selenium.chrome.ChromeDriver;
4+
import org.openqa.selenium.chrome.ChromeOptions;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class WebDriverFactory {
9+
10+
public static ChromeDriver createDriver() {
11+
ChromeOptions options = new ChromeOptions();
12+
options.addArguments("--disable-blink-features=AutomationControlled");
13+
14+
ChromeDriver driver = new ChromeDriver(options);
15+
Map<String, Object> params = new HashMap<>();
16+
params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })");
17+
driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params);
18+
19+
return driver;
20+
}
21+
}
22+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.selenium.avoidbot;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.mockito.Mockito;
6+
import org.openqa.selenium.By;
7+
import org.openqa.selenium.Keys;
8+
import org.openqa.selenium.WebDriver;
9+
import org.openqa.selenium.WebElement;
10+
11+
import static org.mockito.Mockito.*;
12+
13+
class GoogleSearchServiceUnitTest {
14+
15+
private WebDriver driver;
16+
private WebElement searchBox;
17+
private GoogleSearchService googleSearchService;
18+
19+
@BeforeEach
20+
void setUp() {
21+
driver = Mockito.mock(WebDriver.class);
22+
searchBox = Mockito.mock(WebElement.class);
23+
when(driver.findElement(By.name("q"))).thenReturn(searchBox);
24+
googleSearchService = new GoogleSearchService(driver);
25+
}
26+
27+
@Test
28+
void givenGoogleSearchService_whenNavigateToGoogle_thenDriverLoadsGoogleHomePage() {
29+
googleSearchService.navigateToGoogle();
30+
verify(driver).get("https://www.google.com");
31+
}
32+
33+
@Test
34+
void givenGoogleSearchService_whenSearchWithQuery_thenSearchBoxReceivesQueryAndEnterKey() {
35+
googleSearchService.search("baeldung");
36+
verify(searchBox).sendKeys("baeldung");
37+
verify(searchBox).sendKeys(Keys.ENTER);
38+
}
39+
40+
@Test
41+
void givenGoogleSearchService_whenGetPageTitle_thenReturnExpectedTitle() {
42+
when(driver.getTitle()).thenReturn("Google - Baeldung");
43+
String title = googleSearchService.getPageTitle();
44+
verify(driver).getTitle();
45+
assert title.equals("Google - Baeldung");
46+
}
47+
48+
@Test
49+
void givenGoogleSearchService_whenQuit_thenDriverIsClosed() {
50+
googleSearchService.quit();
51+
verify(driver).quit();
52+
}
53+
}
54+

0 commit comments

Comments
 (0)