Skip to content

Commit 86e620b

Browse files
committed
phones
1 parent 96c4524 commit 86e620b

File tree

2 files changed

+264
-14
lines changed

2 files changed

+264
-14
lines changed

java-maven-selenium/Makefile

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,67 @@
1-
-include ../.env
1+
SHELL := /bin/bash
22

3-
DRIVER_LOCATION=geckodriver
4-
DRIVER_URL=https://github.com/mozilla/geckodriver/releases/download/v0.32.2/geckodriver-v0.32.2-linux64.tar.gz
5-
FIREFOX_LOCATION=firefox/firefox
3+
OS := $(shell uname -s)
4+
ARCH := $(shell uname -m)
65

6+
DRIVER_LOCATION := geckodriver
77

8-
$(FIREFOX_LOCATION):
9-
wget --content-disposition "https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-US" -O firefox.tar.bz2
10-
bzip2 -d firefox.tar.bz2
11-
tar -xf firefox.tar
8+
# Linux only
9+
FIREFOX_DIR := firefox
10+
FIREFOX_LOCATION_LINUX := $(FIREFOX_DIR)/firefox
11+
FIREFOX_TARBALL := firefox.tar
12+
FIREFOX_URL_LINUX := https://download.mozilla.org/?product=firefox-latest-ssl&os=linux64&lang=en-US
13+
14+
# macOS uses installed Firefox
15+
FIREFOX_LOCATION_MAC := /Applications/Firefox.app/Contents/MacOS/firefox
16+
17+
# Geckodriver URLs (yours)
18+
ifeq ($(OS),Darwin)
19+
ifeq ($(ARCH),arm64)
20+
DRIVER_URL := https://github.com/mozilla/geckodriver/releases/download/v0.36.0/geckodriver-v0.36.0-macos-aarch64.tar.gz
21+
else
22+
DRIVER_URL := https://github.com/mozilla/geckodriver/releases/download/v0.36.0/geckodriver-v0.36.0-macos.tar.gz
23+
endif
24+
else
25+
ifeq ($(ARCH),aarch64)
26+
DRIVER_URL := https://github.com/mozilla/geckodriver/releases/download/v0.36.0/geckodriver-v0.36.0-linux-aarch64.tar.gz
27+
else
28+
DRIVER_URL := https://github.com/mozilla/geckodriver/releases/download/v0.36.0/geckodriver-v0.36.0-linux64.tar.gz
29+
endif
30+
endif
1231

1332
$(DRIVER_LOCATION):
14-
curl -s -L "$(DRIVER_URL)" | tar -xz
33+
curl -fL "$(DRIVER_URL)" | tar -xz
1534
chmod +x $(DRIVER_LOCATION)
1635

17-
test: $(DRIVER_LOCATION) $(FIREFOX_LOCATION)
18-
API_KEY=$(API_KEY) \
19-
PATH_TO_WEBDRIVER=$(realpath $(DRIVER_LOCATION)) \
20-
PATH_TO_FIREFOX=$(realpath $(FIREFOX_LOCATION)) \
21-
mvn install test
36+
# Linux: download Firefox locally
37+
$(FIREFOX_LOCATION_LINUX):
38+
rm -rf $(FIREFOX_DIR) firefox.tar firefox
39+
mkdir -p $(FIREFOX_DIR)
40+
curl -fL -o $(FIREFOX_TARBALL) "$(FIREFOX_URL_LINUX)"
41+
tar -xaf $(FIREFOX_TARBALL)
42+
mv firefox/* $(FIREFOX_DIR)/
43+
rmdir firefox
44+
chmod +x $(FIREFOX_LOCATION_LINUX)
45+
46+
# macOS: verify Firefox exists
47+
.PHONY: firefox
48+
firefox:
49+
ifeq ($(OS),Darwin)
50+
@test -x "$(FIREFOX_LOCATION_MAC)" || (echo "Firefox not found at $(FIREFOX_LOCATION_MAC). Install Firefox or set FIREFOX_LOCATION_MAC."; exit 1)
51+
else
52+
@$(MAKE) $(FIREFOX_LOCATION_LINUX)
53+
endif
54+
55+
test: $(DRIVER_LOCATION) firefox
56+
ifeq ($(OS),Darwin)
57+
API_KEY=$(API_KEY) PATH_TO_WEBDRIVER=$(realpath $(DRIVER_LOCATION)) \
58+
PATH_TO_FIREFOX=$(FIREFOX_LOCATION_MAC) \
59+
mvn test -Dtest=com.mailslurp.examples.SmsUsageTest
60+
else
61+
API_KEY=$(API_KEY) PATH_TO_WEBDRIVER=$(realpath $(DRIVER_LOCATION)) \
62+
PATH_TO_FIREFOX=$(realpath $(FIREFOX_LOCATION_LINUX)) \
63+
mvn test -Dtest=com.mailslurp.examples.SmsUsageTest
64+
endif
65+
66+
clean:
67+
rm -rf $(FIREFOX_DIR) firefox.tar geckodriver firefox
Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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

Comments
 (0)