Clase 6: Page Object Model + Herencia Copy

				
					package clase5.tests;

import clase5.pages.LandingPage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
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 clase5.pages.AuthenticationPage;
import clase5.pages.MyAccountPage;
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);
        MyAccountPage myAccountPage = helper.completeRegistration(emailAddress);

        String accountName = myAccountPage.getAccountName();

        //Checking the account is created.....
        String fullName = Constants.FAKER_FIRST_NAME + " " + Constants.FAKER_LAST_NAME;
        Assert.assertEquals(accountName, fullName, "Se esperaba otro nombre!!");
        Assert.assertTrue(accountName.contains(Constants.FAKER_FIRST_NAME), "El primer nombre no se encontró");
        Assert.assertTrue(accountName.contains(Constants.FAKER_LAST_NAME), "El apellido no se encontró");
        Assert.assertEquals(myAccountPage.getLogoutBtnText(), "Sign out", "El link Signout deberia estar presente");

        //LOG OUT....
        AuthenticationPage authenticationPage = myAccountPage.clickOnLogoutBtn();

/*
        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 clase5.pages.AuthenticationPage;
import clase5.pages.CreateAccountPage;
import clase5.pages.LandingPage;
import clase5.pages.MyAccountPage;
import clase5.utilities.Constants;
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 MyAccountPage completeRegistration(String emailAddress) throws InterruptedException {
        landingPage = new LandingPage(driver);

        AuthenticationPage authenticationPage = landingPage.loginBtnClick();

        Assert.assertEquals(authenticationPage.getPageTitle(), Constants.AUTHENTICATION_PAGE_TITLE, "Se esperaba otro titlo!!");
        Assert.assertEquals(authenticationPage.getPageURL(), Constants.AUTHENTICATION_PAGE_URL, "Se esperaba otra URL!!");
        Assert.assertTrue(authenticationPage.getPageURL().contains("authentication"));

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

        authenticationPage.fillEmailAddress(emailAddress);

        CreateAccountPage createAccountPage = authenticationPage.clickOnCreateAccountBtn();

        Thread.sleep(6000);
        String populatedEmail = createAccountPage.getEmailValue();

        Assert.assertEquals(populatedEmail, emailAddress, "Los emails deberian de coincidir!!!");

        createAccountPage.fillRegistrationFields();

        //clicking on register button
        MyAccountPage myAccountPage = createAccountPage.clickOnRegisterAccountBtn();
        return myAccountPage;
    }


}

				
			
				
					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
    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.pages;

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

public class LandingPage {
    public WebDriver driver;

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

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

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


}

				
			
				
					package clase5.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class AuthenticationPage{
    public WebDriver driver;

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

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

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

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

    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 CreateAccountPage clickOnCreateAccountBtn(){
        createAccountBtn.click();
        CreateAccountPage nextPage = new CreateAccountPage(driver);
        return nextPage;
    }

    public void clickOnSignInBtn(){
        WebElement nameElement = driver.findElement(By.xpath("(//*[@href='http://automationpractice.com/index.php?controller=my-account']/span)[1]"));
    }
}

				
			
				
					package clase5.pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class AuthenticationPage{
    public WebDriver driver;

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

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

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

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

    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 CreateAccountPage clickOnCreateAccountBtn(){
        createAccountBtn.click();
        CreateAccountPage nextPage = new CreateAccountPage(driver);
        return nextPage;
    }

    public void clickOnSignInBtn(){
        WebElement nameElement = driver.findElement(By.xpath("(//*[@href='http://automationpractice.com/index.php?controller=my-account']/span)[1]"));
    }
}

				
			
				
					package clase5.pages;

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

public class MyAccountPage {
    public WebDriver driver;

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

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

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

    public AuthenticationPage clickOnLogoutBtn(){
        WebElement logoutElement = driver.findElement(By.className("logout"));
        AuthenticationPage nextPage = new AuthenticationPage(driver);
        return nextPage;
    }

    public String getLogoutBtnText(){
        WebElement logoutElement = driver.findElement(By.className("logout"));
        return logoutElement.getText();
    }

    public String getAccountName(){
        WebElement nameElement = driver.findElement(By.xpath("(//*[@href='http://automationpractice.com/index.php?controller=my-account']/span)[1]"));
        return nameElement.getText();
    }

}