From 91aae734fb29be0c26a205388b26b60c7d0b4229 Mon Sep 17 00:00:00 2001 From: programenth <139003443+programenth@users.noreply.github.com> Date: Sat, 20 Sep 2025 11:13:28 +0530 Subject: [PATCH 1/2] BAEL-8377 | Adding code for avoiding bot detection Hi , Could you please the following PR related to avoiding bot detection using Selenium! --- .../avoidbot/AvoidBotDetectionSelenium.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/AvoidBotDetectionSelenium.java diff --git a/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/AvoidBotDetectionSelenium.java b/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/AvoidBotDetectionSelenium.java new file mode 100644 index 000000000000..ad62b48d2bc6 --- /dev/null +++ b/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/AvoidBotDetectionSelenium.java @@ -0,0 +1,36 @@ +package com.baeldung.selenium.avoidbotDetectionSelenium; + +import org.openqa.selenium.By; +import org.openqa.selenium.Keys; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.chrome.ChromeDriver; +import org.openqa.selenium.chrome.ChromeOptions; +import java.util.HashMap; +import java.util.Map; + +public class AvoidBotDetectionSelenium { + + public static void main(String[] args) { + ChromeOptions options = new ChromeOptions(); + options.addArguments("--disable-blink-features=AutomationControlled"); + + ChromeDriver driver = new ChromeDriver(options); + Map params = new HashMap(); + params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })"); + driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params); + driver.get("https://www.google.com"); + System.out.println("Navigated to Google's homepage."); + + WebElement searchBox = driver.findElement(By.name("q")); + System.out.println("Found the search box."); + + searchBox.sendKeys("baeldung"); + System.out.println("Entered 'baeldung' into the search box."); + + searchBox.sendKeys(Keys.ENTER); + System.out.println("Submitted the search query."); + System.out.println("Page title is: " + driver.getTitle()); + + driver.quit(); + } +} From 486d06cbac1159f895e8bd8e781f9b38a93bb0ef Mon Sep 17 00:00:00 2001 From: programenth <139003443+programenth@users.noreply.github.com> Date: Sat, 27 Sep 2025 13:58:24 +0000 Subject: [PATCH 2/2] reorganizing code and adding unit test --- .../avoidbot/AvoidBotDetectionSelenium.java | 35 +++--------- .../avoidbot/GoogleSearchService.java | 34 ++++++++++++ .../selenium/avoidbot/WebDriverFactory.java | 22 ++++++++ .../avoidbot/GoogleSearchServiceUnitTest.java | 54 +++++++++++++++++++ 4 files changed, 117 insertions(+), 28 deletions(-) create mode 100644 testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/GoogleSearchService.java create mode 100644 testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/WebDriverFactory.java create mode 100644 testing-modules/selenium/src/test/java/com/baeldung/selenium/avoidbot/GoogleSearchServiceUnitTest.java diff --git a/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/AvoidBotDetectionSelenium.java b/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/AvoidBotDetectionSelenium.java index ad62b48d2bc6..ae7f96e6e04f 100644 --- a/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/AvoidBotDetectionSelenium.java +++ b/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/AvoidBotDetectionSelenium.java @@ -1,36 +1,15 @@ -package com.baeldung.selenium.avoidbotDetectionSelenium; +package com.baeldung.selenium.avoidbot; -import org.openqa.selenium.By; -import org.openqa.selenium.Keys; -import org.openqa.selenium.WebElement; +import com.baeldung.selenium.avoidbot.GoogleSearchService; import org.openqa.selenium.chrome.ChromeDriver; -import org.openqa.selenium.chrome.ChromeOptions; -import java.util.HashMap; -import java.util.Map; public class AvoidBotDetectionSelenium { public static void main(String[] args) { - ChromeOptions options = new ChromeOptions(); - options.addArguments("--disable-blink-features=AutomationControlled"); - - ChromeDriver driver = new ChromeDriver(options); - Map params = new HashMap(); - params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })"); - driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params); - driver.get("https://www.google.com"); - System.out.println("Navigated to Google's homepage."); - - WebElement searchBox = driver.findElement(By.name("q")); - System.out.println("Found the search box."); - - searchBox.sendKeys("baeldung"); - System.out.println("Entered 'baeldung' into the search box."); - - searchBox.sendKeys(Keys.ENTER); - System.out.println("Submitted the search query."); - System.out.println("Page title is: " + driver.getTitle()); - - driver.quit(); + ChromeDriver driver = WebDriverFactory.createDriver(); + GoogleSearchService googleService = new GoogleSearchService(driver); + googleService.navigateToGoogle(); + googleService.search("baeldung"); + googleService.quit(); } } diff --git a/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/GoogleSearchService.java b/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/GoogleSearchService.java new file mode 100644 index 000000000000..f47f084de2e3 --- /dev/null +++ b/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/GoogleSearchService.java @@ -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(); + } +} + diff --git a/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/WebDriverFactory.java b/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/WebDriverFactory.java new file mode 100644 index 000000000000..777a5c2a936c --- /dev/null +++ b/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/WebDriverFactory.java @@ -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 params = new HashMap<>(); + params.put("source", "Object.defineProperty(navigator, 'webdriver', { get: () => undefined })"); + driver.executeCdpCommand("Page.addScriptToEvaluateOnNewDocument", params); + + return driver; + } +} + diff --git a/testing-modules/selenium/src/test/java/com/baeldung/selenium/avoidbot/GoogleSearchServiceUnitTest.java b/testing-modules/selenium/src/test/java/com/baeldung/selenium/avoidbot/GoogleSearchServiceUnitTest.java new file mode 100644 index 000000000000..d36acb751eb9 --- /dev/null +++ b/testing-modules/selenium/src/test/java/com/baeldung/selenium/avoidbot/GoogleSearchServiceUnitTest.java @@ -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(); + } +} +