User already exist when I try to automate registration with selenium?

Viewed 46

I'm making UI automation with Selenium and Junit, and my program works fine, but the page returns error: "User already registered with this name" whatever I try to pass with userNameField.sendKeys("name"));

My friend told me that I should concat the name with a date, since thats always unique. But how can I do that? My code looks like this:

public class Tester {

@Test
public void registerUserTest(){

        WebDriver driver = new ChromeDriver();

        driver.navigate().to("http://localhost:8080/sportsbetting-web/register");

        WebElement userNameField = driver.findElement(By.name("userName"));
        WebElement nameField = driver.findElement(By.name("name"));
        WebElement dateOfBirth = driver.findElement(By.name("dateOfBirth"));
        WebElement accountNumber = driver.findElement(By.name("accountNumber"));

        WebElement currencyDropdown = driver.findElement(By.xpath("//*[@id=\"account-box\"]/form/div[9]/select"));
        currencyDropdown.click();
        WebDriverWait wait = new WebDriverWait(driver, Duration.of(3, SECONDS));
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"account-box\"]/form/div[9]/select/option[1]")));
        Select currencySelect = new Select(currencyDropdown);
        currencySelect.selectByValue("EUR");
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        WebElement passWord = driver.findElement(By.name("password"));
        WebElement passwordMatcher = driver.findElement(By.name("passwordAgain"));
        WebElement button = driver.findElement(By.xpath("//*[@id=\"account-box\"]/form/button"));


        userNameField.sendKeys("asd" + LocalDate.now());
        nameField.sendKeys("asd");
        dateOfBirth.sendKeys("0019970929");
        accountNumber.sendKeys("11111111");
        passWord.sendKeys("asd");
        passwordMatcher.sendKeys("asd");
        button.click();

        String actualUrl = "http://localhost:8080/sportsbetting-web/home";
        String expectedUrl = driver.getCurrentUrl();
        Assert.assertEquals(actualUrl, expectedUrl);

}

}

1 Answers

Well, regarding the message you are sending always the same name, not userName. Another thing is the LocalDate.now(), better use LocalDateTime.now()

public static String timestampOfPattern(String pattern) {
    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
    return now.format(dateTimeFormatter);
}

String userName = "foo" + timestampOfPattern("YYMMDD_HHmmss");
userNameField.sendKeys(userName);
nameField.sendKeys(userName);

PS: those two strings has to be vice versa otherwise in case of test failure the error text will be wrong.

String actualUrl = "http://localhost:8080/sportsbetting-web/home";
String expectedUrl = driver.getCurrentUrl();

Even the assert is wrong, expected value has to be the first argument.

Related