package HaviskarProject;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class GoogleSearchTest {
private WebDriver driver;
private WebDriverWait wait;
@BeforeEach
public void setUp() {
// Set the path to the ChromeDriver executable
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Admin\\Downloads\\selenium
files\\chromedriver-win64\\chromedriver.exe");
// Initialize the WebDriver
driver = new ChromeDriver();
// Initialize the WebDriverWait with a 10 seconds timeout
wait = new WebDriverWait(driver, java.time.Duration.ofSeconds(10));
// Print message to console
System.out.println("Browser opened and WebDriver initialized.");
@AfterEach
//public void tearDown() {
// if (driver != null) {
// driver.quit();
// Print message to console
// System.out.println("Browser closed and WebDriver quit.");
// }
// }
@Test
public void testGoogleSearch() {
// Open Google
driver.get("https://www.google.com");
System.out.println("Navigated to Google homepage.");
// Wait for the search box to be visible
WebElement searchBox =
wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("q")));
System.out.println("Search box located.");
// Enter search query into the search box
searchBox.sendKeys("Ubuntu 22.04 LTS");
System.out.println("Search query 'Ubuntu 22.04 LTS' entered.");
// Submit the search
searchBox.submit();
System.out.println("Search submitted.");
// Wait for the title to contain the search term
wait.until(ExpectedConditions.titleContains("Ubuntu 22.04 LTS"));
System.out.println("Title contains 'Ubuntu 22.04 LTS'.");
// Assert that the title contains the search term
assertTrue(driver.getTitle().contains("Ubuntu 22.04 LTS"));
System.out.println("Test passed: Title contains 'Ubuntu 22.04 LTS'.");