Best JavaScript code snippet using chromeless
local-runtime.ts
Source:local-runtime.ts  
...80        return this.returnScreenshot(command.selector, command.options)81      case 'returnHtml':82        return this.returnHtml()83      case 'returnHtmlUrl':84        return this.returnHtmlUrl()85      case 'returnPdf':86        return this.returnPdf(command.options)87      case 'returnInputValue':88        return this.returnInputValue(command.selector)89      case 'type':90        return this.type(command.input, command.selector)91      case 'press':92        return this.press(command.keyCode, command.count, command.modifiers)93      case 'scrollTo':94        return this.scrollTo(command.x, command.y)95      case 'scrollToElement':96        return this.scrollToElement(command.selector)97      case 'deleteCookies':98        return this.deleteCookies(command.name, command.url)99      case 'clearCookies':100        return this.clearCookies()101      case 'setHtml':102        return this.setHtml(command.html)103      case 'setExtraHTTPHeaders':104        return this.setExtraHTTPHeaders(command.headers)105      case 'cookies':106        return this.cookies(command.nameOrQuery)107      case 'allCookies':108        return this.allCookies()109      case 'setCookies':110        return this.setCookies(command.nameOrCookies, command.value)111      case 'mousedown':112        return this.mousedown(command.selector)113      case 'mouseup':114        return this.mouseup(command.selector)115      case 'focus':116        return this.focus(command.selector)117      case 'clearInput':118        return this.clearInput(command.selector)119      case 'setFileInput':120        return this.setFileInput(command.selector, command.files)121      default:122        throw new Error(`No such command: ${JSON.stringify(command)}`)123    }124  }125  private async goto(url: string): Promise<void> {126    const { Network, Page } = this.client127    await Promise.all([Network.enable(), Page.enable()])128    if (!this.userAgentValue) this.userAgentValue = `Chromeless ${version}`129    await Network.setUserAgentOverride({ userAgent: this.userAgentValue })130    await Page.navigate({ url })131    await Page.loadEventFired()132    this.log(`Navigated to ${url}`)133  }134  private async clearCache(): Promise<void> {135    const { Network } = this.client136    const canClearCache = await Network.canClearBrowserCache137    if (canClearCache) {138      await Network.clearBrowserCache()139      this.log(`Cache is cleared`)140    } else {141      this.log(`Cache could not be cleared`)142    }143  }144  private async clearStorage(145    origin: string,146    storageTypes: string,147  ): Promise<void> {148    const { Storage, Network } = this.client149    const canClearCache = await Network.canClearBrowserCache150    if (canClearCache) {151      await Storage.clearDataForOrigin({ origin, storageTypes })152      this.log(`${storageTypes} for ${origin} is cleared`)153    } else {154      this.log(`${storageTypes} could not be cleared`)155    }156  }157  private async setUserAgent(useragent: string): Promise<void> {158    this.userAgentValue = useragent159    await this.log(`Set useragent to ${this.userAgentValue}`)160  }161  private async waitTimeout(timeout: number): Promise<void> {162    this.log(`Waiting for ${timeout}ms`)163    await wait(timeout)164  }165  private async waitSelector(166    selector: string,167    waitTimeout: number = this.chromelessOptions.waitTimeout,168  ): Promise<void> {169    this.log(`Waiting for ${selector} ${waitTimeout}`)170    await waitForNode(this.client, selector, waitTimeout)171    this.log(`Waited for ${selector}`)172  }173  private async click(selector: string): Promise<void> {174    if (this.chromelessOptions.implicitWait) {175      this.log(`click(): Waiting for ${selector}`)176      await waitForNode(177        this.client,178        selector,179        this.chromelessOptions.waitTimeout,180      )181    }182    const exists = await nodeExists(this.client, selector)183    if (!exists) {184      throw new Error(`click(): node for selector ${selector} doesn't exist`)185    }186    const { scale } = this.chromelessOptions.viewport187    if (this.chromelessOptions.scrollBeforeClick) {188      await scrollToElement(this.client, selector)189    }190    await click(this.client, selector, scale)191    this.log(`Clicked on ${selector}`)192  }193  private async returnCode<T>(fn: string, ...args: any[]): Promise<T> {194    return (await evaluate(this.client, fn, ...args)) as T195  }196  private async scrollTo<T>(x: number, y: number): Promise<void> {197    return scrollTo(this.client, x, y)198  }199  private async scrollToElement<T>(selector: string): Promise<void> {200    if (this.chromelessOptions.implicitWait) {201      this.log(`scrollToElement(): Waiting for ${selector}`)202      await waitForNode(203        this.client,204        selector,205        this.chromelessOptions.waitTimeout,206      )207    }208    return scrollToElement(this.client, selector)209  }210  private async mousedown(selector: string): Promise<void> {211    if (this.chromelessOptions.implicitWait) {212      this.log(`mousedown(): Waiting for ${selector}`)213      await waitForNode(214        this.client,215        selector,216        this.chromelessOptions.waitTimeout,217      )218    }219    const exists = await nodeExists(this.client, selector)220    if (!exists) {221      throw new Error(222        `mousedown(): node for selector ${selector} doesn't exist`,223      )224    }225    const { scale } = this.chromelessOptions.viewport226    await mousedown(this.client, selector, scale)227    this.log(`Mousedown on ${selector}`)228  }229  private async mouseup(selector: string): Promise<void> {230    if (this.chromelessOptions.implicitWait) {231      this.log(`mouseup(): Waiting for ${selector}`)232      await waitForNode(233        this.client,234        selector,235        this.chromelessOptions.waitTimeout,236      )237    }238    const exists = await nodeExists(this.client, selector)239    if (!exists) {240      throw new Error(`mouseup(): node for selector ${selector} doesn't exist`)241    }242    const { scale } = this.chromelessOptions.viewport243    await mouseup(this.client, selector, scale)244    this.log(`Mouseup on ${selector}`)245  }246  private async setHtml(html: string): Promise<void> {247    await setHtml(this.client, html)248  }249  private async focus(selector: string): Promise<void> {250    if (this.chromelessOptions.implicitWait) {251      this.log(`focus(): Waiting for ${selector}`)252      await waitForNode(253        this.client,254        selector,255        this.chromelessOptions.waitTimeout,256      )257    }258    const exists = await nodeExists(this.client, selector)259    if (!exists) {260      throw new Error(`focus(): node for selector ${selector} doesn't exist`)261    }262    await focus(this.client, selector)263    this.log(`Focus on ${selector}`)264  }265  async type(text: string, selector?: string): Promise<void> {266    if (selector) {267      if (this.chromelessOptions.implicitWait) {268        this.log(`type(): Waiting for ${selector}`)269        await waitForNode(270          this.client,271          selector,272          this.chromelessOptions.waitTimeout,273        )274      }275      const exists = await nodeExists(this.client, selector)276      if (!exists) {277        throw new Error(`type(): Node not found for selector: ${selector}`)278      }279    }280    await type(this.client, text, selector)281    this.log(`Typed ${text} in ${selector}`)282  }283  async cookies(nameOrQuery?: string | CookieQuery): Promise<Cookie[]> {284    return await getCookies(this.client, nameOrQuery as string | undefined)285  }286  async allCookies(): Promise<Cookie[]> {287    return await getAllCookies(this.client)288  }289  async setExtraHTTPHeaders(headers: Headers): Promise<void> {290    return await setExtraHTTPHeaders(this.client, headers)291  }292  async setCookies(293    nameOrCookies: string | Cookie | Cookie[],294    value?: string,295  ): Promise<void> {296    if (typeof nameOrCookies !== 'string' && !value) {297      const cookies = Array.isArray(nameOrCookies)298        ? nameOrCookies299        : [nameOrCookies]300      return await setCookies(this.client, cookies)301    }302    if (typeof nameOrCookies === 'string' && typeof value === 'string') {303      const fn = () => location.href304      const url = (await evaluate(this.client, `${fn}`)) as string305      const cookie: Cookie = {306        url,307        name: nameOrCookies,308        value,309      }310      return await setCookies(this.client, [cookie])311    }312    throw new Error(`setCookies(): Invalid input ${nameOrCookies}, ${value}`)313  }314  async deleteCookies(name: string, url: string): Promise<void> {315    const { Network } = this.client316    const canClearCookies = await Network.canClearBrowserCookies()317    if (canClearCookies) {318      await deleteCookie(this.client, name, url)319      this.log(`Cookie ${name} cleared`)320    } else {321      this.log(`Cookie ${name} could not be cleared`)322    }323  }324  async clearCookies(): Promise<void> {325    const { Network } = this.client326    const canClearCookies = await Network.canClearBrowserCookies()327    if (canClearCookies) {328      await clearCookies(this.client)329      this.log('Cookies cleared')330    } else {331      this.log('Cookies could not be cleared')332    }333  }334  async press(keyCode: number, count?: number, modifiers?: any): Promise<void> {335    this.log(`Sending keyCode ${keyCode} (modifiers: ${modifiers})`)336    await press(this.client, keyCode, count, modifiers)337  }338  async returnExists(selector: string): Promise<boolean> {339    return await nodeExists(this.client, selector)340  }341  async returnInputValue(selector: string): Promise<string> {342    const exists = await nodeExists(this.client, selector)343    if (!exists) {344      throw new Error(`value: node for selector ${selector} doesn't exist`)345    }346    return getValue(this.client, selector)347  }348  // Returns the S3 url or local file path349  async returnScreenshot(350    selector?: string,351    options?: ScreenshotOptions,352  ): Promise<string> {353    if (selector) {354      if (this.chromelessOptions.implicitWait) {355        this.log(`screenshot(): Waiting for ${selector}`)356        await waitForNode(357          this.client,358          selector,359          this.chromelessOptions.waitTimeout,360        )361      }362      const exists = await nodeExists(this.client, selector)363      if (!exists) {364        throw new Error(365          `screenshot(): node for selector ${selector} doesn't exist`,366        )367      }368    }369    const data = await screenshot(this.client, selector)370    if (isS3Configured()) {371      return await uploadToS3(data, 'image/png')372    } else {373      return writeToFile(data, 'png', options && options.filePath)374    }375  }376  async returnHtml(): Promise<string> {377    return await html(this.client)378  }379  async returnHtmlUrl(options?: { filePath?: string }): Promise<string> {380    const data = await html(this.client)381    if (isS3Configured()) {382      return await uploadToS3(data, 'text/html')383    } else {384      return writeToFile(data, 'html', options && options.filePath)385    }386  }387  // Returns the S3 url or local file path388  async returnPdf(options?: PdfOptions): Promise<string> {389    const { filePath, ...cdpOptions } = options || { filePath: undefined }390    const data = await pdf(this.client, cdpOptions)391    if (isS3Configured()) {392      return await uploadToS3(data, 'application/pdf')393    } else {...Using AI Code Generation
1const Chromeless = require('chromeless').Chromeless2async function run() {3  const chromeless = new Chromeless()4    .type('chromeless', 'input[name="q"]')5    .press(13)6    .wait('#resultStats')7    .returnHtmlUrl()8    .end()9}10run().catch(console.error.bind(console))11const Chromeless = require('chromeless').Chromeless12async function run() {13  const chromeless = new Chromeless()14    .type('chromeless', 'input[name="q"]')15    .press(13)16    .wait('#resultStats')17    .returnHtml()18    .end()19}20run().catch(console.error.bind(console))21const Chromeless = require('chromeless').Chromeless22async function run() {23  const chromeless = new Chromeless()24    .type('chromeless', 'input[name="q"]')25    .press(13)26    .wait('#resultStats')27    .returnPdfUrl()28    .end()29}30run().catch(console.error.bind(console))31const Chromeless = require('chromeless').Chromeless32async function run() {33  const chromeless = new Chromeless()34    .type('chromeless', 'input[name="q"]')35    .press(13)36    .wait('#resultStats')37    .returnPdf()38    .end()39}40run().catch(console.error.bind(console))41const Chromeless = require('chromeless').Chromeless42async function run() {Using AI Code Generation
1const Chromeless = require('chromeless').Chromeless2async function run() {3  const chromeless = new Chromeless()4    .type('chromeless', 'input[name="q"]')5    .press(13)6    .wait('#resultStats')7    .screenshot()8  await chromeless.end()9}10run().catch(console.error.bind(console))11const Chromeless = require('chromeless').Chromeless12async function run() {13  const chromeless = new Chromeless()14    .type('chromeless', 'input[name="q"]')15    .press(13)16    .wait('#resultStats')17    .html()18  await chromeless.end()19}20run().catch(console.error.bind(console))21const Chromeless = require('chromeless').Chromeless22async function run() {23  const chromeless = new Chromeless()24    .type('chromeless', 'input[name="q"]')25    .press(13)26    .wait('#resultStats')27    .html({pretty: true})28  await chromeless.end()29}30run().catch(console.error.bind(console))31const Chromeless = require('chromeless').Chromeless32async function run() {33  const chromeless = new Chromeless()34    .type('chromeless', 'input[name="q"]')35    .press(13)36    .wait('#resultStats')37    .html({selector: '#resultStats'})38  await chromeless.end()39}40run().catch(console.error.bind(console))Using AI Code Generation
1const chromeless = new Chromeless()2  .type('chromeless', 'input[name="q"]')3  .press(13)4  .wait('#resultStats')5  .returnHtmlUrl()6  .end()7const chromeless = new Chromeless()8  .type('chromeless', 'input[name="q"]')9  .press(13)10  .wait('#resultStats')11  .returnHtml()12  .end()13const chromeless = new Chromeless()14  .type('chromeless', 'input[name="q"]')15  .press(13)16  .wait('#resultStats')17  .returnHtml()18  .end()19const chromeless = new Chromeless()20  .type('chromeless', 'input[name="q"]')21  .press(13)22  .wait('#resultStats')23  .returnHtml()24  .end()25const chromeless = new Chromeless()26  .type('chromeless', 'input[name="q"]')27  .press(13)28  .wait('#resultStats')29  .returnHtml()30  .end()31const chromeless = new Chromeless()32  .type('chromeless', 'input[name="q"]')33  .press(13)34  .wait('#resultStats')Using AI Code Generation
1const chromeless = new Chromeless()2  .type('chromeless', 'input[name="q"]')3  .press(13)4  .wait('#resultStats')5  .returnHtmlUrl()6  .end()7const chromeless = new Chromeless()8  .type('chromeless', 'input[name="q"]')9  .press(13)10  .wait('#resultStats')11  .returnHtmlUrl({12  })13  .end()Using AI Code Generation
1const chromeless = new Chromeless()2  .type('chromeless', 'input[name="q"]')3  .press(13)4  .wait('#resultStats')5  .returnHtmlUrl()6  .end()Using AI Code Generation
1const Chromeless = require('chromeless').Chromeless2const chromeless = new Chromeless()3  .type('chromeless', 'input[name="q"]')4  .press(13)5  .wait('#resultStats')6  .returnHtmlUrl()7  .then(htmlUrl => console.log(htmlUrl))8  .catch(console.error)9  .finally(() => chromeless.end())10### `new Chromeless(options)`11- `chrome`: (optional) object - options to pass to Chrome. See [puppeteer](Using AI Code Generation
1const chromeless = new Chromeless()2  .type('chromeless', 'input[name="q"]')3  .press(13)4  .wait('#resultStats')5  .returnHtmlUrl()6  .end()7const chromeless = new Chromeless()8  .type('chromeless', 'input[name="q"]')9  .press(13)10  .wait('#resultStats')11  .returnHtml()12  .end()13const chromeless = new Chromeless()14  .type('chromeless', 'input[name="q"]')15  .press(13)16  .wait('#resultStats')17  .returnHtml({ inline: true })18  .end()19const chromeless = new Chromeless()20  .type('chromeless', 'input[name="q"]')21  .press(13)22  .wait('#resultStats')23  .returnHtml({ inline: true, inlineLimit: 1000 })24  .end()25const chromeless = new Chromeless()26  .type('chromeless', 'input[name="q"]')27  .press(13)28  .wait('#resultStats')29  .returnHtml({ inline: true, inlineLimit: 1000, url: true })30  .end()31const chromeless = new Chromeless()Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
