Skip to content

Commit

Permalink
Added UT for getDOM()
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanDeSmedt committed Jan 20, 2020
1 parent d0df783 commit 5d85a3e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ $headlessChromer = new HeadlessChrome();
$headlessChromer->setBinaryPath('C:\Program Files (x86)\Google\Chrome\Application\chrome');
$headlessChromer->setOutputDirectory(__DIR__);
$headlessChromer->setHTMLFile(__DIR__ . '\assets\HTMLFile.html');

var_dump($headlessChromer->getDOM());
```

Expand Down
54 changes: 37 additions & 17 deletions tests/HeadlessChromeTestSuite.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,48 @@
use daandesmedt\PHPHeadlessChrome\HeadlessChrome;
use PHPUnit\Framework\TestCase;

final class HeadlessChromeTestSuite extends TestCase{
final class HeadlessChromeTestSuite extends TestCase
{

protected $binaryPath = 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe';
protected $url = 'http://www.google.be';
protected $ouputDirectory = 'C:/output/headlesschrome';


public function testConstructor(){
public function testConstructor()
{
$headlessChromer = new HeadlessChrome($this->url, $this->binaryPath);
$this->assertEquals($this->url, $headlessChromer->getUrl());
$this->assertEquals($this->binaryPath, $headlessChromer->getBinaryPath());
}


public function testSetBinaryPath() {
public function testSetBinaryPath()
{
$headlessChromer = new HeadlessChrome();
$headlessChromer->setBinaryPath($this->binaryPath);
$this->assertEquals($this->binaryPath, $headlessChromer->getBinaryPath());
}


public function testSetURL() {
public function testSetURL()
{
$headlessChromer = new HeadlessChrome();
$headlessChromer->setUrl($this->url);
$this->assertEquals($this->url, $headlessChromer->getUrl());
}


public function testSetOutputDirectory() {
public function testSetOutputDirectory()
{
$headlessChromer = new HeadlessChrome();
$headlessChromer->setOutputDirectory($this->ouputDirectory);
$this->assertEquals(realpath($this->ouputDirectory), $headlessChromer->getOutputDirectory());
}


public function testSetArguments() {
public function testSetArguments()
{
$arguments = [
'--headless' => '',
'--disable-gpu' => '',
Expand All @@ -54,7 +60,8 @@ public function testSetArguments() {
}


public function testSetArgumentsEmpty() {
public function testSetArgumentsEmpty()
{
$arguments = [
'' => '',
' ' => ' ',
Expand All @@ -68,7 +75,8 @@ public function testSetArgumentsEmpty() {
}


public function testSetArgument() {
public function testSetArgument()
{
$arguments = [
'--headless' => '',
'--disable-gpu' => '',
Expand All @@ -77,47 +85,59 @@ public function testSetArgument() {
'--added-argument=' => 'value',
];
$headlessChromer = new HeadlessChrome();
$headlessChromer->setArgument('--added-argument','value');
$headlessChromer->setArgument('--added-argument', 'value');
foreach ($arguments as $argument => $value) {
$this->assertArrayHasKey($argument, $headlessChromer->getArguments());
$this->assertEquals($value, $headlessChromer->getArguments()[$argument]);
}
}


public function testWindowSize() {

public function testWindowSize()
{
$headlessChromer = new HeadlessChrome();
$headlessChromer->setWindowSize(1024,2048);
$headlessChromer->setWindowSize(1024, 2048);
$this->assertEquals('1024,2048', $headlessChromer->getArguments()['--window-size=']);
}


public function testSetHTML() {
public function testSetHTML()
{
$HTML = 'test HTML content';
$headlessChromer = new HeadlessChrome();
$headlessChromer->setHTML($HTML);
$this->assertEquals('data:text/html,' . rawurlencode($HTML), $headlessChromer->getUrl());
}


public function testSetHTMLFile() {
public function testSetHTMLFile()
{
$filePath = __DIR__ . '\..\examples\assets\HTMLFile.html';
$headlessChromer = new HeadlessChrome();
$headlessChromer->setHTMLFile($filePath);
$this->assertEquals("file://$filePath", $headlessChromer->getUrl());
}

public function testSetHTMLFileException() {
public function testSetHTMLFileException()
{
$filePath = __DIR__ . '\HTMLFile.html';
$headlessChromer = new HeadlessChrome();
$this->setExpectedException(Exception::class);
$headlessChromer->setHTMLFile($filePath);
}

public function testUseMobile() {
public function testUseMobile()
{
$headlessChromer = new HeadlessChrome();
$headlessChromer->useMobile();
$this->assertEquals('Mozilla/5.0 (Linux; U; Android 4.0.3; ko-kr; LG-L160L Build/IML74K) AppleWebkit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30', $headlessChromer->getArguments()['--user-agent=']);
}

}
public function testGetDOM()
{
$HTML = '<div>DOM</div>';
$headlessChromer = new HeadlessChrome();
$headlessChromer->setHTML($HTML);
$this->assertEquals($headlessChromer->getDOM(), $HTML);
}
}

0 comments on commit 5d85a3e

Please sign in to comment.