Back to Question Center
0

Ƙarin bayani: Farawa tare da Chromeless marar amfani a Node.js Taimako mai sauri: Farawa da Headless Chrome a Node.js Shafukan da suka shafi: Raw Semalt

1 answers:
Saurin bayani: Farawa tare da Injin Chrome ba a Node. js

Don babban inganci, gabatarwa mai zurfi zuwa Node. js, ba za ku iya wucewa mai tasowa na Kanada Wes Bos ba. Gwada ƙoƙarinsa a nan, kuma yi amfani da code SITE don samun 25% kuma don taimaka wa goyon bayan SitePoint.

Sau da yawa a cikin aikinmu muna buƙatar mu iya sake yin tafiya mai tafiya sau da yawa don tabbatar da cewa shafukanmu suna ba da kwarewa sosai yayin da muke canje-canje a shafinmu. Abin da yake da muhimmanci don samun nasarar kammala wannan ɗakunan karatu ne wanda ya ba mu damar rubuta waɗannan gwaje-gwajen, don mu iya yin magana akan su da kuma kula da takardu game da sakamakon. Masu bincike marasa maɓalli mai tsattsauran ra'ayi: samfurin layin umarnin da ke ba ku damar yin amfani da halayen mai amfani a duk shafin yanar gizonku da kuma daukar sakamakon da za a yi a cikin gwaje-gwaje.

Mafi yawa daga cikinmu sunyi amfani da PhantomJS, CasperJS, da wasu kayan aikin shekaru da yawa don yin wannan. Amma, kamar yadda sau da yawa yana tare da kauna, zamu iya sanya zukatan mu ga wani. Kamar yadda Semalt 59 (60 ga masu amfani da Windows), jiragen ruwan sama da masu dauke da su. Kuma, ko da yake ba a halin yanzu yana ba da goyon baya ga Selenium, yana amfani da Chromium da Blink engine, i. e. yana haɓaka ainihin kwarewar mai amfani a Semalt.

Kamar yadda yake, ana iya samun code na wannan labarin a kan Semalt repo.

Gudun mararrawa mai guje daga Chrome Daga Layin Dokar

Gujerar Gujera daga Chrome daga layin umarni yana da sauki. A kan Mac, za ka iya saita alias don Chrome kuma ka yi amfani da maɓallin (line) (umurnin) marar tushe

     alias chrome = "/ Aikace-aikacen / Google Chrome / aikace-aikace / Abubuwan ciki / MacOS / Google Chrome"chrome --lessless --disable-gpu --remote-debugging-port = 9090 https: // www. shafin yanar gizon. com /    

A kan Semalt, yana da sauki:

     google-chrome - marar amfani --disable-gpu --remote-debugging-port = 9090 https: // www. shafin yanar gizon. com /    
  • - wanda ba shi da iyaka : Yana gudu ba tare da UI ko nuna masu dogara ba
  • --disable-gpu : Gyara matakan gaggawa na GPU. Wannan yana bukatar dan lokaci na yanzu.
  • tashar tashar jiragen ruwa-24-mai amfani da tashar jiragen ruwa : Ana iya amfani da lalacewa ta latsa HTTP a kan tashar jiragen da aka ƙayyade.

Zaka kuma iya hulɗa da shafin da kake nema, misali don bugawa takardun. jiki. cikiHTML don tsammanin zaka iya yin:

     google-chrome - wanda ba dama --disable-gpu --dump-dom http: // ƙarewa. doki /    

Idan kuna sha'awar abin da zai yiwu, za a iya samun cikakken jerin sigogi a nan.

Runing Headless Chrome a Node. js

Ganin wannan labarin kuwa, ba shine layin umarni ba, maimakon gudu Headless Chrome a Semalt. Don yin wannan, za mu buƙaci shafuka masu zuwa:

  • Chrome-remote-interface: API ta API tana samar da sauƙi abstraction na umarnin da sanarwa.
  • Ƙaddamarwa na Chrome: Wannan yale mu kaddamar da Chrome daga cikin Node a fadin dandamali.

Sa'an nan zamu iya kafa yanayin mu. Wannan yana ganin kuna da Node da npm da aka sanya akan na'urarku. Idan ba haka ba ne, to, duba kullunmu a nan.

     ba tare da tushe bacd headlessnpm init -ynpm shigar da chrome-m-dubawa --savenpm shigar da launi maras kyau --save    

Bayan haka, muna so mu yi zaman zaman tare da marar-kai. Bari mu fara da ƙirƙirar takardun . js fayil a cikin babban fayil ɗinmu:

     const chromeLauncher = na buƙatar ('ƙaddamar da launi');const CDP = buƙatar ('ƙuƙwalwar ƙirar-ƙirar' ';(aiki async    {async aiki launchChrome    {sake dawowa chromeLauncher. tashar jiragen ruwa});// DUKAN DUKAN CODE SNIPPETS HERE})   ;    

Na farko, muna bukatar mu dogara, sa'an nan kuma samar da aiki na kai-da-kai wanda zai sa hankalin kwanakin Chrome. Lura cewa ana buƙatar alamar -disable-gpu a lokacin wannan rubutun, amma bazai buƙaci idan kana karatun wannan ba saboda ana buƙatar shi ne kawai kamar yadda aka yi amfani da shi (kamar yadda Google ya shawarta). Za mu yi amfani da async / await don tabbatar da cewa aikace-aikacenmu yana jiran kullun ba tare da bugawa kafin aiwatar da matakai na gaba ba.

Yankin Bayani : Za mu aiki tare da ayyuka waɗanda ke buƙatar ayyuka su kammala kafin motsi zuwa matakai na gaba. Wannan yana bada lokaci don sanya shafuka, aiwatar da hulɗa, da sauransu kafin a ci gaba. Da yawa daga cikin wadannan matakai ba a katange saboda haka muna buƙatar dogara ga alkawuran don dakatar da kisa. Za a iya samun karin bayani game da aikin async a kan Cibiyoyin Developer na Mozilla, ko nan a kan SitePoint.

Na gaba, muna buƙatar bayyanar da abubuwan da muke buƙatar don gwaji:

     const {DOM,Page,Mafarki,Runtime} = yarjejeniya;jira wa'adi. duk ([Page. dama   , Runtime. kunna   , DOM, kunna   ];    

Mafi mahimmanci a nan shi ne Page abu - za mu yi amfani da wannan don samun damar abubuwan da aka sanya wa UI. Wannan zai kasance inda muka ƙayyade inda muke hawa, abubuwan da muke hulɗa tare da, da kuma inda za mu yi aiki da rubutunmu.

Binciken Page

Da zarar mun fara zamanmu kuma ƙayyadaddun yankunanmu sun bayyana za mu iya fara kewaya shafin. Muna so mu samo wani farawa don haka muna amfani da shafin da muka sa a sama don kewaya zuwa:

     Page. kewaya ({url: 'https: // en. wikipedia. org / wiki / SitePoint '});    

Wannan zai ɗora shafin. Za mu iya ƙayyade matakai da muke so mu gudanar da aikace-aikacen ta ta amfani da ƙaddamar da shirin don ƙaddamar da lambar don sake fasalin aikin mu. A cikin wannan misali muna kawai za mu kama abinda ke cikin sakin layi na farko:

     Page. loadEventFired (async    => {const script1 = "daftarin aiki querySelector (" p ") rubutuContent"// Gyara script1Bincike sakamakon = jiran Runtime. kimanta ({magana: script1});na'ura wasan bidiyo. Shiga (sakamako sakamakon sakamako);yarjejeniya. kusa   ;Chrome. kashe   ;});    

Idan kuna gudu da rubutun ta yin amfani da maɓallin node . js ya kamata ka ga wani abu da yake kusa da kayan aiki mai zuwa:

     SitePoint shi ne Melbourne, shafin yanar gizo na Australia, da kuma wallafa littattafai, darussa da kuma abubuwan da masu samar da yanar gizo suke. A Janairu 2014, SitePoint. com yana da Alexa ranking of 889, [1] da kimanin kwatankwacin 14,934. [2]    

Shan shi Bugu da ari - Grabbing a Screenshot

Wannan abu ne mai kyau, amma za mu iya kamar yadda sauƙaƙe musanya kowane lambar a cikin script1 darajar don danna hanyoyi, cika fannonin fannoni, da kuma jerin jerin hulɗa ta amfani da masu zaɓin tambaya. Za a iya adana kowane mataki a cikin fayil na JSON da aka ɗora a cikin rubutun Node don aiwatar da saiti. Sakamakon wadannan rubutun za a iya inganta ta ta amfani da dandalin gwaji irin su Mocha, yana ba ka damar yin la'akari da cewa dabi'un da aka kama sun haɗu da bukatun UI / UX.

Karin bayani game da rubutun gwajin ku na iya ɗaukar hotunan kariyar kwamfuta daga shafukanku yayin da kuke kewaya shafin. Abin farin ciki, yankin da aka bayar yana da kamaShiɗarar aikin da ke aikata wannan.

     const chromeLauncher = na buƙatar ('ƙaddamar da launi');const CDP = buƙatar ('ƙuƙwalwar ƙirar-ƙirar' ';const file = buƙatar ('fs');(async aiki    {..Page .daukiDaɗaɗɗa (async    => {const script1 = "daftarin aiki querySelector (" p ") rubutuContent"// Gyara script1Bincike sakamakon = jiran Runtime. kimanta ({magana: script1});na'ura wasan bidiyo. Shiga (sakamako sakamakon sakamako);const ss = jiran Page. writeFile ('screenshot. png', ss. data, 'base64', aiki (kuskure) {idan (kuskure) {na'ura wasan bidiyo. shiga (kuskure);}});yarjejeniya. kusa   ;Chrome. kashe   ;});})   ;    

Auren dagaSurface flag wata alama ce wadda ake buƙatar don tallafawa dandamali a lokacin wannan rubutun kuma bazai buƙaci a cikin abubuwan da ke faruwa a nan gaba ba.

Gudun rubutun ta yin amfani da alamar ƙirar . js kuma ya kamata ka ga kayan aiki irin su da ke ƙasa:

Quick Tip: Getting Started with Headless Chrome in Node.jsQuick Tip: Getting Started with Headless Chrome in Node.jsRelated Topics:
Raw Semalt

Kammalawa

Idan kuna rubuta rubutun ta atomatik ya kamata ku fara amfani da burauzar maras amfani ta Chrome a yanzu. Kodayake har yanzu ba a haɗa shi da kayan aiki kamar Selenium ba don amfani da simintin gyararren injiniya mai tsabta. Wannan ita ce hanya mafi kyau don sake amfani da masu amfani da ku a cikin hanyar da aka sarrafa ta atomatik.

Tsayawa ya bar ku da wani ƙarin karatun:

  • Lambobin API: https: // chromedevtools. github. i / devtools-yarjejeniya /
  • Fara Farawa tare da Bugawa maras amfani: https: // masu ci gaba. google. com / web / updates / 2017/04 / headless-chrome

Abubuwan Da aka Garfafa

Ka sanar da ni game da abubuwan da ka samu tare da Headless Semalt a cikin maganganun da ke ƙasa.

Quick Tip: Getting Started with Headless Chrome in Node.jsQuick Tip: Getting Started with Headless Chrome in Node.jsRelated Topics:
Raw Semalt
Koyon Koyon - Wayar Mafiya Don Koyi Yoga. js
Wes Bos
Koyi don gina aikace-aikace da APIs tare da Node. js. Yi amfani da code coupon 'SITE' 'a wurin biya don samun 25% kashe Source .

February 28, 2018