1.简介
通过前边两篇文章跟随宏哥学习想必到这里已经将环境搭建好了,今天就在Java项目搭建环境中简单地实践一下: 启动两大浏览器。按市场份额来说,全球前三大浏览器是:IE.Firefox.Chrome。但是微软已经在Win10中不维护IE浏览器了,用Edge浏览器代替或者兼容IE模式的浏览器,因此宏哥这里主要介绍一下如何在Windows系统中启动这两大浏览器即可,其他平台系统的浏览器类似的启动方法,照猫画虎就可以了。但是在实践过程中,宏哥发现Java语言的Playwright目前好像支持三款浏览器:Chrome、Firefox和Webkit,索性就全部讲解和介绍了吧。可能有的小伙伴或者童鞋们不相信就仅仅支持三款浏览器,也可能是宏哥理解错了,欢迎纠错哦!下边说一下宏哥的依据。
1.1下载的浏览器
跟随宏哥首先看一下,我们运行代码下载的浏览器,只有三款:Chrome、Firefox和Webkit。如下图所示:

1.2BrowserType(浏览器类型)
再跟随随宏哥,看一下代码中提供的浏览器类型就三个:chromium()、firefox()和webkit(),如下图所示:

2.启动浏览器准备工作
2.1准备工作
创建一个Playwright的Java项目,用来练习,具体步骤如下:
1.打开Eclipse,创建一个名称为bjhg_playwright的Java 项目,如下图所示:

2.选择项目,右键项目,选择Build Path,然后选择Confige Build Path...,弹出框点击Libraries,如下图所示:

3.这里点击Add External JARs...,弹出框,选择当前电脑本地之前下载好的所有与playwright相关的jar包,添加到新建的项目中,如下图所示:

4.点击"OK",如下图所示:

准备工作就介绍到这里,下边介绍如何启动两大浏览器。
3.启动Chrome浏览器
大致思路:打开Chrome浏览器,访问百度网址,获取网址的title,然后再关闭Chrome浏览器。根据思路进行代码设计。
3.1代码设计

3.2参考代码
package bjhg_playwright;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
/**
* @author 北京-宏哥
*
* @公众号:北京宏哥(微信搜索,关注宏哥,提前解锁更多测试干货)
*
* 《刚刚问世》系列初窥篇-Java+Playwright自动化测试-3-启动浏览器(详细教程)
*
* 2024年7月09日
*/
public class LaunchChrome {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(50));
Page page = browser.newPage();
page.navigate("https://www.baidu.com/");
System.out.println(page.title());
page.close();
}
}
}
3.3运行代码
1.运行代码,右键Run AS->java Application,就可以看到控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作。如下图所示:

4.启动Firefox浏览器
大致思路和启动Chrome浏览器相同:打开Firefox浏览器,访问百度网址,获取网址的title,然后再关闭Firefox浏览器。根据思路进行代码设计。
4.1代码设计

4.2参考代码
package bjhg_playwright;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
/**
* @author 北京-宏哥
*
* @公众号:北京宏哥(微信搜索,关注宏哥,提前解锁更多测试干货)
*
* 《刚刚问世》系列初窥篇-Java+Playwright自动化测试-3-启动浏览器(详细教程)
*
* 2024年7月09日
*/
public class LaunchFirefox {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(50));
Page page = browser.newPage();
page.navigate("https://www.baidu.com/");
System.out.println(page.title());
page.close();
}
}
}
4.3运行代码
1.运行代码,右键Run AS->java Application,就可以看到控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作。如下图所示:

5.启动webkit浏览器
大致思路和启动Chrome浏览器相同:打开webkit浏览器,访问百度网址,获取网址的title,然后再关闭webkit浏览器。根据思路进行代码设计。
5.1代码设计

5.2参考代码
package bjhg_playwright;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
/**
* @author 北京-宏哥
*
* @公众号:北京宏哥(微信搜索,关注宏哥,提前解锁更多测试干货)
*
* 《刚刚问世》系列初窥篇-Java+Playwright自动化测试-3-启动浏览器(详细教程)
*
* 2024年7月09日
*/
public class LaunchWebkit {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
Browser browser = playwright.webkit().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(50));
Page page = browser.newPage();
page.navigate("https://www.baidu.com/");
System.out.println(page.title());
page.close();
}
}
}
5.3运行代码
1.运行代码,右键Run AS->java Application,就可以看到控制台输出,如下图所示:

2.运行代码后电脑端的浏览器的动作。如下图所示:

6.关键代码说明
package bjhg_playwright;
import com.microsoft.playwright.Browser;
import com.microsoft.playwright.BrowserType;
import com.microsoft.playwright.Page;
import com.microsoft.playwright.Playwright;
/**
* @author 北京-宏哥
*
* @公众号:北京宏哥(微信搜索,关注宏哥,提前解锁更多测试干货)
*
* 《刚刚问世》系列初窥篇-Java+Playwright自动化测试-3-启动浏览器(详细教程)
*
* 2024年7月09日
*/
public class LaunchChrome {
public static void main(String[] args) {
try (Playwright playwright = Playwright.create()) {
//使用chromium浏览器,# 浏览器配置,设置以GUI模式启动Chrome浏览器(要查看浏览器UI,在启动浏览器时传递 headless=false 标志。您还可以使用 slowMo 来减慢执行速度。
Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false).setSlowMo(50));
//创建page
Page page = browser.newPage();
//浏览器打开百度
page.navigate("https://www.baidu.com/");
//打印title
System.out.println(page.title());
//关闭page
page.close();
}
}
}
通过上边在代码中增加的注释,想必小伙伴或者童鞋们就比较好理解了,也知道宏哥在第一篇文章运行代码时,为何没有看到浏览器UI了吧!
7.小结
宏哥因为之前做过python、java语言和selenium,经常碰到头疼的问题就是:出现浏览器版本和驱动版本匹配的问题,新手一定要注意这个问题。但是playwright无论是Java还是python语言,无论是新手还是老鸟就都不需要担心这些问题了,而且今天讲解和分享的非常简单,就是简单换个方法就可以启动不同的浏览器了。好了,今天关于三大浏览器的驱动宏哥就分享到这里,感谢你耐心的阅读。