I. Requirements:
Registration and login is a very familiar procedure in many systems. Meanwhile, many systems require email authentication when signing up for an account. When you tested the system, you should be familiar with this registration and authentication.
However, the number of personal emails is limited, so how can there be enough emails to test and retest this function, not to mention when testing the functionality behind, you need to log in, it can be said the number of emails must use is great. So we have disposable email services (basically one-time email), many of you are probably familiar with these services / systems, so now I would like to list a few websites that support disposable email. email:
Popular disposable email systems:
So how to do it?
Usually, disposable email you can enter the email name by yourself, after @ will use the domain of these systems. For example with qa.team, when registering we can use email [email protected] to register, then enter qa.team enter tam.nguyen and can receive emails as usual.
For temporary email system like https://temp-mail.org , the system will generate email for us, we just need to copy the email to register and return to the system to receive mail.
So how about doing automation with the system that requires email verification?
Similar to manual, we can do it on the UI layer. As for manual, we can do it with automation like that. Although these systems have support APIs, to get close to the user behavior, in this article I will use Selenium Java to implement automation right on the UI.
II. Use Selenium java to perform automate for the email verification system
Let’s try writing an automation project for our website https://playground.mailslurp.com/
1. Import the required libraries
First, let’s also create a demo project for this requirement. I use Maven project and add the necessary depedencies to: Here are just libraries of Selenium, TestNG and WebDriverManager to manage webdriver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<dependencies> <!-- TestNG --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.8</version> </dependency> <!-- Selenium Java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-support</artifactId> <version>3.141.59</version> </dependency> <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>3.6.0</version> </dependency> </dependencies> |
2. Steps to register with verify email for demo website https://playground.mailslurp.com/
The manual steps here would look like this:
1. Sign up
- Enter your email
- Enter the password
- Open a new tab and go to https://mailsac.com/
- Enter the email you just used to sign up
- Open the mailbox
- Get the confirmation code
- Back to website https://playground.mailslurp.com/
- Enter confirmation code
- Click Sign Up
2. Sign in
- Enter the email you just registered
- Enter the password
- Click the login button
- Successful login test
Likewise, embark on writing a class to run offline
2. Write a test class to automate the steps above
Here I have a class that uses Java, Selenium and TestNG to automate the above steps as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
public class DisposableEmail { private WebDriver driver; private String email, password, emailContent, confirmationCode; private JavascriptExecutor jsExecutor; /** * Hàm này chạy trước mọi method khác * Gán và khởi tạo Chrome driver * Khai báo hàm jsExecutor để sử dụng javascript executor * gán giá trị random cho email tránh trùng lặp dữ liệu * gán giá trị cho password */ @BeforeClass public void beforeTest(){ //Sử dụng thư viện WebDriverManager để phát hiện phiên bản chrome đang dùng, từ dó tự động download phiên bản webdriver tương ứng WebDriverManager.chromedriver().setup(); //Khởi tạo driver driver = new ChromeDriver(); //Phóng to màn hình chrome để script có thể chạy ổn định hơn driver.manage().window().maximize(); //Set implicitwait, bạn nào chưa hiểu về wait thì google thêm nhé driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); //Khởi tạo jvavascript executor jsExecutor = (JavascriptExecutor) driver; //Gán giá trị cho email và password. Ở đây email mình sử dụng thêm randomNumber để tránh trùng email khi chạy nhiều lần email = "tam.nguyen" + randomNumber() + "@mailsac.com"; password = "12345678"; } /** * Đăng ký tài khoản và xác minh địa chỉ email */ @Test(priority = 1) public void registerAccount(){ //Truy cập vào website demo driver.get("https://playground.mailslurp.com/"); //Click vào register account link driver.findElement(By.xpath("//a[text()='Create account']")).click(); //Điền vào email textbox - email là biến đã được gán số random ở trên, email sẽ có dạng <a class="__cf_email__" href="/cdn-cgi/l/email-protection">[email protected]</a> driver.findElement(By.xpath("//input[@name='email']")).sendKeys(email); //Điền vào password textbox = 12345678 driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password); //Click vào nút "Create account" driver.findElement(By.xpath("//button[text()='Create Account']")).click(); //Sử dụng javascript executor để mở một tab mới và truy cập website mailsac.com jsExecutor.executeScript("window.open('http://mailsac.com','_blank');"); //Lấy các tabs hiện tại đang có và switch qua tab có index = 1, lúc này mailsac.com đang ở tab thứ 2 nên sẽ có index = 1 ArrayList<String> tabs = new ArrayList<String>(driver.getWindowHandles()); //Switch qua tab mailsac.com vừa mở driver.switchTo().window(tabs.get(1)); //Điền email mới đăng ký vào textbox driver.findElement(By.xpath("//input[@placeholder='anything']")).sendKeys(email); //Nhập vào nút "Check the email" để truy cập mailbox driver.findElement(By.xpath("//button[@class='btn btn-primary']")).click(); //Click vào mail để mở nội dung email driver.findElement(By.xpath("//td[@class='col-xs-5 ng-binding']")).click(); //lấy text của nội dung email và gán cho biến "emailContent" emailContent = driver.findElement(By.xpath("//div[@class='ng-binding ng-scope']")).getText(); //Sử dụng regex để xác định dãy số có 6 chữ số trong email content Pattern date = Pattern.compile("[0-9]{6}"); Matcher matcher = date.matcher(emailContent); matcher.find(); //Gán giá trị số có 6 chữ số (confirmation code) cho biến confirmationCode confirmationCode =emailContent.substring(matcher.start(), matcher.end()); //Switch về lại tab playground.mailslurp.com (index = 0) driver.switchTo().window(tabs.get(0)); //Điền confirmation code driver.findElement(By.xpath("//input[@name='code']")).sendKeys(confirmationCode); //Click to Confirm button driver.findElement(By.xpath("//button[text()='Confirm']")).click(); } /** * Đăng nhập vào hệ thống bằng email vừa mới đăng ký ở bước trên */ @Test(priority = 2) public void loginToSystem(){ //Truy cập lại website driver.get("https://playground.mailslurp.com/"); //Điền email vừa đăng ký driver.findElement(By.xpath("//input[@name='username']")).sendKeys(email); //Điền password driver.findElement(By.xpath("//input[@name='password']")).sendKeys(password); //Click sign in button driver.findElement(By.xpath("//button[text()='Sign In']")).click(); //Kiểm tra đăng nhập thành công //Kiểm tra màn hình welcome xuất hiện Assert.assertTrue(driver.findElement(By.xpath("//h1")).getText().equals("Welcome")); } @AfterClass public void afterTest(){ driver.quit(); } /** * Hàm sinh ra random number * @return randomNumber để gán vào email, tránh bị trùng lặp email khi chạy nhiều lần */ public int randomNumber(){ Random random = new Random(); int randomNumber = random.nextInt(); return randomNumber; } } |
Above, most of the commands have been explained in the comments, mostly performing the same steps as the manual. If you have any questions, you can google or leave a comment
Here, I just create a class written step by step, everyone applies it to their own framework
Referencies: https://github.com/npmtam/Automation-Testing-Articles/issues/2