Skip to content

Instantly share code, notes, and snippets.

@CXwudi
Last active April 10, 2023 17:34
Show Gist options
  • Save CXwudi/472fd7049124d528e32f7b9c1c31ed94 to your computer and use it in GitHub Desktop.
Save CXwudi/472fd7049124d528e32f7b9c1c31ed94 to your computer and use it in GitHub Desktop.
Complex Scenario implementation of CMS using Cucumber, taken from the private repo for COMP5903
package scs.comp5903.cmscucumber.util;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
/**
* @author CX无敌
* @date 2022-06-03
*/
public class CmsPageUtils {
public static void getToPageAndLogin(WebDriver driver, String url, String username, String password) {
driver.get(url);
CmsPageUtils.login(driver, username, password);
}
private static void login(WebDriver driver, String username, String password) {
driver.findElement(By.id("username")).sendKeys(username);
driver.findElement(By.id("password")).sendKeys(password);
driver.findElement(By.cssSelector("#login > form > div.submit-container.form-actions > button")).click();
}
}
package scs.comp5903.cmscucumber.util;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.time.Duration;
/**
* @author CX无敌
* @date 2022-06-01
*/
public class SeleniumFactory {
static {
WebDriverManager.chromedriver().setup();
}
public static WebDriver getDriver() {
var chromeDriver = new ChromeDriver();
chromeDriver.manage().timeouts().implicitlyWait(Duration.ofMinutes(10));
return chromeDriver;
}
}
Feature: CMS can handle race condition between two students
Scenario: two students try to register for the last spot
Given student one and student two
And both are on the course registration page
When student one and student two click the register button on COMP3004 at the same time
Then only one of them should be registered for the course
package scs.comp5903.cmscucumber.cucumber.twostudents;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import lombok.extern.slf4j.Slf4j;
import org.jooq.lambda.Unchecked;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import scs.comp5903.cmscucumber.util.CmsPageUtils;
import scs.comp5903.cmscucumber.util.Constants;
import scs.comp5903.cmscucumber.util.SeleniumFactory;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import static org.junit.jupiter.api.Assertions.assertNotSame;
/**
* @author CX无敌
* @date 2022-06-01
*/
@Slf4j
public class TwoStudentsRaceConditionStepDef {
private WebDriver student1Driver;
private WebDriver student2Driver;
private CompletableFuture<Boolean> student1Registration;
private CompletableFuture<Boolean> student2Registration;
private final CountDownLatch timingLock = new CountDownLatch(1);
@Given("student one and student two")
public void studentOneAndStudentTwo() {
student1Driver = SeleniumFactory.getDriver();
student2Driver = SeleniumFactory.getDriver();
}
@And("both are on the course registration page")
public void bothAreAtTheCourseRegistrationPage() {
CmsPageUtils.getToPageAndLogin(student1Driver, Constants.URL_CMS + "/student/registration", "student1", "pass1234");
CmsPageUtils.getToPageAndLogin(student2Driver, Constants.URL_CMS + "/student/registration", "student2", "pass1234");
}
@When("student one and student two click the register button on COMP3004 at the same time")
public void studentOneAndStudentTwoClickTheRegisterButtonOnCOMPAtTheSameTime() throws InterruptedException {
student1Registration = generateRegistrationStep(student1Driver);
student2Registration = generateRegistrationStep(student2Driver);
Thread.sleep(1200);
timingLock.countDown();
log.info("Race condition starts");
}
@Then("only one of them should be registered for the course")
public void onlyOneOfThemShouldBeRegisteredForTheCourse() throws ExecutionException, InterruptedException {
var result1 = student1Registration.get();
var result2 = student2Registration.get();
assertNotSame(result1, result2);
}
private CompletableFuture<Boolean> generateRegistrationStep(WebDriver driver) {
return CompletableFuture.supplyAsync(Unchecked.supplier(() -> {
var registrableCourseList = driver.findElement(By.id("registrable_course_list"));
var comp3004ElementOpt = registrableCourseList.findElements(By.className("list-group-item")).stream().filter(e -> {
var titleElement = e.findElement(By.cssSelector("div.row.d-flex.justify-content-between.ml-1.mr-1 > div"));
return titleElement.getText().contains("COMP3004");
}).findFirst();
var comp3004Element = comp3004ElementOpt.orElseThrow();
log.debug("found comp3004");
var registrationButton = comp3004Element.findElement(By.cssSelector("button"));
timingLock.await();
registrationButton.click();
registrableCourseList = driver.findElement(By.id("registrable_course_list"));
var comp3004ElementAgainOpt = registrableCourseList.findElements(By.className("list-group-item")).stream().filter(e -> {
var titleElement = e.findElement(By.cssSelector("div.row.d-flex.justify-content-between.ml-1.mr-1 > div"));
return titleElement.getText().contains("COMP3004");
}).findFirst();
return comp3004ElementAgainOpt.isEmpty();
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment