206 lines
5.2 KiB
Markdown
206 lines
5.2 KiB
Markdown
Cheat sheet for using Selenium2TestCase v1.4 extension PHPUnit
|
|
=====
|
|
Prepare browser
|
|
|
|
(URL)[https://gist.githubusercontent.com/julienhay/47f878aa9d1eb58c462f/raw/d6bd976e81d990006d587b881753bc65d6ac5473/Selenium2TestCase-cheat-sheet.md]
|
|
|
|
----
|
|
- Full screen browser
|
|
```php
|
|
class ClassTest extends \PHPUnit_Extensions_Selenium2TestCase
|
|
{
|
|
public static $browsers = array(
|
|
array(
|
|
'browserName' => 'chrome',
|
|
'host' => 'hub.browserstack.com',
|
|
'port' => 80,
|
|
),
|
|
);
|
|
|
|
protected function setUp()
|
|
{
|
|
parent::setUp();
|
|
$this->setBrowserUrl("http://www.example.com");
|
|
|
|
// Full screen browser
|
|
$this->prepareSession()->currentWindow()->maximize();
|
|
}
|
|
}
|
|
```
|
|
|
|
- Navigate
|
|
```php
|
|
$this->url("http://www.google.fr");
|
|
```
|
|
|
|
Selectors
|
|
----
|
|
- Select by name
|
|
```php
|
|
$this->byName("comment");
|
|
```
|
|
- Select By Css Selector
|
|
```php
|
|
$this->byCssSelector('.element');
|
|
```
|
|
- Select by label
|
|
```php
|
|
$select = $this->select($this->byCssSelector('#selectWithOptgroup'));
|
|
$select->selectOptionByLabel("Second");
|
|
```
|
|
|
|
Element's Operation
|
|
----
|
|
- Get text
|
|
```php
|
|
$element->text()
|
|
```
|
|
- Displayed
|
|
```php
|
|
$element->displayed()
|
|
```
|
|
- Title page
|
|
```php
|
|
$this->title();
|
|
````
|
|
- Get attribute
|
|
```php
|
|
$element->attribute('class')
|
|
````
|
|
|
|
Operations
|
|
----
|
|
|
|
- Text field send keys
|
|
```php
|
|
$this->keys("content");
|
|
```
|
|
|
|
- Wait Until (ajax)
|
|
```php
|
|
$this->waitUntil(function () use($var) {
|
|
if($end) {
|
|
return true;
|
|
}
|
|
}, 5000);
|
|
```
|
|
- Count elements
|
|
```php
|
|
$elements = $this->byCssSelector('body')->elements($this->using('css selector')->value('.element'));
|
|
count($elements));
|
|
// OR
|
|
$elements = $this->elements($this->using('css selector')->value('div'));
|
|
count($elements));
|
|
```
|
|
|
|
Javascript
|
|
----
|
|
- Simple execution JS
|
|
```php
|
|
$this->execute(array(
|
|
'script' => "alert('test');",
|
|
'args' => array()
|
|
));
|
|
```
|
|
- Return value from Javascript
|
|
```php
|
|
$script = 'return $("#element").html()';
|
|
$data = $this->execute(array(
|
|
'script' => $script,
|
|
'args' => array()
|
|
));
|
|
```
|
|
|
|
Full Examples
|
|
----
|
|
- Fill form and submit
|
|
```php
|
|
$element = $this->byName("comment");
|
|
$element->click();
|
|
$this->keys("content");
|
|
$element->submit();
|
|
```
|
|
|
|
|
|
To document
|
|
---
|
|
```php
|
|
|
|
void acceptAlert() Press OK on an alert, or confirms a dialog
|
|
|
|
mixed alertText() alertText($value = NULL) Gets the alert dialog text, or sets the text for a prompt dialog
|
|
|
|
void back()
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element byClassName() byClassName($value)
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element byCssSelector() byCssSelector($value)
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element byId() byId($value)
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element byLinkText() byLinkText($value)
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element byName() byName($value)
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element byTag() byTag($value)
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element byXPath() byXPath($value)
|
|
|
|
void click() click(int $button = 0) Click any mouse button (at the coordinates set by the last moveto command).
|
|
|
|
void clickOnElement() clickOnElement($id)
|
|
|
|
string currentScreenshot() BLOB of the image file
|
|
|
|
void dismissAlert() Press Cancel on an alert, or does not confirm a dialog
|
|
|
|
void doubleclick() Double clicks (at the coordinates set by the last moveto command).
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element element() element(\PHPUnit_Extensions_Selenium2TestCase_ElementCriteria $criteria) Retrieves an element
|
|
|
|
array elements() elements(\PHPUnit_Extensions_Selenium2TestCase_ElementCriteria $criteria) Retrieves an array of Element instances
|
|
|
|
string execute() execute($javaScriptCode) Injects arbitrary JavaScript in the page and returns the last
|
|
|
|
string executeAsync() executeAsync($javaScriptCode) Injects arbitrary JavaScript and wait for the callback (last element of arguments) to be called
|
|
|
|
void forward()
|
|
|
|
void frame() frame(mixed $element) Changes the focus to a frame in the page (by frameCount of type int, htmlId of type string, htmlName of type string or element of type \PHPUnit_Extensions_Selenium2TestCase_Element)
|
|
|
|
void moveto() moveto(\PHPUnit_Extensions_Selenium2TestCase_Element $element) Move the mouse by an offset of the specificed element.
|
|
|
|
void refresh()
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element_Select select() select($element)
|
|
|
|
string source() Returns the HTML source of the page
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Session_Timeouts timeouts()
|
|
|
|
string title()
|
|
|
|
void|string url() url($url = NULL)
|
|
|
|
PHPUnit_Extensions_Selenium2TestCase_ElementCriteria using() using($strategy) Factory Method for Criteria objects
|
|
|
|
void window() window($name) Changes the focus to another window
|
|
|
|
string windowHandle() Retrieves the current window handle
|
|
|
|
string windowHandles() Retrieves a list of all available window handles
|
|
|
|
string keys() Send a sequence of key strokes to the active element.
|
|
|
|
string file($file_path) Upload a local file. Returns the fully qualified path to the transferred file.
|
|
|
|
array log(string $type) Get the log for a given log type. Log buffer is reset after each request.
|
|
|
|
array logTypes() Get available log types.
|
|
|
|
void closeWindow() Close the current window.
|
|
|
|
void close() Close the current window and clear session data.
|
|
|
|
\PHPUnit_Extensions_Selenium2TestCase_Element active() Get the element on the page that currently has focus.
|
|
``` |