自動化テストでSnapmail APIを使用する

ウェブサイトの自動化テストに関しては、ユーザー登録の自動化のケースをカバーする必要があります。 Snapmail APIを使用すると、すぐに操作できます。

Web UI自動化テストの手順例

  • 希望するSnapmail電子メールアドレスを使用してWebサイトに新しいユーザーを登録します。たとえば、richard@snapmail.ccを使用します。

  • 次に、メールサービスはアカウント検証メールをrichard@snapmail.ccに送信します

    SMTPサービスがない場合は、Snapmail SMTPを使ってメールを送ることができます。 詳細を見る >

  • Snapmail APIを使用して検証メールを取得するときです(Python, C#)。

      # Code in Python
      import time
      import requests
      import json
      import re
      
      
      def get_verification_code():
          # We want to get account validation code in email
          validation_code = None
          # We will retry the request every 6 seconds to get the email
          for i in range(50):
              # Get emails from an email box
              data = {
                  "key": "3b7bb46b-5564-xxxx-ac27-f59061361578",
                  "emailAddress": "test@snapmail.cc",
                  "isPrefix": False,
                  "page": 1,
                  "count": 1
              }
              req = requests.post('https://www.snapmail.cc/emaillist/filter', json=data)
              if req.status_code == 200:
                  # Get email text of the first email,
                  # take "This is a test email." for example,
                  # email_text = "This is a test email."
                  first_email = json.loads(req.text)[0]
                  if "text" in first_email:
                      email_text = first_email['text']
                  else:
                      email_text = first_email['html']
                  # Use regex to get the validation code, we'll get "test" here.
                  # validation_code = "test"
                  validation_code = re.search(r'This is a ([a-zA-Z0-9]{4}) email', email_text)
                  break
      
              print("Waiting for next retry")
              time.sleep(6)
          if validation_code:
              print('validation_code:' + validation_code.group(1))
              return validation_code.group(1)
      
      
      get_verification_code()
    
      // Code in C#
      using System;
      using System.Net.Http;
      using System.Net.Http.Json;
      using System.Text.RegularExpressions;
      using System.Threading;
      using System.Threading.Tasks;
      
      using Newtonsoft.Json.Linq;
      
      namespace ConsoleApp1
      {
          class Program
          {
              static HttpClient client = new HttpClient();
              static void Main(string[] args)
              {
                  Task t = new Task(GetValidationCode);
                  t.Start();
                  Console.ReadLine();
              }
      
              private static async void GetValidationCode()
              {
                  // Get emails from an email box
                  var url = "https://www.snapmail.cc/emaillist/filter";
                  // We want to get account validation code in email
                  var validation_code = "";
      
                  // We will retry the request every 6 seconds to get the email
                  for (int i = 0; i < 50; i++)
                  {
                      HttpResponseMessage response = await client.PostAsJsonAsync(url, new
                      {
                          key = "3b7bb46b-5564-xxxx-ac27-f59061361578",
                          emailAddress = "test@snapmail.cc",
                          isPrefix = false,
                          page = 1,
                          count = 1
                      });
                      // If the response status is 200
                      if (response.StatusCode == System.Net.HttpStatusCode.OK)
                      {
                          var result = await response.Content.ReadAsStringAsync();
                          // Get the emails in array format
                          var emails = JArray.Parse(result);
                          // Get the email text of first email, 
                          // take "This is a test email." for example.
                          // email_text = "This is a test email."
                          var email_text = emails[0]["text"].ToString();
                          Regex regex = new Regex("This is a ([a-zA-Z0-9]{4}) email");
                          Match match = regex.Match(email_text);
                          // Use regex to get the validation code, we'll get "test" here.
                          // validation_code = "test"
                          validation_code = match.Groups[1].Value; 
                          Console.WriteLine(validation_code);
                          break;
                      }
                      Console.WriteLine("Waiting for next retry");
                      // Sleep 6 seconds
                      Thread.Sleep(6000);
                  }
      
              }
          }
      }
    
  • 任意の電子メールアドレスを使用できます。このようなシナリオではプレフィックスの電子メールアドレスを使用することをお勧めします。Snapmail.ccに追加する必要はありません

体験Snapmail

トップページに戻る