This repository was archived by the owner on May 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathclass.BaseTest.php
More file actions
115 lines (93 loc) · 3.91 KB
/
class.BaseTest.php
File metadata and controls
115 lines (93 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
require_once 'initTests.php';
class BaseTest extends PHPUnit_Framework_TestCase {
/**
* @var \RemoteWebDriver
*/
public static $webDriver;
public static function initialize() {
if (!isset(self::$webDriver)) {
$capabilities = array(\WebDriverCapabilityType::BROWSER_NAME => 'firefox');
self::$webDriver = RemoteWebDriver::create('http://127.0.0.1:4444/wd/hub', $capabilities);
error_log("d");
}
}
public function __construct() {
self::initialize();
}
public static function closeWebDriver() {
self::$webDriver->close();
}
public static function assertElementFound($element) {
$el = self::$webDriver->findElement(WebDriverBy::cssSelector($element));
if (!count($el)) {
self::fail("Element was found");
}
}
public static function visit($url) {
self::$webDriver->get($url);
}
public static function getELementByLinkText($content) {
return self::$webDriver->findElement(WebDriverBy::linkText($content));
}
public static function login($username, $password) {
error_log("logging in as " . TEST_USERNAME_ADMIN . " " . TEST_PASSWORD_ADMIN);
self::visit(SOURCE_ROOT_PATH);
self::writeTo($username, '#inputs > input[type="text"]:nth-child(3)');
self::writeTo($password, '#password');
self::click("#submit");
}
public static function logout() {
self::visit(SOURCE_ROOT_PATH);
self::getELementByLinkText("Logout")->click();
}
public static function type($element, $text) {
BaseTest::writeTo($text, $element);
}
public static function writeTo($text, $element) {
self::click($element);
self::$webDriver->getKeyboard()->sendKeys($text);
}
public static function click($element) {
$search = self::$webDriver->findElement(WebDriverBy::cssSelector($element));
$search->click();
}
public static function getElement($element) {
return self::$webDriver->findElement(WebDriverBy::cssSelector($element));
}
public static function assertErrorMessage($text) {
$el = self::getElement("#usermessage > p");
assert($el->isDisplayed());
error_log($el->getText());
self::assertContains($text, $el->getText());
}
public static function getChallengeUrl($pageSrc) {
$element = array();
$url = array();
preg_match("/\<a.+try_me.+/", $pageSrc, $element);
preg_match("/\/[\S]+/", $element[0], $url);
$array1 = explode("/", $url[0]);
$array2 = explode("/", SOURCE_ROOT_PATH);
$diff = join(' ', array_diff($array1, $array2));
return html_entity_decode(SOURCE_ROOT_PATH . $diff);
}
public function createUser() {
BaseTest::login(TEST_USERNAME_ADMIN, TEST_PASSWORD_ADMIN);
BaseTest::visit(ADMIN_DASHBOARD_URL);
$this->getELementByLinkText("User Manager")->click();
$this->click("#content > div.main_content > div.header_bar > div.right.action_button > table > tbody > tr > td:nth-child(1) > div > a > img");
$this->writeTo($this->username, '#form > table > tbody > tr:nth-child(1) > td:nth-child(2) > input[type="text"]');
$this->writeTo('Test User1', '#form > table > tbody > tr:nth-child(2) > td:nth-child(2) > input[type="text"]');
$this->writeTo($this->password, '#password');
$this->writeTo($this->password, '#confirm_password');
$this->click('#form > table > tbody > tr:nth-child(6) > td.radio > input[type="radio"]:nth-child(1)');
$this->click('#submit');
}
public function deleteUser($username) {
BaseTest::visit(ADMIN_DASHBOARD_URL);
$this->getELementByLinkText("User Manager")->click();
$this->getELementByLinkText($username)->click();
$this->click("#deletesubmit");
}
}