Introduced Cucumber with Selenium for a QA assignment

This commit is contained in:
Adam Klinkosz 2021-04-15 17:35:21 +02:00
parent e2fbc56130
commit 1f56baddab
8 changed files with 185 additions and 0 deletions

43
pom.xml
View file

@ -119,6 +119,49 @@
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- Testing-->
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>6.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>6.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-spring</artifactId>
<version>6.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<version>${junit-jupiter.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>4.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.19.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View file

@ -0,0 +1,43 @@
package org.springframework.samples.petclinic.integration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class Browser {
public final static WebDriver webDriver = createDriver();
private static WebDriver createDriver() {
setupDriver();
ChromeDriver chromeDriver = new ChromeDriver(
new ChromeDriverService.Builder().withSilent(true).build(),
chromeOptions()
);
chromeDriver.manage().window().setSize(new Dimension(1024, 768));
chromeDriver.manage().timeouts().implicitlyWait(0L, TimeUnit.SECONDS);
return chromeDriver;
}
private static void setupDriver() {
WebDriverManager
.chromedriver()
.timeout(60)
.setup();
}
private static ChromeOptions chromeOptions() {
final ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("enable-automation");
chromeOptions.addArguments("--no-sandbox");
chromeOptions.addArguments("--disable-infobars");
chromeOptions.addArguments("--disable-dev-shm-usage");
chromeOptions.addArguments("--disable-browser-side-navigation");
return chromeOptions;
}
}

View file

@ -0,0 +1,16 @@
package org.springframework.samples.petclinic.integration;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
plugin = { "pretty", "html:target/cucumber-html-report" },
glue = { "org.springframework.samples.petclinic.integration" },
features = "classpath:scenarios"
)
public class CucumberRunner {
}

View file

@ -0,0 +1,10 @@
package org.springframework.samples.petclinic.integration;
import org.springframework.boot.test.context.SpringBootTest;
import io.cucumber.spring.CucumberContextConfiguration;
@CucumberContextConfiguration
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class SpringIntegrationTest {
}

View file

@ -0,0 +1,51 @@
package org.springframework.samples.petclinic.integration;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class StepDefinition extends SpringIntegrationTest {
private final WebDriver webDriver = Browser.webDriver;
@Given("^I go to the main page$")
public void mainPage() {
webDriver.navigate().to("http://localhost:8080");
}
@When("^I click on the link with title \"([^\"]*)\"$")
public void clickOnLinkWithTitle(String linkTitle) {
By elementSelector = By.xpath(String.format("//*[@title='%s']", linkTitle));
new WebDriverWait(webDriver, 60L).until(driver -> driver.findElement(elementSelector).isDisplayed());
WebElement webElement = webDriver.findElement(elementSelector);
webElement.click();
}
@Then("^I should see the \"([^\"]*)\" page$")
public void shouldSeeThePage(String pageTitle) {
By elementSelector = By.tagName("h2");
new WebDriverWait(webDriver, 60L).until(driver -> driver.findElement(elementSelector).isDisplayed());
WebElement webElement = webDriver.findElement(elementSelector);
assertThat(webElement.getText()).isEqualTo(pageTitle);
}
@When("^I fill the field named \"([^\"]*)\" with value \"([^\"]*)\"$")
public void fillInputBoxWithValue(String name, String value) {
By elementSelector = By.name(name);
WebElement webElement = webDriver.findElement(elementSelector);
webElement.sendKeys(value);
}
@When("^I submit the form \"([^\"]*)\"$")
public void submitForm(String id) {
By elementSelector = By.id(id);
WebElement webElement = webDriver.findElement(elementSelector);
webElement.submit();
}
}

View file

@ -0,0 +1,3 @@
Feature: Add owners
# Add scenarios covering all cases.

View file

@ -0,0 +1,14 @@
Feature: Find owners
Scenario: Find owners page
When I click on the link with title "find owners"
Then I should see the "Find Owners" page
Scenario: Should find an owner
When I fill the field named "lastName" with value "Franklin"
Then I should see the "Owner Information" page
Scenario: Should find multiple owners
When I fill the field named "lastName" with value "Davis"
Then I should see the "Owners" page
# Add additional checks here.

View file

@ -0,0 +1,5 @@
Feature: Main page
Scenario: Main page
When I go to the main page
Then I should see the "Welcome" page