diff --git a/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/CouldNotVerifyException.java b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/CouldNotVerifyException.java new file mode 100644 index 000000000..3ea040181 --- /dev/null +++ b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/CouldNotVerifyException.java @@ -0,0 +1,12 @@ +package org.jboss.arquillian.graphene.assertions; + +public class CouldNotVerifyException extends RuntimeException { + + public CouldNotVerifyException(){ + super(); + } + + public CouldNotVerifyException(String message){ + super(message); + } +} diff --git a/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/Exists.java b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/Exists.java new file mode 100644 index 000000000..9100b2acb --- /dev/null +++ b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/Exists.java @@ -0,0 +1,17 @@ +package org.jboss.arquillian.graphene.assertions; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; + +public class Exists extends WebElementAssert { + + public Exists(WebElement actual) { + super(actual); + } + + public WebElement find(String locator, WebDriver browser){ + WebElement possible = browser.findElement(By.id(locator)); + return possible; + } +} diff --git a/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/GrapheneVerify.java b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/GrapheneVerify.java new file mode 100644 index 000000000..e5ecab707 --- /dev/null +++ b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/GrapheneVerify.java @@ -0,0 +1,25 @@ +/*package org.jboss.arquillian.graphene.assertions; + +import org.jboss.arquillian.graphene.context.GrapheneContext; +import org.jboss.arquillian.graphene.page.document.Document; +import org.jboss.arquillian.graphene.proxy.GrapheneProxy; +import org.jboss.arquillian.graphene.proxy.GrapheneProxyInstance; +import org.jboss.arquillian.graphene.proxy.Interceptor; +import org.jboss.arquillian.graphene.proxy.InvocationContext; +import org.jboss.arquillian.graphene.request.RequestGuard; +import org.jboss.arquillian.graphene.request.RequestType; +import org.jboss.arquillian.graphene.wait.FluentWait; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; +import org.jboss.arquillian.graphene.guard.RequestGuardFactory; + +import java.io.*; +import java.util.Scanner; +import java.util.concurrent.TimeUnit; + +import org.apache.commons.io.input.ReversedLinesFileReader; + +public class GrapheneVerify extends Actions { +} +*/ diff --git a/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/WebElementAssert.java b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/WebElementAssert.java index e5b5ea772..a04072c22 100644 --- a/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/WebElementAssert.java +++ b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/WebElementAssert.java @@ -1,18 +1,208 @@ package org.jboss.arquillian.graphene.assertions; import org.assertj.core.api.AbstractAssert; +import org.jboss.arquillian.graphene.findby.ByJQuery; +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.ui.Select; import static org.assertj.core.api.Assertions.assertThat; public class WebElementAssert extends AbstractAssert { + private boolean negated; + public WebElementAssert(WebElement actual) { super(actual, WebElementAssert.class); } public WebElementAssert hasText(String expectedText) { - assertThat(this.actual.getText()).isEqualTo(expectedText); + if(this.negated){ + assertThat(this.actual.getText()).as("checking strings are not equal").overridingErrorMessage("The content of the element should not be equal to " + expectedText +", but it is.").isNotEqualTo(expectedText); + this.negated = false; + return this; + } + else { + assertThat(this.actual.getText()).as("checking equality").overridingErrorMessage("The content of the element should be equal to" + expectedText + "but it is not.").isEqualTo(expectedText); + return this; + } + } + + public WebElementAssert hasChild(){ + if (this.negated) { + assertThat(this.actual.findElements(ByJQuery.selector("*"))).asList().size().isEqualTo(0); + this.negated = false; + return this; + } else { + assertThat(this.actual.findElement(ByJQuery.selector("*"))); + return this; + } + } + + public WebElementAssert hasParent(){ + if (this.negated){ + assertThat(this.actual.findElements(By.xpath(".."))).asList().size().isEqualTo(0); + return this; + } + else { + assertThat(this.actual.findElement(By.xpath(".."))); + return this; + } + } + + public WebElementAssert isDisplayed(){ + if(this.negated){ + assertThat(Boolean.valueOf(this.actual.isDisplayed())).as("checking element is not displayed").overridingErrorMessage("element is displayed").isFalse(); + this.negated = false; + return this; + } + else { + assertThat(Boolean.valueOf(this.actual.isDisplayed())).as("checking element is displayed").overridingErrorMessage("element is not displayed").isTrue(); + return this; + } + } + + //doesn't work for dropboxes + public WebElementAssert isSelected(){ + if(this.negated){ + assertThat(Boolean.valueOf(this.actual.isSelected())).as("checking element is not selected").overridingErrorMessage("element is selected").isFalse(); + this.negated = false; + return this; + } + else { + assertThat(Boolean.valueOf(this.actual.isSelected())).as("checking element is selected").overridingErrorMessage("element is not selected").isTrue(); + return this; + } + } + + //support's dropboxes, supply dropbox and it will assert that the webelement is the chosen option + public WebElementAssert isChosenD(Select dropdown){ + if(this.negated){ + assertThat(dropdown.getFirstSelectedOption()).as("checking supplied choice is not chosen").overridingErrorMessage("supplied choice is chosen").isNotEqualTo(this.actual); + this.negated = false; + return this; + } + else { + assertThat(dropdown.getFirstSelectedOption()).as("checking supplied choice is chosen").overridingErrorMessage("supplied choice is not chosen").isEqualTo(this.actual); + return this; + } + } + + + public WebElementAssert containsText(String expectedText){ + String text = this.actual.getAttribute("value"); + if(this.negated){ + assertThat(text).as("checking text value is not the same as the supplied string").overridingErrorMessage("The element contains " + expectedText +", the supplied string.").isNotEqualTo(expectedText); + this.negated = false; + return this; + } + else { + assertThat(text).as("checking text value is the same as the supplied string").overridingErrorMessage("The element does not contain " + expectedText + ", it contains some other string.").isEqualTo(expectedText); + return this; + } + } + + public WebElementAssert isEmpty(){ + String content = this.actual.getAttribute("value"); + if(this.negated){ + assertThat(content).as("checking text input is not empty").overridingErrorMessage("is actually empty").isNotEqualTo(""); + this.negated = false; + return this; + } + else { + assertThat(content).as("checking text input is empty").overridingErrorMessage("is not empty").isEqualTo(""); + return this; + } + } + + public WebElementAssert isTypeOf(String expectedType){ + if(this.negated){ + assertThat(this.actual.getAttribute("type")).as("checking not supplied type").overridingErrorMessage("The element contains" + expectedType + ", the supplied type.").isNotEqualTo(expectedType); + this.negated = false; + return this; + } + else { + assertThat(this.actual.getAttribute("type")).as("checking is supplied type").overridingErrorMessage("The element does not contain" + expectedType + ", it is some other type.").isEqualTo(expectedType); + return this; + } + } + + public WebElementAssert hasCssClass(String expectedClass){ + if(this.negated){ + assertThat(this.actual.getAttribute("class")).as("checking css class does not match supplied css class").overridingErrorMessage("The element has " + expectedClass + ", the supplied css class.").isNotEqualTo(expectedClass); + this.negated = false; + return this; + } + else { + assertThat(this.actual.getAttribute("class")).as("checking css class matches supplied class").overridingErrorMessage("The element has not the " + expectedClass + ", it contains another css class.").isEqualTo(expectedClass); + return this; + } + } + + public WebElementAssert isFocused(WebDriver browser){ + if(this.negated){ + assertThat(this.actual).as("checking element is not in focus").overridingErrorMessage("element is in focus").isNotEqualTo(browser.switchTo().activeElement()); + this.negated = false; + return this; + } + else { + assertThat(this.actual).as("checking element is in focus").overridingErrorMessage("element is not in focus").isEqualTo(browser.switchTo().activeElement()); + return this; + } + } + + public WebElementAssert isEnabled(){ + if(this.negated){ + assertThat(Boolean.valueOf(this.actual.isEnabled())).as("checking element is not enabled").overridingErrorMessage("element is enabled").isFalse(); + this.negated = false; + return this; + } + else { + assertThat(Boolean.valueOf(this.actual.isEnabled())).as("checking element is enabled").overridingErrorMessage("element is not enabled").isTrue(); + return this; + } + } + + public WebElementAssert textMatchesRegex(String regex){ + if(this.negated){ + assertThat(Boolean.valueOf(this.actual.getText().matches(regex))).as("checking regex does not match text").overridingErrorMessage("The regex " + regex + " matches the text.").isFalse(); + this.negated = false; + return this; + } + else { + assertThat(Boolean.valueOf(this.actual.getText().matches(regex))).as("checking regex matches text").overridingErrorMessage("The regex " + regex + " does not match text.").isTrue(); + return this; + } + } + + public WebElementAssert caseInsensitiveMatching(String match) { + if (this.negated) { + assertThat(this.actual.getText()).as("checking string does not matches text, regardless of capitalisaton").overridingErrorMessage("The string, " + match + " is the same as the text.").isNotEqualTo(match.toLowerCase()); + this.negated = false; + return this; + } + else { + assertThat(this.actual.getText()).as("checking string matches text, regardless of capitalisaton").overridingErrorMessage("The string, " + match + " differs to the text.").isEqualTo(match.toLowerCase()); + return this; + } + } + + public WebElementAssert subStringMatching(String match){ + if(this.negated){ + assertThat(Boolean.valueOf(this.actual.getText().contains(match))).as("checking if substring is not in text").overridingErrorMessage("The substring " + match + " is in the text.").isFalse(); + this.negated = false; + return this; + } + else { + assertThat(Boolean.valueOf(this.actual.getText().contains(match))).as("checking if substring is in text").overridingErrorMessage("The substring " + match + " is not in the text.").isTrue(); + return this; + } + } + + public WebElementAssert isNot(){ + this.negated = true; return this; } -} + +} \ No newline at end of file diff --git a/ftest/src/test/java/org/jboss/arquillian/graphene/ftest/element/EventHandler.java b/ftest/src/test/java/org/jboss/arquillian/graphene/ftest/element/EventHandler.java new file mode 100644 index 000000000..aa4b978c7 --- /dev/null +++ b/ftest/src/test/java/org/jboss/arquillian/graphene/ftest/element/EventHandler.java @@ -0,0 +1,33 @@ +/*package org.jboss.arquillian.graphene.ftest.element; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.events.WebDriverEventListener; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; + +import org.jboss.arquillian.graphene.guard.RequestGuardFactory; + +public class EventHandler implements WebDriverEventListener { + + public void beforeClick(WebElement element, WebDriver browser){ + guard(); + } + + public void afterClick(WebDriver browser, WebElement element){ + try { + PrintWriter writer = new PrintWriter("log.txt", "UTF-8"); + writer.println("after click " + element); + writer.close(); + } + catch (FileNotFoundException, UnsupportedEncodingException e){ + System.out.println("logs not here"); + } + } + + public void +}*/ diff --git a/ftest/src/test/java/org/jboss/arquillian/graphene/ftest/element/TestGrapheneAssertions.java b/ftest/src/test/java/org/jboss/arquillian/graphene/ftest/element/TestGrapheneAssertions.java index 356872965..8cd589504 100644 --- a/ftest/src/test/java/org/jboss/arquillian/graphene/ftest/element/TestGrapheneAssertions.java +++ b/ftest/src/test/java/org/jboss/arquillian/graphene/ftest/element/TestGrapheneAssertions.java @@ -33,9 +33,12 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.ui.Select; import static org.jboss.arquillian.graphene.assertions.GrapheneAssert.assertThat; @@ -55,6 +58,48 @@ public class TestGrapheneAssertions { @FindBy(id = "pseudoroot") private WebElement div; + @FindBy(id = "root") + private WebElement divHead; + + @FindBy(id="option two") + private WebElement secondOption; + + @FindBy(id="form") + private WebElement inputForm; + + @FindBy(id="unseen") + private WebElement invisible; + + @FindBy(id="invisible") + private WebElement aboveInvisible; + + @FindBy(id="select") + private WebElement selected; + + @FindBy(id="inactive") + private WebElement inactiveInputForm; + + @FindBy(id="disabled button") + private WebElement disabledButton; + + @FindBy(id = "java1") + private WebElement javaRadioButton; + + @FindBy(id = "php1") + private WebElement phpRadioButton; + + @FindBy(id = "seleniumbox") + private WebElement seleniumCheckBox; + + @FindBy(id = "restapibox") + private WebElement restApiCheckBox; + + @FindBy(id ="kid") + private WebElement testChild; + + @FindBy(id ="noParent") + private WebElement title; + @Deployment public static WebArchive createTestArchive() { return Resources.inPackage(SAMPLE_PACKAGE).all().buildWar("test.war"); @@ -67,7 +112,110 @@ public void loadPage() { @Test public void should_be_able_to_assert_text_on_web_element() { - assertThat(div).hasText("pseudo root"); + assertThat(testChild).hasText("test kid"); + } + + @Test + public void should_confirm_element_has_child_web_element(){ + assertThat(divHead).hasChild();} + + @Test + public void should_confirm_element_has_parent_web_element(){ + assertThat(title).hasParent();} + + @Test + public void should_confirm_that_web_element_is_displayed_on_page(){ + assertThat(div).isDisplayed();} + + @Test + public void should_confirm_that_dropdown_element_is_Chosen(){ + Select dropdown = new Select(browser.findElement(By.id("select"))); + dropdown.selectByVisibleText("option two"); + assertThat(secondOption).isChosenD(dropdown); + } + + @Test + public void should_assert_text_in_input_form(){ + inputForm.sendKeys("should assert correct"); + assertThat(inputForm).containsText("should assert correct"); + } + + @Test + public void should_assert_input_is_empty(){ + inputForm.sendKeys("not empty"); + assertThat(inputForm).isNot().isEmpty(); + } + + @Test + public void should_assert_type_attribute_of_WebElement(){ + assertThat(inputForm).isTypeOf("text"); + } + + @Test + public void should_assert_css_class_of_element(){ + assertThat(aboveInvisible).hasCssClass("opague"); + } + + @Test + public void confirm_the_element_is_in_focus(){ + new Actions(browser).moveToElement(inputForm).click().perform(); + assertThat(inputForm).isFocused(browser); } + @Test + public void confirm_the_element_is_enabled(){ + System.out.println(disabledButton.isEnabled()); + assertThat(disabledButton.isEnabled()); + } + + @Test + public void confirm_element_text_matches_regex(){ + System.out.println(invisible.getText().matches("([A-Z])\\w+")); + assertThat(invisible).isNot().textMatchesRegex("([A-Z])\\w+"); + } + + + @Test + public void asserts_that_the_correct_radio_button_is_chosen(){ + new Actions(browser).moveToElement(phpRadioButton).click(); + assertThat(phpRadioButton).isDisplayed(); + } + + + @Test + public void should_assert_the_correct_checkbox_is_chosen(){ + new Actions(browser).moveToElement(restApiCheckBox).click(); + assertThat(restApiCheckBox).isSelected(); + + } + + @Test + public void should_assert_the_element_has_no_children(){ + assertThat(testChild).isNot().hasChild(); + } + + @Test + public void should_check_error_messages_are_working_as_expected(){ + assertThat(inactiveInputForm).isNot().isEnabled(); + } + + /*@Test + public void should_confirm_if_exists_method_works(){ + find("pseudoroot", browser); + }*/ + + /*@Test + public void should_confirm_web_interactions_work_as_expected(){ + new Actions(browser).moveToElement(inputForm).click().perform().verify();//possible incarnation + }*/ + + @Test + public void should_confirm_case_sensitive_matching_works_as_expected(){ + assertThat(div).subStringMatching("seudo"); + } + + @Test + public void should_confirm_that_case_insensitive_matching_works_as_expected(){ + assertThat(testChild).caseInsensitiveMatching("TEST KID"); + } } \ No newline at end of file diff --git a/ftest/src/test/resources/org/jboss/arquillian/graphene/ftest/assertions/sample.html b/ftest/src/test/resources/org/jboss/arquillian/graphene/ftest/assertions/sample.html index a189151df..5d74fc9cd 100644 --- a/ftest/src/test/resources/org/jboss/arquillian/graphene/ftest/assertions/sample.html +++ b/ftest/src/test/resources/org/jboss/arquillian/graphene/ftest/assertions/sample.html @@ -22,23 +22,59 @@ --> - + - Sample Page + Sample Page +
+ +
not correct
correct -
pseudo root
+
pseudo root +

test kid

+
+ text input +
+ disabled input +
+ + + +
Check Boxes & Radio Buttons
+

+ + + + + + + +
Selenium
+ Rest Api  + +
+ Java               + PHP + +

+ +

+

+ +

+
+ \ No newline at end of file