SeleniumTestCase   B
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 16

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 16
dl 0
loc 80
rs 8.4614
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B setUp() 0 29 2
A assertSeleniumIsRunning() 0 4 1
A assertSeleniumIsNotRunning() 0 4 1
A getSeleniumStatus() 0 9 1
A startSelenium() 0 14 1
1
<?php
2
3
namespace BeubiQA\tests;
4
5
use BeubiQA\Application\Command;
6
use BeubiQA\Application\Lib;
7
use BeubiQA\Application\Selenium;
8
use GuzzleHttp\Client;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Tester\CommandTester;
11
use Symfony\Component\Process\ExecutableFinder;
12
use Symfony\Component\Process\Process;
13
14
class SeleniumTestCase extends \PHPUnit_Framework_TestCase
15
{
16
    /** @var SeleniumHandler */
17
    protected $handler;
18
19
    public function setUp()
20
    {
21
        parent::setUp();
22
        $seleniumStarterOptions = new Selenium\Options\SeleniumStartOptions();
23
        $process = new Process('');
24
        $exeFinder = new ExecutableFinder();
25
        $httpClient = new Client();
26
        $waiter = new Lib\ResponseWaitter($httpClient);
27
        $starter = new Selenium\SeleniumStarter($seleniumStarterOptions, $process, $waiter, $exeFinder);
28
29
        $seleniumOptions = new Selenium\Options\SeleniumStopOptions();
30
        $stopper = new Selenium\SeleniumStopper($seleniumOptions, $waiter, $httpClient);
31
32
        $seleniumOptions = new Selenium\Options\SeleniumDownloaderOptions();
33
        $downloader = new Selenium\SeleniumDownloader($seleniumOptions, $httpClient);
34
35
        $logWatcher = new Lib\LogWatcher();
36
37
        $this->handler = new Selenium\SeleniumHandler($starter, $stopper, $downloader, $logWatcher);
38
39
        $stopCmd = new Command\StopSeleniumCommand($this->handler);
40
        $stopCmdTester = new CommandTester($stopCmd);
41
        try {
42
            $stopCmdTester->execute([]);
43
        } catch (\Exception $exc) {
44
            // just to avoid failing when selenium is already stopped
45
        }
46
        $this->assertSeleniumIsNotRunning();
47
    }
48
49
    protected function assertSeleniumIsRunning()
50
    {
51
        $this->assertNotFalse($this->getSeleniumStatus());
52
    }
53
54
    protected function assertSeleniumIsNotRunning()
55
    {
56
        $this->assertFalse($this->getSeleniumStatus());
57
    }
58
59
    protected function getSeleniumStatus()
60
    {
61
        $ch = curl_init('http://localhost:4444/selenium-server/driver/?cmd=getLogMessages');
62
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
63
        $status = curl_exec($ch);
64
        curl_close($ch);
65
66
        return $status;
67
    }
68
69
    protected $seleniumJarLocation = 'bin/selenium-server-standalone.jar';
70
    protected $seleniumBasicCommand = '/usr/bin/java -jar bin/selenium-server-standalone.jar -port 4444';
71
    protected $seleniumJarDir = './bin';
72
73
    /**
74
     * @param array $extraOptions
75
     * @param array $inpuOptions
76
     *
77
     * @return string
78
     */
79
    protected function startSelenium(
80
        array $extraOptions = [],
81
        array $inpuOptions = ['-l' => 'bin/selenium-server-standalone.jar']
82
    ) {
83
        $input = array_merge($inpuOptions, $extraOptions);
84
        $output = [];
85
        $output['verbosity'] = OutputInterface::VERBOSITY_VERY_VERBOSE;
86
87
        $startCmd = new Command\StartSeleniumCommand($this->handler);
88
        $startCmdTester = new CommandTester($startCmd);
89
        $startCmdTester->execute($input, $output);
90
91
        return $startCmdTester->getDisplay();
92
    }
93
}
94