Skip to content
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
wants to merge 12 commits into
base: assertions
Choose a base branch
from
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);
}
}
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;
}
}
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 {
Copy link
Member

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 :)


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?*/
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;
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}

}
Loading