|
| 1 | +package StepDefinitions; |
| 2 | + |
| 3 | +import io.cucumber.java.en.Given; |
| 4 | +import io.cucumber.java.en.Then; |
| 5 | +import io.cucumber.java.en.When; |
| 6 | +import org.openqa.selenium.By; |
| 7 | +import org.openqa.selenium.Keys; |
| 8 | +import org.openqa.selenium.WebDriver; |
| 9 | +import org.openqa.selenium.chrome.ChromeDriver; |
| 10 | +import java.util.concurrent.TimeUnit; |
| 11 | + |
| 12 | +public class GoogleSearchSteps { |
| 13 | + |
| 14 | + WebDriver driver = null; |
| 15 | + |
| 16 | + @Given("User is on Google Search Page") |
| 17 | + public void user_is_on_google_search_page() { |
| 18 | + System.out.println("Opening Browser..."); |
| 19 | + System.out.println("Project Path: " + System.getProperty("user.dir")); |
| 20 | + //driver setup |
| 21 | + driver = new ChromeDriver(); // assumes chrome driver is setup and is in Path var |
| 22 | + driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); |
| 23 | + driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS); |
| 24 | + driver.manage().window().maximize(); |
| 25 | + |
| 26 | + driver.get("http://www.google.com"); |
| 27 | + System.out.println("User is on search page"); |
| 28 | + } |
| 29 | + |
| 30 | + // example of passing search term using parameter with regex |
| 31 | + @When("^I enter (.*) in box$") |
| 32 | + public void i_enter_search_term_in_box(String searchterm) { |
| 33 | + |
| 34 | + driver.findElement(By.name("q")).sendKeys(searchterm); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + @When("I click on Search button") |
| 39 | + public void i_click_on_search_button() { |
| 40 | + |
| 41 | + driver.findElement(By.name("q")).sendKeys(Keys.ENTER); |
| 42 | + |
| 43 | + } |
| 44 | + |
| 45 | + @Then("Show Results") |
| 46 | + public void show_results() throws InterruptedException { |
| 47 | + System.out.println("Showing results"); |
| 48 | + |
| 49 | + Thread.sleep(2); |
| 50 | + driver.close(); |
| 51 | + driver.quit(); |
| 52 | + System.out.println("Closed Browser"); |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +} |
0 commit comments