使用Playwright基于多浏览器进行javascript自动化测试的简单教程- Applitools

解道jdon 2021-05-03 06:33:14
浏览器 使用 进行 浏览 playwright


Playwright由Microsoft创建,是一个开放源代码浏览器自动化框架,使JavaScript工程师可以在Chromium,W​​ebkit和Firefox浏览器上测试其Web应用程序。

 

启动浏览器并导航到应用程序的URL

第一步是要声明您希望针对哪个浏览器引擎运行测试。同样,Playwright允许您针对Chromium,Firefox和Webkit运行测试。

如何针对chromium浏览器:

const { chromium } = require('playwright')

默认情况下,Playwright以无头模式运行。如果您想在执行测试时实际看到浏览器,请在启动调用中将headless设置为false:

browser = await playwright.chromium.launch({headless: false})

接下来,为浏览器和页面对象声明变量。

const { chromium } = require('playwright')
let browser,  page

关于Playwright的重要注意事项是它们的所有API都是异步的,因此需要async / await来调用它们。因此,我利用这种模式来启动浏览器并初始化页面对象。

const { chromium } = require('playwright')
let browser, page
(async () => {
    browser = await chromium.launch()
    page = await browser.newPage()
})()

现在有了页面对象,我可以使用page.goto()转到我的应用程序:

(async () => {
    browser = await chromium.launch()
    page = await browser.newPage()
    await page.goto('https://automationbookstore.dev/')
})()

 

增加断言库

与其他许多自动测试工具一样,关于Playwright的重要注意事项是,它旨在自动执行浏览器交互,但是您必须使用断言工具  进行验证。 在本示例中,我将使用Mocha

const { chromium } = require('playwright')
const assert = require('assert')

我还将添加一个套件(使用'describe'),并将其中的Playwright调用移至before/after函数内。

const { chromium } = require('playwright')
const assert = require('assert')
describe('Automation Bookstore', function () {
    let browser,  page
    before(async () => {
        browser = await chromium.launch()
    })
    after(async () => {
        await browser.close()
    })
    beforeEach(async () => {
        page = await browser.newPage()
        await page.goto('https://automationbookstore.dev/')
    })
    afterEach(async () => {
        await page.close()
    })
})

  

访问元素

在尝试新工具时,我想从一个非常简单的示例开始。基本上,Web测试自动化的“ Hello World”正在验证页面上的标题。因此,让我们从那里开始进行第一个测试!

Playwright提供了许多访问元素的方法,  包括典型的CSS和Xpath选择器。

检查此应用程序的DOM时,显示标题的元素的ID为'page-title',而我要验证的文本是此元素的内部文本。

<h1 id="page-title" class="ui-title" role="heading" aria-level="1">Automation Bookstore</h1>

使用page.innerText获取CSS选择器ID为"#page-title"的值,断言它的结果。

it('should have title', async () => {
    assert.equal(await page.innerText('#page-title'), 'Automation Bookstore')
})

就这样,我进行了第一次测试!

const { chromium } = require('playwright')
const assert = require('assert')
describe('Automation Bookstore', function () {
    let browser,  page
    before(async () => {
        browser = await chromium.launch()
    })
    after(async () => {
        await browser.close()
    })
    beforeEach(async () => {
        page = await browser.newPage()
        await page.goto('https://automationbookstore.dev/')
    })
    afterEach(async () => {
        await page.close()
    })
    it('should have title', async () => {
        assert.equal(await page.innerText('#page-title'), 'Automation Bookstore')
    })
})

如果希望输入搜索文字:

it('should return one book when exact title is given', async () => {
    await page.fill('#searchBar', 'Agile Testing')
})

  

视觉测试

Playwright提供了图像和视频捕获,但它更像是一种可视记录机制,不能在断言本身中使用。因此,我将使用适当的可视化测试断言库Applitools。

安装Applitools后,我指定第3行所需的Applitools类。

const { chromium } = require('playwright')
const assert = require('assert')
const { Eyes, ClassicRunner, Target, RectangleSize} = require('@applitools/eyes-playwright')

我声明并初始化了“eyes”对象(第3行),该对象是进行视觉测试的API。

describe('Automation Bookstore', function () {
  let browser,  page
  const eyes = new Eyes(new ClassicRunner())
  
  //...
})

然后使用视觉测试做一个测试。

it('should be visually perfect', async () => {
    await page.fill('#searchBar', 'test')
    await page.waitForSelector('li.ui-screen-hidden', { state: 'attached' })
    //Visual Testing
    await eyes.open(page, 'Automation Bookstore - Playwright', 'Partial Search', new RectangleSize(800, 600))
    await eyes.check(Target.window().fully())
    await eyes.close()
})

 

跨平台测试

虽然Playwright提供了对Chromium,W​​ebkit和Firefox的支持,但没有对Safari或IE的内置支持。

将Playwright与Applitools的Ultrafast Grid集成后,我现在可以对我的测试进行进一步的介绍了!

与上面使用Applitools Classic Runner相对,我将对其进行修改以使用Visual Grid Runner。在第5行上,请注意,我使用数字10。这表示我希望并行运行的测试数量,从而使运行速度更快!

const {VisualGridRunner, Eyes, Target, Configuration, RectangleSize, BatchInfo, BrowserType, DeviceName, ScreenOrientation} = require('@applitools/eyes-playwright')
describe('Automation Bookstore', function () {
    let browser,  page
    const eyes = new Eyes(new VisualGridRunner(10))
    //...
})

现在好了!我可以指定要使用的所有浏览器,设备和视口-为我的测试提供最终的跨平台覆盖。不仅要在功能上进行验证,而且还要在视觉上进行验证。

beforeEach(async () => {
    page = await browser.newPage()
    await page.goto('https://automationbookstore.dev/')
    const configuration = new Configuration()
    configuration.setBatch(new BatchInfo('Cross Platform Tests'))
    configuration.addBrowser(800, 600, BrowserType.CHROME);
    configuration.addBrowser(700, 500, BrowserType.FIREFOX);
    configuration.addBrowser(1024, 768, BrowserType.EDGE_CHROMIUM);
    configuration.addBrowser(800, 600, BrowserType.SAFARI);
    configuration.addBrowser(1600, 1200, BrowserType.IE_11);
    configuration.addDeviceEmulation(DeviceName.iPhone_X, ScreenOrientation.PORTRAIT);
    eyes.setConfiguration(configuration);
})

测试本身不需要更改,它们将自动在所有指定的配置中运行。

版权声明
本文为[解道jdon]所创,转载请带上原文链接,感谢
https://www.jdon.com/55029

  1. HTML + CSS + JavaScript to achieve cool Fireworks (cloud like particle text 3D opening)
  2. HTML + CSS + JavaScript realizes 520 advertising love tree (including music), which is necessary for programmers to express themselves
  3. Solve the problem of Web front-end deployment server (it can be deployed online without a server)
  4. HTML + CSS + JS make wedding countdown web page template (520 / Tanabata Valentine's Day / programmer advertisement)
  5. What else can driverless minibus do besides "Park connection"?
  6. Cloud native leads the era of all cloud development
  7. NRM mirror source management tool
  8. Bring it to you, flex Jiugong
  9. Lolstyle UI component development practice (II) -- button group component
  10. Deconstruction assignment in ES6
  11. Luo 2 peerless Tang clan was officially launched. The official gave a key point, and the broadcast time was implied
  12. 20初识前端HTML(1)
  13. 当新零售遇上 Serverless
  14. 20 initial knowledge of front-end HTML (1)
  15. When new retail meets serverless
  16. [golang] - go into go language lesson 5 type conversion
  17. [golang] - go into go language lesson 6 conditional expression
  18. HTML5(八)——SVG 之 path 详解
  19. HTML5 (8) -- detailed explanation of SVG path
  20. 需要开通VIP以后页面内容才能复制怎么办?控制台禁用javascript即可
  21. Web前端|CSS入门教程(超详细的CSS使用讲解,适合前端初学者)
  22. 实践积累 —— 用Vue3简单写一个单行横向滚动组件
  23. Serverless 全能选手,再下一城
  24. What if you need to open a VIP to copy the page content? Just disable JavaScript on the console
  25. Web front end | CSS introductory tutorial (super detailed CSS explanation, suitable for front-end beginners)
  26. Practice accumulation - write a single line horizontal scroll component simply with vue3
  27. Dili Reba is thin again. She looks elegant and high in a strapless hollow skirt, and her "palm waist" is beautiful to a new height
  28. Serverless all-round player, next city
  29. The difference between MySQL semi synchronous replication and lossless semi synchronous replication
  30. Vue表单设计器的终极解决方案
  31. The ultimate solution for Vue form designer
  32. Nginx从理论到实践超详细笔记
  33. Yu Shuxin's red backless swimsuit is split to the waist and tail, with a concave convex figure and excessive color matching, and his face is white to dazzling
  34. Nginx ultra detailed notes from theory to practice
  35. 【动画消消乐|CSS】086.炫酷水波浪Loading过渡动画
  36. typecho全站启用https
  37. CCTV has another popular employee. The off-site interpretation is very professional, and the appearance ability is no less than that of Wang Bingbing
  38. [animation Xiaole | CSS] 086. Cool water wave loading transition animation
  39. Enable HTTPS in Typecho
  40. 50天用JavaScript完成50个web项目,我学到了什么?
  41. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  42. What have I learned from completing 50 web projects with JavaScript in 50 days?
  43. "My neighbor doesn't grow up" has hit the whole network. There are countless horse music circles, and actor Zhou Xiaochuan has successfully made a circle
  44. 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
  45. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  46. Implement the Ajax of jQuery according to the native XMLHttpRequest in JavaScript
  47. 30 + women still wear less T-shirts and jeans. If they wear them like stars, they will lose weight
  48. 数栈技术分享前端篇:TS,看你哪里逃~
  49. Several stack technology sharing front end: TS, see where you escape~
  50. 舍弃Kong和Nginx,Apache APISIX 在趣链科技 BaaS 平台的落地实践
  51. Abandon the landing practice of Kong and nginx, Apache apisik on the baas platform of fun chain technology
  52. 浪迹天涯king教你用elementui做复杂的表格,去处理报表数据(合并表头,合并表体行和列)
  53. 前端HTML两万字图文大总结,快来看看你会多少!【️熬夜整理&建议收藏️】
  54. Wandering around the world king teaches you to use elementui to make complex tables and process report data (merge header, merge table body rows and columns)
  55. 路由刷新数据丢失 - vuex数据读取的问题
  56. Front end HTML 20000 word graphic summary, come and see how much you can【 Stay up late to sort out & suggestions]
  57. Route refresh data loss - vuex data reading problem
  58. Systemctl系统启动Nginx服务脚本
  59. Systemctl system startup nginx service script
  60. sleepless