From d25030fb92476fd9eaf1748046e19e20a8a54e7e Mon Sep 17 00:00:00 2001
From: Aditya Bhansali <adityab3@illinois.edu>
Date: Fri, 6 Nov 2020 04:25:02 -0600
Subject: [PATCH] Added Selenium tests for valid and invalid patient
 preregistration

---
 iTrust/WebRoot/loginMenu.jsp                  |   2 +-
 iTrust/WebRoot/util/PreRegisterPatient.jsp    |   2 +-
 .../selenium/PreRegisterPatientTest.java      | 103 ++++++++++++++++++
 3 files changed, 105 insertions(+), 2 deletions(-)
 create mode 100644 iTrust/test/edu/ncsu/csc/itrust/selenium/PreRegisterPatientTest.java

diff --git a/iTrust/WebRoot/loginMenu.jsp b/iTrust/WebRoot/loginMenu.jsp
index 1760757..38d20c4 100644
--- a/iTrust/WebRoot/loginMenu.jsp
+++ b/iTrust/WebRoot/loginMenu.jsp
@@ -43,7 +43,7 @@ function fillLoginFields(u,p) {
 	<br /><br />
 	<a style="font-size: 80%;" href="/iTrust/util/resetPassword.jsp">Reset Password</a>
 	<a>        </a>
-	<a style="font-size: 80%;" href="/iTrust/util/PreRegisterPatient.jsp">Pre-Register</a>
+	<a style="font-size: 80%;" href="/iTrust/util/PreRegisterPatient.jsp" id="preregister_link">Pre-Register</a>
 
 	</form>
 <%
diff --git a/iTrust/WebRoot/util/PreRegisterPatient.jsp b/iTrust/WebRoot/util/PreRegisterPatient.jsp
index 5a99c5e..03ecc88 100755
--- a/iTrust/WebRoot/util/PreRegisterPatient.jsp
+++ b/iTrust/WebRoot/util/PreRegisterPatient.jsp
@@ -189,7 +189,7 @@ to recover the password.</div>
 
 <br />
 
-<input type="submit" style="font-size: 16pt; font-weight: bold;" value="Patient Pre-Register">
+<input type="submit" style="font-size: 16pt; font-weight: bold;" value="Patient Pre-Register" id="submit_preregister">
 </form>
 <br />
 </div>
diff --git a/iTrust/test/edu/ncsu/csc/itrust/selenium/PreRegisterPatientTest.java b/iTrust/test/edu/ncsu/csc/itrust/selenium/PreRegisterPatientTest.java
new file mode 100644
index 0000000..0633b19
--- /dev/null
+++ b/iTrust/test/edu/ncsu/csc/itrust/selenium/PreRegisterPatientTest.java
@@ -0,0 +1,103 @@
+package edu.ncsu.csc.itrust.selenium;
+
+import org.openqa.selenium.*;
+import java.util.concurrent.TimeUnit;
+import org.openqa.selenium.htmlunit.HtmlUnitDriver;
+
+
+
+import edu.ncsu.csc.itrust.enums.TransactionType;
+
+public class PreRegisterPatientTest extends iTrustSeleniumTest {
+	
+	protected WebDriver driver;
+	
+	@Override
+	protected void setUp() throws Exception {
+		super.setUp();
+		gen.clearAllTables();
+        gen.standardData();
+
+        driver = new HtmlUnitDriver();
+        driver.manage().timeouts().implicitlyWait(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
+    }
+
+    private void goToPreRegister() {
+        // Go to preregister page
+        driver.get(ADDRESS);
+        driver.findElement(By.id("preregister_link")).click();
+    }
+
+    public void testPreRegisterPatientButton() throws Exception {
+        goToPreRegister();
+        assertEquals("iTrust - PreRegister Patient", driver.getTitle());
+    }
+
+    public void testPreRegisterPatientSuccessRequired() throws Exception {
+        goToPreRegister();
+
+        // Fill the form
+        driver.findElement(By.xpath("//input[@name='firstName']")).sendKeys("fname");
+        driver.findElement(By.xpath("//input[@name='lastName']")).sendKeys("lname");
+        driver.findElement(By.xpath("//input[@name='email']")).sendKeys("name@email.com");
+        driver.findElement(By.xpath("//input[@name='password']")).sendKeys("Password123");
+        driver.findElement(By.xpath("//input[@name='verifyPassword']")).sendKeys("Password123");
+
+        // Submit
+        driver.findElement(By.id("submit_preregister")).click();
+        
+        assertTrue(driver.findElement(By.xpath("//body")).getText().contains("New Pre-registered Patient Information"));
+    }
+
+    public void testMissingElements() {
+        goToPreRegister();
+
+        // Check that all elements have the required attribute
+        assertTrue(
+            Boolean.parseBoolean(driver.findElement(By.xpath("//input[@name='firstName']")).getAttribute("required"))
+            && Boolean.parseBoolean(driver.findElement(By.xpath("//input[@name='lastName']")).getAttribute("required"))
+            && Boolean.parseBoolean(driver.findElement(By.xpath("//input[@name='email']")).getAttribute("required"))
+            && Boolean.parseBoolean(driver.findElement(By.xpath("//input[@name='password']")).getAttribute("required"))
+            && Boolean.parseBoolean(driver.findElement(By.xpath("//input[@name='verifyPassword']")).getAttribute("required"))
+        );
+
+        // Make sure that the form does not submit
+        driver.findElement(By.xpath("//input[@name='firstName']")).sendKeys("");
+        driver.findElement(By.xpath("//input[@name='lastName']")).sendKeys("lname");
+        driver.findElement(By.xpath("//input[@name='email']")).sendKeys("name@email.com");
+        driver.findElement(By.xpath("//input[@name='password']")).sendKeys("Password123");
+        driver.findElement(By.xpath("//input[@name='verifyPassword']")).sendKeys("Password123");
+
+        driver.findElement(By.id("submit_preregister")).click();
+
+        assertFalse(driver.findElement(By.xpath("//body")).getText().contains("New Pre-registered Patient Information"));
+    }
+
+    public void testInvalidName() {
+        goToPreRegister();
+
+        driver.findElement(By.xpath("//input[@name='firstName']")).sendKeys("123abc");
+        driver.findElement(By.xpath("//input[@name='lastName']")).sendKeys("lname");
+        driver.findElement(By.xpath("//input[@name='email']")).sendKeys("name@email.com");
+        driver.findElement(By.xpath("//input[@name='password']")).sendKeys("Password123");
+        driver.findElement(By.xpath("//input[@name='verifyPassword']")).sendKeys("Password123");
+
+        driver.findElement(By.id("submit_preregister")).click();
+
+        assertTrue(driver.findElement(By.xpath("//body")).getText().contains("This form has not been validated correctly"));
+    }
+
+    public void testInvalidEmail() {
+        goToPreRegister();
+
+        driver.findElement(By.xpath("//input[@name='firstName']")).sendKeys("fname");
+        driver.findElement(By.xpath("//input[@name='lastName']")).sendKeys("lname");
+        driver.findElement(By.xpath("//input[@name='email']")).sendKeys("not-an-email");
+        driver.findElement(By.xpath("//input[@name='password']")).sendKeys("Password123");
+        driver.findElement(By.xpath("//input[@name='verifyPassword']")).sendKeys("Password123");
+
+        driver.findElement(By.id("submit_preregister")).click();
+
+        assertTrue(driver.findElement(By.xpath("//body")).getText().contains("This form has not been validated correctly"));
+    }
+}
\ No newline at end of file
-- 
GitLab