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..ae7f96e6e04f --- /dev/null +++ b/testing-modules/selenium/src/main/java/com/baeldung/selenium/avoidbot/AvoidBotDetectionSelenium.java @@ -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(); + } +} 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(); + } +} +