Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 22.35 KB | None | 0 0
  1. Feature: Patient Exercise Diary Functionality
  2. I want to be able to add, edit, and view exercise diaries
  3. So I can share and view my health data
  4.  
  5. Scenario Outline: Patient has no exercise diary entries (patientViewExerciseOnStartup)
  6. Given There is a patient in the system, and there are no exercise diary entries
  7. Then I log on as a patient for exercise diaries.
  8. When I navigate to the view exercise diary entries page.
  9. Then <text> shows.
  10. Examples:
  11. | text |
  12. | There are no Exercise Diary entries. |
  13.  
  14. Scenario Outline: The Add exercise diary entry as a patient displays correctly on startup (patientAddOnStartup)
  15. Given There is a patient in the system.
  16. Then I log on as a patient for exercise diaries.
  17. When I navigate to the Add Exercise Diary Entry page.
  18. Then the exercise diary entry is blank
  19.  
  20. Scenario Outline: Add exercise diary entry as patient (TestAddExerciseValid)
  21. Given There is a patient in the system.
  22. Then I log on as a patient for exercise diaries.
  23. When I navigate to the Add Exercise Diary Entry page.
  24. And I choose to add a new exercise diary entry with <date>, <type>, <name>, <duration>, <caloriesBurned>.
  25. Then The exercise diary entry is added successfully and the patient is returned to the view exercise diary page.
  26.  
  27. Examples:
  28. | date | type | name | duration | caloriesBurned |
  29. | 02/06/2019 | Endurance | Cross-Country Ski | 60 | 500 |
  30.  
  31.  
  32. Scenario Outline: Invalid add exercise diary entry as patient (TestAddExerciseInvalid)
  33. Given There is a patient in the system.
  34. Then I log on as a patient for exercise diaries.
  35. When I navigate to the Add Exercise Diary Entry page.
  36. And I choose to incorrectly add a new exercise diary entry with <date>, <type>, <name>, <duration>, <caloriesBurned>.
  37. Then "Please input a valid date." shows
  38. And The exercise diary entry is not added.
  39.  
  40. Examples:
  41. | date | type | name | duration | caloriesBurned |
  42. | 02/06/2020 | Strength | Building Strong Character | 60 | 400 |
  43.  
  44. Scenario Outline: View exercise diary entry as a Patient (patientViewExercise)
  45. Given There is a patient in the system.
  46. And The patient has added an entry <date>, <type>, <name>, <duration>, <caloriesBurned>.
  47. Then The patient has navigated to view exercise diary page.
  48. And The patient chooses <date>.
  49. Then The patient can view the entry <date>, <type>, <name>, <duration>, <caloriesBurned>.
  50. And This patient's action is logged on the iTrust2 homepage.
  51.  
  52. Examples:
  53. | date | type | name | duration | caloriesBurned |
  54. | 02/06/2018 | Endurance | Cross-Country Ski | 60 | 500 |
  55.  
  56. Scenario Outline: View exercise diary entry as an HCP (hcpView)
  57. Given There is an patient in the system.
  58. And There is an HCP in the system.
  59. And The patient has added an entry <date>, <type>, <name>, <duration>, <caloriesBurned>.
  60. Then The HCP logs in and navigates to the view patient exercise diary entry page.
  61. And The HCP chooses the entries for the patient on <date>.
  62. Then The HCP can view the entry <date>, <type>, <name>, <duration>, <caloriesBurned>.
  63. And The HCP's action is logged on the iTrust2 homepage.
  64.  
  65. Examples:
  66. | date | type | name | duration | caloriesBurned |
  67. | 02/06/2018 | Endurance | Cross-Country Ski | 60 | 500 |
  68.  
  69.  
  70. Scenario Outline: Edit exercise diary entry successfully (TestEditExerciseValid)
  71. Given There is an patient in the system.
  72. And The patient has added an entry <date>, <type>, <name>, <duration>, <caloriesBurned>.
  73. Then I log on as a patient for exercise diaries.
  74. When I navigate to the Edit Exercise Diary Entry page.
  75. And The patient chooses the name <name>.
  76. Then I successfully edit that exercise to have name: <newName>.
  77. And The recipe is edited.
  78.  
  79.  
  80. Scenario Outline: Edit exercise diary entry unsuccessfully (TestEditExerciseInvalid1, TestEditExerciseInvalid2)
  81. Given There is an patient in the system.
  82. And The patient has added an entry <date>, <type>, <name>, <duration>, <caloriesBurned>.
  83. Then I log on as a patient for exercise diaries.
  84. When I navigate to the Edit Exercise Diary Entry page.
  85. And The patient chooses <name>.
  86. Then I edit the duration to be: <negativeNumber>
  87. And <text> shows.
  88. When I edit the date to be <invalidDate>
  89. Then <text> shows
  90.  
  91.  
  92.  
  93. Examples:
  94. | 06/07/2019 | Endurance | Enduring CSC326 | -60 | 400 |
  95. | mm/dd/yyyy | Flexibility | Yoga | 1 | 400 |
  96.  
  97.  
  98. package edu.ncsu.csc.itrust2.cucumber;
  99.  
  100. import static org.junit.Assert.assertEquals;
  101. import static org.junit.Assert.assertTrue;
  102. import static org.junit.Assert.fail;
  103.  
  104. import java.util.List;
  105.  
  106. import org.openqa.selenium.By;
  107. import org.openqa.selenium.JavascriptExecutor;
  108. import org.openqa.selenium.WebElement;
  109. import org.openqa.selenium.support.ui.ExpectedConditions;
  110. import org.openqa.selenium.support.ui.Select;
  111. import org.openqa.selenium.support.ui.WebDriverWait;
  112.  
  113. import cucumber.api.java.en.And;
  114. import cucumber.api.java.en.Given;
  115. import cucumber.api.java.en.Then;
  116. import cucumber.api.java.en.When;
  117. import edu.ncsu.csc.itrust2.models.enums.Role;
  118. import edu.ncsu.csc.itrust2.models.persistent.Patient;
  119. import edu.ncsu.csc.itrust2.models.persistent.User;
  120.  
  121. /**
  122. * Step definitions for FoodDiaryEntry feature.
  123. *
  124. * Citation: Most functions follow FoodDiaryEntryStepDefs very closely, 2/16/19.
  125. *
  126. * @author Frances Henshall (fahensha)
  127. * @author Daniel Izatt (djizatt)
  128. */
  129. public class ExerciseDiaryEntryStepDefs extends CucumberTest {
  130.  
  131. private final String baseUrl = "http://localhost:8080/iTrust2";
  132. private final String patientString = "janice";
  133. private final String hcpString = "janiceDoctor";
  134.  
  135. /**
  136. * Asserts that the text is on the page. Function copied from
  137. * FoodDiaryEntryStepDefs on 2/16/19.
  138. *
  139. * @param text
  140. * text to check
  141. */
  142. public void assertTextPresent ( final String text ) {
  143. try {
  144. assertTrue( driver.getPageSource().contains( text ) );
  145. }
  146. catch ( final Exception e ) {
  147. fail();
  148. }
  149. }
  150.  
  151. private void clickAndCheckDateButton ( final String viewDate ) {
  152. final List<WebElement> radioList = driver.findElements( By.name( "date" ) );
  153.  
  154. for ( final WebElement element : radioList ) {
  155. if ( element.getAttribute( "value" ).equals( viewDate ) ) {
  156. element.click();
  157. assertTextPresent( "Exercise Diary Entries for: " + viewDate );
  158. return;
  159. }
  160. }
  161.  
  162. fail( "The date isn't in the radio list." );
  163. }
  164.  
  165. private void clickAndCheckNameButton ( final String viewName ) {
  166. final List<WebElement> radioList = driver.findElements( By.name( "name" ) );
  167.  
  168. for ( final WebElement element : radioList ) {
  169. if ( element.getAttribute( "value" ).equals( viewName ) ) {
  170. element.click();
  171. assertTextPresent( "Edit" );
  172. return;
  173. }
  174. }
  175.  
  176. fail( "The date isn't in the radio list." );
  177. }
  178.  
  179. /**
  180. * Attempts to log out any User who is logged in, then creates a new patient
  181. * User. The patient User is also created as a new Patient object.
  182. */
  183. @Given ( "^There is a patient in the system.$" )
  184. public void patientExistsDiaries () {
  185. attemptLogout();
  186.  
  187. // Create the test User
  188. final User user = new User( patientString, "$2a$10$EblZqNptyYvcLm/VwDCVAuBjzZOI7khzdyGPBr08PpIi0na624b8.",
  189. Role.ROLE_PATIENT, 1 );
  190. user.save();
  191.  
  192. // The User must also be created as a Patient
  193. // to show up in the list of Patients
  194. final Patient patient = new Patient( user.getUsername() );
  195. patient.save();
  196.  
  197. // All tests can safely assume the existence of the 'hcp', 'admin', and
  198. // 'patient' users
  199. }
  200.  
  201. /**
  202. * Attempts to log out any User who is logged in, then creates a new hcp
  203. * User.
  204. */
  205. @Given ( "^There is an HCP in the system.$" )
  206. public void hcpExistsDiaries () {
  207. attemptLogout();
  208.  
  209. final User hcp = new User( hcpString, "$2a$10$EblZqNptyYvcLm/VwDCVAuBjzZOI7khzdyGPBr08PpIi0na624b8.",
  210. Role.ROLE_HCP, 1 );
  211. hcp.save();
  212.  
  213. // All tests can safely assume the existence of the 'hcp', 'admin', and
  214. // 'patient' users
  215. }
  216.  
  217. /**
  218. * Attempts to log out any User who is logged in, then logs in as a patient.
  219. */
  220. @Then ( "^I log on as a patient for exercise diaries.$" )
  221. public void loginPatientDiaries () {
  222. attemptLogout();
  223.  
  224. driver.get( baseUrl );
  225. final WebElement username = driver.findElement( By.name( "username" ) );
  226. username.clear();
  227. username.sendKeys( patientString );
  228. final WebElement password = driver.findElement( By.name( "password" ) );
  229. password.clear();
  230. password.sendKeys( "123456" );
  231. final WebElement submit = driver.findElement( By.className( "btn" ) );
  232. submit.click();
  233.  
  234. assertEquals( "iTrust2: Patient Home", driver.getTitle() );
  235. }
  236.  
  237. /**
  238. * Navigates to the view Exercise Diary Entry page by type casting the
  239. * WebDriver to a JavascriptExecutor. Asserts that the title of the page is
  240. * correct.
  241. */
  242. @When ( "^I navigate to the view exercise diary entries page.$" )
  243. public void navigateToView () {
  244. ( (JavascriptExecutor) driver ).executeScript( "document.getElementById('viewExerciseDiaryEntries').click();" );
  245.  
  246. assertEquals( "iTrust2: View Exercise Diary Entries", driver.getTitle() );
  247. }
  248.  
  249. /**
  250. * Asserts that the correct text is displayed for the
  251. * viewExerciseDiaryEntries page when there are no entries.
  252. *
  253. * @param text
  254. * the text to display
  255. */
  256. @Then ( "^(.+) shows.$" )
  257. public void noDiaryEntries ( final String text ) {
  258. waitForAngular();
  259. assertTextPresent( text );
  260. }
  261.  
  262. /**
  263. * Navigates to the add Exercise Diary Entry page by type casting the
  264. * WebDriver to a JavascriptExecutor. Asserts that the title of the page is
  265. * correct.
  266. */
  267. @When ( "^I navigate to the Add Exercise Diary Entry page.$" )
  268. public void requestAddDiaryPage () {
  269. ( (JavascriptExecutor) driver ).executeScript( "document.getElementById('addExerciseDiaryEntry').click();" );
  270. final WebDriverWait wait = new WebDriverWait( driver, 20 );
  271. wait.until( ExpectedConditions.titleContains( "Add Exercise Diary Entry" ) );
  272. assertEquals( "iTrust2: Add Exercise Diary Entry", driver.getTitle() );
  273. }
  274.  
  275. /**
  276. * Adds a new exercise diary entry with valid input.
  277. *
  278. * @param date
  279. * the date of the exercise diary entry
  280. * @param type
  281. * the date of the exercise diary entry
  282. * @param name
  283. * the name of the exercise diary entry
  284. * @param duration
  285. * the duration of the exercise diary entry
  286. * @param caloriesBurned
  287. * the calories burned for the exercise diary entry
  288. */
  289. @And ( "^I choose to add a new exercise diary entry with (.+), (.+), (.+), (\\d+), (\\d+)\\.$" )
  290. public void addEntry ( final String date, final String type, final String name, final int duration,
  291. final int caloriesBurned ) {
  292. waitForAngular();
  293.  
  294. final WebElement dateElement = driver.findElement( By.name( "date" ) );
  295. dateElement.sendKeys( date.replace( "/", "" ) );
  296.  
  297. final Select dropdown = new Select( driver.findElement( By.name( "exerciseType" ) ) );
  298. dropdown.selectByVisibleText( type );
  299.  
  300. driver.findElement( By.name( "name" ) ).clear();
  301. driver.findElement( By.name( "name" ) ).sendKeys( name );
  302. driver.findElement( By.name( "duration" ) ).clear();
  303. driver.findElement( By.name( "duration" ) ).sendKeys( Integer.toString( duration ) );
  304. driver.findElement( By.name( "caloriesBurned" ) ).clear();
  305. driver.findElement( By.name( "caloriesBurned" ) ).sendKeys( Integer.toString( caloriesBurned ) );
  306.  
  307. driver.findElement( By.name( "submit" ) ).click();
  308.  
  309. }
  310.  
  311. /**
  312. * Waits 20 seconds, and then waits again until the user is returned to the
  313. * view page. Asserts that the title of the page is correct.
  314. */
  315. @Then ( "^The diary entry is added successfully and the patient is returned to the view exercise diary page.$" )
  316. public void checkForSuccess () {
  317. final WebDriverWait wait = new WebDriverWait( driver, 20 );
  318. wait.until( ExpectedConditions.titleContains( "View Exercise Diary Entries" ) );
  319. assertEquals( "iTrust2: View Exercise Diary Entries", driver.getTitle() );
  320. }
  321.  
  322. /**
  323. * Attempts to add a new exercise diary entries with invalid input.
  324. *
  325. * @param date
  326. * the date of the exercise diary entry
  327. * @param type
  328. * the date of the exercise diary entry
  329. * @param name
  330. * the name of the exercise diary entry
  331. * @param duration
  332. * the duration of the exercise diary entry
  333. * @param caloriesBurned
  334. * the calories burned for the exercise diary entry
  335. */
  336. @And ( "^I choose to incorrectly add a new diary entry with (.+), (.+), (.+), (.+), (.+)\\.$" )
  337. public void incorrectlyAdd ( final String date, final String type, final String name, final String duration,
  338. final String caloriesBurned ) {
  339. waitForAngular();
  340.  
  341. final WebElement dateElement = driver.findElement( By.name( "date" ) );
  342. dateElement.sendKeys( date.replace( "/", "" ) );
  343.  
  344. final Select dropdown = new Select( driver.findElement( By.name( "mealType" ) ) );
  345. dropdown.selectByVisibleText( type );
  346. driver.findElement( By.name( "name" ) ).clear();
  347. driver.findElement( By.name( "name" ) ).sendKeys( name );
  348. driver.findElement( By.name( "duration" ) ).clear();
  349. driver.findElement( By.name( "duration" ) ).sendKeys( duration );
  350. driver.findElement( By.name( "caloriesBurned" ) ).clear();
  351. driver.findElement( By.name( "caloriesBurned" ) ).sendKeys( caloriesBurned );
  352.  
  353. driver.findElement( By.name( "submit" ) ).click();
  354. }
  355.  
  356. /**
  357. * Checks to make sure that the right validation error message displays on
  358. * an invalid add.
  359. */
  360. @Then ( "^The exercise diary entry is not added.$" )
  361. public void checkForFailure () {
  362. assertTrue( driver.getPageSource().contains( "Could not add diary entry." ) );
  363. // assertTextPresent( "Could not add diary entry", driver );
  364. }
  365.  
  366. /**
  367. * Adds the exercise diary entry as a precondition.
  368. *
  369. * @param date
  370. * the date of the exercise diary entry
  371. * @param type
  372. * the date of the exercise diary entry
  373. * @param name
  374. * the name of the exercise diary entry
  375. * @param duration
  376. * the duration of the exercise diary entry
  377. * @param caloriesBurned
  378. * the calories burned for the exercise diary entry
  379. */
  380. @And ( "^The patient has added an entry (.+), (.+), (.+), (\\d+), (\\d+)\\.$" )
  381. public void addDiaryEntry ( final String date, final String type, final String name, final int duration,
  382. final int caloriesBurned ) {
  383.  
  384. // logs in as the patient
  385. loginPatientDiaries();
  386.  
  387. // navigates to the add exercise diary page
  388. requestAddDiaryPage();
  389.  
  390. // adds a new entry with valid input
  391. addEntry( date, type, name, duration, caloriesBurned );
  392. }
  393.  
  394. /**
  395. * Waits until the user is returned to the view exercise diary page, and
  396. * asserts that the title of the page is correct.
  397. */
  398. @Then ( "^The patient has navigated to view exercise diary page.$" )
  399. public void onViewPagePatient () {
  400. final WebDriverWait wait = new WebDriverWait( driver, 20 );
  401. wait.until( ExpectedConditions.titleContains( "View Exercise Diary Entries" ) );
  402.  
  403. assertEquals( "iTrust2: View Exercise Diary Entries", driver.getTitle() );
  404. }
  405.  
  406. /**
  407. * Selects the radio button with the date viewDate using a private helper
  408. * method.
  409. *
  410. * @param viewDate
  411. * the date on the button to view
  412. */
  413. @And ( "^The patient chooses (.+)." )
  414. public void selectDate ( final String viewDate ) {
  415. clickAndCheckDateButton( viewDate );
  416. }
  417.  
  418. /**
  419. * Views the exercise diary entry on the patient page to make sure it
  420. * matches the values of the parameters.
  421. *
  422. * @param date
  423. * the date of the exercise diary entry
  424. * @param type
  425. * the date of the exercise diary entry
  426. * @param name
  427. * the name of the exercise diary entry
  428. * @param duration
  429. * the duration of the exercise diary entry
  430. * @param caloriesBurned
  431. * the calories burned for the exercise diary entry
  432. */
  433. @Then ( "^The patient can view the entry (.+), (.+), (.+), (\\d+), (\\d+)\\.$" )
  434. public void viewEntryPatient ( final String date, final String type, final String name, final int duration,
  435. final int caloriesBurned ) {
  436. assertEquals( type, driver.findElement( By.id( "exerciseType-0" ) ).getText() );
  437. assertEquals( name, driver.findElement( By.id( "name-0" ) ).getText() );
  438. assertEquals( duration, Integer.parseInt( driver.findElement( By.id( "duration-0" ) ).getText() ) );
  439. assertEquals( caloriesBurned, Integer.parseInt( driver.findElement( By.id( "caloriesBurned-0" ) ).getText() ) );
  440. }
  441.  
  442. /**
  443. * Checks the logging functionality by asserting that the action performed
  444. * appears on the home page.
  445. */
  446. @And ( "^This patient's action is logged on the iTrust2 homepage.$" )
  447. public void checkLogsPatient () {
  448. driver.get( baseUrl );
  449.  
  450. waitForAngular();
  451. assertTextPresent( "Patient Views Exercise Diary Entry" );
  452. }
  453.  
  454. /**
  455. * Logs in as the HCP, navigates to the view Patient Exercise Diary page,
  456. * and asserts that the page title is correct.
  457. */
  458. @Then ( "^The HCP logs in navigates to the veiw patient exercise diary entry page.$" )
  459. public void onViewPageHCP () {
  460. attemptLogout();
  461.  
  462. driver.get( baseUrl );
  463. final WebElement username = driver.findElement( By.name( "username" ) );
  464. username.clear();
  465. username.sendKeys( hcpString );
  466. final WebElement password = driver.findElement( By.name( "password" ) );
  467. password.clear();
  468. password.sendKeys( "123456" );
  469. final WebElement submit = driver.findElement( By.className( "btn" ) );
  470. submit.click();
  471.  
  472. assertEquals( "iTrust2: HCP Home", driver.getTitle() );
  473.  
  474. ( (JavascriptExecutor) driver ).executeScript( "document.getElementById('HCPExerciseDiary').click();" );
  475.  
  476. assertEquals( "iTrust2: View Patient Exercise Diary", driver.getTitle() );
  477. }
  478.  
  479. /**
  480. * Clicks on the patient and then the date while on the HCP View Patient
  481. * Exercise Diary page. The date button is checked with a private helper
  482. * method.
  483. *
  484. * @param viewDate
  485. * the date to view
  486. */
  487. @And ( "^The HCP chooses the entries for the patient on (.+).$" )
  488. public void selectDateHCP ( final String viewDate ) {
  489. waitForAngular();
  490. driver.findElement( By.id( "janice" ) ).click();
  491.  
  492. waitForAngular();
  493. clickAndCheckDateButton( viewDate );
  494. }
  495.  
  496. /**
  497. * Views the exercise diary entry on the hcp page to make sure it matches
  498. * the values of the parameters.
  499. *
  500. * @param date
  501. * the date of the exercise diary entry
  502. * @param type
  503. * the date of the exercise diary entry
  504. * @param name
  505. * the name of the exercise diary entry
  506. * @param duration
  507. * the duration of the exercise diary entry
  508. * @param caloriesBurned
  509. * the calories burned for the exercise diary entry
  510. */
  511. @Then ( "^The HCP can view the entry (.+), (.+), (.+), (\\d+), (\\d+)\\.$" )
  512. public void viewEntryHCP ( final String date, final String type, final String name, final int duration,
  513. final int caloriesBurned ) throws Throwable {
  514. assertEquals( type, driver.findElement( By.id( "exerciseType-0" ) ).getText() );
  515. assertEquals( name, driver.findElement( By.id( "name-0" ) ).getText() );
  516. assertEquals( duration, Integer.parseInt( driver.findElement( By.id( "duration-0" ) ).getText() ) );
  517. assertEquals( caloriesBurned, Integer.parseInt( driver.findElement( By.id( "caloriesBurned-0" ) ).getText() ) );
  518. }
  519.  
  520. /**
  521. * Checks that the HCP viewing the Exercise Diary Entry action is logged
  522. * correctly on the homepage.
  523. */
  524. @And ( "^The HCP's action is logged on the iTrust2 homepage.$" )
  525. public void checkLoggingHCP () {
  526. driver.get( baseUrl );
  527.  
  528. waitForAngular();
  529. assertTextPresent( "HCP Views Exercise Diary Entry" );
  530. }
  531.  
  532. /**
  533. * Navigates to the edit Exercise Diary Entry page by type casting the
  534. * WebDriver to a JavascriptExecutor. Asserts that the title of the page is
  535. * correct.
  536. */
  537. @When ( "^I navigate to the edit exercise diary entries page.$" )
  538. public void navigateToEdit () {
  539. ( (JavascriptExecutor) driver ).executeScript( "document.getElementById('editExerciseDiaryEntries').click();" );
  540. assertEquals( "iTrust2: Edit Exercise Diary Entries", driver.getTitle() );
  541. }
  542.  
  543. /**
  544. * Logs in as the HCP, navigates to the view Patient Exercise Diary page,
  545. * and asserts that the page title is correct.
  546. */
  547. @Then ( "^I successfully edit that exercise to have name: <newName>.$" )
  548. public void SuccessfullyEditName () {
  549. driver.findElement( By.name( "name" ) ).clear();
  550. driver.findElement( By.name( "name" ) ).sendKeys( "Getting Big" );
  551. driver.findElement( By.cssSelector( "input[type=\"submit\"]" ) ).click();
  552.  
  553. }
  554.  
  555. /**
  556. * navigates to the view Patient Exercise Diary page, and asserts that the
  557. * page title is correct.
  558. */
  559. @And ( "^The recipe is edited.$" )
  560. public void validateRecipe ( final String date ) {
  561. clickAndCheckDateButton( date );
  562. assertTextPresent( "Getting Big" );
  563.  
  564. }
  565. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement