Skip to content

Instantly share code, notes, and snippets.

@julienhay
Last active July 17, 2021 20:50
Show Gist options
  • Save julienhay/47f878aa9d1eb58c462f to your computer and use it in GitHub Desktop.
Save julienhay/47f878aa9d1eb58c462f to your computer and use it in GitHub Desktop.
Cheat sheet for using Selenium2TestCase v1.4 extension PHPUnit (PHPUnit_Extensions_Selenium2TestCase)

Cheat sheet for using Selenium2TestCase v1.4 extension PHPUnit

Prepare browser

  • Full screen browser
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
$this->url("http://www.google.fr");

Selectors

  • Select by name
$this->byName("comment");
  • Select By Css Selector
$this->byCssSelector('.element');
  • Select by label
$select = $this->select($this->byCssSelector('#selectWithOptgroup'));
$select->selectOptionByLabel("Second");

Element's Operation

  • Get text
$element->text()
  • Displayed
$element->displayed()
  • Title page
$this->title();
  • Get attribute
$element->attribute('class')

Operations

  • Text field send keys
$this->keys("content");
  • Wait Until (ajax)
$this->waitUntil(function () use($var) {
  if($end) {
    return true;
  }
}, 5000);
  • Count elements
$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
$this->execute(array(
    'script' => "alert('test');",
    'args'   => array()
));
  • Return value from Javascript
$script = 'return $("#element").html()';
$data = $this->execute(array(
    'script' => $script,
    'args'   => array()
));

Full Examples

  • Fill form and submit
$element = $this->byName("comment");
$element->click();
$this->keys("content");
$element->submit();

To document

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.
@apt-webdev
Copy link

apt-webdev commented Jun 7, 2018

Hi! thanks for this, it's helps a lot.
But I have a problem when I try to extent PHPUnit_Extensions_Selenium2TestCase class, what are the dependencies to extend the class?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment