Skip to content

Instantly share code, notes, and snippets.

@avnidas
avnidas / Selenium4Behat.php
Last active October 18, 2017 18:30
Selenium for Behat
//Take a screenshot after the failed Step
/**
* @AfterStep
* @param AfterStepScope $event
* @throws UnsupportedDriverActionException
*/
public function takeScreenshotAfterFailedStep($event)
{
$this->getSession()->wait(2000, 'window.jQuery !== undefined');
@avnidas
avnidas / MultiLineInput
Last active August 8, 2016 04:01
Behat scenario multi line input
/**
* Sets the specified multi-line value to the field
*
* @Given /^I set the text field "(?P<field_string>(?:[^"]|\\")*)" with multi-line text:/
*/
public function i_set_the_text_field_with_multi_line_text($field, \Behat\Gherkin\Node\PyStringNode $value) {
$this->getSession()->getPage()->fillField($field, $value);
}
@avnidas
avnidas / xpathFinder
Last active August 8, 2016 04:02
Behat step definition for finding elements with xpath (requires selenium)
/**
* @When /^I click on the element with xpath "([^"]*)"$/
* @param $xpath1
*/
public function iClickOnTheElementWithXpath($xpath1)
{
$session = $this->getSession();
$element = $session->getPage()->find('xpath',$session->getSelectorsHandler()->selectorToXpath('xpath', '*//*[text()="'. $xpath1.'"]')
@avnidas
avnidas / FailedStepScreenshot
Last active August 8, 2016 04:03
Captures a screenshot for a failed step (requires selenium)
/**
* @AfterStep
* @param AfterStepScope $event
* @throws UnsupportedDriverActionException
*/
public function takeScreenshotAfterFailedStep($event)
{
$this->getSession()->wait(2000, 'window.jQuery !== undefined');
if (!$event->getTestResult()->isPassed()) {
$this->takeScreenshot("Fail");
@avnidas
avnidas / CSS_Selector
Last active August 8, 2016 04:04
Find and click an element by is CSS Selector value (requires selenium driver)
/**
* @Then /^I click on the element with CSS selector "([^"]*)"$/
*/
public function iClickOnTheElementWithCSSSelector($cssSelector)
{
$element = $this->getSession()->getPage()->find(
'xpath', $this->getSession()->getSelectorsHandler()->selectorToXpath('css', $cssSelector));
if (null === $element) {
throw new \InvalidArgumentException(sprintf('Could not evaluate CSS Selector: "%s"', $cssSelector));
}
/**
* @Given /^I provide a string with a alphanumeric value$/
*/
public function iProvideAStringWithAAlphanumericValue()
{
$length = 10;
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$charactersLength = strlen($characters);
$randomString = '';