Friday, 4 August 2017

Page Object - LoginPage

package com.test.automation.uiAutomation.uiActions;

import org.apache.log4j.Logger;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Reporter;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 31-07-2017
 *
 */

public class LoginPage {

public static final Logger log = Logger.getLogger(LoginPage.class.getName());
WebDriver driver;
HomePage homePage;

@FindBy(id = "name")
private WebElement loginUsername;

@FindBy(id = "password")
private WebElement loginPassword;

@FindBy(id = "loginButton")
private WebElement loginButton;

@FindBy(linkText = "Sign Up")
private WebElement signUp;

@FindBy(id = "forgotPassword")
private WebElement forgetPassword;

@FindBy(id = "alertMessage")
private WebElement errMessage;

@FindBy(id = "alertMessageForSuccess")
private WebElement infoMessage;

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

public void waitForElementClickable(WebElement element) {
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(element));
}

public void clearFields() {
loginUsername.clear();
loginPassword.clear();
}

public void loginToApplication(String username, String password) throws InterruptedException {
homePage = new HomePage(driver);
homePage.clickToLogin();

loginUsername.sendKeys(username);
log.info("entered username:-" + username + "and object is " + loginUsername.toString());
loginPassword.sendKeys(password);
log.info("entered password:-" + password + "and object is " + loginPassword.toString());

waitForElementClickable(loginButton);
loginButton.click();
log.info("clicked on login button and object is:- " + loginButton.toString());

}

public String getInvalidLoginText() {
log.info("error message is:- " + errMessage.getText());
return errMessage.getText();
}

public String getTitleName() {
log.info("current title name is :- " + driver.getTitle());
return driver.getTitle();
}

public void switchToDefaultContent() {
driver.switchTo().defaultContent();
log("switched to the default Content");
}

public void log(String data) {
log.info(data);
Reporter.log(data);
}

public void loginToDemoSite(String emailAddress, String loginPassword) {

loginUsername.sendKeys(emailAddress);
this.loginPassword.sendKeys(loginPassword);
loginButton.click();
}
}

No comments:

Post a Comment

Code Example - File Download By Robot Class In Firefox Browser

Exported from Notepad++ package com . practiceCode ; import java . awt . AWTException ; import java . awt . Robot ; import j...