package selenium.mtgstocks.pages;

import org.assertj.core.api.Assertions;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import selenium.AbstractPage;
import selenium.NavigationConstant;

public class MtgStocksHomePage extends AbstractPage<MtgStocksHomePage> {

    public static final String GREETING_TEXT
            = "Your favorite resource for daily price updates on Magic: the Gathering cards. " +
            "We track prices of all cards and report the ones that " +
            "have recently fluctuated the most for your Magic finance convenience!";

    private MtgStocksHomePage(WebDriver driver) {
        super(driver, NavigationConstant.MTG_STOCKS_HOME_PAGE, "MTGStocks.com");
    }

    public MtgStocksHomePage assertGreetingText(String greetingText) {
        Assertions.assertThat(driver.findElement(By.xpath("//div[@class='jumbotron']/p")).getText())
                .isEqualTo(greetingText);
        return self();
    }

    public MtgStocksHomePage assertHasMenuTowards(String menuName) {
        WebElement navbar = findNavigationBar();
        Assertions.assertThat(navbar.findElements(By.xpath("//a[contains(text(),'" + menuName +"')]"))).isNotEmpty();
        return self();
    }

    private WebElement findNavigationBar() {
        return driver.findElement(By.id("navbar"));
    }

    public MtgStocksSetsPage goToSetsPage() {
        WebElement SetsMenuButton = findNavigationBar().findElement(By.linkText("Sets"));
        return MtgStocksSetsPage.createByClickingWebElement(driver, SetsMenuButton);
    }

    public static MtgStocksHomePage createByBrowsing(WebDriver driver) {
        return new MtgStocksHomePage(driver).browseTo();
    }
}

@@ -187,140 +187,519 @@
 ort 
-selenium.AbstractPage;%0Aimport selenium.NavigationConstant;%0A%0Apublic class MtgStocksHomePage extends AbstractPage%3CMtgStocksHomePage%3E %7B
+org.openqa.selenium.support.FindBy;%0Aimport org.openqa.selenium.support.PageFactory;%0Aimport org.openqa.selenium.support.pagefactory.AjaxElementLocatorFactory;%0Aimport org.openqa.selenium.support.pagefactory.ElementLocatorFactory;%0Aimport selenium.AbstractPage;%0Aimport selenium.NavigationConstant;%0A%0Apublic class MtgStocksHomePage extends AbstractPage%3CMtgStocksHomePage%3E %7B%0A%0A    @FindBy(id = %22navbar%22)%0A    private WebElement navBar;%0A%0A    @FindBy(xpath = %22//div%5B@class='jumbotron'%5D/p%22)%0A    private WebElement greeting;
 %0A%0A  
@@ -1238,95 +1238,27 @@
 hat(
-driver.findElement(By.xpath(%22//div%5B@class='jumbotron'%5D/p%22)).getText())%0A                
+greeting.getText())
 .isE
@@ -1382,57 +1382,8 @@
 ) %7B%0A
-        WebElement navbar = findNavigationBar();%0A
     
@@ -1411,17 +1411,17 @@
 That(nav
-b
+B
 ar.findE
@@ -1530,112 +1530,8 @@
  %7D%0A%0A
-    private WebElement findNavigationBar() %7B%0A        return driver.findElement(By.id(%22navbar%22));%0A    %7D%0A%0A
     
@@ -1612,27 +1612,14 @@
 n = 
-findNavigation
+nav
 Bar
-()
 .fin
@@ -1822,22 +1822,53 @@
         
-return
+MtgStocksHomePage mtgStocksHomePage =
  new Mtg
@@ -1898,20 +1898,196 @@
 browseTo();%0A
+        ElementLocatorFactory finder = new AjaxElementLocatorFactory(driver, 5);%0A        PageFactory.initElements(finder, mtgStocksHomePage);%0A        return mtgStocksHomePage;%0A
     %7D%0A%7D%0A

introduce annotation based locating of WebElements