Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Heckenlights Test with JBehave, Selenide and Guice
import org.jbehave.core.annotations.BeforeScenario;
import org.jbehave.web.selenium.SeleniumContext;
import org.jbehave.web.selenium.WebDriverProvider;
import org.openqa.selenium.WebDriver;
import javax.inject.Inject;
public abstract class AbstractSeleniumSteps {
@Inject
private WebDriverProvider webDriverProvider;
@Inject
private SeleniumContext seleniumContext;
@BeforeScenario
public void beforeTheScenario() {
seleniumContext.setCurrentScenario(getClass().getSimpleName());
}
protected WebDriver webDriver() {
return webDriverProvider.get();
}
}
Scenario: Visit heckenlights.org
Given website heckenlights
Then the website is displayed
And the title is HECKENLIGHTS
And the controls are visible
And 2 tikales are in the playlist
When I click on Contact
Then the page scrolls down
Given website heckenlights
Then there are at least 2 presets
And upload alert is invisible
When I click preset number 1
Then upload is successful
import static com.codeborne.selenide.Selenide.$;
import static com.codeborne.selenide.Selenide.$$;
import static com.codeborne.selenide.Selenide.executeJavaScript;
import static com.codeborne.selenide.Selenide.title;
import static org.openqa.selenium.By.linkText;
import com.codeborne.selenide.ElementsCollection;
import com.codeborne.selenide.Selenide;
import com.codeborne.selenide.SelenideElement;
/**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
*/
public class HeckenlightsPage {
public String getTitle() {
return title();
}
public SelenideElement getControls() {
return $("#controls");
}
public ElementsCollection getPlaylistTitles() {
return $$("#playlist a");
}
public void clickLink(String item) {
$(linkText(item)).click();
Selenide.sleep(500);
}
public Number height() {
return executeJavaScript("return (document.height !== undefined) ? document.height : document.body.offsetHeight;");
}
public Number scrollY() {
return executeJavaScript("return window.scrollY;");
}
public ElementsCollection getPresets() {
return $$("#presets a");
}
public SelenideElement getUploadSuccessAlert()
{
return $("#uploadsuccess");
}
}
import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selenide.Wait;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.assertj.core.api.Assertions.assertThat;
import com.codeborne.selenide.CollectionCondition;
import com.codeborne.selenide.ex.ListSizeMismatch;
import com.codeborne.selenide.impl.WebElementsCollection;
import com.google.common.base.Predicate;
import org.jbehave.core.annotations.Given;
import org.jbehave.core.annotations.Then;
import org.jbehave.core.annotations.When;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import javax.inject.Inject;
import java.util.List;
/**
* @author <a href="mailto:mpaluch@paluch.biz">Mark Paluch</a>
*/
public class HeckenlightsSteps extends AbstractSeleniumSteps {
@Inject
private HeckenlightsPage page;
private String baseUri = "http://heckenlights.org/static/";
@Given("website heckenlights")
public void givenNavigate() {
webDriver().get(baseUri);
}
@When("I click on $item")
public void whenIClickOn(String item) {
page.clickLink(item);
}
@When("I click preset number $number")
public void whenIClickOnPreset(int position) {
page.getPresets().get(position + 1).click();
}
@Then("the website is displayed")
public void thenWebsiteIsDisplayed() {
assertThat(webDriver().getPageSource()).isNotEmpty();
assertThat(webDriver().getCurrentUrl()).contains(baseUri);
}
@Then("the controls are visible")
public void thenControlsAreVisible() {
page.getControls().shouldBe(visible);
}
@Then("$number titles are in the playlist")
public void thenNumberOfTitlesAreInThePlaylist(int number) {
page.getPlaylistTitles().shouldHaveSize(number);
}
@Then("the title is $title")
public void thenTitleIs(String title) {
assertThat(page.getTitle()).isEqualTo(title);
}
@Then("upload alert is invisible")
public void thenUploadAlertIsInvisible() {
page.getUploadSuccessAlert().shouldNotBe(visible);
}
@Then("upload is successful")
public void thenUploadIsSucessful() {
page.getUploadSuccessAlert().isDisplayed();
page.getUploadSuccessAlert().has(text("Enqeued your track "));
}
@Then("there are at least $number presets")
public void thenThereAreAtLeastCountPresets(int count) {
page.getPresets().shouldHave(atLeastElements(count));
}
@Then("the page scrolls down")
public void thenThePageScrollsDown() {
final int half = halfHeight();
Wait().withTimeout(2, SECONDS).until(new Predicate<WebDriver>() {
@Override
public boolean apply(WebDriver input) {
int scroll = page.scrollY().intValue();
return scroll > half;
}
});
}
private int halfHeight() {
int height = page.height().intValue();
return height / 2;
}
private CollectionCondition atLeastElements(final int count) {
return new CollectionCondition() {
@Override
public void fail(WebElementsCollection collection, List<WebElement> elements, Exception lastError, long timeoutMs) {
throw new ListSizeMismatch(count, collection, elements, lastError, timeoutMs);
}
@Override
public boolean apply(List<WebElement> input) {
return input.size() >= count;
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment