昨天我们已经在windows/mac上配置好了 playwrigt框架,今天来写代码看看该框架怎么运行。
在写第一段代码之前,补充下上次没有讲完 playwright框架的优点。跨浏览器、跨平台、跨语言、可测试的移动网络。适用于Android和Mobile safiri的Google Chrome 原生移动仿真。相同的渲染引擎适用于桌面和云端。小程序目前还没有尝试。
第一次使用时,需要下载对应各个系统的浏览器,具体代码如下:
我把代码copy出来,一行一行地分析下:
public class PlaywrightInstall {
public static void main(String[] args) throws InterruptedException {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium(); // Can be “msedge”, “chrome-beta”, “msedge-beta”, “msedge-dev”, etc. Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel(“chrome”)); Page page = browser.newPage(); page.navigate(“https://www.baidu.com”); Thread.sleep(5000);// page.navigate(“https://lpt.liepin.com”);// Thread.sleep(3000); } catch (InterruptedException e) {
e.printStackTrace(); }
==========================================
Playwright playwright = Playwright.create()
调用Playwright包中create()方法,返回了PlaywrightImpl类中的create()方法
PlaywrightImpl类中包含多种浏览器和api
BrowserType chromium = playwright.chromium();
指定谷歌浏览器作为测试浏览器
Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel(“chrome”));
设置浏览器的样式,无头模式
Page page = browser.newPage();
定义浏览器的页面
page.navigate(“https://www.baidu.com”);
浏览器地址为: https://www.baidu.com
Thread.sleep(5000);
进程沉睡5秒,页面因为网络等原因,一般都会有loading时间
加上这段代码后,就需要加上catch来捕捉异常
下面我们来运行下这段代码,看看什么效果
运行后会下载chromium浏览器的二进制文件
Build日志如下:
Executing pre-compile tasks…
Running ‘before’ tasks
Checking sources
Copying resources… [playwrightDemo]
Parsing java… [playwrightDemo]
Writing classes… [playwrightDemo]
Checking dependencies… [playwrightDemo]
Dependency analysis found 0 affected files
Updating dependency information… [playwrightDemo]
Adding nullability assertions… [playwrightDemo]
Adding threading assertions… [playwrightDemo]
Adding pattern assertions… [playwrightDemo]
Running ‘after’ tasks
javac 8 was used to compile java sources
Finished, saving caches…
Executing post-compile tasks…
Finished, saving caches…
Synchronizing output directories…
2023/5/27 10:38 – Build completed successfully in 2 sec, 957 ms
浏览器会被调用 百度网页展示
=====================================
如果我想先后打开两个浏览器,应该怎么写代码呢?
package org.example;import com.microsoft.playwright.Browser;import com.microsoft.playwright.BrowserType;import com.microsoft.playwright.Page;import com.microsoft.playwright.Playwright;public class PlaywrightInstall {
public static void main(String[] args) throws InterruptedException {
try (Playwright playwright = Playwright.create()) {
BrowserType chromium = playwright.chromium(); // Can be “msedge”, “chrome-beta”, “msedge-beta”, “msedge-dev”, etc. Browser browser = chromium.launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel(“chrome”)); Page page = browser.newPage(); page.navigate(“https://www.baidu.com”); Thread.sleep(5000);// page.navigate(“https://lpt.liepin.com”);// Thread.sleep(3000); } catch (InterruptedException e) {
e.printStackTrace(); }
//Thread.sleep(3000); PlaywrightInstall playwrightInstall = new PlaywrightInstall(); playwrightInstall.PlaywrightBrower(); }
public void PlaywrightBrower() {
try {
Playwright playwright = Playwright.create(); BrowserType browserType = playwright.chromium(); Browser browser = browserType.launch(new BrowserType.LaunchOptions().setHeadless(false).setChannel(“chrome”)); Page page = browser.newPage(); page.navigate(“https://www.163.com”); Thread.sleep(1000); System.out.println(page.title()); } catch (InterruptedException e){
e.printStackTrace(); }
}
}
这样就可以调用两个浏览器了
===========================================
最后我们尝试下当打开浏览器后,在首页截图这种情况
public void playwrightImage(){
try (Playwright playwright = Playwright.create()) {
BrowserType browserType = playwright.chromium(); Browser browser = browserType.launch(new BrowserType.LaunchOptions().setHeadless(false)); Page page = browser.newPage(); page.navigate(“https://www.baidu.com”); page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(“PlaywrightInstall.png”))); }
}
截图成功
今天先到这里
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
http://www.leapbeyond.com/ric/TSvnPD/ 服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net 机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net相关推荐: …