Dieses Blog durchsuchen

Freitag, 5. August 2016

Selenium 3 and phpunit with facebooks webdriver

Today, we want to extend our simple test, in wichh we have loaded a page and compared its title,  to a litte more comlex test.

If you do not have the prerequisits installed, you can follow the previous tutorial here:
http://magento2-tuts.blogspot.de/2016/08/phpunit-simple-tests-with-selenium.html





Our goal for today is to:

1) Setup and configure the webdriver, so that all tests are running in Firefox and chrome.

2) execute our test which fills a searchform and submits it to the server. Gets the searchresult and schecks the result for a specific string

3) closes the connection to the webdriver

4) quits the browser.

sounds interresting or?


Lets start with the setup:
<?php
/**
* Created by PhpStorm.
* User: root
* Date: 05.08.16
* Time: 10:58
*/
class GitHubSearchTest extends PHPUnit_Framework_TestCase
{
/**
* @var RemoteWebDriver
*/
protected $_webdriver;
protected $_url = 'https://github.com';
public function setUp()
{
$capabilities = [
\WebDriverCapabilityType::BROWSER_NAME=>'chrome',
\WebDriverCapabilityType::BROWSER_NAME=>'firefox'
];
$this->_webdriver = RemoteWebDriver::create('http://localhost:4443/wd/hub', $capabilities);
}
}
view raw Setup hosted with ❤ by GitHub

As you can see in the setUp() method, the driver gets simply configured. You can setup a list with browsers, which are used for testing by the webdriver.


Lets repeat a simple test.
This test will only load the page and search for a String "GitHub" 
public function testGitHub()
{
//get content of an url
$this->_webdriver->get($this->_url);
//check for word "GitHub"
$this->assertContains('GitHub', $this->_webdriver->getTitle());
}
view raw simple test hosted with ❤ by GitHub


Lets make an automated search.
The next test will automaticly search the searchfield and type i  a serachterm
After that the result gets parsed for the searchstring and gets sserted for it


public function testSearch()
{
$term = 'vagrant_puppet_magento';
//get url
$this->_webdriver->get($this->_url);
//find the searchfield
$search = $this->_webdriver->findElement(WebDriverBy::cssSelector('.header-search-input'));
//set focus on this field
$search->click();
//type in searchstring and submit the form
$search->sendKeys($term)->submit();
$searchUrl = $this->_url . "/search?utf8=%E2%9C%93&q=".$term;
$this->_webdriver->get($searchUrl);
$content = $this->_webdriver->findElement(WebDriverBy::tagName('body'))->getText();
$this->assertContains($term, $content);
}
view raw serach hosted with ❤ by GitHub
 
At last we want to sutdown the webdriverconnection and close the window
public function tearDown()
{
$this->_webdriver->close();
$this->_webdriver->quit();
}
view raw close hosted with ❤ by GitHub

Thats it. Both tests are green,

You can download  the code here:
https://github.com/pboethig/unittest



Keine Kommentare:

Kommentar veröffentlichen