Improving robustness of JS Tests

Fixed timing issues that occurred during javascript updates.
This commit is contained in:
Martin Stockhammer 2017-06-09 21:35:31 +02:00
parent 75f14a95d9
commit 85352aae7d
6 changed files with 85 additions and 46 deletions

View File

@ -48,30 +48,34 @@ public class RepositoryAdminTest
public void testManagedRepository()
{
login( getAdminUsername(), getAdminPassword() );
WebDriverWait wait = new WebDriverWait(getWebDriver(), 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("menu-repositories-list-a")));
clickLinkWithLocator( "menu-repositories-list-a");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("managed-repositories-view-a")));
clickLinkWithXPath( "//a[@href='#remote-repositories-content']");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("remote-repositories-view-a")));
clickLinkWithXPath( "//a[@href='#remote-repository-edit']", false );
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("remote-repository-save-button")));
WebDriverWait wait = new WebDriverWait(getWebDriver(), 20);
WebElement el;
el = wait.until(ExpectedConditions.elementToBeClickable(By.id("menu-repositories-list-a")));
tryClick( el, ExpectedConditions.presenceOfElementLocated( By.id( "managed-repositories-view-a" ) ),
"Managed Repositories not activated");
el = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@href='#remote-repositories-content']")));
tryClick(el,ExpectedConditions.visibilityOfElementLocated(By.id("remote-repositories-view-a")),
"Remote Repositories View not available");
el = wait.until(ExpectedConditions.elementToBeClickable( By.xpath("//a[@href='#remote-repository-edit']") ));
el = tryClick(el, ExpectedConditions.visibilityOfElementLocated(By.id("remote-repository-save-button")),
"Repository Save Button not available");
setFieldValue( "id", "myrepoid" );
setFieldValue( "name", "My repo name" );
setFieldValue( "url", "http://www.repo.org" );
clickButtonWithLocator( "remote-repository-save-button");
el.click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("remote-repositories-view-a")));
clickLinkWithLocator( "menu-proxy-connectors-list-a");
el = wait.until(ExpectedConditions.elementToBeClickable( By.id("menu-proxy-connectors-list-a") ));
el.click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("proxy-connectors-view-tabs-a-network-proxies-grid")));
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("main-content"), "Proxy Connectors"));
// proxy connect
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("proxy-connectors-view"), "central" ));
assertTextNotPresent( "myrepoid" );
clickButtonWithLocator( "proxy-connectors-view-tabs-a-edit");
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("proxy-connector-btn-save")));
el = wait.until(ExpectedConditions.elementToBeClickable( By.id("proxy-connectors-view-tabs-a-edit") ));
el.click();
el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("proxy-connector-btn-save")));
selectValue( "sourceRepoId", "internal" );
// Workaround
// TODO: Check after upgrade of htmlunit, bootstrap or jquery
@ -80,7 +84,7 @@ public class RepositoryAdminTest
// End of Workaround
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("targetRepoId")));
selectValue( "targetRepoId", "myrepoid" );
clickButtonWithLocator( "proxy-connector-btn-save");
el.click();
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("user-messages"),"ProxyConnector added"));
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("proxy-connectors-view"), "central" ));
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("proxy-connectors-view"), "myrepoid" ));
@ -88,7 +92,7 @@ public class RepositoryAdminTest
// This is needed here for HTMLUnit Tests. Currently do not know why, wait is not working for the
// list entries down
waitPage();
WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("proxy-connector-edit-order-div")));
el = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("proxy-connector-edit-order-div")));
assertTextPresent( "internal" );
List<WebElement> repos = el.findElements(By.xpath("./div"));
Assert.assertTrue("First repo is myrepo", repos.get(0).getText().contains("myrepoid"));

View File

@ -141,13 +141,6 @@ public class WebDriverBrowseTest
{
elements = find( By.id("login-link-a"));
WebElement webElement = elements.get( 0 ).getElement();
for(FluentWebElement element : elements) {
log.info("Found login link: "+element.getElement().getTagName()+ " "+ element.getElement().getText());
}
log.info("innerText: "+webElement.getAttribute("innerText"));
log.info("value: "+webElement.getAttribute("value"));
log.info("innerHTML: "+webElement.getAttribute( "innerHTML" ));
log.info("JS: "+((( JavascriptExecutor)getDriver()).executeScript("return $(arguments[0]).text();", webElement)));
if (getDriver() instanceof HtmlUnitDriver) {
Assert.assertEquals( "LOGIN", webElement.getText().toUpperCase() );
} else

View File

@ -51,7 +51,7 @@ public class WebDriverTest
extends FluentTest
{
final Logger log = LoggerFactory.getLogger( WebDriver.class );
final Logger log = LoggerFactory.getLogger( WebDriverTest.class );
@Override
public void takeScreenShot( String fileName )
@ -100,10 +100,6 @@ public class WebDriverTest
log.info("Found login link: "+element.getElement().getTagName()+ " "+ element.getElement().getText());
}
WebElement webElement = elements.get(0).getElement();
log.info("innerText: "+webElement.getAttribute("innerText"));
log.info("value: "+webElement.getAttribute("value"));
log.info("innerHTML: "+webElement.getAttribute( "innerHTML" ));
log.info("JS: "+(((JavascriptExecutor)getDriver()).executeScript("return $(arguments[0]).text();", webElement)));
if (getDriver() instanceof HtmlUnitDriver ) {
Assert.assertEquals( "LOGIN", webElement.getText().toUpperCase() );
} else

View File

@ -3,6 +3,7 @@ package org.apache.archiva.web.test.parent;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
@ -101,10 +102,11 @@ public abstract class AbstractArchivaTest
{
login( getAdminUsername(), getAdminPassword() );
WebDriverWait wait = new WebDriverWait(getWebDriver(), 10);
clickLinkWithLocator( "menu-users-list-a");
wait.until(ExpectedConditions.elementToBeClickable(By.id("users-view-tabs-li-user-edit-a")));
clickLinkWithLocator( "users-view-tabs-li-user-edit-a");
wait.until(ExpectedConditions.elementToBeClickable(By.id("user-create-form-register-button")));
WebElement el = wait.until(ExpectedConditions.elementToBeClickable( By.id("menu-users-list-a") ));
el = tryClick(el, ExpectedConditions.elementToBeClickable(By.id("users-view-tabs-li-user-edit-a")), "User List not available");
el = tryClick(el, ExpectedConditions.elementToBeClickable(By.id("users-view-tabs-li-user-edit-a")),"User Edit View not available");
el = tryClick(el, ExpectedConditions.elementToBeClickable(By.id("user-create-form-register-button")),
"Register Form not available");
assertCreateUserPage();
setFieldValue( "username", userName );
setFieldValue( "fullname", fullName );
@ -112,10 +114,9 @@ public abstract class AbstractArchivaTest
setFieldValue( "password", password );
setFieldValue( "confirmPassword", confirmPassword );
clickLinkWithLocator( "user-create-form-register-button");
el.click();
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("users-grid-user-id-" + userName)));
assertTextPresent( "User " + userName + " created." );
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("user-messages"),"User " + userName + " created." ));
assertElementPresent( "users-grid-user-id-" + userName );
if ( valid )

View File

@ -19,30 +19,22 @@ package org.apache.archiva.web.test.parent;
* under the License.
*/
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
import org.apache.archiva.web.test.tools.ArchivaSeleniumExecutionRule;
import org.apache.archiva.web.test.tools.WebdriverUtility;
import org.junit.Assert;
import org.junit.Rule;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.function.Function;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
@ -294,10 +286,7 @@ public abstract class AbstractSeleniumTest
WebElement passwordField = wait.until(ExpectedConditions.visibilityOf(getWebDriver().findElement(By.id("user-login-form-password"))));
wait = new WebDriverWait(getWebDriver(),5);
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("modal-login-ok")));
logger.info("Login form visible");
logger.info("Enter username "+username);
usernameField.sendKeys(username);
logger.info("Enter password "+password);
passwordField.sendKeys(password);
/*
if ( rememberMe )
@ -631,6 +620,59 @@ public abstract class AbstractSeleniumTest
}
}
/**
* Executes click() on the WebElement <code>el</code> and waits for the conditions.
* If the condition is not fulfilled in <code>maxWaitTimeInS</code>, the click is executed again
* and waits again for the condition.
* After the number of attempts as given by the parameter an assertion error will be thrown, with
* the given <code>message</code>.
*
* If the click was successful the element is returned that was created by the condition.
*
* @param el The element where the click is executed
* @param conditions The conditions to wait for after the click
* @param message The assertion messages
* @param attempts Maximum number of click attempts
* @param maxWaitTimeInS The time in seconds to wait that the condition is fulfilled.
* @param <V> The return type
* @return
*/
public <V> V tryClick( WebElement el, Function<? super WebDriver, V> conditions, String message, int attempts, int maxWaitTimeInS)
{
int count = attempts;
WebDriverWait wait = new WebDriverWait( getWebDriver(), maxWaitTimeInS );
V result = null;
Exception ex = null;
while(count>0)
{
el.click();
try
{
result = wait.until( conditions );
count=0;
ex = null;
} catch (Exception e) {
ex = e;
count--;
}
}
if (ex!=null) {
Assert.fail( message);
}
return result;
}
public <V> V tryClick(WebElement el, Function<? super WebDriver, V> conditions, String message, int attempts )
{
return tryClick( el, conditions, message, attempts, 10 );
}
public <V> V tryClick(WebElement el, Function<? super WebDriver, V> conditions, String message)
{
return tryClick( el, conditions, message, 3);
}
public void setFieldValues( Map<String, String> fieldMap )
{
Map.Entry<String, String> entry;

View File

@ -41,12 +41,15 @@ public class ArchivaSeleniumExecutionRule
{
try
{
log.info("Test "+method.getDeclaringClass().getName()+"#"+method.getName());
( (AbstractSeleniumTest) target ).open();
method.getMethod().invoke( target );
}
catch ( Throwable e )
{
log.info("Exception thrown in Selenium test: "+e.getClass().getName()+" - "+e.getMessage());
log.info("Method "+method.getName());
String fileName =
( (AbstractSeleniumTest) target ).captureScreenShotOnFailure( e, method.getMethod().getName(),
target.getClass().getName() );