CakeHomePageTest.java Source code

Java tutorial

Introduction

Here is the source code for CakeHomePageTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;
import java.util.List;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.fail;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

/**
 *
 * @author jimmarczyk
 */
public class CakeHomePageTest {

    private String baseUrl;
    private WebDriver driver;

    private void clickNavElm(String elm) {

        List<WebElement> elms = driver.findElements(By.cssSelector(".navbar-inner .nav li a"));

        for (WebElement element : elms) {
            System.out.println(element.getText());
            if (elm.equals(element.getText())) {
                element.click();
                return;
            }
        }

    }

    @Before
    public void openBrowser() {
        baseUrl = "http://getcake.com/";
        driver = new FirefoxDriver();
        driver.get(baseUrl);
        try {
            Thread.sleep(4000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
    }

    @After
    public void saveScreenshotAndCloseBrowser() throws IOException {
        try {
            Thread.sleep(4000);
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        driver.quit();
    }

    @Test
    public void pricing() throws IOException {
        //WebElement button = driver.findElement(By.linkText("/pricing/"));
        clickNavElm("Pricing");

    }

    @Test
    public void support() throws IOException {
        //WebElement button = driver.findElement(By.linkText("/pricing/"));
        clickNavElm("Support");

    }

    @Test
    public void software() throws IOException {
        //WebElement button = driver.findElement(By.linkText("/pricing/"));
        clickNavElm("Software");
        WebElement button = driver.findElement(By.id("have-taste-btn"));
        button.click();

        List<WebElement> elements = driver.findElements(By.cssSelector(".entry-title"));
        Boolean isRightPage = false;
        for (WebElement element : elements) {
            if ("Insights delivered to your dashboard at the touch of a button.".equals(element.getText())) {
                isRightPage = true;
            }
        }
        if (!isRightPage) {

            fail("Not on the right page!");
        }
    }

    @Test
    public void company() throws IOException {
        //WebElement button = driver.findElement(By.linkText("/pricing/"));
        clickNavElm("Company");

    }

}