我正致力于自动化。 我有学生注册表。首先,我想为五个学生创建一个循环,进行五次注册,之后我想在我的电子邮件中增加。这个增量将在新学生注册后完成。
例如,当第一个学生电子邮件在注册后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");
}
}
}
答案 0 :(得分:0)
您好,您可以尝试此代码。我在方法中添加了所有随机生成器。我还添加了一个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;
}
}