From 3eaf6986319b1def27167f401aa68bb013781875 Mon Sep 17 00:00:00 2001 From: mookt Date: Tue, 11 Jul 2017 17:09:54 +0100 Subject: [PATCH 01/12] a few minor changes in relation to comments --- .../graphene/assertions/WebElementAssert.java | 40 +++++++++++++ .../ftest/element/TestGrapheneAssertions.java | 60 +++++++++++++++++++ .../graphene/ftest/assertions/sample.html | 10 +++- 3 files changed, 108 insertions(+), 2 deletions(-) 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..53310df83 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,6 +1,8 @@ package org.jboss.arquillian.graphene.assertions; +import org.apache.commons.lang3.BooleanUtils; import org.assertj.core.api.AbstractAssert; +import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import static org.assertj.core.api.Assertions.assertThat; @@ -15,4 +17,42 @@ public WebElementAssert hasText(String expectedText) { assertThat(this.actual.getText()).isEqualTo(expectedText); return this; } + + + public WebElementAssert hasChild(){ + assertThat(this.actual.findElement(By.cssSelector("*"))); + return this; + } + + public WebElementAssert hasParent(){ + assertThat(this.actual.findElement(By.xpath(".."))); + return this; + } + + public WebElementAssert isVisible(){ + assertThat(this.actual.isDisplayed()); + return this; + } + + public WebElementAssert isChosen(){ + assertThat(this.actual.isSelected()); + return this; + } + + public WebElementAssert containsValue(String expectedText){ + String text = this.actual.getAttribute("value"); + assertThat(text).isEqualTo(expectedText); + return this; + } + + public WebElementAssert isEmpty(){ + String content = this.actual.getAttribute("value"); + assertThat(content).isEqualTo(""); + return this; + } + + public WebElementAssert isNotVisible(){ + assertThat(BooleanUtils.isFalse(this.actual.isDisplayed())); + return this; + } } 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..10236dd1a 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 @@ -22,6 +22,7 @@ package org.jboss.arquillian.graphene.ftest.element; import java.net.URL; + import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.RunAsClient; import org.jboss.arquillian.drone.api.annotation.Drone; @@ -33,9 +34,11 @@ 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.support.FindBy; +import org.openqa.selenium.support.ui.Select; import static org.jboss.arquillian.graphene.assertions.GrapheneAssert.assertThat; @@ -55,6 +58,21 @@ public class TestGrapheneAssertions { @FindBy(id = "pseudoroot") private WebElement div; + @FindBy(id = "root") + private WebElement divHead; + + @FindBy(id="option two") + private WebElement selection; + + @FindBy(id="form") + private WebElement input_form; + + @FindBy(id="unseen") + private WebElement invisible; + + @FindBy(id="invisible") + private WebElement top; + @Deployment public static WebArchive createTestArchive() { return Resources.inPackage(SAMPLE_PACKAGE).all().buildWar("test.war"); @@ -70,4 +88,46 @@ public void should_be_able_to_assert_text_on_web_element() { assertThat(div).hasText("pseudo root"); } + @Test + public void should_confirm_element_has_child_web_element(){ + assertThat(divHead).hasChild();} + + @Test + public void should_confirm_element_has_parent_web_element(){ + assertThat(div).hasParent();} + + @Test + public void should_confirm_that_web_element_is_displayed_on_page(){ + assertThat(div).isVisible();} + + @Test + public void should_confirm_that_element_is_selected(){ + Select dropdown = new Select(browser.findElement(By.id("select"))); + dropdown.selectByVisibleText("option one"); + assertThat(selection).isChosen(); + } + + @Test + public void should_assert_text_in_input_form(){ + input_form.sendKeys("should assert correct"); + assertThat(input_form).containsValue("should assert correct"); + } + + @Test + public void should_assert_input_is_empty(){ + assertThat(input_form).isEmpty(); + } + + @Test + public void combining_assertions(){ + assertThat(div).hasText("pseudo root").isVisible(); + } + + @Test + public void should_assert_item_is_not_visible(){ + assertThat(invisible).isNotVisible(); + } + + + } \ 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..b71b2a499 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,11 +22,14 @@ --> - + Sample Page +
+ +
not correct
correct @@ -36,9 +39,12 @@ + text input + + \ No newline at end of file From ca1721e44f14b46bbf94c991d352570f3fa68584 Mon Sep 17 00:00:00 2001 From: mookt Date: Fri, 14 Jul 2017 18:40:17 +0100 Subject: [PATCH 02/12] added a few more assertions and tried updating parent/child assertions --- .../graphene/assertions/WebElementAssert.java | 46 ++++++++++++-- .../ftest/element/TestGrapheneAssertions.java | 63 +++++++++++++++---- .../graphene/ftest/assertions/sample.html | 4 +- 3 files changed, 95 insertions(+), 18 deletions(-) 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 53310df83..61fa1725e 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 @@ -3,7 +3,9 @@ import org.apache.commons.lang3.BooleanUtils; import org.assertj.core.api.AbstractAssert; import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; +import org.openqa.selenium.JavascriptExecutor; import static org.assertj.core.api.Assertions.assertThat; @@ -18,11 +20,12 @@ public WebElementAssert hasText(String expectedText) { return this; } - - public WebElementAssert hasChild(){ - assertThat(this.actual.findElement(By.cssSelector("*"))); +//returning webDriverException: Can't find variable $, and the same message even if replaced with jQuery + /*public WebElementAssert hasChild(WebDriver browser){ + String jQuerySelector = "arguments[0]"; + assertThat(((JavascriptExecutor) browser).executeScript("return jQuery(arguments[0]).children();", this.actual)); return this; - } + }*/ public WebElementAssert hasParent(){ assertThat(this.actual.findElement(By.xpath(".."))); @@ -55,4 +58,39 @@ public WebElementAssert isNotVisible(){ assertThat(BooleanUtils.isFalse(this.actual.isDisplayed())); return this; } + + public WebElementAssert addErrorString(String error) { + assertThat(this.actual).withFailMessage(error); + return this; + } + + public WebElementAssert typeIs(String expectedType){ + assertThat(this.actual.getAttribute("type")).isEqualTo(expectedType); + return this; + } + + public WebElementAssert hasCssClass(String expectedClass){ + assertThat(this.actual.getAttribute("class")).isEqualTo(expectedClass); + return this; + } + + public WebElementAssert isFocused(WebDriver browser){ + assertThat(this.actual).isEqualTo(browser.switchTo().activeElement()); + return this; + } + + public WebElementAssert isEnabled(){ + assertThat(this.actual.isEnabled()); + return this; + } + + public WebElementAssert isntEnabled(){ + assertThat(BooleanUtils.isFalse(this.actual.isEnabled())); + return this; + } + + public WebElementAssert textMatchesRegex(String regex){ + assertThat(this.actual.getText().matches(regex)); + return this; + } } 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 10236dd1a..95d20c718 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 @@ -37,9 +37,11 @@ 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.assertj.core.api.Assertions.setRemoveAssertJRelatedElementsFromStackTrace; import static org.jboss.arquillian.graphene.assertions.GrapheneAssert.assertThat; @RunWith(Arquillian.class) @@ -62,16 +64,22 @@ public class TestGrapheneAssertions { private WebElement divHead; @FindBy(id="option two") - private WebElement selection; + private WebElement secondOption; @FindBy(id="form") - private WebElement input_form; + private WebElement inputForm; @FindBy(id="unseen") private WebElement invisible; @FindBy(id="invisible") - private WebElement top; + private WebElement aboveInvisible; + + @FindBy(id="select") + private WebElement selected; + + @FindBy(id="inactive") + private WebElement inactiveInputForm; @Deployment public static WebArchive createTestArchive() { @@ -88,10 +96,10 @@ public void should_be_able_to_assert_text_on_web_element() { assertThat(div).hasText("pseudo root"); } - @Test + /*@Test public void should_confirm_element_has_child_web_element(){ - assertThat(divHead).hasChild();} - + assertThat(divHead).hasChild(browser);} +*/ @Test public void should_confirm_element_has_parent_web_element(){ assertThat(div).hasParent();} @@ -104,30 +112,59 @@ public void should_confirm_that_web_element_is_displayed_on_page(){ public void should_confirm_that_element_is_selected(){ Select dropdown = new Select(browser.findElement(By.id("select"))); dropdown.selectByVisibleText("option one"); - assertThat(selection).isChosen(); + assertThat(secondOption).isChosen(); } @Test public void should_assert_text_in_input_form(){ - input_form.sendKeys("should assert correct"); - assertThat(input_form).containsValue("should assert correct"); + inputForm.sendKeys("should assert correct"); + assertThat(inputForm).containsValue("should assert correct"); } @Test public void should_assert_input_is_empty(){ - assertThat(input_form).isEmpty(); + assertThat(inputForm).isEmpty(); } - @Test + //not working as expected, not haveing any effect on error message + /*@Test public void combining_assertions(){ - assertThat(div).hasText("pseudo root").isVisible(); - } + assertThat(invisible).hasText("unseen element").isVisible().withFailMessage("Expected <%s> to be visible but was <%s>", invisible, assertThat(invisible).isVisible()); + }*/ @Test public void should_assert_item_is_not_visible(){ assertThat(invisible).isNotVisible(); } + @Test + public void should_assert_type_attribute_of_WebElement(){ + assertThat(inputForm).typeIs("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(selected).click().perform(); + assertThat(selected).isFocused(browser); + } + + @Test + public void confirm_the_element_is_enabled(){ + assertThat(inputForm).typeIs("text").isVisible().isEnabled(); + } + + @Test + public void confirm_the_element_isnt_enabled(){ + assertThat(inactiveInputForm).isntEnabled(); + } + + @Test + public void confirm_element_text_matches_regex(){ + assertThat(invisible).textMatchesRegex("([a-z])\\w+"); + } } \ 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 b71b2a499..a3461dfec 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 @@ -27,7 +27,7 @@ Sample Page -
+
not correct @@ -45,6 +45,8 @@ text input + disabled input + \ No newline at end of file From 9d43def78f83f2a6344373df269adb1dca0162bd Mon Sep 17 00:00:00 2001 From: mookt Date: Tue, 18 Jul 2017 16:40:58 +0100 Subject: [PATCH 03/12] some bug fixes --- .../graphene/assertions/WebElementAssert.java | 44 +++++++--- .../ftest/element/TestGrapheneAssertions.java | 84 ++++++++++++------- .../graphene/ftest/assertions/sample.html | 30 ++++++- 3 files changed, 115 insertions(+), 43 deletions(-) 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 61fa1725e..f93a80df2 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 @@ -2,10 +2,12 @@ import org.apache.commons.lang3.BooleanUtils; import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.JUnitSoftAssertions; +import org.junit.Rule; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; -import org.openqa.selenium.JavascriptExecutor; +import org.openqa.selenium.support.ui.Select; import static org.assertj.core.api.Assertions.assertThat; @@ -27,22 +29,31 @@ public WebElementAssert hasText(String expectedText) { return this; }*/ - public WebElementAssert hasParent(){ + /*public WebElementAssert hasParent(){ assertThat(this.actual.findElement(By.xpath(".."))); return this; - } + }*/ + public WebElementAssert isVisible(){ - assertThat(this.actual.isDisplayed()); + assertThat(this.actual.isDisplayed()).isTrue(); return this; } + //doesn't work for dropboxes public WebElementAssert isChosen(){ - assertThat(this.actual.isSelected()); + assertThat(this.actual.isSelected()).isTrue(); return this; } - public WebElementAssert containsValue(String expectedText){ + //support's dropboxes, supply dropbox and text of chosen option to assert it's chosen + public WebElementAssert isChosenD(Select dropdown){ + assertThat(dropdown.getFirstSelectedOption()).isEqualTo(this.actual); + return this; + } + + + public WebElementAssert containsText(String expectedText){ String text = this.actual.getAttribute("value"); assertThat(text).isEqualTo(expectedText); return this; @@ -55,7 +66,7 @@ public WebElementAssert isEmpty(){ } public WebElementAssert isNotVisible(){ - assertThat(BooleanUtils.isFalse(this.actual.isDisplayed())); + assertThat(Boolean.valueOf(this.actual.isDisplayed())); return this; } @@ -64,7 +75,7 @@ public WebElementAssert addErrorString(String error) { return this; } - public WebElementAssert typeIs(String expectedType){ + public WebElementAssert isTypeOf(String expectedType){ assertThat(this.actual.getAttribute("type")).isEqualTo(expectedType); return this; } @@ -80,17 +91,28 @@ public WebElementAssert isFocused(WebDriver browser){ } public WebElementAssert isEnabled(){ - assertThat(this.actual.isEnabled()); + assertThat(this.actual.isEnabled()).isTrue(); return this; } public WebElementAssert isntEnabled(){ - assertThat(BooleanUtils.isFalse(this.actual.isEnabled())); + assertThat(Boolean.valueOf(this.actual.isEnabled())); return this; } public WebElementAssert textMatchesRegex(String regex){ - assertThat(this.actual.getText().matches(regex)); + assertThat(this.actual.getText().matches(regex)).isTrue(); return this; } + + /*public WebElementAssert softly(){ + JUnitSoftAssertions softly = new JUnitSoftAssertions(); + softly.assertThat(this.actual); + return this; + }*/ + + /*public WebElementAssert isNot(){ + assertThat(this.actual); + return this; + }*/ } 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 95d20c718..147a49790 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 @@ -22,7 +22,6 @@ package org.jboss.arquillian.graphene.ftest.element; import java.net.URL; - import org.jboss.arquillian.container.test.api.Deployment; import org.jboss.arquillian.container.test.api.RunAsClient; import org.jboss.arquillian.drone.api.annotation.Drone; @@ -41,7 +40,6 @@ import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.ui.Select; -import static org.assertj.core.api.Assertions.setRemoveAssertJRelatedElementsFromStackTrace; import static org.jboss.arquillian.graphene.assertions.GrapheneAssert.assertThat; @RunWith(Arquillian.class) @@ -81,6 +79,21 @@ public class TestGrapheneAssertions { @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; + @Deployment public static WebArchive createTestArchive() { return Resources.inPackage(SAMPLE_PACKAGE).all().buildWar("test.war"); @@ -98,27 +111,27 @@ public void should_be_able_to_assert_text_on_web_element() { /*@Test public void should_confirm_element_has_child_web_element(){ - assertThat(divHead).hasChild(browser);} -*/ - @Test + assertThat(divHead).hasChild(browser);}*/ + + /*@Test public void should_confirm_element_has_parent_web_element(){ - assertThat(div).hasParent();} + assertThat(div).hasParent();}*/ @Test public void should_confirm_that_web_element_is_displayed_on_page(){ - assertThat(div).isVisible();} + assertThat(invisible).isVisible();} @Test - public void should_confirm_that_element_is_selected(){ + public void should_confirm_that_dropdown_element_is_Chosen(){ Select dropdown = new Select(browser.findElement(By.id("select"))); - dropdown.selectByVisibleText("option one"); - assertThat(secondOption).isChosen(); + dropdown.selectByVisibleText("option two"); + assertThat(secondOption).isChosenD(dropdown); } @Test public void should_assert_text_in_input_form(){ inputForm.sendKeys("should assert correct"); - assertThat(inputForm).containsValue("should assert correct"); + assertThat(inputForm).containsText("should assert correct"); } @Test @@ -126,20 +139,9 @@ public void should_assert_input_is_empty(){ assertThat(inputForm).isEmpty(); } - //not working as expected, not haveing any effect on error message - /*@Test - public void combining_assertions(){ - assertThat(invisible).hasText("unseen element").isVisible().withFailMessage("Expected <%s> to be visible but was <%s>", invisible, assertThat(invisible).isVisible()); - }*/ - - @Test - public void should_assert_item_is_not_visible(){ - assertThat(invisible).isNotVisible(); - } - @Test public void should_assert_type_attribute_of_WebElement(){ - assertThat(inputForm).typeIs("text"); + assertThat(inputForm).isTypeOf("text"); } @Test @@ -149,22 +151,44 @@ public void should_assert_css_class_of_element(){ @Test public void confirm_the_element_is_in_focus(){ - new Actions(browser).moveToElement(selected).click().perform(); - assertThat(selected).isFocused(browser); + new Actions(browser).moveToElement(inputForm).click().perform(); + assertThat(inputForm).isFocused(browser); } @Test public void confirm_the_element_is_enabled(){ - assertThat(inputForm).typeIs("text").isVisible().isEnabled(); + System.out.println(disabledButton.isEnabled()); + assertThat(disabledButton.isEnabled()); } @Test - public void confirm_the_element_isnt_enabled(){ - assertThat(inactiveInputForm).isntEnabled(); + public void confirm_element_text_matches_regex(){ + System.out.println(invisible.getText().matches("([A-Z])\\w+")); + assertThat(invisible).textMatchesRegex("([A-Z])\\w+"); } + /*@Test + public void softly_assert_conditions(){ + (assertThat(invisible).hasText("unseen")).softly(); + (assertThat(inputForm).typeIs("int")).softly(); + (assertThat(inactiveInputForm).isntEnabled()).softly(); + + }*/ + + @Test - public void confirm_element_text_matches_regex(){ - assertThat(invisible).textMatchesRegex("([a-z])\\w+"); + public void asserts_that_the_correct_radio_button_is_chosen(){ + new Actions(browser).moveToElement(phpRadioButton).click(); + assertThat(phpRadioButton).isChosen(); } + + + @Test + public void should_assert_the_correct_checkbox_is_chosen(){ + new Actions(browser).moveToElement(restApiCheckBox).click(); + assertThat(restApiCheckBox).isChosen(); + + } + + } \ 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 a3461dfec..4d9302ad8 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 @@ -28,7 +28,7 @@
- +
not correct
@@ -44,9 +44,35 @@ text input +
+ disabled input +
- disabled input + +
Check Boxes & Radio Buttons
+

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

+ +

+

+ +

+
\ No newline at end of file From ffe4a887a5407e626594fb63c8a0ce3a50f18467 Mon Sep 17 00:00:00 2001 From: mookt Date: Thu, 27 Jul 2017 18:52:25 +0100 Subject: [PATCH 04/12] negation, error messages and some further testing --- .../graphene/assertions/WebElementAssert.java | 181 ++++++++++++------ .../ftest/element/TestGrapheneAssertions.java | 35 ++-- 2 files changed, 138 insertions(+), 78 deletions(-) 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 f93a80df2..2a3d605c9 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 @@ -2,9 +2,9 @@ import org.apache.commons.lang3.BooleanUtils; import org.assertj.core.api.AbstractAssert; -import org.assertj.core.api.JUnitSoftAssertions; -import org.junit.Rule; +import org.jboss.arquillian.graphene.findby.ByJQuery; import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.Select; @@ -13,106 +13,169 @@ 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); - return this; + if(this.negated){ + assertThat(this.actual.getText()).as("checking strings are not equal").overridingErrorMessage("contains expected string").isNotEqualTo(expectedText); + this.negated = false; + return this; + } + else { + assertThat(this.actual.getText()).as("checking equality").overridingErrorMessage("contains other string").isEqualTo(expectedText); + return this; + } } -//returning webDriverException: Can't find variable $, and the same message even if replaced with jQuery - /*public WebElementAssert hasChild(WebDriver browser){ - String jQuerySelector = "arguments[0]"; - assertThat(((JavascriptExecutor) browser).executeScript("return jQuery(arguments[0]).children();", this.actual)); - 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(){ - assertThat(this.actual.findElement(By.xpath(".."))); + //work in progress! + public WebElementAssert hasParent(){ + assertThat(this.actual.findElement(ByJQuery.selector(""))); return this; - }*/ + } - public WebElementAssert isVisible(){ - assertThat(this.actual.isDisplayed()).isTrue(); - return this; + public WebElementAssert isDisplayed(){ + if(this.negated){ + assertThat(this.actual.isDisplayed()).as("checking element is not displayed").overridingErrorMessage("element is displayed").isFalse(); + this.negated = false; + return this; + } + else { + assertThat(this.actual.isDisplayed()).as("checking element is displayed").overridingErrorMessage("element is not displayed").isTrue(); + return this; + } } //doesn't work for dropboxes - public WebElementAssert isChosen(){ - assertThat(this.actual.isSelected()).isTrue(); - return this; + public WebElementAssert isSelected(){ + if(this.negated){ + assertThat(this.actual.isSelected()).as("checking element is not selected").overridingErrorMessage("element is selected").isFalse(); + this.negated = false; + return this; + } + else { + assertThat(this.actual.isSelected()).as("checking element is selected").overridingErrorMessage("element is not selected").isTrue(); + return this; + } } //support's dropboxes, supply dropbox and text of chosen option to assert it's chosen public WebElementAssert isChosenD(Select dropdown){ - assertThat(dropdown.getFirstSelectedOption()).isEqualTo(this.actual); - return this; + 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"); - assertThat(text).isEqualTo(expectedText); - return this; + if(this.negated){ + assertThat(text).as("checking text value is not the same as the supplied string").overridingErrorMessage("contains 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("contains other string").isEqualTo(expectedText); + return this; + } } public WebElementAssert isEmpty(){ String content = this.actual.getAttribute("value"); - assertThat(content).isEqualTo(""); - return this; - } - - public WebElementAssert isNotVisible(){ - assertThat(Boolean.valueOf(this.actual.isDisplayed())); - return this; - } - - public WebElementAssert addErrorString(String error) { - assertThat(this.actual).withFailMessage(error); - return this; + 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){ - assertThat(this.actual.getAttribute("type")).isEqualTo(expectedType); - return this; + if(this.negated){ + assertThat(this.actual.getAttribute("type")).as("checking not supplied type").overridingErrorMessage("contains supplied type").isNotEqualTo(expectedType); + this.negated = false; + return this; + } + else { + assertThat(this.actual.getAttribute("type")).as("checking is supplied type").overridingErrorMessage("contains other type").isEqualTo(expectedType); + return this; + } } public WebElementAssert hasCssClass(String expectedClass){ - assertThat(this.actual.getAttribute("class")).isEqualTo(expectedClass); - return this; + if(this.negated){ + assertThat(this.actual.getAttribute("class")).as("checking css class does not match supplied css class").overridingErrorMessage("is supplied css class").isNotEqualTo(expectedClass); + this.negated = false; + return this; + } + else { + assertThat(this.actual.getAttribute("class")).as("checking css class matches supplied class").overridingErrorMessage("is not supplied css class").isEqualTo(expectedClass); + return this; + } } public WebElementAssert isFocused(WebDriver browser){ - assertThat(this.actual).isEqualTo(browser.switchTo().activeElement()); - return this; + 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(){ - assertThat(this.actual.isEnabled()).isTrue(); - return this; - } - - public WebElementAssert isntEnabled(){ - assertThat(Boolean.valueOf(this.actual.isEnabled())); - return this; + if(this.negated){ + assertThat(this.actual.isEnabled()).as("checking element is not enabled").overridingErrorMessage("element is enabled").isFalse(); + this.negated = false; + return this; + } + else { + assertThat(this.actual.isEnabled()).as("checking element is enabled").overridingErrorMessage("element is not enabled").isTrue(); + return this; + } } public WebElementAssert textMatchesRegex(String regex){ - assertThat(this.actual.getText().matches(regex)).isTrue(); - return this; + if(this.negated){ + assertThat(this.actual.getText().matches(regex)).as("checking regex does not match text").overridingErrorMessage("regex matches text").isFalse(); + this.negated = false; + return this; + } + else { + assertThat(this.actual.getText().matches(regex)).as("checking regex matches text").overridingErrorMessage("regex does not match text").isTrue(); + return this; + } } - /*public WebElementAssert softly(){ - JUnitSoftAssertions softly = new JUnitSoftAssertions(); - softly.assertThat(this.actual); + public WebElementAssert isNot(){ + this.negated = true; return this; - }*/ - - /*public WebElementAssert isNot(){ - assertThat(this.actual); - return this; - }*/ + } } 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 147a49790..cc625d415 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 @@ -109,17 +109,17 @@ public void should_be_able_to_assert_text_on_web_element() { assertThat(div).hasText("pseudo root"); } - /*@Test + @Test public void should_confirm_element_has_child_web_element(){ - assertThat(divHead).hasChild(browser);}*/ + assertThat(div).hasChild();} - /*@Test + @Test public void should_confirm_element_has_parent_web_element(){ - assertThat(div).hasParent();}*/ + assertThat(div).hasParent();} @Test public void should_confirm_that_web_element_is_displayed_on_page(){ - assertThat(invisible).isVisible();} + assertThat(div).isNot().isDisplayed();} @Test public void should_confirm_that_dropdown_element_is_Chosen(){ @@ -136,7 +136,8 @@ public void should_assert_text_in_input_form(){ @Test public void should_assert_input_is_empty(){ - assertThat(inputForm).isEmpty(); + inputForm.sendKeys("not empty"); + assertThat(inputForm).isNot().isEmpty(); } @Test @@ -164,31 +165,27 @@ public void confirm_the_element_is_enabled(){ @Test public void confirm_element_text_matches_regex(){ System.out.println(invisible.getText().matches("([A-Z])\\w+")); - assertThat(invisible).textMatchesRegex("([A-Z])\\w+"); + assertThat(invisible).isNot().textMatchesRegex("([A-Z])\\w+"); } - /*@Test - public void softly_assert_conditions(){ - (assertThat(invisible).hasText("unseen")).softly(); - (assertThat(inputForm).typeIs("int")).softly(); - (assertThat(inactiveInputForm).isntEnabled()).softly(); - - }*/ - - @Test + /*@Test public void asserts_that_the_correct_radio_button_is_chosen(){ new Actions(browser).moveToElement(phpRadioButton).click(); - assertThat(phpRadioButton).isChosen(); - } + assertThat(phpRadioButton).isDisplayed(); + }*/ @Test public void should_assert_the_correct_checkbox_is_chosen(){ new Actions(browser).moveToElement(restApiCheckBox).click(); - assertThat(restApiCheckBox).isChosen(); + assertThat(restApiCheckBox).isSelected(); } + /*@Test + public void should_assert_the_element_has_no_children(){ + assertThat(div).isNot().hasChild(); + }*/ } \ No newline at end of file From 129e5a3d303d8ebf7035e95fba4e762c91d91257 Mon Sep 17 00:00:00 2001 From: mookt Date: Tue, 15 Aug 2017 18:42:31 +0100 Subject: [PATCH 05/12] a few changes --- .../graphene/assertions/GrapheneVerify.java | 14 ++++++++++ .../graphene/assertions/WebElementAssert.java | 24 +++++++++++------ .../ftest/element/TestGrapheneAssertions.java | 27 ++++++++++++++++--- 3 files changed, 53 insertions(+), 12 deletions(-) create mode 100644 assertions/src/main/java/org/jboss/arquillian/graphene/assertions/GrapheneVerify.java 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..4b5b07604 --- /dev/null +++ b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/GrapheneVerify.java @@ -0,0 +1,14 @@ +/* +package org.jboss.arquillian.graphene.assertions; + +import org.openqa.selenium.interactions.Actions; + +public class GrapheneVerify extends Actions { + + */ +/*public static verify(){ + return + }*//* + +} +*/ 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 2a3d605c9..283b83e46 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,6 +1,7 @@ package org.jboss.arquillian.graphene.assertions; import org.apache.commons.lang3.BooleanUtils; +import org.apache.xpath.operations.Bool; import org.assertj.core.api.AbstractAssert; import org.jboss.arquillian.graphene.findby.ByJQuery; import org.openqa.selenium.By; @@ -51,12 +52,12 @@ public WebElementAssert hasParent(){ public WebElementAssert isDisplayed(){ if(this.negated){ - assertThat(this.actual.isDisplayed()).as("checking element is not displayed").overridingErrorMessage("element is displayed").isFalse(); + assertThat(Boolean.valueOf(this.actual.isDisplayed())).as("checking element is not displayed").overridingErrorMessage("element is displayed").isFalse(); this.negated = false; return this; } else { - assertThat(this.actual.isDisplayed()).as("checking element is displayed").overridingErrorMessage("element is not displayed").isTrue(); + assertThat(Boolean.valueOf(this.actual.isDisplayed())).as("checking element is displayed").overridingErrorMessage("element is not displayed").isTrue(); return this; } } @@ -64,12 +65,12 @@ public WebElementAssert isDisplayed(){ //doesn't work for dropboxes public WebElementAssert isSelected(){ if(this.negated){ - assertThat(this.actual.isSelected()).as("checking element is not selected").overridingErrorMessage("element is selected").isFalse(); + assertThat(Boolean.valueOf(this.actual.isSelected())).as("checking element is not selected").overridingErrorMessage("element is selected").isFalse(); this.negated = false; return this; } else { - assertThat(this.actual.isSelected()).as("checking element is selected").overridingErrorMessage("element is not selected").isTrue(); + assertThat(Boolean.valueOf(this.actual.isSelected())).as("checking element is selected").overridingErrorMessage("element is not selected").isTrue(); return this; } } @@ -152,24 +153,24 @@ public WebElementAssert isFocused(WebDriver browser){ public WebElementAssert isEnabled(){ if(this.negated){ - assertThat(this.actual.isEnabled()).as("checking element is not enabled").overridingErrorMessage("element is enabled").isFalse(); + assertThat(Boolean.valueOf(this.actual.isEnabled())).as("checking element is not enabled").overridingErrorMessage("element is enabled").isFalse(); this.negated = false; return this; } else { - assertThat(this.actual.isEnabled()).as("checking element is enabled").overridingErrorMessage("element is not enabled").isTrue(); + 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(this.actual.getText().matches(regex)).as("checking regex does not match text").overridingErrorMessage("regex matches text").isFalse(); + assertThat(Boolean.valueOf(this.actual.getText().matches(regex))).as("checking regex does not match text").overridingErrorMessage("regex matches text").isFalse(); this.negated = false; return this; } else { - assertThat(this.actual.getText().matches(regex)).as("checking regex matches text").overridingErrorMessage("regex does not match text").isTrue(); + assertThat(Boolean.valueOf(this.actual.getText().matches(regex))).as("checking regex matches text").overridingErrorMessage("regex does not match text").isTrue(); return this; } } @@ -178,4 +179,11 @@ public WebElementAssert isNot(){ this.negated = true; return this; } + + public WebElementAssert find(String locator, WebDriver browser){ + WebElement possible = browser.findElement(By.id(locator)); + assertThat(possible); + return (WebElementAssert) possible; + } + } 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 cc625d415..6a08f27a4 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 @@ -111,15 +111,15 @@ public void should_be_able_to_assert_text_on_web_element() { @Test public void should_confirm_element_has_child_web_element(){ - assertThat(div).hasChild();} + assertThat(divHead).hasChild();} - @Test + /*@Test public void should_confirm_element_has_parent_web_element(){ - assertThat(div).hasParent();} + assertThat(div).hasParent();}*/ @Test public void should_confirm_that_web_element_is_displayed_on_page(){ - assertThat(div).isNot().isDisplayed();} + assertThat(div).isDisplayed();} @Test public void should_confirm_that_dropdown_element_is_Chosen(){ @@ -188,4 +188,23 @@ public void should_assert_the_element_has_no_children(){ assertThat(div).isNot().hasChild(); }*/ + /*@Test + public void should_check_error_messages_are_working_as_expected(){ + assertThat(inputForm).isNot().isEnabled(); + }*/ + + /*@Test + public void should_confirm_if_exists_method_works(){ + ; + }*/ + + /*@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).caseSensitiveMatch("pseudo test"); + } } \ No newline at end of file From 609f3b9516252ff5fa96cece98e89df94d2facb4 Mon Sep 17 00:00:00 2001 From: mookt Date: Wed, 23 Aug 2017 22:09:31 +0100 Subject: [PATCH 06/12] beginings of interaction verification --- .../assertions/CouldNotVerifyException.java | 4 ++ .../graphene/assertions/Exists.java | 4 ++ .../graphene/assertions/GrapheneVerify.java | 52 +++++++++++++++++-- .../graphene/assertions/WebElementAssert.java | 41 ++++++++------- .../graphene/ftest/element/EventHandler.java | 38 ++++++++++++++ .../ftest/element/TestGrapheneAssertions.java | 15 ++++-- 6 files changed, 127 insertions(+), 27 deletions(-) create mode 100644 assertions/src/main/java/org/jboss/arquillian/graphene/assertions/CouldNotVerifyException.java create mode 100644 assertions/src/main/java/org/jboss/arquillian/graphene/assertions/Exists.java create mode 100644 ftest/src/test/java/org/jboss/arquillian/graphene/ftest/element/EventHandler.java 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..53a1a11d0 --- /dev/null +++ b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/CouldNotVerifyException.java @@ -0,0 +1,4 @@ +package org.jboss.arquillian.graphene.assertions; + +public class CouldNotVerifyException { +} 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..1425fc540 --- /dev/null +++ b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/Exists.java @@ -0,0 +1,4 @@ +package org.jboss.arquillian.graphene.assertions; + +public class Exists { +} 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 index 4b5b07604..70ba79d77 100644 --- a/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/GrapheneVerify.java +++ b/assertions/src/main/java/org/jboss/arquillian/graphene/assertions/GrapheneVerify.java @@ -1,14 +1,58 @@ /* 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.request.RequestGuard; +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 org.apache.commons.io.input.ReversedLinesFileReader; public class GrapheneVerify extends Actions { - */ -/*public static verify(){ - return - }*//* + private final RequestGuard guard; + private final Document document; + private final FluentWait waitGuard; + private final GrapheneContext context; + private final DocumentReady documentReady = new DocumentReady(); //method from other class + private final RequestIsDone requestIsDone = new RequestIsDone(); // both from requestGuardFactory + private final RequestChange requestChange = new RequestChange(); + + public Actions Verify(WebDriver driver){ + return new Actions(driver); + } + + public static WebElement verify(WebElement element) { + + } } */ + +/*try{ + ReversedLinesFileReader n = new ReversedLinesFileReader(new File("./log.txt")); + String line; + line = n.readLine(); + if(line.equals("after click " + element)) + line = n.readLine(); + if(line.equals("before click " + element)) + return element; + else + throw new CouldNotVerifyException("Cannot verify the action was performed"); + + + + } + catch(FileNotFoundException e){ + System.out.println("no file"); + } + catch(IOException e){ + System.out.println("io issues"); + } + } //what can i do here?*/ \ No newline at end of file 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 283b83e46..1975b97a9 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,11 +1,8 @@ package org.jboss.arquillian.graphene.assertions; -import org.apache.commons.lang3.BooleanUtils; -import org.apache.xpath.operations.Bool; import org.assertj.core.api.AbstractAssert; import org.jboss.arquillian.graphene.findby.ByJQuery; import org.openqa.selenium.By; -import org.openqa.selenium.NoSuchElementException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.Select; @@ -22,12 +19,12 @@ public WebElementAssert(WebElement actual) { public WebElementAssert hasText(String expectedText) { if(this.negated){ - assertThat(this.actual.getText()).as("checking strings are not equal").overridingErrorMessage("contains expected string").isNotEqualTo(expectedText); + 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("contains other string").isEqualTo(expectedText); + 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; } } @@ -49,7 +46,6 @@ public WebElementAssert hasParent(){ 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(); @@ -92,12 +88,12 @@ public WebElementAssert isChosenD(Select dropdown){ 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("contains the supplied string").isNotEqualTo(expectedText); + 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("contains other string").isEqualTo(expectedText); + 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; } } @@ -117,24 +113,24 @@ public WebElementAssert isEmpty(){ public WebElementAssert isTypeOf(String expectedType){ if(this.negated){ - assertThat(this.actual.getAttribute("type")).as("checking not supplied type").overridingErrorMessage("contains supplied type").isNotEqualTo(expectedType); + 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("contains other type").isEqualTo(expectedType); + 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("is supplied css class").isNotEqualTo(expectedClass); + 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("is not supplied css class").isEqualTo(expectedClass); + 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; } } @@ -165,25 +161,30 @@ public WebElementAssert isEnabled(){ public WebElementAssert textMatchesRegex(String regex){ if(this.negated){ - assertThat(Boolean.valueOf(this.actual.getText().matches(regex))).as("checking regex does not match text").overridingErrorMessage("regex matches text").isFalse(); + 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("regex does not match text").isTrue(); + 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){ + assertThat(this.actual.getText().contains(match)); + return this; + }*/ + + /*public WebElementAssert subStringMatching(String match){ + CharSequence sequence = (CharSequence) match; + assertThat(this.actual.getText().contains(sequence)); + return this; + }*/ + public WebElementAssert isNot(){ this.negated = true; return this; } - public WebElementAssert find(String locator, WebDriver browser){ - WebElement possible = browser.findElement(By.id(locator)); - assertThat(possible); - return (WebElementAssert) possible; - } - } 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..be02a6134 --- /dev/null +++ b/ftest/src/test/java/org/jboss/arquillian/graphene/ftest/element/EventHandler.java @@ -0,0 +1,38 @@ +/* +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; + +public class EventHandler implements WebDriverEventListener { + + public void beforeClick(WebElement element, WebDriver browser){ + try { + PrintWriter writer = new PrintWriter("log.txt", "UTF-8"); + writer.println("before click " + element); + writer.close(); + } + catch (FileNotFoundException, UnsupportedEncodingException e){ + System.out.println("logs not here"); + } + } + + 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"); + } + } +} +*/ 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 6a08f27a4..22bc96290 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 @@ -38,6 +38,7 @@ import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.events.EventFiringWebDriver; import org.openqa.selenium.support.ui.Select; import static org.jboss.arquillian.graphene.assertions.GrapheneAssert.assertThat; @@ -54,6 +55,9 @@ public class TestGrapheneAssertions { @Drone private WebDriver browser; + /*EventFiringWebDriver eventDriver = new EventFiringWebDriver(browser); + EventHandler handler = new EventHandler(); + eventDriver.register(handler);*/ @FindBy(id = "pseudoroot") private WebElement div; @@ -195,7 +199,7 @@ public void should_check_error_messages_are_working_as_expected(){ /*@Test public void should_confirm_if_exists_method_works(){ - ; + find("pseudoroot", browser); }*/ /*@Test @@ -203,8 +207,13 @@ public void should_confirm_web_interactions_work_as_expected(){ new Actions(browser).moveToElement(inputForm).click().perform().verify();//possible incarnation }*/ - @Test + /*@Test public void should_confirm_case_sensitive_matching_works_as_expected(){ assertThat(div).caseSensitiveMatch("pseudo test"); - } + }*/ + + /*@Test + public void should_confirm_that_case_insensitive_matching_works_as_expected(){ + assertThat(inactiveInputForm).subStringMatching("zz"); + }*/ } \ No newline at end of file From c788ca66fda15277a4d4a00d21c3d88819d9e269 Mon Sep 17 00:00:00 2001 From: mookt Date: Mon, 28 Aug 2017 15:48:51 +0100 Subject: [PATCH 07/12] final WebElementAssert.java --- .../graphene/assertions/WebElementAssert.java | 66 ++++++++++++------- 1 file changed, 42 insertions(+), 24 deletions(-) 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 1975b97a9..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 @@ -19,12 +19,12 @@ public WebElementAssert(WebElement actual) { public WebElementAssert hasText(String 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); + 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); + 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; } } @@ -40,10 +40,15 @@ public WebElementAssert hasChild(){ } } - //work in progress! public WebElementAssert hasParent(){ - assertThat(this.actual.findElement(ByJQuery.selector(""))); - return this; + 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(){ @@ -71,7 +76,7 @@ public WebElementAssert isSelected(){ } } - //support's dropboxes, supply dropbox and text of chosen option to assert it's chosen + //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); @@ -88,12 +93,12 @@ public WebElementAssert isChosenD(Select dropdown){ 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); + 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); + 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; } } @@ -113,24 +118,24 @@ public WebElementAssert isEmpty(){ 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); + 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); + 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); + 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); + 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; } } @@ -161,30 +166,43 @@ public WebElementAssert isEnabled(){ 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(); + 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(); + 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){ - assertThat(this.actual.getText().contains(match)); - 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){ - CharSequence sequence = (CharSequence) match; - assertThat(this.actual.getText().contains(sequence)); - 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 From 0f80b1397fbd84eff95681d267da1fed86cd23e5 Mon Sep 17 00:00:00 2001 From: mookt Date: Mon, 28 Aug 2017 16:34:55 +0100 Subject: [PATCH 08/12] final TestGrapheneAssertions.java --- .../ftest/element/TestGrapheneAssertions.java | 40 ++++++++++--------- 1 file changed, 21 insertions(+), 19 deletions(-) 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 22bc96290..a7bd99405 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 @@ -38,7 +38,6 @@ import org.openqa.selenium.WebElement; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.FindBy; -import org.openqa.selenium.support.events.EventFiringWebDriver; import org.openqa.selenium.support.ui.Select; import static org.jboss.arquillian.graphene.assertions.GrapheneAssert.assertThat; @@ -55,9 +54,6 @@ public class TestGrapheneAssertions { @Drone private WebDriver browser; - /*EventFiringWebDriver eventDriver = new EventFiringWebDriver(browser); - EventHandler handler = new EventHandler(); - eventDriver.register(handler);*/ @FindBy(id = "pseudoroot") private WebElement div; @@ -98,6 +94,12 @@ public class TestGrapheneAssertions { @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"); @@ -117,9 +119,9 @@ public void should_be_able_to_assert_text_on_web_element() { public void should_confirm_element_has_child_web_element(){ assertThat(divHead).hasChild();} - /*@Test + @Test public void should_confirm_element_has_parent_web_element(){ - assertThat(div).hasParent();}*/ + assertThat(title).hasParent();} @Test public void should_confirm_that_web_element_is_displayed_on_page(){ @@ -173,11 +175,11 @@ public void confirm_element_text_matches_regex(){ } - /*@Test + @Test public void asserts_that_the_correct_radio_button_is_chosen(){ new Actions(browser).moveToElement(phpRadioButton).click(); assertThat(phpRadioButton).isDisplayed(); - }*/ + } @Test @@ -187,15 +189,15 @@ public void should_assert_the_correct_checkbox_is_chosen(){ } - /*@Test + @Test public void should_assert_the_element_has_no_children(){ - assertThat(div).isNot().hasChild(); - }*/ + assertThat(testChild).isNot().hasChild(); + } - /*@Test + @Test public void should_check_error_messages_are_working_as_expected(){ assertThat(inputForm).isNot().isEnabled(); - }*/ + } /*@Test public void should_confirm_if_exists_method_works(){ @@ -207,13 +209,13 @@ public void should_confirm_web_interactions_work_as_expected(){ new Actions(browser).moveToElement(inputForm).click().perform().verify();//possible incarnation }*/ - /*@Test + @Test public void should_confirm_case_sensitive_matching_works_as_expected(){ - assertThat(div).caseSensitiveMatch("pseudo test"); - }*/ + assertThat(div).subStringMatching("seudo"); + } - /*@Test + @Test public void should_confirm_that_case_insensitive_matching_works_as_expected(){ - assertThat(inactiveInputForm).subStringMatching("zz"); - }*/ + assertThat(div).caseInsensitiveMatching("PSEUDO ROOT"); + } } \ No newline at end of file From 8f4e729af21cf3b57cc9a5ec5dd9f80438b75f83 Mon Sep 17 00:00:00 2001 From: mookt Date: Mon, 28 Aug 2017 16:39:10 +0100 Subject: [PATCH 09/12] final sample.html --- .../jboss/arquillian/graphene/ftest/assertions/sample.html | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 4d9302ad8..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 @@ -24,7 +24,7 @@ - Sample Page + Sample Page
@@ -34,7 +34,9 @@
correct -
pseudo root
+
pseudo root +

test kid

+