繁体   English   中英

如何为Java创建或编码以自动递增电子邮件

[英]How can I create or code to auto increment email, for java

我正在研究自动化。 我有学生登记表。 首先,我想为五个学生创建一个循环以进行五次注册,之后我想增加自己的电子邮件。 该增加将在新学生注册后完成。

就像注册后第一位学生的电子邮件为aali3009@outlook.com一样,第二位学生的电子邮件将自动将其递增为aali3010@gmail.com

请给我建议我该怎么做。 我正在提供我的代码。 我需要你的帮助。

public class Cartus   
{
    private static String email="aali3009@outlook.com";
    public static  void Cartusfill ()
    {
        System.setProperty("webdriver.chrome.driver","E:\\workspace\\chromeDriver\\chromeDriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.get("http://qa-0.ls.vu/v2/landing-page/cartus/en");
        for(int i=0; i<5;i++)
        {
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[1]/div")).click();
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[1]/div/select/option[2]")).click();
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys("");
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[1]/input")).sendKeys("");
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[2]/div[2]/input")).sendKeys("Mohaname");
            driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[1]/input")).sendKeys("0221-1234567");
               {
                    email = email + 1;
                    driver.findElement(By.xpath("/html/body/div[4]/div/form/div[3]/div[2]/input")).sendKeys(email);
               }
            driver.findElementById("submitButton").click();
            System.out.println("Test case succesfully run");
        }
    }
}

嗨,您可以尝试此代码。 我在该方法中添加了所有随机生成器。 我还添加了一个JDBC,以检查邮件是否在数据库中

    import java.math.BigInteger;
    import java.security.SecureRandom;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Random;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;

    public class TestingItTest {

        String commonXpath = "/html/body/div[4]/div/form/div";
        WebDriver driver;
        int email;

        @BeforeClass
        public void beforeClass() {
//Getting the unique id for email
            email = uniqueSessionId();
            System.setProperty("webdriver.chrome.driver", "E:\\workspace\\chromeDriver\\chromeDriver.exe");
            driver = new ChromeDriver();
            driver.get("http://qa-0.ls.vu/v2/landing-page/cartus/en");

        }

        @Test
        public void f() {
            String StudenEmail;

//looping through the email by incrementing the email
            for (int i = 0; i < 5; i++) {
    //Creating a new email for every loop
                StudenEmail = "aali" + email + "@outlook.com";

                String Driver = "com.mysql.jdbc.Driver";
                String URL = "jdbc:mysql://localhost:3306/LoginForm";
//Checking for email already exist in DB or not             
try {
                    Class.forName(Driver);
                    Connection Conn = DriverManager.getConnection(URL, "root", "12345");
                    PreparedStatement ps = Conn.prepareStatement("SELECT studenId FROM StudentDetail WHERE email = ?");
                    ps.setString(1, StudenEmail);
                    ResultSet rs = ps.executeQuery();

                    if (rs.next()) {
                        // Email Already exist Do something

                    }

//Method to register the email 
                    DotheMailAction(StudenEmail);

                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }

        private void DotheMailAction(String studenEmail2) {
//Method to do the registration
            driver.findElement(By.xpath(commonXpath + "[1]/div")).click();
            driver.findElement(By.xpath(commonXpath + "[1]/div/select/option[2]")).click();
            driver.findElement(By.xpath(commonXpath + "[2]/div[1]/input")).sendKeys("");
            driver.findElement(By.xpath(commonXpath + "[2]/div[1]/input")).sendKeys("");
            driver.findElement(By.xpath(commonXpath + "[2]/div[2]/input")).sendKeys("Mohaname");
            driver.findElement(By.xpath(commonXpath + "[3]/div[1]/input")).sendKeys("0221-1234567");
            driver.findElement(By.xpath(commonXpath + "[3]/div[2]/input")).sendKeys(studenEmail2);
            driver.findElement(By.id("submitButton")).click();

            email++;
            System.out.println("Test case succesfully run");
        }

        @AfterClass
        public void afterClass() {
//After the process the code closed the driver
            if (driver != null) {
                driver.quit();
            }
        }

        public int nextSessionId() {
            //Random number generator
            SecureRandom random = new SecureRandom();
            int num = random.nextInt(100000);
            String formatted = String.format("%05d", num);
            Integer formatInt = Integer.valueOf(formatted);
            return formatInt;
        }

        public String nextSessionString() {
            //Random single Char generator
            Random r = new Random();
            char random_3_Char = (char) (48 + r.nextInt(47));
            return random_3_Char + "";
        }

        public int uniqueSessionId() {
            // Every time random unique number generator
            DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            Date date = new Date();
            Integer formatInt = Integer.valueOf(dateFormat.format(date));
            return formatInt;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM