Как автоматически сделать полноэкранный снимок веб-страницы с фиксированным элементом, используя java selenium webdriver ashot firefox - PullRequest
0 голосов
/ 27 февраля 2019

Я использую комбинацию java selenium и ashot, чтобы сделать полноэкранный снимок веб-страницы.

Окружающая среда:

  1. Firefox Quantum 66.0b10 (64-бит)

  2. geckodriver-v0.24.0-win64

  3. jdk-8u201-windows-x64

  4. apache-maven-3.6.0-bin

  5. IntellJ IDEA 2018.3.4

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>
    
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-firefox-driver</artifactId>
            <version>3.141.59</version>
        </dependency>
    
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>htmlunit-driver</artifactId>
            <version>2.33.2</version>
        </dependency>
    
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-server</artifactId>
            <version>3.141.59</version>
        </dependency>
    
        <dependency>
            <groupId>ru.yandex.qatools.ashot</groupId>
            <artifactId>ashot</artifactId>
            <version>1.5.2</version>
        </dependency>
    

Проблема:

К сожалению, есть фиксированный элемент, который продолжает существовать в верхней части страницы при прокрутке, который я не хотел.Я просто хочу, чтобы он показывал один раз при первой прокрутке, а затем скрыл.

Что я пытаюсь:

  1. Я попытался найти код для прокрутки в функции ashot takeScreen () для настройкивысота для прокрутки, но не смог ее найти.
  2. Я попытался изменить встроенный код Ashot, но не разрешил (файл только для чтения)

Сайт для тестирования:

http://www.nettruyen.com/truyen-tranh/kingdom-vuong-gia-thien-ha/chap-590/446770

Код

package com.swtestacademy.webdriver;

//Info: When you write your code IntelliJ automatically adds required classes
//Also you can select and add required classes by pressing ALT+Enter then select related class

import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.shooting.ShootingStrategies;

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


/**
 * Created by ONUR BASKIRT on 26.08.2015.
 */
public class FirstAutomationTest {

//We should add @Test annotation that JUnit will run below method
@Test
//Start to write our test method. It should ends with "Test"
public void firefoxTest() throws InterruptedException, IOException {

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.nettruyen.com/truyen-tranh/kingdom-vuong-gia-thien-ha/chap-590/446770");
    Thread.sleep(3000);
    JavascriptExecutor js = (JavascriptExecutor)driver;
    //How to excute print screen for one time when class "chapter-nav scroll-to-fixed-fixed" is showed
    //then hidden "chapter-nav scroll-to-fixed-fixed"
    //and continue to takeScreenshot()
    js.executeScript("window.scrollTo(0,600)");
    js.executeScript("document.getElementsByClassName(\"chapter-nav scroll-to-fixed-fixed\")[0].classList.add(\"hidden\")");
    Screenshot screenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(100)).takeScreenshot(driver);
    BufferedImage image = screenshot.getImage();
    ImageIO.write(image, "PNG", new File("D:\\" + "DDD.png"));
    driver.close();

}

}

Ожидаемый результат:

Пожалуйста, посмотрите на эти изображения

https://drive.google.com/open?id=1r3tkoqs46RMZuL0U-H2U6Y5d-BB6y-jt

1 Ответ

0 голосов
/ 27 февраля 2019

Вы можете использовать <body> WebElement, чтобы сделать скриншот всей страницы

WebElement body = driver.findElement(By.tagName("body"));
File scrFile = ((TakesScreenshot)body).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\tmp\\screenshot.png"));
...