|
| 1 | +package com.mailslurp.examples; |
| 2 | + |
| 3 | +import com.mailslurp.apis.SmsControllerApi; |
| 4 | +import com.mailslurp.apis.PhoneControllerApi; |
| 5 | +import com.mailslurp.apis.WaitForControllerApi; |
| 6 | +import com.mailslurp.clients.ApiClient; |
| 7 | +import com.mailslurp.clients.ApiException; |
| 8 | +import com.mailslurp.clients.Configuration; |
| 9 | +import com.mailslurp.models.*; |
| 10 | +import org.junit.AfterClass; |
| 11 | +import org.junit.BeforeClass; |
| 12 | +import org.junit.FixMethodOrder; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runners.MethodSorters; |
| 15 | +import org.openqa.selenium.By; |
| 16 | +import org.openqa.selenium.WebDriver; |
| 17 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 18 | +import org.openqa.selenium.firefox.FirefoxOptions; |
| 19 | +import org.openqa.selenium.firefox.FirefoxProfile; |
| 20 | +import org.openqa.selenium.remote.CapabilityType; |
| 21 | +import org.slf4j.Logger; |
| 22 | +import org.slf4j.LoggerFactory; |
| 23 | + |
| 24 | +import java.io.File; |
| 25 | +import java.time.Duration; |
| 26 | +import java.time.temporal.ChronoUnit; |
| 27 | +import java.util.Random; |
| 28 | +import java.util.regex.Matcher; |
| 29 | +import java.util.regex.Pattern; |
| 30 | + |
| 31 | +import static org.junit.Assert.*; |
| 32 | + |
| 33 | +/** |
| 34 | + * Example Selenium test-suite that loads a dummy authentication website |
| 35 | + * and tests user sign-up with an SMS verification process |
| 36 | + * |
| 37 | + * See https://www.mailslurp.com/examples/ for more information. |
| 38 | + */ |
| 39 | +@FixMethodOrder(MethodSorters.NAME_ASCENDING) |
| 40 | +public class SmsUsageTest { |
| 41 | + // website useful for testing, has a real authentication flow |
| 42 | + private static final String PLAYGROUND_URL = "https://playground-sms.mailslurp.com"; |
| 43 | + |
| 44 | + // get a MailSlurp API Key free at https://app.mailslurp.com |
| 45 | + private static final String YOUR_API_KEY = System.getenv("API_KEY"); |
| 46 | + |
| 47 | + private static final String WEBDRIVER_PATH = System.getenv("PATH_TO_WEBDRIVER"); |
| 48 | + private static final String FIREFOX_PATH = System.getenv("PATH_TO_FIREFOX"); |
| 49 | + private static final Boolean UNREAD_ONLY = true; |
| 50 | + private static final Long TIMEOUT_MILLIS = 30000L; |
| 51 | + |
| 52 | + private static ApiClient mailslurpClient; |
| 53 | + private static WebDriver driver; |
| 54 | + |
| 55 | + private static final String TEST_PASSWORD = "password-" + new Random().nextLong(); |
| 56 | + |
| 57 | + private static PhoneNumberProjection phone; |
| 58 | + private static SmsDto sms; |
| 59 | + private static String confirmationCode; |
| 60 | + |
| 61 | + private Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 62 | + |
| 63 | + /** |
| 64 | + * Setup selenium webdriver and MailSlurp client (for fetching emails) |
| 65 | + */ |
| 66 | + //<gen>selenium_sms_maven_before_all |
| 67 | + @BeforeClass |
| 68 | + public static void beforeAll() { |
| 69 | + assertNotNull(YOUR_API_KEY); |
| 70 | + assertNotNull(WEBDRIVER_PATH); |
| 71 | + assertNotNull(FIREFOX_PATH); |
| 72 | + |
| 73 | + // setup mailslurp |
| 74 | + mailslurpClient = Configuration.getDefaultApiClient(); |
| 75 | + mailslurpClient.setApiKey(YOUR_API_KEY); |
| 76 | + mailslurpClient.setConnectTimeout(TIMEOUT_MILLIS.intValue()); |
| 77 | + |
| 78 | + // setup webdriver (expects geckodriver binary at WEBDRIVER_PATH) |
| 79 | + assertTrue(new File(WEBDRIVER_PATH).exists()); |
| 80 | + System.setProperty("webdriver.gecko.driver", WEBDRIVER_PATH); |
| 81 | + FirefoxProfile profile = new FirefoxProfile(); |
| 82 | + profile.setAssumeUntrustedCertificateIssuer(true); |
| 83 | + profile.setAcceptUntrustedCertificates(true); |
| 84 | + FirefoxOptions options = new FirefoxOptions(); |
| 85 | + // expects firefox binary at FIREFOX_PATH |
| 86 | + options.setBinary(FIREFOX_PATH); |
| 87 | + options.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, true); |
| 88 | + options.setProfile(profile); |
| 89 | + options.setAcceptInsecureCerts(true); |
| 90 | + driver = new FirefoxDriver(options); |
| 91 | + driver.manage().timeouts().implicitlyWait(Duration.of(TIMEOUT_MILLIS, ChronoUnit.MILLIS)); |
| 92 | + } |
| 93 | + //</gen> |
| 94 | + |
| 95 | + /** |
| 96 | + * Load the playground site in selenium |
| 97 | + */ |
| 98 | + @Test |
| 99 | + public void test1_canLoadAuthenticationPlayground() { |
| 100 | + logger.info("Load playground"); |
| 101 | + driver.get(PLAYGROUND_URL); |
| 102 | + assertEquals(driver.getTitle(), "React App"); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Start the sign-up process |
| 107 | + */ |
| 108 | + @Test |
| 109 | + public void test2_canClickSignUpButton() { |
| 110 | + logger.info("Click sign up button"); |
| 111 | + driver.findElement(By.cssSelector("[data-test=sign-in-create-account-link]")).click(); |
| 112 | + } |
| 113 | + |
| 114 | + //<gen>selenium_sms_maven_inbox |
| 115 | + /** |
| 116 | + * Get a MailSlurp phone numberand use it to start sign-up on the playground |
| 117 | + */ |
| 118 | + @Test |
| 119 | + public void test3_fetchPhoneAndSignUp() throws ApiException { |
| 120 | + // fetch one of the phone numbers we have created in app.mailslurp.com |
| 121 | + logger.info("Get a phone number from account"); |
| 122 | + PhoneControllerApi phoneControllerApi = new PhoneControllerApi(mailslurpClient); |
| 123 | + phone = phoneControllerApi.getPhoneNumbers().execute().getContent().get(0); |
| 124 | + |
| 125 | + logger.info("Assert phone exists"); |
| 126 | + assertNotNull(phone.getId()); |
| 127 | + |
| 128 | + logger.info("Fill elements"); |
| 129 | + // fill the playground app's sign-up form with the MailSlurp |
| 130 | + // email address and a random password |
| 131 | + driver.findElement(By.id("phone_line_number")).sendKeys(phone.getPhoneNumber().replace("+1", "")); |
| 132 | + driver.findElement(By.name("password")).sendKeys(TEST_PASSWORD); |
| 133 | + |
| 134 | + logger.info("Submit sign-up button"); |
| 135 | + // submit the form to trigger the playground's phone confirmation process |
| 136 | + // we will need to receive the confirmation sms and extract a code |
| 137 | + driver.findElement(By.cssSelector("[data-test=sign-up-create-account-button]")).click(); |
| 138 | + } |
| 139 | + //</gen> |
| 140 | + |
| 141 | + /** |
| 142 | + * Use MailSlurp to receive the confirmation SMS that is sent by playground |
| 143 | + */ |
| 144 | + @Test |
| 145 | + public void test4_canReceiveConfirmationSms() throws ApiException { |
| 146 | + // receive a verification email from playground using mailslurp |
| 147 | + WaitForControllerApi waitForControllerApi = new WaitForControllerApi(mailslurpClient); |
| 148 | + WaitForSingleSmsOptions waitOptions = new WaitForSingleSmsOptions().phoneNumberId(phone.getId()).timeout(TIMEOUT_MILLIS); |
| 149 | + sms = waitForControllerApi.waitForLatestSms(waitOptions).execute(); |
| 150 | + |
| 151 | + // verify the contents is present |
| 152 | + assertNotNull(sms); |
| 153 | + |
| 154 | + // if we need to send a reply like "CONFIRM" can do so |
| 155 | + // PhoneControllerApi phoneControllerApi = new PhoneControllerApi(mailslurpClient); |
| 156 | + // phoneControllerApi.sendSmsFromPhoneNumber(phone.getId(), new SmsSendOptions().to(...).body(...)).execute(); |
| 157 | + |
| 158 | + // create a regex for matching the code we expect in the sms body |
| 159 | + Pattern p = Pattern.compile(".*([0-9]{6}).*"); |
| 160 | + Matcher matcher = p.matcher(sms.getBody()); |
| 161 | + |
| 162 | + // find first occurrence and extract |
| 163 | + assertTrue(matcher.find()); |
| 164 | + confirmationCode = matcher.group(1); |
| 165 | + |
| 166 | + assertTrue(confirmationCode.length() == 6); |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * Submit the confirmation code to the playground to confirm the user |
| 171 | + */ |
| 172 | + @Test |
| 173 | + public void test5_canSubmitVerificationCodeToPlayground() throws InterruptedException { |
| 174 | + driver.findElement(By.name("code")).sendKeys(confirmationCode); |
| 175 | + driver.wait(1000L); |
| 176 | + driver.findElement(By.cssSelector("[data-test=confirm-sign-up-confirm-button]")).click(); |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Test sign-in as confirmed user |
| 181 | + */ |
| 182 | + @Test |
| 183 | + public void test6_canLoginWithConfirmedUser() { |
| 184 | + // load the main playground login page |
| 185 | + driver.get(PLAYGROUND_URL); |
| 186 | + |
| 187 | + // login with now confirmed email address |
| 188 | + driver.findElement(By.name("username")).sendKeys(phone.getPhoneNumber()); |
| 189 | + driver.findElement(By.name("password")).sendKeys(TEST_PASSWORD); |
| 190 | + driver.findElement(By.cssSelector("[data-test=sign-in-sign-in-button]")).click(); |
| 191 | + |
| 192 | + // verify that user can see authenticated content |
| 193 | + assertTrue(driver.findElement(By.tagName("h1")).getText().contains("Welcome")); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * After tests close selenium |
| 198 | + */ |
| 199 | + @AfterClass |
| 200 | + public static void afterAll() { |
| 201 | + driver.close(); |
| 202 | + } |
| 203 | + |
| 204 | +} |
0 commit comments