5
5
import io .cucumber .java .en .Given ;
6
6
import io .cucumber .java .en .Then ;
7
7
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
+
16
11
import java .util .List ;
17
12
18
13
import static org .assertj .core .api .Assertions .assertThat ;
19
14
20
15
public class SearchStepDefs {
21
16
private WebDriver driver ;
17
+ SearchPage googleSearch = new SearchPage ();
18
+
22
19
23
20
@ Before
24
21
public void setup () {
@@ -30,25 +27,39 @@ public void teardown() {
30
27
driver .quit ();
31
28
}
32
29
30
+ private String allSearchTerm ;
33
31
@ Given ("^I am on the Google UK homepage$" )
34
32
public void iAmOnTheGoogleUkHomepage () {
35
- driver . manage (). window (). maximize ( );
36
- driver . get ( "https://www.google.co.uk" );
33
+ googleSearch . SearchPage ( driver );
34
+ googleSearch . googleURL ( );
37
35
}
38
36
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 ();
45
50
}
46
51
47
52
@ Then ("results relevant to the search term are returned" )
48
53
public void resultsRelevantToTheSearchTermAreReturned () {
49
- List <WebElement > resultHeaders = driver . findElements ( By . xpath ( "//a/h3" ) );
54
+ List <WebElement > resultHeaders = googleSearch . getResultHeaders ( );
50
55
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 );
52
57
}
53
58
}
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
+ }
54
65
}
0 commit comments