Skip to content

Commit

Permalink
test: 添加等待图片加载完毕再截图的测试方法
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong920 committed Dec 26, 2024
1 parent 8aab6ef commit 88e38ab
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion example/src/main/java/com/ruiyun/example/Q_ScreenshotTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@
import com.ruiyun.jvppeteer.api.core.Browser;
import com.ruiyun.jvppeteer.api.core.ElementHandle;
import com.ruiyun.jvppeteer.api.core.Page;
import com.ruiyun.jvppeteer.api.events.PageEvents;
import com.ruiyun.jvppeteer.cdp.core.Puppeteer;
import com.ruiyun.jvppeteer.cdp.entities.ConsoleMessage;
import com.ruiyun.jvppeteer.cdp.entities.ElementScreenshotOptions;
import com.ruiyun.jvppeteer.cdp.entities.FrameAddStyleTagOptions;
import com.ruiyun.jvppeteer.cdp.entities.GoToOptions;
import com.ruiyun.jvppeteer.cdp.entities.ImageType;
import com.ruiyun.jvppeteer.cdp.entities.ScreenshotOptions;
import com.ruiyun.jvppeteer.cdp.entities.WaitForOptions;
import com.ruiyun.jvppeteer.cdp.entities.WaitForSelectorOptions;
import com.ruiyun.jvppeteer.common.PuppeteerLifeCycle;
import com.ruiyun.jvppeteer.util.Helper;
import java.util.Collections;
import java.util.function.Consumer;
import org.junit.Test;

public class Q_ScreenshotTest extends A_LaunchTest {
Expand Down Expand Up @@ -71,6 +77,7 @@ public void test5() throws Exception {
page.screenshot(screenshotOptions);
cdpBrowser.close();
}

//某个元素截图
@Test
public void test6() throws Exception {
Expand Down Expand Up @@ -130,12 +137,56 @@ public void test8() throws Exception {
Page page = browser.newPage();
GoToOptions options = new GoToOptions();
options.setWaitUntil(Collections.singletonList(PuppeteerLifeCycle.networkIdle));
page.goTo("https://www.baidu.com/s?wd=jvppeteer&rsv_spt=1&rsv_iqid=0x864033a90040d9e7&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=68018901_16_pg&rsv_enter=1&rsv_dl=tb&rsv_sug3=9&rsv_sug1=7&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=1778&rsv_sug4=1778",options);
page.goTo("https://www.baidu.com/s?wd=jvppeteer&rsv_spt=1&rsv_iqid=0x864033a90040d9e7&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&tn=68018901_16_pg&rsv_enter=1&rsv_dl=tb&rsv_sug3=9&rsv_sug1=7&rsv_sug7=100&rsv_sug2=0&rsv_btype=i&inputT=1778&rsv_sug4=1778", options);
ElementScreenshotOptions screenshotOptions = new ElementScreenshotOptions();
screenshotOptions.setFromSurface(true);
screenshotOptions.setPath("long.png");
screenshotOptions.setFullPage(true);
page.screenshot(screenshotOptions);
browser.close();
}


//等待图片加载完毕
@Test
public void test9() throws Exception {
launchOptions.setDevtools(true);
Browser browser = Puppeteer.launch(launchOptions);
//打开一个页面
Page page = browser.newPage();
page.on(PageEvents.Console, (Consumer<ConsoleMessage>) message -> System.out.println("console: "+message.text()));
WaitForOptions waitForOptions = new WaitForOptions();
waitForOptions.setWaitUntil(Collections.singletonList(PuppeteerLifeCycle.networkIdle));

page.setContent(" <!DOCTYPE html>\n" +
"<html lang=\"en\">\n" +
"<head>\n" +
" <meta charset=\"UTF-8\">\n" +
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
" <title>Icon Home Example</title>\n" +
"</head>\n" +
"<body>\n" +
" <h1>Home Icon Example</h1>\n" +
" <img id=\"icon-home\" src='https://cdn-icons-png.flaticon.com/512/3222/3222434.png'></img>\n" +
"</body>\n" +
"</html>", waitForOptions);
WaitForSelectorOptions options = new WaitForSelectorOptions();
options.setVisible(true);
ElementHandle elementHandle = page.waitForSelector("#icon-home",options);
while (true){
Object complete = elementHandle.evaluate("(element) => {\n" +
" return element.complete\n" +
"}");
if((boolean)complete){
break;
}
}

ElementScreenshotOptions screenshotOptions = new ElementScreenshotOptions();
screenshotOptions.setFromSurface(true);
screenshotOptions.setPath("图片.png");
screenshotOptions.setFullPage(true);
page.screenshot(screenshotOptions);
browser.close();
}
}

0 comments on commit 88e38ab

Please sign in to comment.