Best JavaScript code snippet using playwright-internal
AsyncTestRunnerPluginTest.js
Source:AsyncTestRunnerPluginTest.js  
1/*2 * Copyright 2010 Google Inc.3 *4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not5 * use this file except in compliance with the License. You may obtain a copy of6 * the License at7 *8 * http://www.apache.org/licenses/LICENSE-2.09 *10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the13 * License for the specific language governing permissions and limitations under14 * the License.15 */16var asyncTestRunnerPluginTest = TestCase('asyncTestRunnerPluginTest');17/**18 * Regression test for Issue 125:19 *     "Error in asynchronous tests:20 *      [Object object].onTestRunConfigurationComplete_ is null."21 * @bug 12522 */23asyncTestRunnerPluginTest.prototype.24    testMultipleAsyncTestCases = function() {25  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(26      Date, function() {}, function() {}, false, function(callback) {callback();});27  var testCase0 = function() {};28  testCase0.prototype.test0 = function(queue) {};29  var info0 = new jstestdriver.TestCaseInfo(30      'testCase0', testCase0, jstestdriver.TestCaseInfo.ASYNC_TYPE);31  var config0 = {32    getTestCaseInfo: function() {return info0;},33    getTests: function() {return ['test0'];}34  };35  // ContinueTesting is the vital element to reproduce Issue 125. We use it to36  // pause the second AsyncTestCase, testCase1, which allows the stack to37  // unwind and return from the AsyncTestRunnerPlugin's first38  // onRunTestConfigurationComplete_() invocation for testCase0 and then set the39  // onRunTestConfigurationComplete_ to null. Setting to null interferes with40  // the second test case's execution, as it's paused, and causes the error:41  //     "this.onTestRunConfigurationComplete_ is not a function."42  var continueTesting;43  var testCase1 = function() {};44  testCase1.prototype.test1 = function(queue) {45    queue.defer(function(pool) {46      continueTesting = pool.add(function() {});47    });48  };49  var info1 = new jstestdriver.TestCaseInfo(50      'testCase1', testCase1, jstestdriver.TestCaseInfo.ASYNC_TYPE);51  var config1 = {52    getTestCaseInfo: function() {return info1;},53    getTests: function() {return ['test1'];}54  };55  var finalOnRunTestConfigurationCompleteIsCalled = false;56  var onTestRunConfigurationComplete = function() {57    asyncTestRunner.runTestConfiguration(config1, function() {}, function() {58      finalOnRunTestConfigurationCompleteIsCalled = true;59    });60  };61  asyncTestRunner.runTestConfiguration(62      config0, function() {}, onTestRunConfigurationComplete);63  continueTesting();64  assertTrue(finalOnRunTestConfigurationCompleteIsCalled);65};66/**67 * Regression test for Issue 137: "expectAsserts does not work with68 *    AsyncTestCase."69 * @bug 13770 */71asyncTestRunnerPluginTest.prototype.72    testExpectedAsserts_correctAmount = function() {73  // save expected assert state74  var priorExpectedAssertCount = jstestdriver.expectedAssertCount;75  var priorAssertCount = jstestdriver.assertCount;76  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(77      Date, function() {}, function() {}, false, function(callback) {callback();});78  var testCase = function() {};79  testCase.prototype.testWithExpectAsserts = function() {80    expectAsserts(3);81    assertEquals('a', 'a');82    assertTrue(true);83    assertNull(null);84  };85  var info = new jstestdriver.TestCaseInfo(86      'testCase', testCase, jstestdriver.TestCaseInfo.ASYNC_TYPE);87  var config = {88    getTestCaseInfo: function() {return info;},89    getTests: function() {return ['testWithExpectAsserts'];}90  };91  var result;92  var onTestDone = function(r) {93    result = r;94  };95  asyncTestRunner.runTestConfiguration(config, onTestDone, function() {});96  // restore expected assert state before we really assert97  jstestdriver.expectedAssertCount = priorExpectedAssertCount;98  jstestdriver.assertCount = priorAssertCount;99  assertEquals('passed', result.result);100};101/**102 * Regression test for Issue 137: "expectAsserts does not work with103 *    AsyncTestCase."104 * @bug 137105 */106asyncTestRunnerPluginTest.prototype.107    testExpectedAsserts_incorrectAmount = function() {108  // save expected assert state109  var priorExpectedAssertCount = jstestdriver.expectedAssertCount;110  var priorAssertCount = jstestdriver.assertCount;111  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(112      Date,113      function() {},114      jstestdriver.utils.serializeErrors,115      false,116      function(callback) {callback();});117  var testCase = function() {};118  testCase.prototype.testWithExpectAsserts = function() {119    expectAsserts(5);120    assertEquals('a', 'a');121    assertTrue(true);122    assertNull(null);123  };124  var info = new jstestdriver.TestCaseInfo(125      'testCase', testCase, jstestdriver.TestCaseInfo.ASYNC_TYPE);126  var config = {127    getTestCaseInfo: function() {return info;},128    getTests: function() {return ['testWithExpectAsserts'];}129  };130  var result;131  var onTestDone = function(r) {132    result = r;133  };134  asyncTestRunner.runTestConfiguration(config, onTestDone, function() {});135  // restore expected assert state before we really assert136  jstestdriver.expectedAssertCount = priorExpectedAssertCount;137  jstestdriver.assertCount = priorAssertCount;138  assertEquals('failed', result.result);139  assertEquals('Expected \'5\' asserts but \'3\' encountered.',140      JSON.parse(result.message)[0].message);141};142asyncTestRunnerPluginTest.prototype.testScopeIsNotWindow = function() {143  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(144      Date, function() {}, function() {}, false, function(callback) {callback();});145  var testCase = function() {};146  var testCaseInstance;147  var setUpScope;148  testCase.prototype.setUp = function() {149    testCaseInstance = asyncTestRunner.testCase_;150    setUpScope = this;151  };152  var testMethodScope;153  testCase.prototype.testMethod = function() {154    testMethodScope = this;155  };156  var tearDownScope;157  testCase.prototype.tearDown = function() {158    tearDownScope = this;159  };160  var testCaseInfo = new jstestdriver.TestCaseInfo(161      'testCase', testCase, jstestdriver.TestCaseInfo.ASYNC_TYPE);162  var testRunConfiguration = {};163  testRunConfiguration.getTestCaseInfo = function() {return testCaseInfo;};164  testRunConfiguration.getTests = function() {return ['testMethod'];};165  asyncTestRunner.runTestConfiguration(166      testRunConfiguration, function() {}, function() {});167  assertFalse('window === setUpScope', window === setUpScope);168  assertFalse('window === testMethodScope', window === testMethodScope);169  assertFalse('window === tearDownScope', window === tearDownScope);170  assertTrue('testCaseInstance === setUpScope',171      testCaseInstance === setUpScope);172  assertTrue('testCaseInstance === testMethodScope',173      testCaseInstance === testMethodScope);174  assertTrue('testCaseInstance === tearDownScope',175      testCaseInstance === tearDownScope);176};177asyncTestRunnerPluginTest.prototype.testTestCaseWithWrongType = function() {178  var testCase = function() {};179  testCase.prototype.setUp = function() {};180  testCase.prototype.testMethod = function() {};181  testCase.prototype.tearDown = function() {};182  // Using DEFAULT_TYPE instead of ASYNC_TYPE183  var testCaseInfo = new jstestdriver.TestCaseInfo(184      'testCase', testCase, jstestdriver.TestCaseInfo.DEFAULT_TYPE);185  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(186      Date, function() {}, function() {}, false, function(callback) {callback();});187  var testRunConfiguration = {};188  testRunConfiguration.getTestCaseInfo = function() {return testCaseInfo;};189  testRunConfiguration.getTests = function() {return ['testMethod'];};190  var testCaseAccepted = asyncTestRunner.runTestConfiguration(191      testRunConfiguration, function() {}, function() {});192  assertFalse(testCaseAccepted);193};194asyncTestRunnerPluginTest.prototype.testTestCaseWithoutSteps = function() {195  var timesSetUpCalled = 0;196  var timesTestMethodCalled = 0;197  var timesTearDownCalled = 0;198  var testCase = function() {};199  testCase.prototype.setUp = function() {timesSetUpCalled += 1;};200  testCase.prototype.testMethod = function() {timesTestMethodCalled += 1;};201  testCase.prototype.tearDown = function() {timesTearDownCalled += 1;};202  var testCaseInfo = new jstestdriver.TestCaseInfo(203      'testCase', testCase, jstestdriver.TestCaseInfo.ASYNC_TYPE);204  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(205      Date, function() {}, function() {}, false, function(callback) {callback();});206  var testDone = false;207  var testResult;208  var onTestDone = function(result) {209    testResult = result;210    testDone = true;211  };212  var testRunConfigurationComplete = false;213  var onTestRunConfigurationComplete = function() {214    testRunConfigurationComplete = true;215  };216  var testRunConfiguration = {};217  testRunConfiguration.getTestCaseInfo = function() {return testCaseInfo;};218  testRunConfiguration.getTests = function() {return ['testMethod'];};219  var testCaseAccepted = asyncTestRunner.runTestConfiguration(220      testRunConfiguration, onTestDone, onTestRunConfigurationComplete);221  assertTrue(testCaseAccepted);222  assertEquals(1, timesSetUpCalled);223  assertEquals(1, timesTestMethodCalled);224  assertEquals(1, timesTearDownCalled);225  assertTrue(testDone);226  assertTrue(testRunConfigurationComplete);227  assertEquals('passed', testResult.result);228};229asyncTestRunnerPluginTest.prototype.230    testTestCaseWithoutStepsWithSetupError = function() {231  var timesSetUpCalled = 0;232  var timesTestMethodCalled = 0;233  var timesTearDownCalled = 0;234  var testCase = function() {};235  testCase.prototype.setUp = function() {timesSetUpCalled += 1; throw 'error';};236  testCase.prototype.testMethod = function() {timesTestMethodCalled += 1;};237  testCase.prototype.tearDown = function() {timesTearDownCalled += 1;};238  var testCaseInfo = new jstestdriver.TestCaseInfo(239      'testCase', testCase, jstestdriver.TestCaseInfo.ASYNC_TYPE);240  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(241      Date, function() {}, function() {}, false, function(callback) {callback();});242  var testDone = false;243  var testResult;244  var onTestDone = function(result) {245    testResult = result;246    testDone = true;247  };248  var testRunConfigurationComplete = false;249  var onTestRunConfigurationComplete = function() {250    testRunConfigurationComplete = true;251  };252  var testRunConfiguration = {};253  testRunConfiguration.getTestCaseInfo = function() {return testCaseInfo;};254  testRunConfiguration.getTests = function() {return ['testMethod'];};255  var testCaseAccepted = asyncTestRunner.runTestConfiguration(256      testRunConfiguration, onTestDone, onTestRunConfigurationComplete);257  assertTrue(testCaseAccepted);258  assertEquals(1, timesSetUpCalled);259  assertEquals(0, timesTestMethodCalled);260  assertEquals(1, timesTearDownCalled);261  assertTrue(testDone);262  assertTrue(testRunConfigurationComplete);263  assertEquals('failed', testResult.result);264};265asyncTestRunnerPluginTest.prototype.testTestCaseWithSteps = function() {266  var timesSetUpCalled = 0;267  var timesSetUpStepCalled = 0;268  var timesTestMethodCalled = 0;269  var timesTestMethodStepCalled = 0;270  var timesTearDownCalled = 0;271  var timesTearDownStepCalled = 0;272  var testCase = function() {};273  testCase.prototype.setUp = function(q) {274    timesSetUpCalled += 1;275    q.defer(function() {timesSetUpStepCalled += 1;});276  };277  testCase.prototype.testMethod = function(q) {278    timesTestMethodCalled += 1;279    q.defer(function() {timesTestMethodStepCalled += 1;});280  };281  testCase.prototype.tearDown = function(q) {282    timesTearDownCalled += 1;283    q.defer(function() {timesTearDownStepCalled += 1;});284  };285  var testCaseInfo = new jstestdriver.TestCaseInfo(286      'testCase', testCase, jstestdriver.TestCaseInfo.ASYNC_TYPE);287  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(288      Date, function() {}, function() {}, false, function(callback) {callback();});289  var testDone = false;290  var testResult;291  var onTestDone = function(result) {292    testResult = result;293    testDone = true;294  };295  var testRunConfigurationComplete = false;296  var onTestRunConfigurationComplete = function() {297    testRunConfigurationComplete = true;298  };299  var testRunConfiguration = {};300  testRunConfiguration.getTestCaseInfo = function() {return testCaseInfo;};301  testRunConfiguration.getTests = function() {return ['testMethod'];};302  var testCaseAccepted = asyncTestRunner.runTestConfiguration(303      testRunConfiguration, onTestDone, onTestRunConfigurationComplete);304  assertTrue(testCaseAccepted);305  assertEquals(1, timesSetUpCalled);306  assertEquals(1, timesSetUpStepCalled);307  assertEquals(1, timesTestMethodCalled);308  assertEquals(1, timesTestMethodStepCalled);309  assertEquals(1, timesTearDownCalled);310  assertEquals(1, timesTearDownStepCalled);311  assertTrue(testDone);312  assertTrue(testRunConfigurationComplete);313  assertEquals('passed', testResult.result);314};315asyncTestRunnerPluginTest.prototype.testTestCaseWithErrback = function() {316  var timesTestMethodCalled = 0;317  var timesTestMethodStepCalled = 0;318  var testCase = function() {};319  testCase.prototype.testMethod = function(driver) {320    timesTestMethodCalled += 1;321    driver.call(function(callbacks) {322      timesTestMethodStepCalled += 1;323      callbacks.addErrback()(new Error('whoopsie daisy'));324    });325  };326  var testCaseInfo = new jstestdriver.TestCaseInfo(327      'testCase', testCase, jstestdriver.TestCaseInfo.ASYNC_TYPE);328  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(329      Date, function() {}, function() {}, false, function(callback) {callback();});330  var testDone = false;331  var testResult;332  var onTestDone = function(result) {333    testResult = result;334    testDone = true;335  };336  var testRunConfigurationComplete = false;337  var onTestRunConfigurationComplete = function() {338    testRunConfigurationComplete = true;339  };340  var testRunConfiguration = {};341  testRunConfiguration.getTestCaseInfo = function() {return testCaseInfo;};342  testRunConfiguration.getTests = function() {return ['testMethod'];};343  var testCaseAccepted = asyncTestRunner.runTestConfiguration(344      testRunConfiguration, onTestDone, onTestRunConfigurationComplete);345  assertTrue(testCaseAccepted);346  assertEquals(1, timesTestMethodCalled);347  assertEquals(1, timesTestMethodStepCalled);348  assertTrue(testDone);349  assertTrue(testRunConfigurationComplete);350  assertEquals('failed', testResult.result);351};352asyncTestRunnerPluginTest.prototype.353    testTestCaseWithStepsWithSetupError = function() {354  var timesSetUpCalled = 0;355  var timesSetUpStepCalled = 0;356  var timesTestMethodCalled = 0;357  var timesTestMethodStepCalled = 0;358  var timesTearDownCalled = 0;359  var timesTearDownStepCalled = 0;360  var testCase = function() {};361  testCase.prototype.setUp = function(q) {362    timesSetUpCalled += 1;363    q.defer(function() {timesSetUpStepCalled += 1; throw 'error';});364  };365  testCase.prototype.testMethod = function(q) {366    timesTestMethodCalled += 1;367    q.defer(function() {timesTestMethodStepCalled += 1;});368  };369  testCase.prototype.tearDown = function(q) {370    timesTearDownCalled += 1;371    q.defer(function() {timesTearDownStepCalled += 1;});372  };373  var testCaseInfo = new jstestdriver.TestCaseInfo(374      'testCase', testCase, jstestdriver.TestCaseInfo.ASYNC_TYPE);375  var asyncTestRunner = new jstestdriver.plugins.async.AsyncTestRunnerPlugin(376      Date, function() {}, function() {}, false, function(callback) {callback();});377  var testDone = false;378  var testResult;379  var onTestDone = function(result) {380    testResult = result;381    testDone = true;382  };383  var testRunConfigurationComplete = false;384  var onTestRunConfigurationComplete = function() {385    testRunConfigurationComplete = true;386  };387  var testRunConfiguration = {};388  testRunConfiguration.getTestCaseInfo = function() {return testCaseInfo;};389  testRunConfiguration.getTests = function() {return ['testMethod'];};390  var testCaseAccepted = asyncTestRunner.runTestConfiguration(391      testRunConfiguration, onTestDone, onTestRunConfigurationComplete);392  assertTrue(testCaseAccepted);393  assertEquals(1, timesSetUpCalled);394  assertEquals(1, timesSetUpStepCalled);395  assertEquals(0, timesTestMethodCalled);396  assertEquals(0, timesTestMethodStepCalled);397  assertEquals(1, timesTearDownCalled);398  assertEquals(1, timesTearDownStepCalled);399  assertTrue(testDone);400  assertTrue(testRunConfigurationComplete);401  assertEquals('failed', testResult.result);...Using AI Code Generation
1const { test } = require('@playwright/test');2test.describe('Sample test', () => {3  test.beforeEach(async ({ page }) => {4  });5  test.afterEach(async ({ page }) => {6    await page.context().teardownScope(page);7  });8  test('should display correct text content', async ({ page }) => {9    const text = page.locator('text=Create a browser automation script in 5 minutes');10    await expect(text).toBeVisible();11  });12});Using AI Code Generation
1const { test } = require('@playwright/test');2test.use({ storageState: 'storage.json' });3test.beforeEach(async ({ page }) => {4});5test('test', async ({ page }) => {6  await page.click('text=Docs');7  await page.click('text=API');8  await page.click('text=Page');9  await page.click('text=page.$eval');10  await page.click('text=page.$eval');11  await page.click('text=page.$eval');12  await page.click('text=page.$eval');13  await page.click('text=page.$eval');14  await page.click('text=page.$eval');15  await page.click('text=page.$eval');16  await page.click('text=page.$eval');17  await page.click('text=page.$eval');18  await page.click('text=page.$eval');19});20test.afterEach(async ({ teardownScope }) => {21  await teardownScope();22});Using AI Code Generation
1const { test } = require('@playwright/test');2test.describe('My Test Suite', () => {3  test.beforeEach(async ({page}) => {4  });5  test.afterEach(async ({teardownScope}) => {6    await teardownScope();7  });8  test('My Test', async ({page}) => {9  });10});11const { test } = require('@playwright/test');12test('My Test', async ({page}) => {13}, { reuseExistingServer: true });14const { test } = require('@playwright/test');15test.describe('My Test Suite', () => {16  test('My Test', async ({page}) => {17  });18}, { reuseExistingServer: true });19const { test } = require('@playwright/test');20test('My Test', async ({page}) => {21}, { browserName: 'firefox' });22const { test } = require('@playwright/test');23test('My Test', async ({page}) => {24}, { browserVersion: '91.0' });LambdaTest’s Playwright tutorial will give you a broader idea about the Playwright automation framework, its unique features, and use cases with examples to exceed your understanding of Playwright testing. This tutorial will give A to Z guidance, from installing the Playwright framework to some best practices and advanced concepts.
Get 100 minutes of automation test minutes FREE!!
