package com.softwareonpurpose.uinavigator;

import com.softwareonpurpose.uinavigator.validators.ClassValidator;
import com.softwareonpurpose.uinavigator.validators.StringValidator;
import org.testng.annotations.Test;

@Test
public class UiElementTest extends TestBase {

    @Test
    public void getRootInstance() {
        final Class expected = UiElement.class;
        UiHost.getInstance().load("http://www.google.com");
        final Class actual = UiElement.getInstance("UiElement", UiElement.LocatorType.ID, "viewport").getClass();
        confirm(ClassValidator.getInstance(expected, actual).validate());
    }

    @Test(dependsOnMethods = "getRootInstance")
    public void getChildInstance() {
        Class expected = UiElement.class;
        UiHost.getInstance().load("http://www.google.com");
        UiElement parent = UiElement.getInstance("Parent UiElement", UiElement.LocatorType.ID, "gsr");
        UiElement child = UiElement.getInstance("Child UiElement", UiElement.LocatorType.ID, "viewport", parent);
        Class actual = child.getClass();
        confirm(ClassValidator.getInstance(expected, actual).validate());
    }

    @Test(dependsOnMethods = "getRootInstance")
    public void getChildOrdinalInstance() {
        Class expected = UiElement.class;
        UiHost.getInstance().load("http://www.google.com");
        UiElement parent = UiElement.getInstance("Parent UiElement", UiElement.LocatorType.ID, "gsr");
        UiElement child = UiElement.getInstance("Child UiElement", UiElement.LocatorType.CLASS, "_Gs", 3, parent);
        Class actual = child.getClass();
        confirm(ClassValidator.getInstance(expected, actual).validate());
    }

    @Test(dependsOnMethods = "getRootInstance")
    public void getChildAttributeInstance() {
        Class expected = UiElement.class;
        UiHost.getInstance().load("http://www.google.com");
        UiElement parent = UiElement.getInstance("Parent UiElement", UiElement.LocatorType.ID, "gsr");
        UiElement child = UiElement
                .getInstance("Child UiElement", UiElement.LocatorType.CLASS, "gb_ua", "data-ved", "0CBQQwi4oAA", parent);
        Class actual = child.getClass();
        confirm(ClassValidator.getInstance(expected, actual).validate());
    }

    @Test(dependsOnMethods = "getRootInstance")
    public void getChildOrdinalGetText() {
        String expected = "Settings";
        UiHost.getInstance().load("http://www.google.com");
        UiElement parent = UiElement.getInstance("Parent UiElement", UiElement.LocatorType.ID, "gsr");
        final UiElement child = UiElement.getInstance("Child UiElement", UiElement.LocatorType.CLASS, "_Gs", 3, parent);
        String actual = child.getText();
        confirm(StringValidator.getInstance("Text of child by ordinal", expected, actual).validate());
    }

    @Test(enabled = false) //  element is now hidden on Google.com
    public void getChildAttributeHref() {
        String expected = "https://mail.google.com/mail/?tab=wm";
        UiHost.getInstance().load("http://www.google.com");
        UiElement parent = UiElement.getInstance("Parent UiElement", UiElement.LocatorType.ID, "gsr");
        UiElement child = UiElement
                .getInstance("Child UiElement", UiElement.LocatorType.CLASS, "gb_ua", "data-ved", "0CBQQwi4oAA", parent);
        String actual = child.getHref();
        confirm(StringValidator.getInstance("Text of child by attribute 'href'", expected, actual).validate());
    }

    @Test(enabled = false) //  element is now hidden on Google.com
    public void getChildAttributeTip() {
        String expected = "Google Apps";
        UiHost.getInstance().load("http://www.google.com");
        UiElement parent = UiElement.getInstance("Parent UiElement", UiElement.LocatorType.ID, "gsr");
        UiElement child = UiElement
                .getInstance("Child UiElement", UiElement.LocatorType.CLASS, "gb_pa", "data-ved", "0CBYQvSc", parent);
        String actual = child.getTip();
        confirm(StringValidator.getInstance("Text of child by attribute 'tip'", expected, actual).validate());
    }

    @Test(dependsOnMethods = "getRootInstance")
    public void set() {
        String expected = "Search phrase";
        UiHost.getInstance().load("http://www.google.com");
        final UiElement searchTextbox = UiElement.getInstance("Search Box", UiElement.LocatorType.NAME, "q");
        searchTextbox.set(expected);
        String actual = searchTextbox.getText();
        confirm(StringValidator.getInstance("Searchbox value", expected, actual).validate());
    }

    @Test(dependsOnMethods = "getRootInstance")
    public void click() {
        String expected = null;
        UiHost.getInstance().load("http://www.google.com");
        UiElement parent = UiElement.getInstance("'Images' link", UiElement.LocatorType.CLASS, "gb_Lc");
        UiElement.getInstance("'Images' link", UiElement.LocatorType.CLASS, "gb_ua", 3, parent).click();
        parent = UiElement.getInstance("'Images' link", UiElement.LocatorType.CLASS, "gb_Lc");
        String actual = UiElement.getInstance("'Images' link", UiElement.LocatorType.CLASS, "gb_ua", 3, parent).getText();
        //noinspection ConstantConditions
        confirm(StringValidator.getInstance("Value after click", expected, actual).validate());
    }

    @Test(dependsOnMethods = "getRootInstance")
    public void getFramedElement() {
        String expected = "Log In";
        UiHost.getInstance().load("http://espn.go.com");
        UiElement.getInstance("'Login' link", UiElement.LocatorType.TAG, "a", UiElement.getInstance("'User' region", UiElement.LocatorType.CLASS, "user")).click();
        UiElement frame = UiElement.getInstance("'Login' frame", UiElement.LocatorType.FRAME, "disneyid-iframe");
        String actual = UiElement.getInstance("'Login' button", UiElement.LocatorType.CLASS, "btn", 2, frame).getText();
        confirm(StringValidator.getInstance("Text of framed element", expected, actual).validate());
    }

    @Test(dependsOnMethods = "getRootInstance")
    public void setAttribute() {
        String expected = "Bogus Alt Text";
        UiHost.getInstance().load("http://www.google.com");
        UiElement element = UiElement.getInstance("Logo", UiElement.LocatorType.ID, "hplogo");
        element.setAttribute("alt", expected);
        String actual = element.getAttribute("alt");
        confirm(StringValidator.getInstance("Attribute value", expected, actual).validate());
    }
}

@@ -2692,35 +2692,37 @@
 torType.CLASS, %22
-_Gs
+ctr-p
 %22, 3, parent);%0D%0A
@@ -5416,703 +5416,8 @@
 %0D%0A%0D%0A
-    @Test(dependsOnMethods = %22getRootInstance%22)%0D%0A    public void getFramedElement() %7B%0D%0A        String expected = %22Log In%22;%0D%0A        UiHost.getInstance().load(%22http://espn.go.com%22);%0D%0A        UiElement.getInstance(%22'Login' link%22, UiElement.LocatorType.TAG, %22a%22, UiElement.getInstance(%22'User' region%22, UiElement.LocatorType.CLASS, %22user%22)).click();%0D%0A        UiElement frame = UiElement.getInstance(%22'Login' frame%22, UiElement.LocatorType.FRAME, %22disneyid-iframe%22);%0D%0A        String actual = UiElement.getInstance(%22'Login' button%22, UiElement.LocatorType.CLASS, %22btn%22, 2, frame).getText();%0D%0A        confirm(StringValidator.getInstance(%22Text of framed element%22, expected, actual).validate());%0D%0A    %7D%0D%0A%0D%0A
     

Remove test related to unsupported 'frame' functionality