Skip to content

Commit 34b3002

Browse files
Merge pull request #6 from ashivananjappa-GS/task_code_akshatha
Completed all task
2 parents 84d61a4 + 39de2bd commit 34b3002

File tree

3 files changed

+99
-22
lines changed

3 files changed

+99
-22
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package pageobjects;
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+
import org.openqa.selenium.support.ui.ExpectedConditions;
8+
import org.openqa.selenium.support.ui.WebDriverWait;
9+
import java.time.Duration;
10+
import java.util.List;
11+
12+
13+
public class SearchPage {
14+
public static final By ACCEPT_ALL = By.id("L2AGLb");
15+
public static final By SEARCH_BUTTON = By.id("APjFqb");
16+
public static final By RESULT_HEADERS = By.xpath("//a/h3");
17+
public static final By FEELING_LUCKY = By.name("btnI");
18+
private WebDriver driver;
19+
private WebDriverWait wait;
20+
21+
22+
public void SearchPage(WebDriver driver) {
23+
this.driver = driver;
24+
this.wait = new WebDriverWait(driver, Duration.ofSeconds(15));
25+
}
26+
27+
public void googleURL() {
28+
driver.manage().window().maximize();
29+
driver.get("https://www.google.co.uk/");
30+
}
31+
32+
public void setAcceptAll(){
33+
wait.until(ExpectedConditions.elementToBeClickable(ACCEPT_ALL)).click();
34+
}
35+
36+
public void searchTerm(String searchTerm) {
37+
WebElement searchField = wait.until(ExpectedConditions.elementToBeClickable(SEARCH_BUTTON));
38+
searchField.sendKeys(searchTerm);
39+
searchField.sendKeys(Keys.ENTER);
40+
}
41+
42+
public void luckySearchTerm(String searchTerm) {
43+
WebElement searchField = wait.until(ExpectedConditions.elementToBeClickable(SEARCH_BUTTON));
44+
searchField.sendKeys(searchTerm);
45+
}
46+
47+
public void clickImFeelingLucky() {
48+
WebElement luckyButtonElement = wait.until(ExpectedConditions.elementToBeClickable(FEELING_LUCKY));
49+
luckyButtonElement.click();
50+
}
51+
52+
public List<WebElement> getResultHeaders() {
53+
return driver.findElements(RESULT_HEADERS);
54+
}
55+
}

src/test/java/stepdefs/SearchStepDefs.java

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,17 @@
55
import io.cucumber.java.en.Given;
66
import io.cucumber.java.en.Then;
77
import io.cucumber.java.en.When;
8-
import org.openqa.selenium.By;
9-
import org.openqa.selenium.Keys;
10-
import org.openqa.selenium.WebDriver;
11-
import org.openqa.selenium.WebElement;
12-
import org.openqa.selenium.support.ui.ExpectedConditions;
13-
import org.openqa.selenium.support.ui.WebDriverWait;
14-
15-
import java.time.Duration;
8+
import org.openqa.selenium.*;
9+
import pageobjects.SearchPage;
10+
1611
import java.util.List;
1712

1813
import static org.assertj.core.api.Assertions.assertThat;
1914

2015
public class SearchStepDefs {
2116
private WebDriver driver;
17+
SearchPage googleSearch = new SearchPage();
18+
2219

2320
@Before
2421
public void setup() {
@@ -30,25 +27,39 @@ public void teardown() {
3027
driver.quit();
3128
}
3229

30+
private String allSearchTerm;
3331
@Given("^I am on the Google UK homepage$")
3432
public void iAmOnTheGoogleUkHomepage() {
35-
driver.manage().window().maximize();
36-
driver.get("https://www.google.co.uk");
33+
googleSearch.SearchPage(driver);
34+
googleSearch.googleURL();
3735
}
3836

39-
@When("I enter a search term")
40-
public void iEnterASearchTerm() {
41-
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
42-
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id='L2AGLb2']"))).click();
43-
driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("BBC news");
44-
driver.findElement(By.xpath("//input[@title='Search']")).sendKeys(Keys.ENTER);
37+
@When("I enter a {string}")
38+
public void iEnterA(String searchTerm) {
39+
allSearchTerm = searchTerm;
40+
googleSearch.setAcceptAll();
41+
googleSearch.searchTerm(searchTerm);
42+
}
43+
44+
@When("entered a search term and clicked on I am Feeling Lucky")
45+
public void enteredASearchTermAndClickedOnIAmFeelingLucky() {
46+
googleSearch.setAcceptAll();
47+
String searchText = "BBC";
48+
googleSearch.luckySearchTerm(searchText);
49+
googleSearch.clickImFeelingLucky();
4550
}
4651

4752
@Then("results relevant to the search term are returned")
4853
public void resultsRelevantToTheSearchTermAreReturned() {
49-
List<WebElement> resultHeaders = driver.findElements(By.xpath("//a/h3"));
54+
List<WebElement> resultHeaders = googleSearch.getResultHeaders();
5055
for(WebElement header : resultHeaders) {
51-
assertThat(header.getText()).as("Search results contains search term").contains("BBC");
56+
assertThat(header.getText()).as("Search results contains search term").containsIgnoringCase(allSearchTerm);
5257
}
5358
}
59+
60+
@Then("navigated to the relavant URL to the search entered")
61+
public void navigatedToTheRelavantUrlToTheSearchEntered() {
62+
String currentUrl = driver.getCurrentUrl();
63+
assertThat(currentUrl).as("Search results contains search term").contains("bbc");
64+
}
5465
}

src/test/resources/GoogleSearch.feature

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,19 @@ Feature: Search
33
I want to search the internet
44
So that results appropriate to my interests are returned
55

6-
@FixMe
7-
Scenario: Relevant search results are returned to the user
6+
@Smoke
7+
Scenario Outline: Relevant search results are returned to the user
88
Given I am on the Google UK homepage
9-
When I enter a search term
10-
Then results relevant to the search term are returned
9+
When I enter a "<search_term>"
10+
Then results relevant to the search term are returned
11+
Examples:
12+
| search_term |
13+
| BBC news |
14+
| GymShark |
15+
| QA process |
16+
17+
@Smoke
18+
Scenario: User is Navigated to the relevant URL
19+
Given I am on the Google UK homepage
20+
When entered a search term and clicked on I am Feeling Lucky
21+
Then navigated to the relavant URL to the search entered

0 commit comments

Comments
 (0)