A simple tutorial for JavaScript automatic testing based on multi browser using playwright - applitools

Jiedao jdon 2021-05-03 06:33:55
simple tutorial javascript automatic testing


Playwright from Microsoft establish , Is an open source browser automation framework , send JavaScript The engineer can be in Chromium,W​​ebkit and Firefox Test it on the browser Web Applications .

 

Start the browser and navigate to the application's URL

The first step is to state which browser engine you want to run the test against . Again ,Playwright Allows you to target Chromium,Firefox and Webkit Run the test .

How to chromium browser :

const { chromium } = require('playwright')

By default ,Playwright Run in headless mode . If you want to actually see the browser when you run the test , Please set... In the startup call headless Set to false:

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

Next , Declare variables for browser and page objects .

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

About Playwright The important thing to note is that all of them API It's all asynchronous , Therefore need async / await To call them . therefore , I use this mode to start the browser and initialize the page object .

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

Now we have the page object , I can use page.goto() Go to my app :

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

 

Add assertion Library

Like many other automated testing tools , About Playwright The important thing to pay attention to is , It's designed to automate browser interaction , But you have to use Assertion tool    To verify .  In this example , I will use Mocha.

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

I'll also add a suite ( Use 'describe'), And take one of them Playwright Call to before/after Within the function .

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()
    })
})

  

Access elements

When trying new tools , I want to start with a very simple example . Basically ,Web Test Automation “ Hello World” Verifying title on page . therefore , Let's start with the first test from there !

Playwright Provides Many ways to access elements ,   Including the typical CSS and Xpath Selectors .

Check the configuration of this application DOM when , Display the elements of the title ID by 'page-title', And the text I want to verify is the internal text of this element .

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

Use page.innerText obtain CSS Selectors ID by "#page-title" Value , Assert its results .

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

That's it , I did my first test !

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')
    })
})

If you want to enter search text :

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

  

Visual tests

Playwright Provides image and video capture , But it's more like a visual recording mechanism , Cannot be used in the assertion itself . therefore , I'll use the appropriate visual test assertion Library Applitools.

install Applitools after , I designated the third 3 Do what you need Applitools class .

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

I declare and initialize “eyes” object ( The first 3 That's ok ), The object is tested visually API.

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

Then use the visual test to do a test .

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()
})

 

Cross platform testing

although Playwright Provide for the right to Chromium,W​​ebkit and Firefox Support for , But it's not true Safari or IE Built in support for .

take Playwright And Applitools Of Ultrafast Grid After the integration , I can now go a step further on my tests !

Use with the above Applitools Classic Runner relative , I will modify it to use Visual Grid Runner. In the 5 Go ahead , Please note that , I use numbers 10. This means the number of tests I want to run in parallel , So that it runs faster !

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))
    //...
})

Ok now ! I can specify all browsers to use , Devices and views - Provide final cross platform coverage for my testing . It's not just about functional validation , And it has to be visually verified .

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);
})

The test itself doesn't need to be changed , They will automatically run in all specified configurations .

版权声明
本文为[Jiedao jdon]所创,转载请带上原文链接,感谢
https://qdmana.com/2021/05/20210503063308795a.html

  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