Skip to content

Commit

Permalink
#1799 restore test case for full-size screenshots in Grid
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Jun 25, 2022
1 parent 251349f commit 9d72073
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
Expand Up @@ -81,7 +81,7 @@ public <T> Optional<T> takeScreenshot(Driver driver, OutputType<T> outputType) {
return defaultImplementation.takeScreenshot(driver, outputType);
}
DevTools devTools = ((HasDevTools) webDriver).getDevTools();
devTools.createSession();
devTools.createSessionIfThereIsNotOne();

Options options = getOptions(remoteWebDriver);
Viewport viewport = new Viewport(0, 0, options.fullWidth(), options.fullHeight(), 1);
Expand Down
@@ -1,14 +1,17 @@
package integration;

import com.codeborne.selenide.Configuration;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

public class ScreenshotTestHelper {
private static final Logger log = LoggerFactory.getLogger(ScreenshotTestHelper.class);
Expand All @@ -18,11 +21,17 @@ static void verifyScreenshotSize(File screenshot, int width, int height) throws
log.info("Verify screenshot {} of size {}x{}", screenshot.getAbsolutePath(), img.getWidth(), img.getHeight());
if (nearlyEqual(img.getWidth(), width * 2) && nearlyEqual(img.getHeight(), height * 2)) {
// it's Retina display, it has 2x more pixels
log.info("Screenshot matches {}x{} on Retina display", width, height);
log.info("Screenshot matches {}x{} size on Retina display", width, height);
}
else if (nearlyEqual(img.getWidth(), width) && nearlyEqual(img.getHeight(), height)) {
log.info("Screenshot matches {}x{} size", width, height);
}
else {
assertThat(img.getWidth()).isBetween(width - 50, width + 50);
assertThat(img.getHeight()).isBetween(height - 50, height + 50);
File archivedFile = new File(Configuration.reportsFolder, UUID.randomUUID() + ".png");
FileUtils.copyFile(screenshot, archivedFile);
log.info("Screenshot matches {}x{} size", width, height);
fail(String.format("Screenshot %s is expected to have size %sx%s, but actual size: %sx%s",
archivedFile.getAbsolutePath(), width, height, img.getWidth(), img.getHeight()));
}
}

Expand Down
2 changes: 2 additions & 0 deletions modules/grid/build.gradle
Expand Up @@ -2,6 +2,8 @@ dependencies {
testImplementation project(':statics')
testImplementation project(':statics').sourceSets.test.output
testImplementation project(':modules:core').sourceSets.test.output
testImplementation project(':modules:full-screenshot')
testImplementation project(':modules:full-screenshot').sourceSets.test.output

testImplementation("org.junit.jupiter:junit-jupiter-api:$junitVersion")
testImplementation("org.junit.jupiter:junit-jupiter-params:$junitVersion")
Expand Down
@@ -0,0 +1,50 @@
package integration;

import com.codeborne.selenide.Configuration;
import com.codeborne.selenide.Selenide;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.OutputType;

import java.io.File;
import java.io.IOException;

import static com.codeborne.selenide.Selenide.webdriver;
import static com.codeborne.selenide.WebDriverConditions.title;
import static integration.ScreenshotTestHelper.verifyScreenshotSize;

public class FullScreenshotsGridTest extends AbstractGridTest {
private static final int width = 2200;
private static final int height = 3300;

@BeforeEach
void setUp() {
Configuration.remote = "http://localhost:" + hubPort + "/wd/hub";
}

@AfterEach
void tearDown() {
Configuration.remote = null;
}

/*
In non-local browser (grid),
It fails or takes a screenshot of the wrong tab.
See https://github.com/SeleniumHQ/selenium/issues/10810
*/
@Test
void canTakeFullScreenshotWithTwoTabs() throws IOException {
openFile("page_of_fixed_size_2200x3300.html");
webdriver().shouldHave(title("Test::page of fixed size 2200x3300"));

Selenide.executeJavaScript("window.open()");
Selenide.switchTo().window(1);
openFile("file_upload_form.html");

Selenide.switchTo().window(0);

File screenshot = Selenide.screenshot(OutputType.FILE);
verifyScreenshotSize(screenshot, width, height);
}
}
Expand Up @@ -20,7 +20,7 @@ public WindowByIndex(int index) {

@Override
@Nullable
public WebDriver apply(@SuppressWarnings("NullableProblems") WebDriver driver) {
public WebDriver apply(WebDriver driver) {
try {
List<String> windowHandles = new ArrayList<>(driver.getWindowHandles());
return driver.switchTo().window(windowHandles.get(index));
Expand Down

0 comments on commit 9d72073

Please sign in to comment.