class BasicContext implements Context (View source)

BasicContext

Context used to define generic steps like following anchors or pressing buttons. Handles timeouts. Handles redirections. Handles AJAX enabled links, buttons and forms - jQuery is assumed.

Traits

Represents a behat context which is aware of a main {SilverStripeContext} context.

Helpers for working with steps

Step tools to help debug failing steps

Properties

protected SilverStripeContext $mainContext from  MainContextAwareTrait
protected string $dateFormat

Date format in date() syntax

protected string $timeFormat

Time format in date() syntax

protected string $datetimeFormat

Date/time format in date() syntax

protected FixtureContext $fixtureContext

Methods

public
getMainContext()

Get the main context

public
$this
setMainContext(SilverStripeContext $mainContext)

No description

public
detectMainContext(BeforeScenarioScope $scope)

Helper method to detect the main context

protected
ScenarioInterface
getStepScenario(FeatureNode $feature, NodeInterface $step)

Get scenario from step in a feature node See https://github.com/Behat/Behat/issues/653

protected
mixed
retryThrowable(callable $callback, int $timeout = 3)

Retry until no exceptions are thrown, or until $timeout seconds are reached.

protected
bool
stepHasTag(StepScope|ScenarioScope $event, string $tag)

Check if a step has a given tag

public
void
resetUtilitiesAfterStep(AfterScenarioScope $event)

Ensure utilty steps are reset for subsequent scenarios

public
iTakeAScreenshotAfterEveryStep()

Useful step for working out why a behat testing isn't working when running the browser headless Remove this step from in a feature file once the test is working correct

public
iDumpTheRenderedHtmlAfterEveryStep()

Utility function for debugging failing behat tests Remove this step from in a feature file once the test is working correct

public
takeScreenshotAfterFailedStep(AfterStepScope $event)

Take a screenshot when step fails, or take a screenshot after every step if the use has specified "I take a screenshot after every step" Works only with FacebookWebDriver.

public
void
dumpHtmlAfterStep(AfterStepScope $event)

Dump HTML when step fails.

public
dumpRenderedHtml(StepScope $event)

Dump rendered HTML to disk Useful for seeing the state of a page when writing and debugging feature files

public
takeScreenshot(StepScope $event)

Take a nice screenshot

public
void
gatherContexts(BeforeScenarioScope $scope)

Get the fixture context of the current module

public
Session
getSession(string $name = null)

Get Mink session from MinkContext

public
appendErrorHandlerBeforeStep(AfterStepScope $event)

No description

public
readErrorHandlerAfterStep(AfterStepScope $event)

No description

public
handleAjaxBeforeStep(BeforeStepScope $event)

Hook into jQuery ajaxStart, ajaxSuccess and ajaxComplete events.

public
handleAjaxAfterStep(AfterStepScope $event)

Wait for the __ajaxStatus()to return anything but 'waiting'.

public
handleAjaxTimeout()

No description

public
closeModalDialog(AfterScenarioScope $event)

Close modal dialog if test scenario fails on CMS page

public
cleanAssetsAfterScenario(AfterScenarioScope $event)

Delete any created files and folders from assets directory

public
stepPageCantBeFound()

No description

public
stepIWaitFor(float $secs)

No description

protected
NodeElement|null
findNamedButton(string $title)

Find visible button with the given text.

public
iShouldSeeAButton(string $negative, string $text)

Example: I should see a "Submit" button Example: I should not see a "Delete" button

public
stepIPressTheButton(string $text)

No description

public
stepIPressTheButtons(string $text)

No description

public
stepIPressTheButtonConfirmingTheDialog(string $button)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

public
stepIPressTheButtonDismissingTheDialog(string $button)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

public
iClickOnTheElement(string $selector)

No description

public
iClickOnTheElementConfirmingTheDialog($selector)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

public
iClickInTheElement(string $clickType, string $text, string $selector)

No description

public
iClickInTheElementConfirmingTheDialog(string $clickType, string $text, string $selector)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

public
iClickInTheElementDismissingTheDialog(string $clickType, string $text, string $selector)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

public
theElementAttributeShouldBe($selector, $attribute, $value)

No description

public
iSeeTheDialogText(string $expected)

No description

public
iTypeIntoTheDialog(string $data)

No description

protected
WebDriverAlert
getExpectedAlert()

Wait for alert to appear, and return handle

public
iConfirmTheDialog()

No description

public
iDismissTheDialog()

No description

protected
WebDriver
getWebDriverSession()

Get Selenium webdriver session.

public
Given
iAttachTheFileTo(string $field, string $path) deprecated

No description

public
iSelectFromInputGroup(string $value, string $labelText)

Select an individual input from within a group, matched by the top-most label.

public
iPutABreakpoint()

Pauses the scenario until the user presses a key. Useful when debugging a scenario.

public
false|string
castRelativeToAbsoluteTime(string $prefix, string $val)

Transforms relative time statements compatible with strtotime().

public
false|string
castRelativeToAbsoluteDatetime(string $prefix, string $val)

Transforms relative date and time statements compatible with strtotime().

public
false|string
castRelativeToAbsoluteDate(string $prefix, string $val)

Transforms relative date statements compatible with strtotime().

public
getDateFormat()

No description

public
setDateFormat($format)

No description

public
getTimeFormat()

No description

public
setTimeFormat($format)

No description

public
getDatetimeFormat()

No description

public
setDatetimeFormat($format)

No description

public
stepFieldShouldBeDisabled(string $name, string $type, string $negate)

Checks that field with specified in|name|label|value is disabled.

public
stepFieldShouldBeEnabled(string $field)

Checks that checkbox with specified in|name|label|value is enabled.

public
iFollowInTheRegion(string $link, string $region)

Clicks a link in a specific region (an element identified by a CSS selector, a "data-title" attribute, or a named region mapped to a CSS selector via Behat configuration).

public
iFillinTheRegion(string $field, string $value, string $region)

Fills in a field in a specfic region similar to (iFollowInTheRegion or iSeeTextInRegion)

public
iSeeTextInRegion(string $negate, string $text, string $region)

Asserts text in a specific region (an element identified by a CSS selector, a "data-title" attribute, or a named region mapped to a CSS selector via Behat configuration).

public
iSelectTheRadioButton(string $radioLabel)

Selects the specified radio button

public
theTableShouldContain(string $selector, string $text)

No description

public
theTableShouldNotContain(string $selector, string $text)

No description

public
iClickOnInTheTable(string $text, string $selector)

No description

protected
NodeElement
getTable(string $selector)

Finds the first visible table by various factors:

  • table[id]
  • table[title]
  • table *[class=title]
  • fieldset[data-name] table
  • table caption

public
theTextBeforeAfter(string $textBefore, string $order, string $textAfter, string $element)

Checks the order of two texts.

public
iWaitXUntilISee(int $wait, string $selector)

Wait until a certain amount of seconds till I see an element identified by a CSS selector.

public
iWaitUntilISee(string $selector)

Wait until a particular element is visible, using a CSS selector. Useful for content loaded via AJAX, or only populated after JS execution.

public
iWaitUntilISeeText(string $text)

Wait until a particular string is found on the page. Useful for content loaded via AJAX, or only populated after JS execution.

public
iScrollToBottom()

No description

public
iScrollToTop()

No description

public
iScrollToField(string $locator, string $type)

Scroll to a certain element by label.

public
iScrollToElement(string $locator)

Scroll to a certain element by CSS selector.

public
bool
spin(callable $lambda, int $wait = 60)

Continuously poll the dom until callback returns true, code copied from (@link http://docs.behat.org/cookbook/using_spin_functions.html) If not found within a given wait period, timeout and throw error

protected
logMessage(string $message)

Log a message

protected
logException(Exception $exception)

We have to catch exceptions and log somehow else otherwise behat falls over

public
iShouldSeeTheElement($not, $cssSelector = '')

Detect element with javascript, rather than having the selector converted to xpath There's already an xpath based function 'I see the "" element' iSeeTheElement() in silverstripe/cms There's also an 'I should see "" element' in MinkContext which also converts the css selector to xpath

public
iSelectFromTheField(string $value, string $locator, string $withJavascript)

Selects the option in select field with specified id|name|label|value Also accepts CSS selectors

public
theRenderedHtmlShouldContain(string $not, string $htmlFragment)

No description

public
iAddToTheTagField(string $value, string $locator)

Add tag values to the react TagField component which uses react-select

public
theFieldShouldHaveTheValue(string $locator, string $value)

No description

public
iDragTheElementToTheElement(string $locatorA, string $locatorB)

No description

public
iDragTheElementBy(string $locatorA, string $xOffset, string $yOffset)

This doesn't seem to work quite right in practice iDragTheElementToTheElement is much more reliable

public
iPressTheKeyGlobally(string $keyCombo)

Globally press the key i.e. not type into an input

public
iAttachTheFileToTheField($filename, $locator)

Use upload fields

public
iFollowWithJavascript(string $locator)

Use this to follow hyperlinks with target="_blank" Behat won't switch to the new tab Also allows use of css selectors

Details

SilverStripeContext getMainContext()

Get the main context

Return Value

SilverStripeContext

$this setMainContext(SilverStripeContext $mainContext)

No description

Parameters

SilverStripeContext $mainContext

Return Value

$this

detectMainContext(BeforeScenarioScope $scope)

Helper method to detect the main context

Parameters

BeforeScenarioScope $scope

protected ScenarioInterface getStepScenario(FeatureNode $feature, NodeInterface $step)

Get scenario from step in a feature node See https://github.com/Behat/Behat/issues/653

Parameters

FeatureNode $feature
NodeInterface $step

Return Value

ScenarioInterface

protected mixed retryThrowable(callable $callback, int $timeout = 3)

Retry until no exceptions are thrown, or until $timeout seconds are reached.

If timeout reached, re-throws the first exception.

Parameters

callable $callback
int $timeout

Return Value

mixed

Exceptions

Exception

protected bool stepHasTag(StepScope|ScenarioScope $event, string $tag)

Check if a step has a given tag

Parameters

StepScope|ScenarioScope $event
string $tag

Return Value

bool

void resetUtilitiesAfterStep(AfterScenarioScope $event)

Ensure utilty steps are reset for subsequent scenarios

Parameters

AfterScenarioScope $event

Return Value

void

iTakeAScreenshotAfterEveryStep()

Useful step for working out why a behat testing isn't working when running the browser headless Remove this step from in a feature file once the test is working correct

iDumpTheRenderedHtmlAfterEveryStep()

Utility function for debugging failing behat tests Remove this step from in a feature file once the test is working correct

takeScreenshotAfterFailedStep(AfterStepScope $event)

Take a screenshot when step fails, or take a screenshot after every step if the use has specified "I take a screenshot after every step" Works only with FacebookWebDriver.

Parameters

AfterStepScope $event

void dumpHtmlAfterStep(AfterStepScope $event)

Dump HTML when step fails.

Parameters

AfterStepScope $event

Return Value

void

dumpRenderedHtml(StepScope $event)

Dump rendered HTML to disk Useful for seeing the state of a page when writing and debugging feature files

Parameters

StepScope $event

takeScreenshot(StepScope $event)

Take a nice screenshot

Parameters

StepScope $event

void gatherContexts(BeforeScenarioScope $scope)

Get the fixture context of the current module

Parameters

BeforeScenarioScope $scope

Return Value

void

Session getSession(string $name = null)

Get Mink session from MinkContext

Parameters

string $name

Return Value

Session

appendErrorHandlerBeforeStep(AfterStepScope $event)

No description

Parameters

AfterStepScope $event

readErrorHandlerAfterStep(AfterStepScope $event)

No description

Parameters

AfterStepScope $event

handleAjaxBeforeStep(BeforeStepScope $event)

Hook into jQuery ajaxStart, ajaxSuccess and ajaxComplete events.

Prepare __ajaxStatus() functions and attach them to these handlers. Event handlers are removed after one run.

Parameters

BeforeStepScope $event

handleAjaxAfterStep(AfterStepScope $event)

Wait for the __ajaxStatus()to return anything but 'waiting'.

Don't wait longer than 5 seconds.

Don't unregister handler if we're dealing with modal windows

Parameters

AfterStepScope $event

handleAjaxTimeout()

No description

closeModalDialog(AfterScenarioScope $event)

Close modal dialog if test scenario fails on CMS page

Parameters

AfterScenarioScope $event

cleanAssetsAfterScenario(AfterScenarioScope $event)

Delete any created files and folders from assets directory

Parameters

AfterScenarioScope $event

stepPageCantBeFound()

No description

stepIWaitFor(float $secs)

No description

Parameters

float $secs

protected NodeElement|null findNamedButton(string $title)

Find visible button with the given text.

Supports data-text-alternate property.

Parameters

string $title

Return Value

NodeElement|null

iShouldSeeAButton(string $negative, string $text)

Example: I should see a "Submit" button Example: I should not see a "Delete" button

Parameters

string $negative
string $text

stepIPressTheButton(string $text)

No description

Parameters

string $text

stepIPressTheButtons(string $text)

No description

Parameters

string $text

A list of button names can be provided by seperating the entries with the | character.

stepIPressTheButtonConfirmingTheDialog(string $button)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

Example1: I press the "Remove current combo" button, confirming the dialog Example2: I follow the "Remove current combo" link, confirming the dialog

Parameters

string $button

stepIPressTheButtonDismissingTheDialog(string $button)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

Example: I follow the "Remove current combo" link, dismissing the dialog

Parameters

string $button

iClickOnTheElement(string $selector)

No description

Parameters

string $selector

iClickOnTheElementConfirmingTheDialog($selector)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

Parameters

$selector

iClickInTheElement(string $clickType, string $text, string $selector)

No description

Parameters

string $clickType
string $text
string $selector

iClickInTheElementConfirmingTheDialog(string $clickType, string $text, string $selector)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

Example: I click "Delete" in the ".actions" element, confirming the dialog

Parameters

string $clickType
string $text
string $selector

iClickInTheElementDismissingTheDialog(string $clickType, string $text, string $selector)

Needs to be in single command to avoid "unexpected alert open" errors in Selenium.

Example: I click "Delete" in the ".actions" element, dismissing the dialog

Parameters

string $clickType
string $text
string $selector

theElementAttributeShouldBe($selector, $attribute, $value)

No description

Parameters

$selector
$attribute
$value

iSeeTheDialogText(string $expected)

No description

Parameters

string $expected

iTypeIntoTheDialog(string $data)

No description

Parameters

string $data

protected WebDriverAlert getExpectedAlert()

Wait for alert to appear, and return handle

Return Value

WebDriverAlert

iConfirmTheDialog()

No description

iDismissTheDialog()

No description

protected WebDriver getWebDriverSession()

Get Selenium webdriver session.

Note: Will fail if current driver isn't FacebookWebDriver

Return Value

WebDriver

Given iAttachTheFileTo(string $field, string $path) deprecated

deprecated 4.5.0 Use iAttachTheFileToTheField() instead

No description

Parameters

string $field
string $path

Return Value

Given

iSelectFromInputGroup(string $value, string $labelText)

Select an individual input from within a group, matched by the top-most label.

Parameters

string $value
string $labelText

iPutABreakpoint()

Pauses the scenario until the user presses a key. Useful when debugging a scenario.

false|string castRelativeToAbsoluteTime(string $prefix, string $val)

Transforms relative time statements compatible with strtotime().

Example: "time of 1 hour ago" might return "22:00:00" if its currently "23:00:00". Customize through setTimeFormat().

Parameters

string $prefix
string $val

Return Value

false|string

false|string castRelativeToAbsoluteDatetime(string $prefix, string $val)

Transforms relative date and time statements compatible with strtotime().

Example: "datetime of 2 days ago" might return "2013-10-10 22:00:00" if its currently the 12th of October 2013. Customize through setDatetimeFormat().

Parameters

string $prefix
string $val

Return Value

false|string

false|string castRelativeToAbsoluteDate(string $prefix, string $val)

Transforms relative date statements compatible with strtotime().

Example: "date 2 days ago" might return "2013-10-10" if its currently the 12th of October 2013. Customize through setDateFormat().

Parameters

string $prefix
string $val

Return Value

false|string

getDateFormat()

No description

setDateFormat($format)

No description

Parameters

$format

getTimeFormat()

No description

setTimeFormat($format)

No description

Parameters

$format

getDatetimeFormat()

No description

setDatetimeFormat($format)

No description

Parameters

$format

stepFieldShouldBeDisabled(string $name, string $type, string $negate)

Checks that field with specified in|name|label|value is disabled.

Example: Then the field "Email" should be disabled Example: Then the "Email" field should be disabled

Parameters

string $name
string $type
string $negate

stepFieldShouldBeEnabled(string $field)

Checks that checkbox with specified in|name|label|value is enabled.

Example: Then the field "Email" should be enabled Example: Then the "Email" field should be enabled

Parameters

string $field

iFollowInTheRegion(string $link, string $region)

Clicks a link in a specific region (an element identified by a CSS selector, a "data-title" attribute, or a named region mapped to a CSS selector via Behat configuration).

Example: Given I follow "Select" in the "header .login-form" region Example: Given I follow "Select" in the "My Login Form" region

Parameters

string $link
string $region

Exceptions

Exception

iFillinTheRegion(string $field, string $value, string $region)

Fills in a field in a specfic region similar to (iFollowInTheRegion or iSeeTextInRegion)

Example: Given I fill in "Hello" with "World"

Parameters

string $field
string $value
string $region

Exceptions

Exception

iSeeTextInRegion(string $negate, string $text, string $region)

Asserts text in a specific region (an element identified by a CSS selector, a "data-title" attribute, or a named region mapped to a CSS selector via Behat configuration).

Supports regular expressions in text value.

Example: Given I should see "My Text" in the "header .login-form" region Example: Given I should not see "My Text" in the "My Login Form" region

Parameters

string $negate
string $text
string $region

Exceptions

Exception

iSelectTheRadioButton(string $radioLabel)

Selects the specified radio button

Parameters

string $radioLabel

theTableShouldContain(string $selector, string $text)

No description

Parameters

string $selector
string $text

theTableShouldNotContain(string $selector, string $text)

No description

Parameters

string $selector
string $text

iClickOnInTheTable(string $text, string $selector)

No description

Parameters

string $text
string $selector

protected NodeElement getTable(string $selector)

Finds the first visible table by various factors:

  • table[id]
  • table[title]
  • table *[class=title]
  • fieldset[data-name] table
  • table caption

Parameters

string $selector

Return Value

NodeElement

theTextBeforeAfter(string $textBefore, string $order, string $textAfter, string $element)

Checks the order of two texts.

Assumptions: the two texts appear in their conjunct parent element once

Parameters

string $textBefore
string $order
string $textAfter
string $element

iWaitXUntilISee(int $wait, string $selector)

Wait until a certain amount of seconds till I see an element identified by a CSS selector.

Example: Given I wait for 10 seconds until I see the ".css_element" element

Parameters

int $wait
string $selector

iWaitUntilISee(string $selector)

Wait until a particular element is visible, using a CSS selector. Useful for content loaded via AJAX, or only populated after JS execution.

Example: Given I wait until I see the "header .login-form" element

Parameters

string $selector

iWaitUntilISeeText(string $text)

Wait until a particular string is found on the page. Useful for content loaded via AJAX, or only populated after JS execution.

Example: Given I wait until I see the text "Welcome back, John!"

Parameters

string $text

iScrollToBottom()

No description

iScrollToTop()

No description

iScrollToField(string $locator, string $type)

Scroll to a certain element by label.

Requires an "id" attribute to uniquely identify the element in the document.

Example: Given I scroll to the "Submit" button Example: Given I scroll to the "My Date" field

Parameters

string $locator
string $type

iScrollToElement(string $locator)

Scroll to a certain element by CSS selector.

Requires an "id" attribute to uniquely identify the element in the document.

Example: Given I scroll to the ".css_element" element

Parameters

string $locator

bool spin(callable $lambda, int $wait = 60)

Continuously poll the dom until callback returns true, code copied from (@link http://docs.behat.org/cookbook/using_spin_functions.html) If not found within a given wait period, timeout and throw error

Parameters

callable $lambda

The function to run continuously

int $wait

Timeout in seconds

Return Value

bool

Returns true if the lambda returns successfully

Exceptions

Exception

protected logMessage(string $message)

Log a message

Parameters

string $message

protected logException(Exception $exception)

We have to catch exceptions and log somehow else otherwise behat falls over

Parameters

Exception $exception

iShouldSeeTheElement($not, $cssSelector = '')

Detect element with javascript, rather than having the selector converted to xpath There's already an xpath based function 'I see the "" element' iSeeTheElement() in silverstripe/cms There's also an 'I should see "" element' in MinkContext which also converts the css selector to xpath

Parameters

$not
$cssSelector

iSelectFromTheField(string $value, string $locator, string $withJavascript)

Selects the option in select field with specified id|name|label|value Also accepts CSS selectors

Parameters

string $value
string $locator
  • select id, name, label or element
string $withJavascript
  • use javascript if having trouble selecting an option e.g. visibility

theRenderedHtmlShouldContain(string $not, string $htmlFragment)

No description

Parameters

string $not
string $htmlFragment

iAddToTheTagField(string $value, string $locator)

Add tag values to the react TagField component which uses react-select

Parameters

string $value
string $locator

theFieldShouldHaveTheValue(string $locator, string $value)

No description

Parameters

string $locator
string $value

iDragTheElementToTheElement(string $locatorA, string $locatorB)

No description

Parameters

string $locatorA
string $locatorB

iDragTheElementBy(string $locatorA, string $xOffset, string $yOffset)

This doesn't seem to work quite right in practice iDragTheElementToTheElement is much more reliable

Parameters

string $locatorA
string $xOffset
string $yOffset

iPressTheKeyGlobally(string $keyCombo)

Globally press the key i.e. not type into an input

Parameters

string $keyCombo
  • e.g. tab / shift-tab / ctrl-c / alt-f4

iAttachTheFileToTheField($filename, $locator)

Use upload fields

Parameters

$filename
$locator

iFollowWithJavascript(string $locator)

Use this to follow hyperlinks with target="_blank" Behat won't switch to the new tab Also allows use of css selectors

Parameters

string $locator