Selenium steps - veepee-oss/gingerspec GitHub Wiki


Check these examples:

For a complete list of steps definitions related to Selenium, please check the corresponding class in the javadoc here


@Given("^I( securely)? browse to '(.+?)'$")

Redirects the browser to the url relative to the basepath (basepath + url). The base path needs to be specified in a prior step using "@Given("^My app is running in '([^:]+?)(:.+?)?'$")"

If the securely keyword is not specified, selenium will default to http protocol and port 80. For example:

Given My app is running in 'www.privalia.com:80'
Given I browse to '/login'
(will redirect to http://www.privalia.com:80/login)

Using the securely keyword, force selenium to use https and port 443

Given My app is running in 'www.privalia-secure.com:443'
Given I securely browse to '/services'
(will redirect to https://www.privalia-secure.com:443/services)

@Given("^My app is running in '([^:]+?)(:.+?)?'$")

An initialization step used to set the basepath for all future urls in the same feature. This is a mandatory first step when working with Selenium related scenarios since it bootstraps all necessary selenium services

For example, setting the base path like this:

Given My app is running in 'www.privalia.com:80'

Will make all future urls in the same feature to be relative to www.privalia.com:80

Given My app is running in 'www.privalia.com:80'
Given I browse to '/login'
(will redirect to http://www.privalia.com:80/login)

@Given("^I maximize the browser$")

Maximizes the current browser window (equivalent to using window().maximize()). It must be executed only after executing the inicialization steps and browse to a specific url

Given My app is running in 'jenkins.privalia.com'
When I browse to '/'
Then I maximize the browser

@Given("^I switch to the iframe on index '(\d+?)'$")

Changes the driver focus to the specified frame by index. For this step to work, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first. This will

1.-) Verify that the number of element(s) with id/xpath exists

2.-) Populates an internal array that will allow to select this elements by their index

For example, lets verify that at least 1 element (iFrame in this case) with id oauth-iframe exists in the page. If found, switch to that iFrame

Once the frame is selected or navigated , all subsequent calls on the WebDriver interface are made to that frame. This means that whatever operations we try to perform on pages will not work and might throw an 'element not found exception' as we have switched to the Frame

Then '1' elements exists with 'id:oauth-iframe'
Given I switch to the iframe on index '0'

(Since only one iFrame is expected in the page, it will be saved in index 0)

@Given("^I switch to iframe with '([^:]?):([^:]?)'$")

Changes the driver focus to the specified frame by its id/xpath

Once the frame is selected or navigated , all subsequent calls on the WebDriver interface are made to that frame. This means that whatever operations we try to perform on pages will not work and might throw an 'element not found exception' as we have switched to the Frame

When I switch to iframe with 'id:myiframe'
@Given("^I switch to a parent frame$")

Switches the browser focus back to the parent frame

When I switch to iframe with 'id:myiframe'
Then I wait '1' seconds
And I switch to a parent frame

@Given("^I switch to the main frame container$")

Switches the browser focus back to the main container. The main container is the one specified during the inicialization (using '@Given("^I( securely)? browse to '(.+?)'$")' step )

When I switch to iframe with 'id:myiframe'
Then I wait '1' seconds
Given I switch to the main frame container

@Given("^a new window is opened$")

No example found!

@When("^I drag '([^:]?):([^:]?)' and drop it to '([^:]?):([^:]?)'$")

A convenience method that performs click-and-hold at the location of the source element, moves to the location of the target element, then releases the mouse.

When I drag 'xpath://*[@id="source"]' and drop it to 'xpath://*[@id="destination"]'

The source is the element to emulate button down at. The destination is the target element to move to and release the mouse at. You can reference to the element using its xpath, id, class, css or name

@When("^I click on the element on index '(\d+?)'$")

Simulates a mouse click on the specified element by idex. Since the element is referenced by an index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

Example

Given My app is running in 'www.privalia.com:80'
When I browse to '/login'
When '1' elements exists with 'id:submit'
And I click on the element on index '0'

@When("^I clear the content on text input at index '(\d+?)'$")

Used to clear the context of a text input element. Since the element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

Example

Then '1' elements exists with 'id:j_username'
And I clear the content on text input at index '0'

@When("^I type '(.+?)' on the element on index '(\d+?)'$")

Fills a text data input element with the specified text. Since the element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

Then '1' elements exists with 'id:user'
When I type 'admin' on the element on index '0'
Then '1' elements exists with 'id:password'
When I type '12345678' on the element on index '0'

@When("^I send '(.+?)'( on the element on index '(\d+?)')?$")

Send explicit key (like user pressing key of key board) events on the element specified by index. Since the element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

This step will not replace the existing text content in the element (unlike the step '@When("^I type '(.+?)' on the element on index '(\d+?)'$")')

For example. simulate the press of the 'ENTER' key on the element with id textElement

When '1' elements exists with 'id:textElement'
Then I type 'privalia' on the element on index '0'
Then I send 'ENTER' on the element on index '0'

Besides enter, we can send any pressable key that aren't text

General Keys Number pad Keys Function keys Other
NULL NUMPAD0 F1 META
CANCEL NUMPAD1 F2 COMMAND
HELP NUMPAD2 F3 ZENKAKU_HANKAKU
BACK_SPACE NUMPAD3 F4
TAB NUMPAD4 F5
CLEAR NUMPAD5 F6
RETURN NUMPAD6 F7
ENTER NUMPAD7 F8
SHIFT NUMPAD8 F9
LEFT_SHIFT NUMPAD9 F10
CONTROL MULTIPLY F11
LEFT_CONTROL ADD F12
ALT SEPARATOR
LEFT_ALT SUBTRACT
PAUSE DECIMAL
ESCAPE DIVIDE
SPACE
PAGE_UP
PAGE_DOWN
END
HOME
LEFT
ARROW_LEFT
UP
ARROW_UP
RIGHT
ARROW_RIGHT
DOWN
ARROW_DOWN
INSERT
DELETE
SEMICOLON
EQUALS

Keys can also be concatenated using the "+" operator, for example:

Then I send 'SHIFT+ENTER' on the element on index '0'

@When("^I select '(.+?)' on the element on index '(\d+?)'$")

Given a SELECT element (referenced by its index), selects the option that matches the given String. Since the select element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

If the element referenced in the index is not a select element, and exception (in this case, an UnexpectedTagNameException) is thrown

This step will select all options that displays text that matches the argument. That is, when given "Bar", this would select an option like

<option value="foo">Bar</option>

Example

Given '1' elements exists with 'id:selectElement'
When I select 'Bar' on the element on index '0'

@When("^I de-select every item on the element on index '(\d+?)'$")

Given a SELECT element (referenced by its index), clear all selected entries. This is only valid when the SELECT supports multiple selections. Since the select element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

Example

Given '1' elements exists with 'id:selectElement'
When I de-select every item on the element on index '0'

@Then("^the element on index '(\d+?)' has '(.+?)' as text$")

Verifies that the given element referenced by its index contains the specified string. Since the element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

Example

When '1' elements exists with 'id:main-panel'
And the element on index '0' has 'User:' as text

@Then("^this text exists:$")

Verifies that the given string exists anywhere on the page

Example

And this text exists:
"""
<span class="myClass1">Random text -&gt; <span class="myClass2">OK
"""

@Then("^(at least )?'(\d+?)' elements? exists? with '([^:]*?):(.+?)'$")

Verifies that a certain amount of web elements exist with the given condition. This condition could be the element id, name, class, xpath or css

For example, using the element id:

Then '1' elements exists with 'id:username'

Using xpath:

Then '1' elements exists with 'xpath://*[@id="login_submit"]'

Using the CSS of the web element:

Then '2' element exists with 'css:a[class="myclass"]'
Then '1' elements exists with 'css:input[id="username_input"]'

Using the class attribute:

Then '2' elements exists with 'class:st-header__submenu-text'

The result is saved in an array that could be later used to reference the found elements by index

Then '1' elements exists with 'id:username'
And I clear the content on text input at index '0'

There might be times when you are not sure of the number of elements that are going to be present on the page or asserting the number of elements is not the main purpose of the test. In those cases, you can use the "at least" keyword at the beginning like this:

Then at least '1' element exists with 'css:a[class="myclass"]'

This will assert that the number of elements is equal to or greater than 1

@Then("^in less than '(\d+?)' seconds, checking each '(\d+?)' seconds, '(\d+?)' elements exists with '([^:]?):([^:]?)'$")

This step verifies on a regular interval, if an specific amount of web elements (referenced by id, name, class, xpath or css) is/are present on the page. If the specified number is not found in the specified maximun time, the test fails.

For example, check every 2 seconds if at least 1 element with id "side-panel" is present on the page. If after 20 seconds no element with this id is found, then the test falis

Then in less than '20' seconds, checking each '2' seconds, '1' elements exists with 'id:side-panel'

This step is useful to avoid waiting for a fixed amount of time for some elements to load on the page, or to wait for elements that are dinamically created after the page is loaded (via javascript)

@Then("^the element on index '(\d+?)' (IS|IS NOT) displayed$")

This method determines if an element is displayed or not. That is, for example, in HTML the element has a CSS "visibility" property to "hidden", or the "display" property to "none". Since the element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

For example, to check that the element by index 0 is displayed

Then the element on index '0' IS displayed

To check the oposite condition (invisible)

Then the element on index '0' IS NOT displayed

@Then("^the element on index '(\d+?)' (IS|IS NOT) enabled$")

This step determines if an element referenced by its index is enabled or not. Since the element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

For example, to check that the element by index 0 is enabled

Then the element on index '0' IS enabled

To check the oposite condition (disabled)

Then the element on index '0' IS NOT enabled

@Then("^the element on index '(\d+?)' (IS|IS NOT) selected$")

This method determines if an element is selected or not. This operation only applies to input elements such as checkboxes, options in a select and radio buttons. Since the element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

For example, to check that the element by index 0 is selected

Then the element on index '0' IS selected

To check the oposite condition (mpt selected)

Then the element on index '0' IS NOT selected

@Then("^the element on index '(\d+?)' has '(.+?)' as '(.+?)'$")

Verifies the value of an specific attribute in the web element. Since the element is referenced by its index, the step '@Then("^'(\d+?)' elements? exists? with '([^:]?):([^:]?)'$")' must be executed first.

For example, giving the following web element

<input id=mywebelement attr1='a' attr2='b' attr3='c'>foo</input>

We can verify its different attributes in the following way

When '1' elements exists with 'id:mywebelement'
Then the element on index '0' has 'attr1' as 'a'
Then the element on index '0' has 'attr2' as 'b'
Then the element on index '0' has 'attr3' as 'c'

@Then("^I take a snapshot$") Takes a screenshot of the current window and saves it under the /target directory in the project folder.

This feature will require modifications, Please check SOM-3560 for more details

Example of use

Given My app is running in 'boit.privalia.com'
When I browse to '/'
Then I take a snapshot

@Then("^we are in page '(.+?)'$")

Used to verify the URL of the current page

Example

Given My app is running in 'boit-uat20.privalia-test.com:80'
And I browse to '/login'
And we are in page '/login'

@Then("^I save selenium cookies in context$")

Saves the cookies present in the current context. In case an HTTP request (POST/GET/DELETE/PATCH, etc) is executed later in the feature, this cookies will be added to the request

Example

Then I click on the element on index '0'
When I wait '5' seconds
Then I save selenium cookies in context

@Then("^I save content of element in index '(\d+?)' in environment variable '(.+?)'$")

Saves the text (inner text) of a web element (equivalent of using getText() via selenium) in an environment variable which can later be used by following steps in the same feature. This its different from getting the element value (the value must be retrieved using step '@Then("^the element on index '(\d+?)' has '(.+?)' as '(.+?)'$")')

Example

Then '1' element exists with 'id:headerTitle'
When I save content of element in index '0' in environment variable 'title'
Then I run 'echo '${title}' | grep "Welcome"' locally with exit status '0'

@Then("^I check every '(\d+)' seconds for at least '(\d+)' seconds until '(\d+)' elements exists with '([^:]?):([^:]?)' and is '(visible|clickable|present|hidden)'$")

This step checks in regular intervals if an specific amount of web elements exists on the page and if all this elements share a particular charasteristic (are visible, clickable, present, hidden). If the condition is not true before a maximun time expires, the step throws an exeption and the feature fails. This step avoids the creation of an static wait ("And I wait 10 seconds"), and allows the feature to continue its execution as soon as the conditions are meet. It is also useful to verify the charasteristic of the elements (for example, verify that the elements are indeed visible before continue),

For example, we could use it to verify that in a login page there are 2 buttons, "Sign in" and "Sign up" and that both are clickable

Given My app is running in 'mywebpage.com:80'
And I browse to '/login'
Then I check every '1' seconds for at least '10' seconds until '2' elements exists with 'css:a[class="buttonsClass"]' and is 'clickable'

Whe can also verify if the element(s) (referenced by its id, class, name, value or xpath) are:

  • visible
  • clickable
  • present
  • hidden

@Then("^I check every '(\d+)' seconds for at least '(\d+)' seconds until an alert appears$")

This step checks in regular intervals if an alert window appear on the page. If no alert appears in the maximun specified time, the steps throws an exception and the feature fails. This step also saves a reference of the alert in memory, so it can later be accepted or dismissed

For example, a web page that contains a button to submit a form, but that requires a confirmation (via an alert message) before sending

Then I check every '1' seconds for at least '10' seconds until '1' elements exists with 'id:sendButton' and is 'clickable'
And I click on the element on index '0'
Then I check every '1' seconds for at least '10' seconds until an alert appears

@Then("^I dismiss the alert$")

Dismiss any open alert in the page. This is the equivalent of clicking "Cancel" on the alert window. For this step to work, the step '@Then("^I check every '(\d+)' seconds for at least '(\d+)' seconds until an alert appears$")' must be executed first

Example

Then I check every '1' seconds for at least '10' seconds until '1' elements exists with 'id:sendButton' and is 'clickable'
And I click on the element on index '0'
Then I check every '1' seconds for at least '10' seconds until an alert appears
Then I dismiss the alert

@Then("^I accept the alert$")

Accepts any open alert in the page. This is the equivalent of clicking "Accept" on the alert window. For this step to work, the step '@Then("^I check every '(\d+)' seconds for at least '(\d+)' seconds until an alert appears$")' must be executed first

Example

Then I check every '1' seconds for at least '10' seconds until '1' elements exists with 'id:sendButton' and is 'clickable'
And I click on the element on index '0'
Then I check every '1' seconds for at least '10' seconds until an alert appears
And I accept the alert

@When("^I change active window$")

Switches to the new openned window (or new tab) Sometimes, when clicking a link on a page, a new window is openned and the selenium driver does not automatically swithes to this new window. This step iterates over all openned windows until it finds one that is not the current one

Given My app is running in 'mywebpage.com:80'
And I browse to '/login'
Then I check every '1' seconds for at least '10' seconds until '2' elements exists with 'css:a[class="buttonsClass"]' and is 'clickable'
And I click on the element on index '0'
Then I change active window

@Then("^I assign the file in '(.+?)' to the element on index '(\d+)'$")

Assigns the given file (relative to schemas/) to the web elements in the given index. This step is suitable for file selectors/file pickers (an input type=file), where the user must specify a file in the local computer as an input in a form

For this example, assume you have a file picker like the following

With id=my-file-picker, and that you want to asign the file myFile.txt in your schemas/ folder. You will have to proceed in the following way:

Given My app is running in 'demoqa.com'
When I browse to '/registration'
When '1' elements exists with 'id:my-file-picker'
Then I assign the file in 'schemas/myFile.txt' to the element on index '0'
And I wait '5' seconds
⚠️ **GitHub.com Fallback** ⚠️