Clase 5: @Find By

				
					package clase4.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindAll;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

public class AuthenticationPage {

    public WebDriver driver;


    @FindBy(tagName = "h3")
    public WebElement createAccountElement;

    @FindBy(tagName = "h3")
    public WebElement personalInformationElement;

    @FindBy(how = How.ID, using = "email_create")
    public WebElement emailCreationFieldElement;

    /*@FindBy(id = "email_create")
    public WebElement emailCreationFieldElement2;*/

    @FindBy(id="SubmitCreate")
    public WebElement createAccountBtn;

    @FindBy(xpath = "//*[@id='email']")
    public WebElement emailElement;

    @FindBy(name = "email_create']")
    public WebElement emailFieldElement;

    @FindBy(className = "emailField']")
    public WebElement emailElementBtn;

    //[FindsBy(How = How.CssSelector,
    //   Using = "body > ux-dialog-container > div > div > ux-dialog > ux-dialog-body > div:nth-child(2) > tsd-button > button")]

    public AuthenticationPage(WebDriver remoteDriver){
        this.driver = remoteDriver;
        PageFactory.initElements(driver, this);
    }

    public String getCreateAnAccountTitle(){
        //WebElement createAccountElement = driver.findElement(By.tagName("h3"));
        return createAccountElement.getText();
    }

    public void completeRegistrationEmailField(String registrationEmail){
        //driver.findElement(By.id("email_create")).sendKeys(registrationEmail);
        emailCreationFieldElement.sendKeys(registrationEmail);
    }

    public void clickOnCreateAccountBtn() throws InterruptedException {
        //driver.findElement(By.id("SubmitCreate")).click();
        createAccountBtn.click();
        Thread.sleep(3500);
    }

    public String validatePersonalInformationTitle(){
        WebDriverWait wait = new WebDriverWait(driver, 10);
        wait.until(ExpectedConditions.elementToBeClickable(By.tagName("h3")));

        //WebElement personalInformationElement = driver.findElement(By.tagName("h3"));
        return personalInformationElement.getText();
    }

    public String getPopulatedEmailValue(){
        String populatedEmail = emailElement.getAttribute("value");
        return populatedEmail;
    }

    public void completeUserInformationFields(String nombre, String apellido){
        driver.findElement(By.id("uniform-id_gender1")).click();
        driver.findElement(By.name("customer_firstname")).sendKeys(nombre);
        driver.findElement(By.name("customer_lastname")).sendKeys(apellido);

        driver.findElement(By.name("passwd")).sendKeys("holamundo");

        WebElement elementoDias = driver.findElement(By.id("days"));
        Select diasSelect = new Select(elementoDias);
        diasSelect.selectByIndex(7);

        WebElement elementoMonth = driver.findElement(By.id("months"));
        Select monthSelect = new Select(elementoMonth);
        monthSelect.selectByIndex(7);

        WebElement yearElement = driver.findElement(By.id("years"));
        Select yearSelector = new Select(yearElement);
        yearSelector.selectByValue("2001");

        WebElement company = driver.findElement(By.id("company"));
        WebElement address1 = driver.findElement(By.id("address1"));
        WebElement address2 = driver.findElement(By.id("address2"));
        WebElement city = driver.findElement(By.id("city"));
        WebElement stateSelector = driver.findElement(By.id("id_state"));
        WebElement postcode = driver.findElement(By.id("postcode"));
        WebElement countrySelector = driver.findElement(By.id("id_country"));
        WebElement additionalInfo = driver.findElement(By.id("other"));
        WebElement phone = driver.findElement(By.id("phone"));
        WebElement mobile = driver.findElement(By.id("phone_mobile"));
        WebElement addressAlias = driver.findElement(By.id("alias"));

        company.sendKeys("MyCompany");
        address1.sendKeys("My address Nr1");
        address2.sendKeys("My address Nr2");
        city.sendKeys("New York");
        Select selectState = new Select(stateSelector);
        selectState.selectByValue("2");
        postcode.sendKeys("10000");
        Select selectCountry = new Select(countrySelector);
        selectCountry.selectByValue("21");
        additionalInfo.sendKeys("Additional information");
        phone.sendKeys("123456");
        mobile.sendKeys("123456789");
        addressAlias.clear();
        addressAlias.sendKeys("MyAlias");
    }

    public MyAccountPage clickOnRegistrationBtn(){
        WebElement registerButton = driver.findElement(By.id("submitAccount"));
        registerButton.click();
        MyAccountPage nextPage = new MyAccountPage(driver);
        return nextPage;
    }

    public String getAlreadyRegisteredEmailErrorText(){
        WebElement errorElement = driver.findElement(By.id("create_account_error"));
        WebElement emailErrorElement = errorElement.findElement(By.tagName("li"));
        return emailErrorElement.getText();
    }


}

				
			
				
					package clase4.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class LandingPage {

    public WebDriver driver;

    public LandingPage(WebDriver remoteDriver){
        this.driver = remoteDriver;
    }

    public AuthenticationPage goToAuthenticationPage(){
        driver.findElement(By.cssSelector(".login")).click();
        AuthenticationPage nextPage = new AuthenticationPage(driver);
        return nextPage;
    }

}
				
			
				
					package clase5.tests;

import clase3.Constants;
import clase4.EcommerceHelper;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;

public class RegistrationTest extends BaseTest {

    //@Test (invocationCount = 5, successPercentage = 60)
    @Test
    public void registrationTest() throws InterruptedException {
        String emailAddress = "seleniumintermedio" + Math.random() + "test@gmail.com";

        EcommerceHelper helper = new EcommerceHelper(driver);
        helper.completeRegistration(emailAddress);

        WebElement myPersonaInformationBtn = driver.findElement(By.xpath("//*[@href='http://automationpractice.com/index.php?controller=identity']"));
        myPersonaInformationBtn.click();

        WebElement myPersonalInformationElement = driver.findElement(By.tagName("h1"));
        Assert.assertEquals(myPersonalInformationElement.getText(), "YOUR PERSONAL INFORMATION", "Se esperaba obtener otro h1!!");

        WebElement nameElement = driver.findElement(By.id("firstname"));
        String actualName = nameElement.getAttribute("value");

        WebElement lastNameElement = driver.findElement(By.id("lastname"));
        String actualLastName = lastNameElement.getAttribute("value");

        Assert.assertEquals(actualName, Constants.FAKER_FIRST_NAME, "Los nombres no coinciden!!" );
        Assert.assertEquals(actualLastName, Constants.FAKER_LAST_NAME, "Los apellidos no coinciden!!" );
        //driver.close();
    }

}

				
			
				
					package clase4.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;

public class MyAccountPage {
    public WebDriver driver;

    public MyAccountPage(WebDriver remoteDriver){
        this.driver = remoteDriver;
    }

    public String getPageUrl(){
        return driver.getCurrentUrl();
    }

    public PersonalInformationPage goToPersonalInformationPage() throws InterruptedException {
        driver.findElement(By.xpath("//*[@href='http://automationpractice.com/index.php?controller=identity']")).click();
        Thread.sleep(5000);

        PersonalInformationPage nextPage = new PersonalInformationPage(driver);
        return nextPage;
    }


    public AuthenticationPage clickOnLogoutBtn(){
        driver.findElement(By.cssSelector(".logout")).click();
        AuthenticationPage nextPage = new AuthenticationPage(driver);
        return nextPage;
    }
}

				
			
				
					package clase4.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class PersonalInformationPage {

    public WebDriver driver;

    public PersonalInformationPage(WebDriver remoteDriver){
        this.driver = remoteDriver;
    }

    public String getPopulatedFirstName(){
        WebElement populatedFirstNameElement = driver.findElement(By.id("firstname"));
        return populatedFirstNameElement.getAttribute("value");
    }

    public String getPopulatedLastName(){
        WebElement populatedLastNameElement = driver.findElement(By.id("lastname"));
        return populatedLastNameElement.getAttribute("value");
    }
}

				
			
				
					package clase4.test;

import clase4.pages.AuthenticationPage;
import clase4.pages.LandingPage;
import clase4.pages.MyAccountPage;
import clase4.pages.PersonalInformationPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

public class EcommerceSiteTest {


    public WebDriver driver;
    private AuthenticationPage authenticationPage;
    private LandingPage landingPage;

    @BeforeMethod
    public void setup() {
        System.setProperty("webdriver.chrome.driver", "drivers/chromedriver");
        driver = new ChromeDriver();
        driver.get("http://automationpractice.com/index.php");
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        landingPage = new LandingPage(driver);
    }

    @Test
    public void registrationTest() throws InterruptedException {
        double randomNumber = Math.random();
        String nombre = "Alan";
        String apellido = "Rodriguez";

        //create an unique email with an random number..
        String registrationEmail = "seleniumcurso" + randomNumber +"@gmail.com";
        System.out.println("---> " + registrationEmail);

        authenticationPage = landingPage.goToAuthenticationPage();

        String h3Title = authenticationPage.getCreateAnAccountTitle();
        Assert.assertEquals(h3Title, "CREATE AN ACCOUNT", "Error: se esperaba otro titulo h3");

        authenticationPage.completeRegistrationEmailField(registrationEmail);

        authenticationPage.clickOnCreateAccountBtn();

        String personalInformationTitle = authenticationPage.validatePersonalInformationTitle();
        Assert.assertEquals(personalInformationTitle, "YOUR PERSONAL INFORMATION", "Error: se esperaba otro titulo h3");

        //check that the email populated is the same as the registration email
        String populatedEmail = authenticationPage.getPopulatedEmailValue();
        Assert.assertEquals(populatedEmail, registrationEmail, "Error: los emails no son iguales" );

        authenticationPage.completeUserInformationFields(nombre, apellido);

        MyAccountPage myAccountPage = authenticationPage.clickOnRegistrationBtn();

        Assert.assertTrue(myAccountPage.getPageUrl().contains("my-account"), "Error: se esperaba my-account en la URL");
        Thread.sleep(8000);

        PersonalInformationPage personalInformationPage = myAccountPage.goToPersonalInformationPage();

        String populatedFirstNameText = personalInformationPage.getPopulatedFirstName();
        String populatedLastNameText = personalInformationPage.getPopulatedLastName();

        Assert.assertEquals(populatedFirstNameText, nombre, "Error: el nombre de la cuenta no es el esperado");
        Assert.assertEquals(populatedLastNameText, apellido, "Error: el apellido de la cuenta no es el esperado");

    }

    @Test
    public void loginTest() throws InterruptedException {
        double randomNumber = Math.random();
        String nombre = "Alan";
        String apellido = "Rodriguez";

        //create an unique email with an random number..
        String registrationEmail = "seleniumcurso" + randomNumber +"@gmail.com";
        authenticationPage = landingPage.goToAuthenticationPage();

        String h3Title = authenticationPage.getCreateAnAccountTitle();
        Assert.assertEquals(h3Title, "CREATE AN ACCOUNT", "Error: se esperaba otro titulo h3");

        authenticationPage.completeRegistrationEmailField(registrationEmail);

        authenticationPage.clickOnCreateAccountBtn();

        String personalInformationTitle = authenticationPage.validatePersonalInformationTitle();
        Assert.assertEquals(personalInformationTitle, "YOUR PERSONAL INFORMATION", "Error: se esperaba otro titulo h3");

        //check that the email populated is the same as the registration email
        String populatedEmail = authenticationPage.getPopulatedEmailValue();
        Assert.assertEquals(populatedEmail, registrationEmail, "Error: los emails no son iguales" );

        authenticationPage.completeUserInformationFields(nombre, apellido);

        MyAccountPage myAccountPage = authenticationPage.clickOnRegistrationBtn();

        Assert.assertTrue(myAccountPage.getPageUrl().contains("my-account"), "Error: se esperaba my-account en la URL");
        Thread.sleep(8000);

        //log out.....
        driver.findElement(By.cssSelector(".logout")).click();

        driver.findElement(By.id("email")).sendKeys(registrationEmail);
        //driver.findElement(By.id("passwd")).sendKeys(password);

        driver.findElement(By.name("SubmitLogin")).click();

        Assert.assertTrue(driver.getTitle().contains("My account"), "Error: se esperaba otro titulo");
        Assert.assertTrue(driver.getCurrentUrl().contains("my-account"), "Error: se esperaba my-account en la URL");

        WebElement accountNameElement = driver.findElement(By.className("account"));

        String fullName = nombre + " " + apellido;

        Assert.assertEquals(accountNameElement.getText(), fullName, "Error: se esperaba el nombre ingresado en el registro: "+ nombre + " " + apellido);
    }

    @Test
    public void uniqueAccountTest() throws InterruptedException {
        double randomNumber = Math.random();
        String nombre = "Alan";
        String apellido = "Rodriguez";

        //create an unique email with an random number..
        String registrationEmail = "seleniumcurso" + randomNumber +"@gmail.com";
        System.out.println("---> " + registrationEmail);

        authenticationPage = landingPage.goToAuthenticationPage();

        String h3Title = authenticationPage.getCreateAnAccountTitle();
        Assert.assertEquals(h3Title, "CREATE AN ACCOUNT", "Error: se esperaba otro titulo h3");

        authenticationPage.completeRegistrationEmailField(registrationEmail);

        authenticationPage.clickOnCreateAccountBtn();

        String personalInformationTitle = authenticationPage.validatePersonalInformationTitle();
        Assert.assertEquals(personalInformationTitle, "YOUR PERSONAL INFORMATION", "Error: se esperaba otro titulo h3");

        //check that the email populated is the same as the registration email
        String populatedEmail = authenticationPage.getPopulatedEmailValue();
        Assert.assertEquals(populatedEmail, registrationEmail, "Error: los emails no son iguales" );

        authenticationPage.completeUserInformationFields(nombre, apellido);

        MyAccountPage myAccountPage = authenticationPage.clickOnRegistrationBtn();

        Assert.assertTrue(myAccountPage.getPageUrl().contains("my-account"), "Error: se esperaba my-account en la URL");
        Thread.sleep(8000);

        //log out.....
        authenticationPage = myAccountPage.clickOnLogoutBtn();

        authenticationPage.completeRegistrationEmailField(registrationEmail);
        authenticationPage.clickOnCreateAccountBtn();

        //get the error
        String duplicateEmailErrorText = authenticationPage.getAlreadyRegisteredEmailErrorText();
        Assert.assertTrue(duplicateEmailErrorText.contains("An account using this email address has already been registered."), "Error: el mensaje de error no es el esperado");
    }
}

				
			
				
					package clase5.pages;

import org.openqa.selenium.WebDriver;

public class LandingPage extends BasePage {
    public WebDriver driver;

    public LandingPage(WebDriver remoteDriver){
        this.driver = remoteDriver;
    }

    public AuthenticationPage loginBtnClick(){
        loginBtnMenu.click();
        AuthenticationPage nextPage = new AuthenticationPage(driver);
        return nextPage;
    }


}