Classes - denhoefel/EcommerceSeleniumProject GitHub Wiki

BaseTests

📘 ⭐

While this class is created you will need to create a "HomePage" class as well


import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import pages.HomePage;

public class BaseTests {

private WebDriver driver;
protected HomePage homePage;

@BeforeClass
public void setUp () {
    System.setProperty("webdriver.chrome.driver", "resources/chromedriver.exe"); //informing the webdriver and location
    driver = new ChromeDriver(); //instantiate ChromeDriver
    driver.get("http://the-internet.herokuapp.com/"); //access to the page you want to test
    driver.manage().window().maximize(); //maximize the window
    homePage = new HomePage(driver); //the object homePage will receive the URL so you can start the tests from there
}
@AfterClass
public void tearDown(){
    driver.quit();
}

}

HomePage - Before adding the link to new pages

📄 ⭐

package pages;

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

public class HomePage {

 private WebDriver driver;

 public HomePage(WebDriver driver) { //constructor
  this.driver = driver;
 }

 public void clickLink(String linkText){ //easy way to reuse the method to click in different links from the homepage by changing the text of the link.
  driver.findElement(By.linkText(linkText)).click();
 }

}

HomePage - Update 1

📄 ⭐

package pages;

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

public class HomePage {

 private WebDriver driver;

 public HomePage(WebDriver driver) { //constructor
  this.driver = driver;
 }

 public void clickLink(String linkText){ //easy way to reuse the method to click in different links from the homepage by changing the text of the link.
  driver.findElement(By.linkText(linkText)).click();
 }

 public LoginPage clickFormAuthentication(){ //click on Form Authentication link
  clickLink("Form Authentication");
  return new LoginPage(driver);
 }

}

Login Page

📄 ⭐

package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.By;
public class LoginPage {

    private WebDriver driver;
    private By userNameField = By.id("username");
    private By passwordField = By.id("password");
    private By loginButton = By.cssSelector("#login button");

    public LoginPage (WebDriver driver){
        this.driver = driver;

    }

    public void SetUsername(String username) {
        driver.findElement(userNameField).sendKeys(username);
        ;
    }

    public void setPassword(String password){
        driver.findElement(passwordField).sendKeys(password);
            }

            public SecureAreaPage clickLoginButton(){
        driver.findElement(loginButton).click();
        return new SecureAreaPage(driver);

            }


}

Secure area Page

📄 ⭐

package pages;
import org.openqa.selenium.By
import org.openqa.selenium.WebDriver;
public class SecureAreaPage {

    private WebDriver driver;
    private By statusAlert= By.id("flash");


        public SecureAreaPage(WebDriver driver){
        this.driver=driver;
    }
        public String getAlertText(){
            return driver.findElement(statusAlert).getText();

        }


}

🔎 LoginTests

package Login;
import base.BaseTests;
import org.testng.annotations.Test;
import pages.LoginPage;
import pages.SecureAreaPage;
import static org.testng.Assert.*;

public class LoginTests extends BaseTests {

@Test
public void testSuccessfulLogin(){
    LoginPage loginPage= homePage.clickFormAuthentication();
    loginPage.SetUsername("tomsmith");
    loginPage.setPassword("SuperSecretPassword!");
    SecureAreaPage secureAreaPage= loginPage.clickLoginButton();
    assertEquals(secureAreaPage.getAlertText(),"You logged into a secure area!\n" +
            "×");
}
}