Sobes.tech
Junior

How can you test an email input field?

sobes.tech AI

Answer from AI

Check the validity of the format (presence of @, domain). Check the maximum length of the field. Check the handling of special characters (!@#$%^&*()_+=-{}[]|\:;"'<>,.?/~). Check the input of an empty string and spaces. Check the input of Cyrillic characters. Check copying/pasting. Check submitting forms with valid and invalid emails. Check the placeholder functionality. Check the styling of the field in different states (focus, error). Check accessibility (for example, using a screen reader). Check display on different devices and browsers.

Automation: Use regular expressions to validate the format.

import re

def is_valid_email(email):
    # Simple regex for example
    pattern = r"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$"
    return re.match(pattern, email) is not None

# Example usage
email1 = "test@example.com"
email2 = "invalid-email"
print(f"{email1}: {is_valid_email(email1)}")
print(f"{email2}: {is_valid_email(email2)}")

Use UI automation frameworks (Selenium, Cypress) to interact with the input field.

// Example in Java with Selenium
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class EmailFieldTest {
    public static void main(String[] args) {
        // Set the path to the driver
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

        try {
            driver.get("http://your-website.com/form"); // Replace with your form page URL

            WebElement emailField = driver.findElement(By.id("email")); // Replace with your field's ID

            // Test with valid input
            emailField.sendKeys("valid.email@test.com");
            // Add check for absence of error message

            // Test with invalid input
            emailField.clear();
            emailField.sendKeys("invalid-email");
            // Add check for presence of error message

        } finally {
            driver.quit();
        }
    }
}