Selenium WebDriver at Cucumber
Our earlier session we created Gherkin feature files and generated class definition according it, In this session we are going to add Selenium WebDriver on every step to control browser. We will do this process 2 way:
-
First we will see the whole Implementation
-
Then will see the every method explanations
Full implemented class
You can copy the full implementation and paste to your project as well. Notice: Sometime Google change their css class name, if the code throw class not found exception please let me know.
import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class StepDefinitions {
private WebDriver webDriver;
private String baseUrl = "https://www.google.com";
@Before
public void setup() {
System.setProperty("webdriver.chrome.driver", "driver/chrome-driver-81.0.4044.69.exe");
webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
}
@Given("Open google.com")
public void open_google_com() {
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.get(baseUrl);
}
@When("Entering number {int} and {int}")
public void entering_number_and(Integer first, Integer second) {
WebElement googleTextBox = webDriver.findElement(By.className("gLFyf"));
googleTextBox.sendKeys(first + " + " + second);
}
@When("Press enter")
public void press_enter() {
WebElement searchButton = webDriver.findElement(By.className("gNO89b"));
searchButton.click();
}
@Then("Result should be {int}")
public void result_should_be(Integer result) {
WebElement calculatorTextBox = webDriver.findElement(By.className("qv3Wpe"));
Integer getResult = Integer.parseInt(calculatorTextBox.getText());
Assert.assertEquals(getResult, result);
webDriver.close();
}
@After
public void end() {
if (webDriver != null) {
webDriver.quit();
}
}
}
Every step explanations
Here we explain the steps of full implementation, you also find the method action definition of previous chapter.
Global Variables
Declaring global variables for access them all over the project.
private WebDriver webDriver;
private String baseUrl = "https://www.google.com";
Instantiate Chrome WebDriver
Instantiating Google Chrome browser driver, the driver stored in our project root >> driver Before run please make sure your chrome browser version and chrome driver versions are same, otherwise it will not able to run project. If you not familiar on it then please see Chrome version check and download driver. After Instantiate it’s maximize the browser.
@Before
public void setup() {
System.setProperty("webdriver.chrome.driver", "driver/chrome-driver-81.0.4044.69.exe");
webDriver = new ChromeDriver();
webDriver.manage().window().maximize();
}
Open google.com into browser
Wait 5 second and then open google.com at chrome browser.
@Given("Open google.com")
public void open_google_com() {
webDriver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
webDriver.get(baseUrl);
}
Enter 2 number into google search bar
Take 2 numbers from gherkin feature file and put them to google search text box.
@When("Entering number {int} and {int}")
public void entering_number_and(Integer first, Integer second) {
WebElement googleTextBox = webDriver.findElement(By.className("gLFyf"));
googleTextBox.sendKeys(first + " + " + second);
}
Click or press enter for calculation
After put numbers in google text box clicking search button for result.
@When("Press enter")
public void press_enter() {
WebElement searchButton = webDriver.findElement(By.className("gNO89b"));
searchButton.click();
}
Compare actual value and expected value
Comparing Search result with value comes from gherkin feature file.
@Then("Result should be {int}")
public void result_should_be(Integer result) {
WebElement calculatorTextBox = webDriver.findElement(By.className("qv3Wpe"));
Integer getResult = Integer.parseInt(calculatorTextBox.getText());
Assert.assertEquals(getResult, result);
webDriver.close();
}
End of the Test
Closing browser when the full process completed.
@After
public void end() {
if (webDriver != null) {
webDriver.quit();
}
}