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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} |
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function testGitHub() | |
{ | |
//get content of an url | |
$this->_webdriver->get($this->_url); | |
//check for word "GitHub" | |
$this->assertContains('GitHub', $this->_webdriver->getTitle()); | |
} |
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
At last we want to sutdown the webdriverconnection and close the window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function tearDown() | |
{ | |
$this->_webdriver->close(); | |
$this->_webdriver->quit(); | |
} |
Thats it. Both tests are green,
You can download the code here:
https://github.com/pboethig/unittest
Keine Kommentare:
Kommentar veröffentlichen