|
| 1 | +package com.mailslurp.examples; |
| 2 | + |
| 3 | +import com.mailslurp.apis.InboxControllerApi; |
| 4 | +import com.mailslurp.apis.WaitForControllerApi; |
| 5 | +import com.mailslurp.clients.ApiClient; |
| 6 | +import com.mailslurp.clients.ApiException; |
| 7 | +import com.mailslurp.clients.Configuration; |
| 8 | +import com.mailslurp.models.Email; |
| 9 | +import com.mailslurp.models.Inbox; |
| 10 | +import org.openqa.selenium.By; |
| 11 | +import org.openqa.selenium.WebDriver; |
| 12 | +import org.openqa.selenium.firefox.FirefoxDriver; |
| 13 | +import org.testng.annotations.AfterSuite; |
| 14 | +import org.testng.annotations.BeforeSuite; |
| 15 | +import org.testng.annotations.Test; |
| 16 | +import ru.stqa.selenium.factory.WebDriverPool; |
| 17 | +import java.io.File; |
| 18 | +import java.util.Random; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.regex.Matcher; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +import static org.testng.Assert.*; |
| 24 | + |
| 25 | +public class SignUpTestNGExample { |
| 26 | + |
| 27 | + // website useful for testing, has a real authentication flow |
| 28 | + private static final String PLAYGROUND_URL = "https://playground.mailslurp.com"; |
| 29 | + |
| 30 | + // get a MailSlurp API Key free at https://app.mailslurp.com |
| 31 | + private static final String YOUR_API_KEY = System.getenv("API_KEY"); |
| 32 | + private static final String WEBDRIVER_PATH = System.getenv("PATH_TO_WEBDRIVER"); |
| 33 | + private static final String TEST_PASSWORD = "password-" + new Random().nextLong(); |
| 34 | + private static final Boolean UNREAD_ONLY = true; |
| 35 | + private static final Long TIMEOUT_MILLIS = 30000L; |
| 36 | + private static Inbox inbox; |
| 37 | + private static Email email; |
| 38 | + private static String confirmationCode; |
| 39 | + private static ApiClient mailslurpClient; |
| 40 | + private static WebDriver driver; |
| 41 | + |
| 42 | + @BeforeSuite |
| 43 | + public void initTestSuite() { |
| 44 | + assertNotNull(YOUR_API_KEY); |
| 45 | + assertNotNull(WEBDRIVER_PATH); |
| 46 | + |
| 47 | + // setup mailslurp |
| 48 | + mailslurpClient = Configuration.getDefaultApiClient(); |
| 49 | + mailslurpClient.setApiKey(YOUR_API_KEY); |
| 50 | + mailslurpClient.setConnectTimeout(TIMEOUT_MILLIS.intValue()); |
| 51 | + |
| 52 | + // setup webdriver (expects geckodriver binary at WEBDRIVER_PATH) |
| 53 | + assertTrue(new File(WEBDRIVER_PATH).exists()); |
| 54 | + System.setProperty("webdriver.gecko.driver", WEBDRIVER_PATH); |
| 55 | + driver = new FirefoxDriver(); |
| 56 | + driver.manage().timeouts().implicitlyWait(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Load the playground site in selenium |
| 61 | + */ |
| 62 | + @Test |
| 63 | + public void test1_canLoadAuthenticationPlayground() { |
| 64 | + driver.get(PLAYGROUND_URL); |
| 65 | + assertEquals(driver.getTitle(), "React App"); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Start the sign-up process |
| 70 | + */ |
| 71 | + @Test |
| 72 | + public void test2_canClickSignUpButton() { |
| 73 | + driver.findElement(By.cssSelector("[data-test=sign-in-create-account-link]")).click(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Create a real email address with MailSlurp and use it to start sign-up on the playground |
| 78 | + */ |
| 79 | + @Test |
| 80 | + public void test3_canCreateEmailAddressAndSignUp() throws ApiException { |
| 81 | + // create a real, randomized email address with MailSlurp to represent a user |
| 82 | + InboxControllerApi inboxControllerApi = new InboxControllerApi(mailslurpClient); |
| 83 | + inbox = inboxControllerApi.createInbox(null, null,null, null,null,null,null, null, null); |
| 84 | + |
| 85 | + // check the inbox was created |
| 86 | + assertNotNull(inbox.getId()); |
| 87 | + assertTrue(inbox.getEmailAddress().contains("@mailslurp.com")); |
| 88 | + |
| 89 | + // fill the playground app's sign-up form with the MailSlurp |
| 90 | + // email address and a random password |
| 91 | + driver.findElement(By.name("email")).sendKeys(inbox.getEmailAddress()); |
| 92 | + driver.findElement(By.name("password")).sendKeys(TEST_PASSWORD); |
| 93 | + |
| 94 | + // submit the form to trigger the playground's email confirmation process |
| 95 | + // we will need to receive the confirmation email and extract a code |
| 96 | + driver.findElement(By.cssSelector("[data-test=sign-up-create-account-button]")).click(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Use MailSlurp to receive the confirmation email that is sent by playground |
| 101 | + */ |
| 102 | + @Test |
| 103 | + public void test4_canReceiveConfirmationEmail() throws ApiException { |
| 104 | + // receive a verification email from playground using mailslurp |
| 105 | + WaitForControllerApi waitForControllerApi = new WaitForControllerApi(mailslurpClient); |
| 106 | + email = waitForControllerApi.waitForLatestEmail(inbox.getId(), TIMEOUT_MILLIS, UNREAD_ONLY); |
| 107 | + |
| 108 | + // verify the contents |
| 109 | + assertTrue(email.getSubject().contains("Please confirm your email address")); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Extract the confirmation code from email body using regex pattern |
| 114 | + */ |
| 115 | + @Test |
| 116 | + public void test5_canExtractConfirmationCodeFromEmail() { |
| 117 | + // create a regex for matching the code we expect in the email body |
| 118 | + Pattern p = Pattern.compile(".*verification code is (\\d+).*"); |
| 119 | + Matcher matcher = p.matcher(email.getBody()); |
| 120 | + |
| 121 | + // find first occurrence and extract |
| 122 | + assertTrue(matcher.find()); |
| 123 | + confirmationCode = matcher.group(1); |
| 124 | + |
| 125 | + assertTrue(confirmationCode.length() == 6); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Submit the confirmation code to the playground to confirm the user |
| 130 | + */ |
| 131 | + @Test |
| 132 | + public void test6_canSubmitVerificationCodeToPlayground() { |
| 133 | + driver.findElement(By.name("code")).sendKeys(confirmationCode); |
| 134 | + driver.findElement(By.cssSelector("[data-test=confirm-sign-up-confirm-button]")).click(); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Test sign-in as confirmed user |
| 139 | + */ |
| 140 | + @Test |
| 141 | + public void test7_canLoginWithConfirmedUser() { |
| 142 | + // load the main playground login page |
| 143 | + driver.get(PLAYGROUND_URL); |
| 144 | + |
| 145 | + // login with now confirmed email address |
| 146 | + driver.findElement(By.name("username")).sendKeys(inbox.getEmailAddress()); |
| 147 | + driver.findElement(By.name("password")).sendKeys(TEST_PASSWORD); |
| 148 | + driver.findElement(By.cssSelector("[data-test=sign-in-sign-in-button]")).click(); |
| 149 | + |
| 150 | + // verify that user can see authenticated content |
| 151 | + assertTrue(driver.findElement(By.tagName("h1")).getText().contains("Welcome")); |
| 152 | + } |
| 153 | + |
| 154 | + @AfterSuite(alwaysRun = true) |
| 155 | + public void tearDown() { |
| 156 | + WebDriverPool.DEFAULT.dismissAll(); |
| 157 | + } |
| 158 | +} |
0 commit comments