/**
 * 
 */
package org.richfaces.realworld.testng;

import org.richfaces.realworld.SeleniumTestBase;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * @author Andrey Markavtsov
 *
 */
public class LoginTest extends SeleniumTestBase {
	
	static final String usernameId = "login:username";
	static final String passwordId = "login:password";
	static final String loginButtonPath= "//div[@class='actionButtons']/input[1]";
	static final String loggedUserPath = "//span[@class='logged-user']";
	
	static final String LOGIN_NAME = "amarkhel";
	static final String LOGIN_PASSWORD = "12345";
	
	@Test
	public void testAuthentication() {
		renderPage();
		
		AssertVisible(usernameId, "Input for username in not visible");
		AssertVisible(passwordId, "Input for password in not visible");
		
		String type = selenium.getAttribute("//*[@id='"+passwordId+"']/@type");
		if (!"password".equals(type)) {
			Assert.fail("Password input should be of 'password' type");
		}
		
		setValueById(usernameId, LOGIN_NAME);
		setValueById(passwordId, LOGIN_PASSWORD);
		
		selenium.click(loginButtonPath);
		waitForPageToLoad();
		
		String loggedText = selenium.getText(loggedUserPath);
		if (!loggedText.contains(LOGIN_NAME)) {
			Assert.fail("Authentication was not succesfull. Logged user text should contain typed login name");
		}
		
	}

	/* (non-Javadoc)
	 * @see org.richfaces.realworld.SeleniumTestBase#getTestUrl()
	 */
	@Override
	public String getTestUrl() {
		return "/index.seam";
	}

}
