-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAutoAttendance.cs
97 lines (75 loc) · 4.48 KB
/
AutoAttendance.cs
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
using System.Reflection;
using ITECHAutoAttendance.Extensions;
using ITECHAutoAttendance.Models;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using SeleniumExtras.WaitHelpers;
namespace ITECHAutoAttendance;
public class AutoAttendance
{
private readonly TimeSpan _defaultTimeout = TimeSpan.FromSeconds(12);
private readonly Configuration _configuration;
public AutoAttendance(Configuration configuration)
{
_configuration = configuration;
}
public void Run()
{
var username = _configuration.Username;
var password = _configuration.Password;
const string loginUrl = "https://moodle.itech-bs14.de/login/index.php";
// Keep in mind, this varies depending on the current block.
var attendanceBlockName = _configuration.AttendanceName;
const string inscribeAttendanceKeyword = "Anwesenheit erfassen";
var classUrl = _configuration.ClassCourseLink;
const string presentAsTextLiteral = "Anwesend";
const string submitChangesHtmlId = "id_submitbutton";
const string usernameHtmlId = "username";
const string passwordHtmlId = "password";
const string loginButtonHtmlId = "loginbtn";
const string errorMessagePrefix = "Failed to find";
const string errorFixPrefix = "This may occur if";
const string findUsernameFailed = $"{errorMessagePrefix} username input field.";
const string findPasswordFailed = $"{errorMessagePrefix} password input field.";
const string findLoginButtonFailed = $"{errorMessagePrefix} login button.";
var attendanceBlockLinkFailed = $"{errorMessagePrefix} '{attendanceBlockName}' link. {errorFixPrefix} the attendance name changed.";
const string inscribeAttendanceLinkFailed = $"{errorMessagePrefix} '{inscribeAttendanceKeyword}' link. {errorFixPrefix} you have already attended today or there a no lessons currently.";
const string presentCheckboxFailed = $"{errorMessagePrefix} '{presentAsTextLiteral}' checkbox.";
const string submitChangesFailed = $"{errorMessagePrefix} button with id '{submitChangesHtmlId}'.";
using var driver = GetWebDriver();
driver.Navigate().GoToUrl(loginUrl);
var usernameInputElement = driver.PerformWithTimeout(ExpectedConditions.ElementIsVisible(By.Id(usernameHtmlId)), _defaultTimeout, findUsernameFailed);
var passwordInputElement = driver.PerformWithTimeout(ExpectedConditions.ElementIsVisible(By.Id(passwordHtmlId)), _defaultTimeout, findPasswordFailed);
usernameInputElement.SendKeys(username);
passwordInputElement.SendKeys(password);
var loginButtonElement = driver.PerformWithTimeout(ExpectedConditions.ElementToBeClickable(By.Id(loginButtonHtmlId)), _defaultTimeout, findLoginButtonFailed);
loginButtonElement.Click();
if (driver.Url == loginUrl)
{
throw new Exception("The browser was not redirect, inferring that the login was unsuccessful. Please double check your login credentials.");
}
driver.Navigate().GoToUrl(classUrl);
var attendanceBlockLinkElement = driver.PerformWithTimeout(ExpectedConditions.ElementToBeClickable(By.LinkText(attendanceBlockName)), _defaultTimeout, attendanceBlockLinkFailed);
attendanceBlockLinkElement.Click();
var inscribeAttendanceLinkElement = driver.PerformWithTimeout(ExpectedConditions.ElementToBeClickable(By.LinkText(inscribeAttendanceKeyword)), _defaultTimeout, inscribeAttendanceLinkFailed);
inscribeAttendanceLinkElement.Click();
var presentCheckboxElement = driver.PerformWithTimeout(ExpectedConditions.ElementIsVisible(By.XPath($"//span[text()='{presentAsTextLiteral}']")), _defaultTimeout, presentCheckboxFailed);
presentCheckboxElement.Click();
var submitChangesElement = driver.PerformWithTimeout(ExpectedConditions.ElementIsVisible(By.Id(submitChangesHtmlId)), _defaultTimeout, submitChangesFailed);
submitChangesElement.Click();
}
private IWebDriver GetWebDriver()
{
var options = new ChromeOptions();
if (_configuration.HideWindow)
{
options.AddArgument("--headless");
}
if (_configuration.UseRemoteDriver)
{
return new RemoteWebDriver(new Uri(_configuration.RemoteDriverUrl!), options);
}
return new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), options);
}
}