-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assertions #173
Open
kealant
wants to merge
12
commits into
arquillian:assertions
Choose a base branch
from
kealant:assertions
base: assertions
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Assertions #173
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3eaf698
a few minor changes in relation to comments
kealant ca1721e
added a few more assertions and tried updating parent/child assertions
kealant 9d43def
some bug fixes
kealant ffe4a88
negation, error messages and some further testing
kealant 129e5a3
a few changes
kealant 609f3b9
beginings of interaction verification
kealant c788ca6
final WebElementAssert.java
kealant 0f80b13
final TestGrapheneAssertions.java
kealant 8f4e729
final sample.html
kealant 060abf2
some small changes
kealant 930808d
Update GrapheneVerify.java
kealant c8b5bad
Update GrapheneVerify.java
kealant File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...tions/src/main/java/org/jboss/arquillian/graphene/assertions/CouldNotVerifyException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.jboss.arquillian.graphene.assertions; | ||
|
||
public class CouldNotVerifyException extends RuntimeException { | ||
|
||
public CouldNotVerifyException(){ | ||
super(); | ||
} | ||
|
||
public CouldNotVerifyException(String message){ | ||
super(message); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
assertions/src/main/java/org/jboss/arquillian/graphene/assertions/Exists.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
155 changes: 155 additions & 0 deletions
155
assertions/src/main/java/org/jboss/arquillian/graphene/assertions/GrapheneVerify.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/*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 { | ||
|
||
private final RequestGuard guard; | ||
private final Document document; | ||
private final FluentWait<WebDriver, Void> 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) { | ||
this.guard = guard; | ||
this.document = document; | ||
this.context = context; | ||
this.waitGuard = waitAjax() | ||
.withTimeout(context.getConfiguration().getWaitGuardInterval(), TimeUnit.SECONDS) | ||
.pollingEvery(Math.min(context.getConfiguration().getWaitGuardInterval() * 100, 200), TimeUnit.MILLISECONDS); | ||
} | ||
|
||
*//** | ||
* Returns the guarded object checking whether the request of the given type is done during each method invocation. If the | ||
* request is not found, the {@link RequestGuardException} is thrown. | ||
* | ||
* @param <T> type of the given target | ||
* @param target object to be guarded | ||
* @param requestExpected the request type being checked after each method invocation | ||
* @param strict indicates that the expected request type can be interleaved by another type | ||
* @return the guarded object | ||
*//* | ||
public <T> T guard(T target, final RequestType requestExpected, final boolean strict) { | ||
if (requestExpected == null) { | ||
throw new IllegalArgumentException("The paremeter [requestExpected] is null."); | ||
} | ||
if (target == null) { | ||
throw new IllegalArgumentException("The paremeter [target] is null."); | ||
} | ||
GrapheneProxyInstance proxy; | ||
if (GrapheneProxy.isProxyInstance(target)) { | ||
proxy = (GrapheneProxyInstance) ((GrapheneProxyInstance) target).copy(); | ||
} else { | ||
proxy = (GrapheneProxyInstance) GrapheneProxy.getProxyForTarget(context, target); | ||
} | ||
proxy.registerInterceptor(new Interceptor() { | ||
@Override | ||
public Object intercept(InvocationContext context) throws Throwable { | ||
|
||
guard.clearRequestDone(); | ||
|
||
Object result = context.invoke(); | ||
|
||
RequestType requestType; | ||
|
||
if (strict) { | ||
requestType = waitForRequestChange(); | ||
} else { | ||
requestType = waitForRequestType(requestExpected); | ||
} | ||
|
||
if (requestType.equals(requestExpected)) { | ||
waitForRequestFinished(); | ||
} else { | ||
throw new RequestGuardException(requestExpected, requestType); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Override | ||
public int getPrecedence() { | ||
return 0; | ||
} | ||
|
||
private RequestType waitForRequestChange() { | ||
try { | ||
return waitGuard.until(requestChange); | ||
} catch (TimeoutException e) { | ||
return RequestType.NONE; | ||
} | ||
} | ||
|
||
private RequestType waitForRequestType(RequestType requestType) { | ||
try { | ||
return waitGuard.until(new RequestTypeDone(requestExpected)); | ||
} catch (TimeoutException e) { | ||
return guard.getRequestType(); | ||
} | ||
} | ||
|
||
private void waitForRequestFinished() { | ||
switch (requestExpected) { | ||
|
||
case HTTP: | ||
waitGuard.withMessage("Document didn't become ready").until(documentReady); | ||
return; | ||
|
||
case XHR: | ||
waitGuard.until(requestIsDone); | ||
return; | ||
|
||
case NONE: | ||
return; | ||
} | ||
} | ||
}); | ||
return (T) proxy; | ||
} | ||
}*/ | ||
|
||
/*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?*/ |
194 changes: 192 additions & 2 deletions
194
assertions/src/main/java/org/jboss/arquillian/graphene/assertions/WebElementAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<WebElementAssert, WebElement> { | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the about fails this line would never be reached. Why are we doing this assignment in the first place? |
||
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; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this code doing here? If it's dead no reason to have it :)