Clase 5: Búsquedas estáticas de elementos: @FindBy

				
					package clase5.tests;

import clase5.pages.LandingPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.BeforeMethod;

import java.util.concurrent.TimeUnit;

public class BaseTest {

    public WebDriver driver;
    public LandingPage landingPage;

    public static String H1_AUTHENTICATION = "AUTHENTICATION";


    @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);
        driver.manage().window().fullscreen();
        PageFactory.initElements(driver, this);
        landingPage = new LandingPage(driver);
    }
    
}

				
			
				
					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 LoginTest extends BaseTest {

    @Test
    public void loginTest() throws InterruptedException {
        String emailAddress = "seleniumintermedio" + Math.random() + "test@gmail.com";

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

        WebElement logoutElement = driver.findElement(By.className("logout"));

        WebElement nameElement = driver.findElement(By.xpath("(//*[@href='http://automationpractice.com/index.php?controller=my-account']/span)[1]"));
        System.out.println("---> " + nameElement.getText());

        String fullName = Constants.FAKER_FIRST_NAME + " " + Constants.FAKER_LAST_NAME;
        Assert.assertEquals(nameElement.getText(), fullName, "Se esperaba otro nombre!!");
        Assert.assertTrue(nameElement.getText().contains(Constants.FAKER_FIRST_NAME), "El primer nombre no se encontró");
        Assert.assertTrue(nameElement.getText().contains(Constants.FAKER_LAST_NAME), "El apellido no se encontró");
        Assert.assertEquals(logoutElement.getText(), "Sign out", "El link Signout deberia estar presente");
x
        //LOG OUT....
        logoutElement.click();

        Assert.assertEquals(loginBtnMenu.getText(), "Sign in", "No se encontro el boton de Sign In");

        WebElement emailAddressLoginElement = driver.findElement(By.id("email"));
        emailAddressLoginElement.sendKeys(emailAddress);

        WebElement passwordElement = driver.findElement(By.id("passwd"));
        passwordElement.sendKeys(Constants.PASSWORD);

        //SIGN IN.....
        loginWithUserAndPasswordBtn.click();

        //Thread.sleep(4000);
        logoutElement = driver.findElement(By.className("logout"));

        nameElement = driver.findElement(By.xpath("(//*[@href='http://automationpractice.com/index.php?controller=my-account']/span)[1]"));
        System.out.println("---> " + nameElement.getText());

        fullName = Constants.FAKER_FIRST_NAME+ " " + Constants.FAKER_LAST_NAME;
        Assert.assertEquals(nameElement.getText(), fullName, "Se esperaba otro nombre!!");
        Assert.assertTrue(nameElement.getText().contains(Constants.FAKER_FIRST_NAME), "El primer nombre no se encontró");
        Assert.assertTrue(nameElement.getText().contains(Constants.FAKER_LAST_NAME), "El apellido no se encontró");
        Assert.assertEquals(logoutElement.getText(), "Sign out", "El link Signout deberia estar presente");

        //driver.close();
    }

}

				
			
				
					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 clase5.tests;

import clase3.Constants;
import clase5.pages.AuthenticationPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
import org.testng.Assert;

public class EcommerceHelper extends BaseTest {

    public WebDriver driver;

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


    public void completeRegistration(String emailAddress) throws InterruptedException {
        //driver.findElement(By.xpath("//*[@href = 'http://automationpractice.com/index.php?controller=my-account']")).click();

        AuthenticationPage authenticationPage = landingPage.loginBtnClick();

        Assert.assertEquals(authenticationPage.getPageTitle(), "Login - My Store", "Se esperaba otro titlo!!");
        Assert.assertEquals(authenticationPage.getPageURL(), "http://automationpractice.com/index.php?controller=authentication&back=my-account", "Se esperaba otra URL!!");
        Assert.assertTrue(authenticationPage.getPageURL().contains("authentication"));

        Assert.assertEquals(authenticationPage.getAutenticationH1Text(), H1_AUTHENTICATION, "Se esperaba el h1: " + H1_AUTHENTICATION);

        // String emailAddress = "seleniumintermedio" + Math.random() + "test@gmail.com";

        //emailNewAccountField.sendKeys(emailAddress);
        authenticationPage.fillEmailAddress(emailAddress);

        //createAccountBtn.click();
        authenticationPage.clickOnCreateAccountBtn();

        Thread.sleep(6000);
        //WebElement emailElement = driver.findElement(By.id("email"));
        String populatedEmail = emailUserField.getAttribute("value");
        System.out.println("-----> populatedEmail: " + populatedEmail);
        Assert.assertEquals(populatedEmail, emailAddress, "Los emails deberian de coincidir!!!");

        maleGenderRadioButton.click();

        /*Faker faker = new Faker();
        String fakeFirstName = faker.name().firstName();
        String fakeLastName = faker.name().lastName();
        */
        WebElement daySelector = driver.findElement(By.id("days"));
        WebElement monthSelector = driver.findElement(By.id("months"));
        WebElement yearSelector = driver.findElement(By.id("years"));
        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"));
        WebElement registerButton = driver.findElement(By.id("submitAccount"));

        maleGenderRadioButton.click();
        firstNameField.sendKeys(Constants.FAKER_FIRST_NAME);
        lastNameField.sendKeys(Constants.FAKER_LAST_NAME);
        passwdField.sendKeys(Constants.PASSWORD);

        Select selectDays = new Select(daySelector);
        selectDays.selectByValue(Constants.BIRTH_DAY);
        Select selectMonths = new Select(monthSelector);
        selectMonths.selectByValue(Constants.BIRTH_MONTH);
        Select selectYears = new Select(yearSelector);
        selectYears.selectByValue(Constants.BIRTH_YEAR);
        company.sendKeys("MyCompany");
        address1.sendKeys("My address Nr1");
        address2.sendKeys("My address Nr2");
        city.sendKeys(Constants.CITY);
        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");

        //clicking on register button
        registerButton.click();
    }


}

				
			
				
					package clase5.pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class BasePage {


    @FindBy(xpath =  "//*[@href = 'http://automationpractice.com/index.php?controller=my-account']")
    public WebElement loginBtnMenu;

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

    @FindBy (tagName = "h1")
    public WebElement autenticationH1;

    @FindBy (name = "email_create")
    public WebElement emailNewAccountField;

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

    @FindBy (id = "email")
    public WebElement emailUserField;

    @FindBy (id = "id_gender1")
    public WebElement maleGenderRadioButton;

    @FindBy (id ="customer_firstname")
    public WebElement firstNameField;

    @FindBy (id ="customer_lastname")
    public WebElement lastNameField;

    @FindBy (id = "passwd")
    public WebElement passwdField;

}

				
			
				
					package clase5.pages;

import org.openqa.selenium.WebDriver;

public class AuthenticationPage extends BasePage{

    public WebDriver driver;

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

    public String getPageTitle(){
        return driver.getTitle();
    }

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

    public String getAutenticationH1Text(){
        return autenticationH1.getText();
    }


    public void fillEmailAddress(String anEmail){
        emailNewAccountField.sendKeys(anEmail);
    }

    public void clickOnCreateAccountBtn(){
        createAccountBtn.click();
    }
}

				
			
				
					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;
    }


}