package com.dungeonstory.ui.test.integration.authentication;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.openqa.selenium.support.PageFactory;

import com.dungeonstory.ui.test.integration.IntegrationTestBase;
import com.vaadin.testbench.elements.NotificationElement;
import com.vaadin.testbench.elements.TextFieldElement;

public class NewUserIT extends IntegrationTestBase {

    private LoginPageObject loginPO;

    @Override
    public void setUp() throws Exception {
        super.setUp();

        // Use the PageFactory to automatically initialize fields.
        loginPO = PageFactory.initElements(getDriver(), LoginPageObject.class);
    }

    @Test
    public void testCreateNewUser() {
        loginPO.open();

        NewUserPageObject newUserPO = loginPO.clickNewUser();
        assertTrue(newUserPO.isVisible());

        newUserPO.enterInfo("test", "test", "test", "test@gmail.com");
        newUserPO.submit();

        assertTrue(loginPO.isVisible());
    }

    @Test
    public void testBlankUsername() {
        loginPO.open();

        NewUserPageObject newUserPO = loginPO.clickNewUser();
        assertTrue(newUserPO.isVisible());

        newUserPO.enterInfo("", "test", "test", "test@gmail.com");
        newUserPO.submit();

        // Verify the notification
        NotificationElement notification = $(NotificationElement.class).first();
        assertEquals("error", notification.getType());
        notification.close();
    }

    @Test
    public void testBlankName() {
        loginPO.open();

        NewUserPageObject newUserPO = loginPO.clickNewUser();
        assertTrue(newUserPO.isVisible());

        newUserPO.enterInfo("test", "test", "", "test@gmail.com");
        newUserPO.submit();

        // Verify the notification
        NotificationElement notification = $(NotificationElement.class).first();
        assertEquals("error", notification.getType());
        notification.close();

        assertTrue($(TextFieldElement.class).id("newName").hasClassName("v-textfield-error"));
    }

    @Test
    public void testBlankPassword() {
        loginPO.open();

        NewUserPageObject newUserPO = loginPO.clickNewUser();
        assertTrue(newUserPO.isVisible());

        newUserPO.enterInfo("test", "", "test", "test@gmail.com");
        newUserPO.submit();

        // Verify the notification
        NotificationElement notification = $(NotificationElement.class).first();
        assertEquals("error", notification.getType());
        notification.close();
    }

    @Test
    public void testInvalidEmail() {
        loginPO.open();

        NewUserPageObject newUserPO = loginPO.clickNewUser();
        assertTrue(newUserPO.isVisible());

        newUserPO.enterInfo("test", "test", "test", "test");
        newUserPO.submit();

        // Verify the notification
        NotificationElement notification = $(NotificationElement.class).first();
        assertEquals("error", notification.getType());
        notification.close();
    }

}

@@ -499,24 +499,91 @@
 loginPO;%0D%0A%0D%0A
+    private static int random = getRandomNumberInRange(1, 100);%0D%0A%0D%0A
     @Overrid
@@ -585,16 +585,16 @@
 erride%0D%0A
-
     publ
@@ -1025,32 +1025,41 @@
 enterInfo(%22test%22
+ + random
 , %22test%22, %22test%22
@@ -1869,32 +1869,41 @@
 enterInfo(%22test%22
+ + random
 , %22test%22, %22%22, %22t
@@ -2479,24 +2479,33 @@
 rInfo(%22test%22
+ + random
 , %22%22, %22test%22
@@ -2948,32 +2948,32 @@
 sVisible());%0D%0A%0D%0A
-
         newUserP
@@ -2986,24 +2986,33 @@
 rInfo(%22test%22
+ + random
 , %22test%22, %22t

fix NewUserIT test if run multiple times