Skip to content

Latest commit

 

History

History
744 lines (407 loc) · 12.3 KB

File metadata and controls

744 lines (407 loc) · 12.3 KB
layout doc
title Codeception - Documentation

Selenium Module

For additional reference, please review the source

Uses Mink to launch and manipulate Selenium Server (formerly the Selenium RC Server).

But we recommend you to use Selenium 2 WebDriver as it is an evolution of SeleniumRC and grants you more stable results. For manipulation with Selenium WebDriver use Selenium2 module

Note, all method takes CSS selectors to fetch elements. For links, buttons, fields you can use names/values/ids of elements. For form fields you can use name of matched label.

Will save a screenshot of browser window to log directory on fail.

Installation

Download Selenium RC Server

Execute it: java -jar selenium-server-standalone-x.xx.xxx.jar

Best used with Firefox browser.

Don't forget to turn on Db repopulation if you are using database.

Status

Configuration

  • url required - start url for your app
  • browser required - browser that would be launched
  • host - Selenium server host (localhost by default)
  • port - Selenium server port (4444 by default)
  • delay - set delay between actions in milliseconds (1/1000 of second) if they run too fast

Example (acceptance.suite.yml)

modules:
   enabled: [Selenium]
   config:
      Selenium:
         url: 'http://localhost/'
         browser: firefox

Public Properties

  • session - contains Mink Session

Actions

amOnPage

Opens the page.

  • param $page

attachFile

Attaches file from Codeception data directory to upload field.

Example:

{% highlight php %}

attachFile('prices.xls'); ?>

{% endhighlight %}

  • param $field
  • param $filename

blur

Removes focus from link or button or any node found by CSS or XPath XPath or CSS selectors are accepted.

  • param $el

checkOption

Ticks a checkbox. For radio buttons use selectOption method.

Example:

{% highlight php %}

checkOption('#agree'); ?>

{% endhighlight %}

  • param $option

click

Perform a click on link or button. Link or button are found by their names or CSS selector. Submits a form if button is a submit type.

If link is an image it's found by alt attribute value of image. If button is image button is found by it's value If link or button can't be found by name they are searched by CSS selector.

The second parameter is a context: CSS or XPath locator to narrow the search.

Examples:

{% highlight php %}

click('Logout'); // button of form $I->click('Submit'); // CSS button $I->click('#form input[type=submit]'); // XPath $I->click('//form/*[@type=submit]') // link in context $I->click('Logout', '#nav'); ?>

{% endhighlight %}

  • param $link
  • param $context

clickWithRightButton

Clicks with right button on link or button or any node found by CSS or XPath

  • param $link

dontSee

Check if current page doesn't contain the text specified. Specify the css selector to match only specific region.

Examples:

{% highlight php %}

dontSee('Login'); // I can suppose user is already logged in $I->dontSee('Sign Up','h1'); // I can suppose it's not a signup page $I->dontSee('Sign Up','//body/h1'); // with XPath {% endhighlight %} * param $text * param null $selector #### dontSeeCheckboxIsChecked Assert if the specified checkbox is unchecked. Use css selector or xpath to match. Example: {% highlight php %} dontSeeCheckboxIsChecked('#agree'); // I suppose user didn't agree to terms $I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user didn't check the first checkbox in form. {% endhighlight %} * param $checkbox #### dontSeeCurrentUrlEquals Checks that current url is not equal to value. Unlike `dontSeeInCurrentUrl` performs a strict check. dontSeeCurrentUrlEquals('/'); ?>
  • param $uri

dontSeeCurrentUrlMatches

Checks that current url does not match a RegEx value

dontSeeCurrentUrlMatches('~$/users/(\d+)~'); ?>
  • param $uri

dontSeeElement

Checks if element does not exist (or is visible) on a page, matching it by CSS or XPath

{% highlight php %}

dontSeeElement('.error'); $I->dontSeeElement(//form/input[1]); ?>

{% endhighlight %}

  • param $selector

dontSeeInCurrentUrl

Checks that current uri does not contain a value

{% highlight php %}

dontSeeInCurrentUrl('/users/'); ?>

{% endhighlight %}

  • param $uri

dontSeeInField

Checks that an input field or textarea doesn't contain value. Field is matched either by label or CSS or Xpath Example:

{% highlight php %}

dontSeeInField('Body','Type your comment here'); $I->dontSeeInField('form textarea[name=body]','Type your comment here'); $I->dontSeeInField('form input[type=hidden]','hidden_value'); $I->dontSeeInField('#searchform input','Search'); $I->dontSeeInField('//form/*[@name=search]','Search'); ?>

{% endhighlight %}

  • param $field
  • param $value

dontSeeLink

Checks if page doesn't contain the link with text specified. Specify url to narrow the results.

Examples:

{% highlight php %}

dontSeeLink('Logout'); // I suppose user is not logged in {% endhighlight %} * param $text * param null $url #### dontSeeOptionIsSelected Checks if option is not selected in select field. {% highlight php %} dontSeeOptionIsSelected('#form input[name=payment]', 'Visa'); ?>

{% endhighlight %}

  • param $selector
  • param $optionText
  • return mixed

doubleClick

Double clicks on link or button or any node found by CSS or XPath

  • param $link

dragAndDrop

Drag first element to second XPath or CSS selectors are accepted.

  • param $el1
  • param $el2

executeInSelenium

Low-level API method. If Codeception commands are not enough, use Selenium RC methods directly

{% highlight php %}

$I->executeInSelenium(function(\Selenium\Browser $browser) { $browser->open('/'); });

{% endhighlight %}

Use Browser Selenium API Not recommended this command too be used on regular basis. If Codeception lacks important Selenium methods implement then and submit patches.

  • param callable $function

executeJs

Executes any JS code.

  • param $jsCode

fillField

Fills a text field or textarea with value.

  • param $field
  • param $value

focus

Moves focus to link or button or any node found by CSS or XPath

  • param $el

grabAttribute

not documented

grabFromCurrentUrl

Takes a parameters from current URI by RegEx. If no url provided returns full URI.

{% highlight php %}

grabFromCurrentUrl('~$/user/(\d+)/~'); $uri = $I->grabFromCurrentUrl(); ?>

{% endhighlight %}

  • param null $uri
  • internal param $url
  • return mixed

grabTextFrom

Finds and returns text contents of element. Element is searched by CSS selector, XPath or matcher by regex.

Example:

{% highlight php %}

grabTextFrom('h1'); $heading = $I->grabTextFrom('descendant-or-self::h1'); $value = $I->grabTextFrom('~

{% endhighlight %}

  • param $cssOrXPathOrRegex
  • return mixed

grabValueFrom

Finds and returns field and returns it's value. Searches by field name, then by CSS, then by XPath

Example:

{% highlight php %}

grabValueFrom('Name'); $name = $I->grabValueFrom('input[name=username]'); $name = $I->grabValueFrom('descendant-or-self::form/descendant::input[@name = 'username']'); ?>

{% endhighlight %}

  • param $field
  • return mixed

moveBack

Moves back in history

moveForward

Moves forward in history

moveMouseOver

Moves mouse over link or button or any node found by CSS or XPath

  • param $link

pressKey

Presses key on element found by css, xpath is focused A char and modifier (ctrl, alt, shift, meta) can be provided.

Example:

{% highlight php %}

pressKey('#page','u'); $I->pressKey('#page','u','ctrl'); $I->pressKey('descendant-or-self::*[@id='page']','u'); ?>

{% endhighlight %}

  • param $element
  • param $char char can be either char ('b') or char-code (98)
  • param null $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')

pressKeyDown

Presses key down on element found by CSS or XPath.

For example see 'pressKey'.

  • param $element
  • param $char char can be either char ('b') or char-code (98)
  • param null $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')

pressKeyUp

Presses key up on element found by CSS or XPath.

For example see 'pressKey'.

  • param $element
  • param $char char can be either char ('b') or char-code (98)
  • param null $modifier keyboard modifier (could be 'ctrl', 'alt', 'shift' or 'meta')

reloadPage

Reloads current page

see

Check if current page contains the text specified. Specify the css selector to match only specific region.

Examples:

{% highlight php %}

see('Logout'); // I can suppose user is logged in $I->see('Sign Up','h1'); // I can suppose it's a signup page $I->see('Sign Up','//body/h1'); // with XPath {% endhighlight %} * param $text * param null $selector #### seeCheckboxIsChecked Assert if the specified checkbox is checked. Use css selector or xpath to match. Example: {% highlight php %} seeCheckboxIsChecked('#agree'); // I suppose user agreed to terms $I->seeCheckboxIsChecked('#signup_form input[type=checkbox]'); // I suppose user agreed to terms, If there is only one checkbox in form. $I->seeCheckboxIsChecked('//form/input[@type=checkbox and * name=agree]'); {% endhighlight %} * param $checkbox #### seeCurrentUrlEquals Checks that current url is equal to value. Unlike `seeInCurrentUrl` performs a strict check. seeCurrentUrlEquals('/'); ?>
  • param $uri

seeCurrentUrlMatches

Checks that current url is matches a RegEx value

seeCurrentUrlMatches('~$/users/(\d+)~'); ?>
  • param $uri

seeElement

Checks element visibility. Fails if element exists but is invisible to user. Eiter CSS or XPath can be used.

  • param $selector

seeInCurrentUrl

Checks that current uri contains a value

{% highlight php %}

seeInCurrentUrl('home'); // to match: /users/1 $I->seeInCurrentUrl('/users/'); ?>

{% endhighlight %}

  • param $uri

seeInField

Checks that an input field or textarea contains value. Field is matched either by label or CSS or Xpath

Example:

{% highlight php %}

seeInField('Body','Type your comment here'); $I->seeInField('form textarea[name=body]','Type your comment here'); $I->seeInField('form input[type=hidden]','hidden_value'); $I->seeInField('#searchform input','Search'); $I->seeInField('//form/*[@name=search]','Search'); ?>

{% endhighlight %}

  • param $field
  • param $value

seeLink

Checks if there is a link with text specified. Specify url to match link with exact this url.

Examples:

{% highlight php %}

seeLink('Logout'); // matches Logout $I->seeLink('Logout','/logout'); // matches Logout {% endhighlight %} * param $text * param null $url #### seeOptionIsSelected Checks if option is selected in select field. {% highlight php %} seeOptionIsSelected('#form input[name=payment]', 'Visa'); ?>

{% endhighlight %}

  • param $selector
  • param $optionText
  • return mixed

selectOption

Selects an option in select tag or in radio button group.

Example:

{% highlight php %}

selectOption('form select[name=account]', 'Premium'); $I->selectOption('form input[name=payment]', 'Monthly'); $I->selectOption('//form/select[@name=account]', 'Monthly'); ?>

{% endhighlight %}

  • param $select
  • param $option

uncheckOption

Unticks a checkbox.

Example:

{% highlight php %}

uncheckOption('#notify'); ?>

{% endhighlight %}

  • param $option

wait

Wait for x milliseconds

  • param $milliseconds

waitForJS

Waits for x milliseconds or until JS condition turns true.

  • param $milliseconds
  • param $jsCondition