package org.tsdes.intro.spring.selenium.jsftests.selenium.ex03;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.tsdes.intro.spring.selenium.jsftests.selenium.PageObject;

import java.util.List;

public class CommentPageObject extends PageObject {

    public CommentPageObject(WebDriver driver) {

    public boolean isOnPage() {
        return getDriver().getTitle().contains("Example using JSTL");

    public void toStartingPage() {
        getDriver().get(getBaseUrl() + "/ex03/ex03.jsf");

    public void createNewComment(String comment){
        WebElement textArea = getDriver().findElement("createForm:createText"));
        WebElement button = getDriver().findElement("createForm:createButton"));;


    public int getNumberOfComments(){
            Bit tricky: here we need to use XPath:


            The idea is that we define a "query" over the HTML/XML web page, and then
            get back a reference to all the XML tags / web elements satisfying such query.
            Here, we are saying:

            //table  :  any "table" tag in the document
            [@id='commentTable'] : among the <table> tags, just consider the one with id 'commentTable'
            //tbody : just consider <tbody> under the selected table(s), and not in any position in the document
            //tr[string-length(text()) > 0] : get the <tr> (table row) that has non-empty text content

            So to check how many comments there are on the page, we can look at how many rows exist
            in the table that is showing them.

            Note: a good way to try XPath is to run them directly in the browser. Eg, in
             Chrome go under "Developer tools", and when you "search" in the Elements view
             you can use XPath
        List<WebElement> elements = getDriver().findElements(
                By.xpath("//table[@id='commentTable']//tbody//tr[string-length(text()) > 0]"));
        return elements.size();

    public String getCommentText(int position){

        String htmlPos = "" + (position + 1);// XPath starts from 1 and not 0

        WebElement comment = getDriver().findElement(

        return comment.getText();

    public void deleteComment(int position){
        String htmlPos = "" + (position + 1);// XPath starts from 1 and not 0

        WebElement button = getDriver().findElement(
                By.xpath("//table[@id='commentTable']//tbody//tr["+htmlPos+"]/td[3]/form/input[@value = 'Delete']"));;