Change-Id: Ie4a940ba2488f0a7739492a1d3967957cdd41099tags/7.6.0.alpha5
/* | |||||
* Copyright 2000-2014 Vaadin Ltd. | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||||
* use this file except in compliance with the License. You may obtain a copy of | |||||
* the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||||
* License for the specific language governing permissions and limitations under | |||||
* the License. | |||||
*/ | |||||
package com.vaadin.tests.application; | |||||
import com.vaadin.server.VaadinRequest; | |||||
import com.vaadin.tests.components.AbstractTestUIWithLog; | |||||
import com.vaadin.ui.Button; | |||||
import com.vaadin.ui.Button.ClickEvent; | |||||
import com.vaadin.ui.Button.ClickListener; | |||||
public class ReconnectDialogUI extends AbstractTestUIWithLog { | |||||
@Override | |||||
protected void setup(VaadinRequest request) { | |||||
if (request.getParameter("reconnectAttempts") != null) { | |||||
getReconnectDialogConfiguration() | |||||
.setReconnectAttempts( | |||||
Integer.parseInt(request | |||||
.getParameter("reconnectAttempts"))); | |||||
} | |||||
Button b = new Button("Say hello"); | |||||
b.addClickListener(new ClickListener() { | |||||
@Override | |||||
public void buttonClick(ClickEvent event) { | |||||
log("Hello from the server"); | |||||
} | |||||
}); | |||||
addComponent(b); | |||||
} | |||||
} |
/* | |||||
* Copyright 2000-2014 Vaadin Ltd. | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||||
* use this file except in compliance with the License. You may obtain a copy of | |||||
* the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||||
* License for the specific language governing permissions and limitations under | |||||
* the License. | |||||
*/ | |||||
package com.vaadin.tests.application; | |||||
import org.junit.Assert; | |||||
import org.junit.Test; | |||||
import org.openqa.selenium.By; | |||||
import org.openqa.selenium.WebDriver; | |||||
import org.openqa.selenium.WebElement; | |||||
import org.openqa.selenium.support.ui.ExpectedCondition; | |||||
import com.jcraft.jsch.JSchException; | |||||
import com.vaadin.testbench.elements.ButtonElement; | |||||
import com.vaadin.tests.tb3.MultiBrowserTestWithProxy; | |||||
public class ReconnectDialogUITest extends MultiBrowserTestWithProxy { | |||||
@Test | |||||
public void reconnectDialogShownAndDisappears() throws JSchException { | |||||
openTestURL(); | |||||
getButton().click(); | |||||
Assert.assertEquals("1. Hello from the server", getLogRow(0)); | |||||
disconnectProxy(); | |||||
getButton().click(); | |||||
waitForReconnectDialogWithText("Server connection lost, trying to reconnect..."); | |||||
connectProxy(); | |||||
waitForReconnectDialogToDisappear(); | |||||
Assert.assertEquals("2. Hello from the server", getLogRow(0)); | |||||
} | |||||
@Test | |||||
public void gaveUpMessageShown() { | |||||
openTestURL("reconnectAttempts=3"); | |||||
getButton().click(); | |||||
Assert.assertEquals("1. Hello from the server", getLogRow(0)); | |||||
disconnectProxy(); | |||||
getButton().click(); | |||||
waitForReconnectDialogWithText("Server connection lost."); | |||||
} | |||||
private void waitForReconnectDialogWithText(final String text) { | |||||
waitForReconnectDialogPresent(); | |||||
final WebElement reconnectDialog = findElement(ReconnectDialogUIThemeTest.reconnectDialogBy); | |||||
waitUntil(new ExpectedCondition<Boolean>() { | |||||
@Override | |||||
public Boolean apply(WebDriver input) { | |||||
return reconnectDialog.findElement(By.className("text")) | |||||
.getText().equals(text); | |||||
} | |||||
}, 10); | |||||
} | |||||
private void waitForReconnectDialogToDisappear() { | |||||
waitForElementNotPresent(ReconnectDialogUIThemeTest.reconnectDialogBy); | |||||
} | |||||
private void waitForReconnectDialogPresent() { | |||||
waitForElementPresent(ReconnectDialogUIThemeTest.reconnectDialogBy); | |||||
} | |||||
private WebElement getButton() { | |||||
return $(ButtonElement.class).first(); | |||||
} | |||||
} |
/* | |||||
* Copyright 2000-2014 Vaadin Ltd. | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||||
* use this file except in compliance with the License. You may obtain a copy of | |||||
* the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||||
* License for the specific language governing permissions and limitations under | |||||
* the License. | |||||
*/ | |||||
package com.vaadin.tests.application; | |||||
import java.awt.image.BufferedImage; | |||||
import java.io.ByteArrayInputStream; | |||||
import java.io.IOException; | |||||
import javax.imageio.ImageIO; | |||||
import org.junit.Assert; | |||||
import org.junit.Test; | |||||
import org.openqa.selenium.By; | |||||
import org.openqa.selenium.OutputType; | |||||
import org.openqa.selenium.TakesScreenshot; | |||||
import org.openqa.selenium.WebDriver; | |||||
import org.openqa.selenium.WebElement; | |||||
import org.openqa.selenium.support.ui.ExpectedCondition; | |||||
import com.vaadin.testbench.elements.ButtonElement; | |||||
import com.vaadin.testbench.parallel.BrowserUtil; | |||||
import com.vaadin.tests.tb3.CustomTestBenchCommandExecutor; | |||||
import com.vaadin.tests.tb3.MultiBrowserThemeTestWithProxy; | |||||
public abstract class ReconnectDialogUIThemeTest extends | |||||
MultiBrowserThemeTestWithProxy { | |||||
public static class ValoReconnectDialogTest extends | |||||
ReconnectDialogUIThemeTest { | |||||
@Override | |||||
protected String getTheme() { | |||||
return "valo"; | |||||
} | |||||
} | |||||
public static class ReindeerReconnectDialogTest extends | |||||
ReconnectDialogUIThemeTest { | |||||
@Override | |||||
protected String getTheme() { | |||||
return "reindeer"; | |||||
} | |||||
} | |||||
public static class RunoReconnectDialogTest extends | |||||
ReconnectDialogUIThemeTest { | |||||
@Override | |||||
protected String getTheme() { | |||||
return "runo"; | |||||
} | |||||
} | |||||
public static class ChameleonReconnectDialogTest extends | |||||
ReconnectDialogUIThemeTest { | |||||
@Override | |||||
protected String getTheme() { | |||||
return "chameleon"; | |||||
} | |||||
} | |||||
public static class BaseReconnectDialogTest extends | |||||
ReconnectDialogUIThemeTest { | |||||
@Override | |||||
protected String getTheme() { | |||||
return "base"; | |||||
} | |||||
} | |||||
static By reconnectDialogBy = By.className("v-reconnect-dialog"); | |||||
@Test | |||||
public void reconnectDialogTheme() throws IOException { | |||||
openTestURL(); | |||||
ButtonElement helloButton = $(ButtonElement.class).caption("Say hello") | |||||
.first(); | |||||
helloButton.click(); | |||||
Assert.assertEquals("1. Hello from the server", getLogRow(0)); | |||||
disconnectProxy(); | |||||
helloButton.click(); | |||||
testBench().disableWaitForVaadin(); | |||||
waitUntil(new ExpectedCondition<Boolean>() { | |||||
@Override | |||||
public Boolean apply(WebDriver input) { | |||||
boolean present = isElementPresent(reconnectDialogBy); | |||||
return present; | |||||
} | |||||
}); | |||||
WebElement dialog = findElement(reconnectDialogBy); | |||||
WebElement spinner = dialog.findElement(By.className("spinner")); | |||||
// Hide spinner to make screenshot stable | |||||
executeScript("arguments[0].style.visibility='hidden';", spinner); | |||||
compareScreen("onscreen-without-spinner"); | |||||
// Show spinner and make sure it is shown by comparing to the screenshot | |||||
// without a spinner | |||||
executeScript("arguments[0].style.visibility='visible';", spinner); | |||||
BufferedImage fullScreen = ImageIO.read(new ByteArrayInputStream( | |||||
((TakesScreenshot) getDriver()) | |||||
.getScreenshotAs(OutputType.BYTES))); | |||||
BufferedImage spinnerImage = CustomTestBenchCommandExecutor | |||||
.cropToElement(spinner, fullScreen, | |||||
BrowserUtil.isIE8(getDesiredCapabilities())); | |||||
assertHasManyColors("Spinner is not shown", spinnerImage); | |||||
} | |||||
private void assertHasManyColors(String message, BufferedImage spinnerImage) { | |||||
int backgroundColor = spinnerImage.getRGB(0, 0); | |||||
for (int x = 0; x < spinnerImage.getWidth(); x++) { | |||||
for (int y = 0; y < spinnerImage.getHeight(); y++) { | |||||
if (Math.abs(spinnerImage.getRGB(x, y) - backgroundColor) > 50) { | |||||
return; | |||||
} | |||||
} | |||||
} | |||||
Assert.fail(message); | |||||
} | |||||
@Override | |||||
protected Class<?> getUIClass() { | |||||
return ReconnectDialogUI.class; | |||||
} | |||||
} |
import org.junit.Before; | import org.junit.Before; | ||||
import org.junit.Test; | import org.junit.Test; | ||||
import org.openqa.selenium.By; | import org.openqa.selenium.By; | ||||
import org.openqa.selenium.JavascriptExecutor; | |||||
import org.openqa.selenium.Keys; | import org.openqa.selenium.Keys; | ||||
import org.openqa.selenium.StaleElementReferenceException; | import org.openqa.selenium.StaleElementReferenceException; | ||||
import org.openqa.selenium.WebDriver; | import org.openqa.selenium.WebDriver; | ||||
checkBoxElement.click(5, 5); | checkBoxElement.click(5, 5); | ||||
} | } | ||||
private Object executeScript(String string, Object... param) { | |||||
return ((JavascriptExecutor) getDriver()).executeScript(string, param); | |||||
} | |||||
private void scrollAndToggle(int row) { | private void scrollAndToggle(int row) { | ||||
setRow(row); | setRow(row); | ||||
getScrollAndToggle().click(); | getScrollAndToggle().click(); |
* {@link org.openqa.selenium.JavascriptExecutor#executeScript(String, Object...)} | * {@link org.openqa.selenium.JavascriptExecutor#executeScript(String, Object...)} | ||||
* returns | * returns | ||||
*/ | */ | ||||
protected Object executeScript(String script) { | |||||
return ((JavascriptExecutor) getDriver()).executeScript(script); | |||||
protected Object executeScript(String script, Object... args) { | |||||
return ((JavascriptExecutor) getDriver()).executeScript(script, args); | |||||
} | } | ||||
/** | /** | ||||
waitUntil(ExpectedConditions.presenceOfElementLocated(by)); | waitUntil(ExpectedConditions.presenceOfElementLocated(by)); | ||||
} | } | ||||
protected void waitForElementNotPresent(final By by) { | |||||
waitUntil(new ExpectedCondition<Boolean>() { | |||||
@Override | |||||
public Boolean apply(WebDriver input) { | |||||
return input.findElements(by).isEmpty(); | |||||
} | |||||
}); | |||||
} | |||||
protected void waitForElementVisible(final By by) { | protected void waitForElementVisible(final By by) { | ||||
waitUntil(ExpectedConditions.visibilityOfElementLocated(by)); | waitUntil(ExpectedConditions.visibilityOfElementLocated(by)); | ||||
} | } |
* @return | * @return | ||||
* @throws IOException | * @throws IOException | ||||
*/ | */ | ||||
private BufferedImage cropToElement(WebElement element, | |||||
public static BufferedImage cropToElement(WebElement element, | |||||
BufferedImage fullScreen, boolean isIE8) throws IOException { | BufferedImage fullScreen, boolean isIE8) throws IOException { | ||||
Point loc = element.getLocation(); | Point loc = element.getLocation(); | ||||
Dimension size = element.getSize(); | Dimension size = element.getSize(); |
/* | |||||
* Copyright 2000-2014 Vaadin Ltd. | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | |||||
* use this file except in compliance with the License. You may obtain a copy of | |||||
* the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | |||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | |||||
* License for the specific language governing permissions and limitations under | |||||
* the License. | |||||
*/ | |||||
package com.vaadin.tests.tb3; | |||||
import java.util.Arrays; | |||||
import java.util.HashSet; | |||||
import java.util.List; | |||||
import java.util.Set; | |||||
import org.openqa.selenium.remote.DesiredCapabilities; | |||||
/** | |||||
* Test which uses theme returned by {@link #getTheme()} for running the test | |||||
*/ | |||||
public abstract class MultiBrowserThemeTestWithProxy extends | |||||
MultiBrowserTestWithProxy { | |||||
protected abstract String getTheme(); | |||||
@Override | |||||
protected boolean requireWindowFocusForIE() { | |||||
return true; | |||||
} | |||||
@Override | |||||
protected void openTestURL(Class<?> uiClass, String... parameters) { | |||||
Set<String> params = new HashSet<String>(Arrays.asList(parameters)); | |||||
params.add("theme=" + getTheme()); | |||||
super.openTestURL(uiClass, params.toArray(new String[params.size()])); | |||||
} | |||||
@Override | |||||
public List<DesiredCapabilities> getBrowsersToTest() { | |||||
List<DesiredCapabilities> browsersToTest = getBrowsersExcludingPhantomJS(); | |||||
browsersToTest.add(PHANTOMJS2()); | |||||
return browsersToTest; | |||||
} | |||||
} |