Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.baeldung.selenium.avoidbot;

import com.baeldung.selenium.avoidbot.GoogleSearchService;
import org.openqa.selenium.chrome.ChromeDriver;

public class AvoidBotDetectionSelenium {

public static void main(String[] args) {
ChromeDriver driver = WebDriverFactory.createDriver();
GoogleSearchService googleService = new GoogleSearchService(driver);
googleService.navigateToGoogle();
googleService.search("baeldung");
googleService.quit();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.selenium.avoidbot;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class GoogleSearchService {

private final WebDriver driver;

public GoogleSearchService(WebDriver driver) {
this.driver = driver;
}

public void navigateToGoogle() {
driver.get("https://www.google.com");
}

public void search(String query) {
WebElement searchBox = driver.findElement(By.name("q"));
searchBox.sendKeys(query);
searchBox.sendKeys(Keys.ENTER);
}

public String getPageTitle() {
return driver.getTitle();
}

public void quit() {
driver.quit();
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.baeldung.selenium.avoidbot;

import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import java.util.HashMap;
import java.util.Map;

public class WebDriverFactory {

public static ChromeDriver createDriver() {
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-blink-features=AutomationControlled");

ChromeDriver driver = new ChromeDriver(options);
Map<String, Object> params = new HashMap<>();
params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })");
driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params);

return driver;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.baeldung.selenium.avoidbot;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import static org.mockito.Mockito.*;

class GoogleSearchServiceUnitTest {

private WebDriver driver;
private WebElement searchBox;
private GoogleSearchService googleSearchService;

@BeforeEach
void setUp() {
driver = Mockito.mock(WebDriver.class);
searchBox = Mockito.mock(WebElement.class);
when(driver.findElement(By.name("q"))).thenReturn(searchBox);
googleSearchService = new GoogleSearchService(driver);
}

@Test
void givenGoogleSearchService_whenNavigateToGoogle_thenDriverLoadsGoogleHomePage() {
googleSearchService.navigateToGoogle();
verify(driver).get("https://www.google.com");
}

@Test
void givenGoogleSearchService_whenSearchWithQuery_thenSearchBoxReceivesQueryAndEnterKey() {
googleSearchService.search("baeldung");
verify(searchBox).sendKeys("baeldung");
verify(searchBox).sendKeys(Keys.ENTER);
}

@Test
void givenGoogleSearchService_whenGetPageTitle_thenReturnExpectedTitle() {
when(driver.getTitle()).thenReturn("Google - Baeldung");
String title = googleSearchService.getPageTitle();
verify(driver).getTitle();
assert title.equals("Google - Baeldung");
}

@Test
void givenGoogleSearchService_whenQuit_thenDriverIsClosed() {
googleSearchService.quit();
verify(driver).quit();
}
}