Skip to content

Commit

Permalink
updated all out put to use the output object for formating. changed o…
Browse files Browse the repository at this point in the history
…ut put format to put the timestamp in its own column, and moved it to the 2nd column of file csv output. Fixed bug with finding login error text.
  • Loading branch information
Ian Greene authored and Ian Greene committed Aug 11, 2022
1 parent 6005894 commit ef4c698
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 36 deletions.
7 changes: 2 additions & 5 deletions src/In.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ public static function run($driver)

// Click the element to login
$in->click();
if (TIMESHEET) { // update to be a config const for debug/ output/ time sheet
Output::print(
'Clocked In',
'' // just prints the date
);
if (TIMESHEET) {
Output::print('Clocked In','');
}
} catch (WebDriverException $th) {
Output::print(
Expand Down
52 changes: 34 additions & 18 deletions src/Login.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,61 +11,77 @@ class Login implements Runable
public static function run($driver)
{
if (DEBUG) {
echo '"DEBUG","Enter Username"' . "\n";
Output::print('DEBUG', 'Enter Username');
}
// Find username element by its id, write the username inside
$driver->findElement(WebDriverBy::id(CLOCK_USER_FIELD_ID)) // find username input element
->sendKeys(CLOCK_USER); // fill the input box

if (DEBUG) {
echo '"DEBUG","Enter Password"' . "\n";
Output::print('DEBUG', 'Enter Password');
}
// Find password element by its id, write the password inside
$driver->findElement(WebDriverBy::id(CLOCK_PASS_FIELD_ID)) // find password input element
->sendKeys(CLOCK_PASS); // fill the input box

if (DEBUG) {
echo '"DEBUG","Find Login Button"' . "\n";
Output::print('DEBUG', 'Find Login Button');
}
// Find the Item to click
$loginButton = $driver->findElement(
WebDriverBy::id(CLOCK_LOGIN_FIELD_ID)
);

if (DEBUG) {
echo '"DEBUG","Click Login Button"' . "\n";
Output::print('DEBUG','Click Login Button');
}
// Click the element to navigate to punch screen
$loginButton->click();

// Check for log in errors
$errorDiv = $driver->findElement(
WebDriverBy::id(CLOCK_PUNCH_ERROR_ID)
);
$errorTextLi = [];

if ($errorDiv) {
try {
if (DEBUG) {
Output::print('DEBUG', 'Checking for Errors');
}
// wait until the text element is found
$driver->wait(10, 1)->until(
$driver->wait(5, 1)->until(
WebDriverExpectedCondition::presenceOfElementLocated(
WebDriverBy::cssSelector(CLOCK_PUNCH_ERROR_TEXT)
)
);

$errorText = $driver->findElement(
// Check for log in errors
$errorTextLi = $driver->findElements(
WebDriverBy::cssSelector(CLOCK_PUNCH_ERROR_TEXT)
)->getText();

Output::print(
'ERROR',
$errorText
);
} catch (\Throwable $th) {
if (DEBUG) {
Output::print('DEBUG', 'No Errors found');
}
}


if (count($errorTextLi)) {
if (DEBUG) {
Output::print('DEBUG','Login error found');
}

$errorText = "When logging in to the time clock application there was an error,\n";
foreach ($errorTextLi as $element) {
$errorText .= $element->getText();
}

Output::print('ERROR',$errorText);

$notification_script = "notify-send -u critical -t 0 'Clock in Authorization Error' '{$errorText}'";
$notification_script = "notify-send -u critical -t 0 'Login Authorization Error:' '{$errorText}'";
$handle = popen($notification_script, 'r');
pclose($handle);

return false;
} else {
if (DEBUG) {
Output::print('DEBUG','Login succeeded');
}
// verify the login, wait 10 seconds until the user drop down is found.
$driver->wait(10, 1)->until(
WebDriverExpectedCondition::presenceOfElementLocated(
Expand Down
2 changes: 1 addition & 1 deletion src/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ class Output
{
const PUNCH_TIME_SHEET_FORMAT = 'm/d/Y h:i a';
public static function print($status, $msg){
echo '"' . $status . '","' . $msg . ' '. date(self::PUNCH_TIME_SHEET_FORMAT) .'"'. "\n";
echo '"' . $status . '","' . date(self::PUNCH_TIME_SHEET_FORMAT) . '","'. $msg .'"'. "\n";
}
}
22 changes: 12 additions & 10 deletions src/PunchTheClock.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Punch\Logout;
use Punch\Holiday;
use Punch\PaidTimeOff;
use Punch\Output;

use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Chrome\ChromeOptions;
Expand All @@ -26,7 +28,7 @@ public function __construct()
$this->validate($_SERVER['argv']);

if (DEBUG) {
echo '"DEBUG","Checking for holidays..."' . "\n";
Output::print('DEBUG', 'Checking for holidays...');
}
// Check for holidays first, no need to punch anything if we're off today!
foreach (HOLIDAYS as $label => $date) {
Expand All @@ -49,7 +51,7 @@ public function __construct()
}

if (DEBUG) {
echo '"DEBUG","Its not a holiday nor are we using Paid Time Off, we need to punch the clock"' . "\n";
Output::print('DEBUG', 'Its not a holiday nor are we using Paid Time Off, we need to punch the clock');
}

// Create an instance of ChromeOptions:
Expand Down Expand Up @@ -84,9 +86,9 @@ public function __construct()
// Wait to simulate human error, between 0 min and either the users input or a max of MAX_WAIT in min
$timeToWait = (((isset($_SERVER['argv'][2])) && $_SERVER['argv'][2] !== null && is_numeric($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : MAX_WAIT);
if (DEBUG) {
echo '"DEBUG","Is input not empty: '. !empty($_SERVER['argv'][2]) . '"' . "\n";
echo '"DEBUG","Time to wait input: '. $_SERVER['argv'][2] . '"' . "\n";
echo '"DEBUG","Time to wait: '. $timeToWait . '"' . "\n";
Output::print('DEBUG', 'Is input not empty: ' . !empty($_SERVER['argv'][2]));
Output::print('DEBUG', 'Time to wait input: ' . $_SERVER['argv'][2]);
Output::print('DEBUG', 'Is input not empty: ' . $timeToWait);
}

// multiply the time to wait by 60 to get number of seconds.
Expand All @@ -106,12 +108,12 @@ public function __construct()
protected function punch($direction)
{
if (DEBUG) {
echo '"DEBUG","Loading url: ' . CLOCK_URL . '"' . "\n";
Output::print('DEBUG', 'Loading url: ' . CLOCK_URL);
}
$this->driver->get(CLOCK_URL);

if (DEBUG) {
echo '"DEBUG","Logging in."' . "\n";
Output::print('DEBUG', 'Logging in.');
}
// Check user input & login
// common login for both scenarios
Expand All @@ -122,7 +124,7 @@ protected function punch($direction)

// Check user input & login
if (DEBUG) {
echo '"DEBUG","Clocking In or Out?: ' . $direction . '"' . "\n";
Output::print('DEBUG', 'Clocking In or Out?: ' . $direction);
}

// punch in
Expand All @@ -131,7 +133,7 @@ protected function punch($direction)
in_array($direction, self::OUT) ? Out::run($this->driver) : null;

if (DEBUG) {
echo '"DEBUG","Logging out."' . "\n";
Output::print('DEBUG', 'Logging out.');
}
// common logout for both scenarios
Logout::run($this->driver);
Expand All @@ -141,7 +143,7 @@ private function driverQuit()
{
if ($this->driver !== null) {
if (DEBUG) {
echo '"DEBUG","Closing the browser."' . "\n";
Output::print('DEBUG', 'Closing the browser.');
}
// close the browser
$this->driver->quit();
Expand Down
4 changes: 2 additions & 2 deletions src/config.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
define('CLOCK_PUNCH_VERIFY_LOGIN_ID', 'user_dropdown');

// Validation Error css elements
define('CLOCK_PUNCH_ERROR_ID', 'ValidationSummary1');
define('CLOCK_PUNCH_ERROR_TEXT', '#ValidationSummary1 ul li');
define('CLOCK_PUNCH_ERROR_ID', 'error text id');
define('CLOCK_PUNCH_ERROR_TEXT', 'error text css path');

define('MAX_WAIT', 5);
// echo text in csv format.
Expand Down

0 comments on commit ef4c698

Please sign in to comment.