Java tutorial
/* * Copyright (C) 2017 FormKiQ Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.formkiq.web; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.formkiq.core.config.LoggerMailSender; import com.formkiq.core.service.notification.MailSenderService; /** * Users Add / Edit Integration Test. * */ @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class, webEnvironment = DEFINED_PORT) @ActiveProfiles("dev") public class UserSeleniumTest extends SeleniumTestBase { /** {@link LoggerMailSender}. */ @Autowired private MailSenderService mail; @Override @Before public void before() throws Exception { super.before(); login(getDefaultEmail()); click(By.id("liusershref")); assertEquals("FormKiQ Server - Users", getTitle()); } /** * Extract URL from {@link String}. * @param s {@link String} * @return {@link String} */ private String extractUrl(final String s) { int pos = s.indexOf("register"); return s.substring(s.lastIndexOf(" ", pos), s.indexOf(" ", pos)); } /** * testLostPassword01(). * valid email */ @Test public void testLostPassword01() { // given String email = "test@formkiq.com"; logout(); // when click(By.id("lilostpasswordhref")); // then assertEquals("FormKiQ Server - Lost Password", getTitle()); // when findElementBy("input", "data-valuekey", "email").sendKeys(email); findElementBy(By.name("_eventId_next")).click(); // then verifyEmailSent(email); } /** * Logged in user 404. */ @Test public void testUser404() { // given String url = "/user/sdfjsldjfs"; // when getDriver().navigate().to(getDefaultHostAndPort() + url); // then assertEquals(getDefaultHostAndPort() + url, getCurrentUrl()); assertEquals("FormKiQ Server - Page Not Found", getTitle()); // when click(By.id("lihomehref")); // then assertEquals(getDefaultHostAndPort() + "/user/dashboard/index", getCurrentUrl()); assertEquals("FormKiQ Server - Dashboard", getTitle()); } /** * Anonymous 404. */ @Test public void test404() { // given logout(); String url = "/sdfjsldjfs"; // when getDriver().navigate().to(getDefaultHostAndPort() + url); // then assertEquals(getDefaultHostAndPort() + "/login", getCurrentUrl()); assertEquals("FormKiQ Server - Login", getTitle()); } /** * testLostPassword02(). * invalid email */ @Test public void testLostPassword02() { // given String email = "nouser@formkiq.com"; logout(); // when click(By.id("lilostpasswordhref")); // then assertEquals("FormKiQ Server - Lost Password", getTitle()); // when findElementBy("input", "data-valuekey", "email").sendKeys(email); click(By.name("_eventId_next")); // then assertEquals(getDefaultHostAndPort() + "/lostpassword?execution=s1e2", getCurrentUrl()); waitUntilPageSourceText("A Reset Password link has been sent to your email address."); LoggerMailSender l = (LoggerMailSender) this.mail.getMailSender(); assertEquals(0, l.getMessages().size()); assertEquals(0, l.getMimeMessages().size()); } /** * testUsers01(). * Add User - missing values * * @throws Exception Exception */ @Test public void testUsers01() throws Exception { // given // when findElementBy(By.id("user_add_button")).click(); getWait().until(ExpectedConditions.visibilityOfElementLocated(By.id("form-modal"))); click(By.name("_eventId_next")); // then waitUntilPageSourceText("Field required"); } /** * testUsers02(). * Add User * * @throws Exception Exception */ @Test public void testUsers02() throws Exception { // given String email = "test2@formkiq.com"; // when findElementBy(By.id("user_add_button")).click(); getWait().until(ExpectedConditions.visibilityOfElementLocated(By.id("form-modal"))); findElementBy("input", "data-valuekey", "email").sendKeys(email); // then assertEquals("ROLE_USER", getSelectedValue(findElementBy("select", "data-valuekey", "role"))); assertEquals("ACTIVE", getSelectedValue(findElementBy("select", "data-valuekey", "status"))); WebElement el = findElementBy(By.name("_eventId_next")); assertEquals("Add User", el.getText()); // when el.click(); // then getWait().until(ExpectedConditions.invisibilityOfElementLocated(By.id("form-modal"))); assertEquals(2, getTableRowCount("results")); List<WebElement> cells = findElements(By.tagName("td")); WebElement e = cells.stream().filter(s -> s.getText().contains(email)).findFirst().get(); int index = cells.indexOf(e); assertEquals("test2@formkiq.com", e.getText()); assertEquals("ROLE_USER", cells.get(index + 1).getText()); assertEquals("INVITE", cells.get(index + 2).getText()); // when click(By.id("edit_" + getDefaultEmail())); // then getWait().until(ExpectedConditions.visibilityOfElementLocated(By.id("form-modal"))); el = findElementBy(By.name("_eventId_next")); assertEquals("Update User", el.getText()); assertEquals(getDefaultEmail(), findElementBy("input", "data-valuekey", "email").getAttribute("value")); assertFalse(getDriver().findElement(getBy("input", "data-valuekey", "password")).isDisplayed()); assertFalse(getDriver().findElement(getBy("input", "data-valuekey", "confirmpassword")).isDisplayed()); assertEquals("ROLE_ADMIN", getSelectedValue(findElementBy("select", "data-valuekey", "role"))); assertEquals("ACTIVE", getSelectedValue(findElementBy("select", "data-valuekey", "status"))); LoggerMailSender l = (LoggerMailSender) this.mail.getMailSender(); assertEquals(0, l.getMessages().size()); assertEquals(1, l.getMimeMessages().size()); MimeMessage m = l.getMimeMessages().get(0); Multipart mp = (Multipart) m.getContent(); assertEquals(2, mp.getCount()); MimeBodyPart part = (MimeBodyPart) mp.getBodyPart(0); assertNull(m.getFrom()); assertEquals(email, String.join(",", Arrays.asList(m.getRecipients(Message.RecipientType.TO)).stream() .map(s -> s.toString()).collect(Collectors.toList()))); assertEquals("Welcome to FormKiQ", m.getSubject()); assertTrue(part.getContent().toString() .contains(getDefaultHostAndPort() + "/register?email=" + email + "&resettoken=")); // given logout(); String url = extractUrl(part.getContent().toString()); // when getDriver().navigate().to(url); // then assertEquals("FormKiQ Server - User Registration", getTitle()); // given // when findElementBy(By.name("password")).sendKeys(getDefaultPass()); findElementBy(By.name("confirmpassword")).sendKeys(getDefaultEmail()); click(By.name("_eventId_next")); // then waitUntilPageSourceText("Passwords do not match"); // given // when findElementBy(By.name("password")).clear(); findElementBy(By.name("confirmpassword")).clear(); findElementBy(By.name("password")).sendKeys(getDefaultPass()); findElementBy(By.name("confirmpassword")).sendKeys(getDefaultPass()); click(By.name("_eventId_next")); // then waitUntilPageSourceText("Registration is complete"); // when login(email); // then assertEquals("FormKiQ Server - Dashboard", getTitle()); } /** * testUsers03(). * duplicate email address * * @throws Exception Exception */ @Test public void testUsers03() throws Exception { // given String email = getDefaultEmail(); // when findElementBy(By.id("user_add_button")).click(); getWait().until(ExpectedConditions.visibilityOfElementLocated(By.id("form-modal"))); findElementBy("input", "data-valuekey", "email").sendKeys(email); click(By.name("_eventId_next")); // then waitUntilPageSourceText("Email is already registered"); assertFalse(getPageSource().contains("Field required")); } /** * test error page. */ @Test public void testError01() { // given // when getDriver().navigate().to(getDefaultHostAndPort() + "/error"); // then assertEquals("FormKiQ Server - Page Not Found", getTitle()); assertTrue(getPageSource().contains("Page Not Found")); } /** * verify email is sent. * @param email {@link String} */ private void verifyEmailSent(final String email) { assertEquals(getDefaultHostAndPort() + "/lostpassword?execution=s1e2", getCurrentUrl()); waitUntilPageSourceText("A Reset Password link has been sent to your email address."); LoggerMailSender l = (LoggerMailSender) this.mail.getMailSender(); assertEquals(0, l.getMessages().size()); assertEquals(1, l.getMimeMessages().size()); } }