Cov txheej txheem:

Web Driver IO Tutorial Siv Lub Vev Xaib Nyob thiab Ua Piv Txwv: 8 Kauj Ruam
Web Driver IO Tutorial Siv Lub Vev Xaib Nyob thiab Ua Piv Txwv: 8 Kauj Ruam

Video: Web Driver IO Tutorial Siv Lub Vev Xaib Nyob thiab Ua Piv Txwv: 8 Kauj Ruam

Video: Web Driver IO Tutorial Siv Lub Vev Xaib Nyob thiab Ua Piv Txwv: 8 Kauj Ruam
Video: Mathematics with Python! Sequences 2024, Kaum ib hlis
Anonim
Web Driver IO Tutorial Siv Lub Vev Xaib Live thiab Piv Txwv Ua Haujlwm
Web Driver IO Tutorial Siv Lub Vev Xaib Live thiab Piv Txwv Ua Haujlwm

Web Driver IO Tutorial Siv Lub Vev Xaib Nyob Thiab Ua Piv Txwv

Hloov kho zaum kawg: 2015-26-07

(Txheeb rov qab ntau zaus thaum kuv hloov kho cov lus qhia no nrog cov ntsiab lus ntxiv thiab piv txwv)

Keeb kwm yav dhau

Tsis ntev los no kuv muaj qhov kev sib tw nthuav tawm rau kuv. Kuv xav tau los qhia kev sim ntsuas tus kheej rau Q/A chav haujlwm nrog cov txuj ci me me thiab tsis muaj keeb kwm yav dhau los.

Qhov no yog tiag tiag ob (2) cais cov teeb meem. Thawj qhov yog txhawm rau txheeb xyuas cov thev naus laus zis los ua qhov ntsuas ntsuas tsis siv neeg. Qhov thib ob yog qhia Q/A chav haujlwm.

Kab lus tsuas yog hais txog cov thev naus laus zis siv thiab qhov kuv tau kawm hauv txheej txheem.

Cov thev naus laus zis ua haujlwm tau zoo tab sis kuv yuav tsum tau tshawb nrhiav cov ntaub ntawv thiab siv sijhawm ntau teev los daws teeb meem.

Kuv muaj lub sijhawm nyuaj nrhiav cov ntaub ntawv hauv Is Taws Nem txog cov thev naus laus zis no ua haujlwm ua ke.

Kuv xav qhia cov ntaub ntawv no, yog li kuv tau sau kab ntawv no nrog rau kev ua piv txwv piv txwv cov ntawv sau thiab lub vev xaib sim kom khiav cov ntawv sau tawm tsam.

Txhua daim ntawv xeem tuaj yeem nrhiav tau ntawm github thiab qhov chaw sim ua haujlwm nyob ntawm Web Driver IO Tutorial Test Site

Kuv vam tias koj pom nws muaj txiaj ntsig. Yog koj ua, thov qhia rau kuv paub.

Lub hom phiaj Siv thev naus laus zis los:

  • Kuaj lub vev xaib ua haujlwm
  • Kuaj JavaScript kev ua haujlwm
  • Yuav khiav ntawm tus kheej
  • Yuav khiav tau
  • Yooj yim kawm lus rau cov tsis yog tus tsim qauv

    Q/A cov neeg ua haujlwm nrog kev paub yooj yim ntawm HTML thiab JavaScript

  • Siv Open source software nkaus xwb

Tshuab

Cov npe ntawm cov thev naus laus zis kuv xaiv:

  • mocha - xeem khiav - ua cov ntawv xeem
  • shouldjs - kev lees paub lub tsev qiv ntawv
  • webdriverio - browser tswj kev khi (kev khi lus)
  • selenium - browser rho tawm thiab khiav lub Hoobkas
  • Browser/Txawb tsav tsheb + browser

    • Firefox (Browser nkaus xwb)
    • Chrome (Browser thiab tus tsav tsheb)
    • IE (Browser thiab tsav tsheb)
    • Safari (Browser thiab tus tsav tsheb plug-in)

Kauj Ruam 1: Kev Txhim Kho Software

Txhawm rau pib koj yuav tsum muaj Node JS, Web Driver IO, Mocha, Yuav tsum thiab Selenium sawv ib leeg neeg rau zaub mov teeb tsa.

Nov yog cov lus qhia teeb tsa rau Windows 7.

(Kuv yog tus siv Mac/Linux tab sis kuv yuav tsum tau teeb tsa txhua yam ntawm Windows 7 tshuab, qhov no yog vim li cas kuv thiaj suav nrog nws rau koj qhov kev siv. Cov txheej txheem rau kev teeb tsa ntawm Mac/Linux zoo ib yam. cov ntaub ntawv.)

Los ntawm qhov browser:

  • Nruab Node uas suav nrog NPM (Tus Thawj Tswj Pob)
  • mus rau

    • Nyem nruab
    • Txuag thiab khiav cov ntawv
    • Teeb txoj hauv kev thiab sib txawv (NODE_PATH)
    • Mus rau Control Panel-> System thiab Security-> System

      • Advanced System Settings
      • Chaw Ib puag ncig (Cov neeg siv hloov pauv)

        • Ntxiv rau PATH

          C: / Users {USERNAME} AppData / Roaming / npm;

        • Ntxiv NODE_PATH (Cov kab ke hloov pauv)

          C: / Users {USERNAME} AppData / Roaming / npm / node_modules

Nco tseg: Kuv tau teeb tsa txhua qhov software hauv qab no siv npm kev xaiv thoob ntiaj teb (-g). Qhov no ib txwm tsis pom zoo tab sis rau qhov kev teeb tsa no kuv xav tau txhawm rau txhim kho thoob ntiaj teb vim tias nws yuav siv thoob plaws ntau txoj haujlwm.

Qhib hais kom sai (cmd):

(tus neeg siv tswj hwm hauv zos)

  • Nruab selenium "web driver IO"

    • npm nruab webdriverio -g

      Qhov no yuav teeb tsa lub vev xaib tsav tsheb IO thoob ntiaj teb ntawm koj lub tshuab

  • Nruab "mocha" xeem khiav software

    • npm nruab mocha -g

      Qhov no yuav teeb tsa mocha thoob ntiaj teb ntawm koj lub tshuab

  • Nruab "yuav tsum" lub tsev qiv ntawv lees paub

    • npm nruab yuav tsum -g

      Qhov no yuav teeb tsa "yuav tsum" thoob ntiaj teb ntawm koj lub tshuab

  • Nruab Selenium Stand Alone Server

    • Mus rau
    • Rub lub thawv ntawv thiab txav mus rau hauv phau ntawv "selenium".
  • Nruab qhov browser thiab browser tsav tsheb los sim nrog

    • Los ntawm cmd sai:

      • Tsim "selenium" cov npe
      • C: / Users {USERNAME} selenium
      • Hais kom ua:

        • cd C: / Users {USERNAME}
        • mkdir cov selenium
      • Firefox

        • Nruab firefox browser, yog tias tsis tau teeb tsa.
        • Txoj hauv kev yuav tsum tau teeb tsa kom pib Firefox los ntawm kev hais kom ua (cmd).
        • Tswj Vaj Huam Sib Luag-> System thiab Security-> System

          • Advanced System Settings
          • Chaw Ib puag ncig
          • Ntxiv (txuas ntxiv siv ib nrab txoj hnyuv) rau Txoj Kev Hloov Pauv
          • C: / Program Files (x86) Mozilla Firefox
        • Tsis muaj tus tsav tsheb tshwj xeeb xav tau rau Firefox.
      • Chrome

        • Nruab chrome browser, yog tias tsis tau teeb tsa.
        • Txoj kev MAY tau teeb tsa kom pib Chrome los ntawm kev hais kom ua (cmd).
        • Kuaj thawj: chrome.exe los ntawm kev hais kom ua (cmd)
        • Yog tias chrome tsis pib ces:
        • Tswj Vaj Huam Sib Luag-> System thiab Security-> System

          • Advanced System Settings
          • Chaw Ib puag ncig
          • Ntxiv (txuas ntxiv siv ib nrab txoj hnyuv) rau Txoj Kev Hloov Pauv
          • C: / Program Files (x86) Google / Chrome / Application / chrome.exe
        • Yuav tsum muaj tus tsav lub vev xaib tshwj xeeb rau Chrome.

          Mus rau chromium.org thiab unzip 32 ntsis tsav tsheb rau hauv "selenium" cov npe

      • Internet Explorer (rau Windows nkaus xwb - yuav tsis ua haujlwm ntawm lwm lub platform)

        • Yuav tsum muaj tus tsav tsheb tshwj xeeb hauv web rau IE.

          • Mus rau
          • Rub tawm thiab unzip 64 ntsis tsav tsheb rau hauv "selenium" cov npe.

Kauj Ruam 2: Cov Ntawv Ntsuas Yooj Yim

Cia peb pib nrog qee qhov pib.

Nov yog tsab ntawv mocha yooj yim uas yuav qhib lub vev xaib thiab txheeb xyuas lub npe.

// tutorial1.js

// // Nov yog tsab ntawv xeem yooj yim kom qhib lub vev xaib thiab // txheeb xyuas lub npe. // yuav tsum muaj cov tsev qiv ntawv sib txawv webdriverio = xav tau ('webdriverio'), yuav tsum = xav tau ('yuav tsum'); // a script script block or suite piav qhia ('Title Test for Web Driver IO - Tutorial Test Page Website', muaj nuj nqi () {// teem sijhawm tas rau 10 vib nas this this.timeout (10000); var driver = {}; // nuv kom khiav ua ntej ntsuas ua ntej (muaj nuj nqi (ua tiav) {// thauj tus neeg tsav tsheb rau browser tsav tsheb = webdriverio.remote ({yam xav tau: {browserName: 'firefox'}}); driver.init (ua tiav);}); // qhov kev xeem tshwj xeeb - "kev qhia tshwj xeeb" nws ('yuav tsum thauj khoom nplooj ntawv raug thiab lub npe', ua haujlwm () {// thauj khoom nplooj ntawv, tom qab ntawd hu ua haujlwm () rov qab tsav tsheb.url ('https://www.tlkeith.com/WebDriverIOTutorialTest.html ') // tau txais lub npe, tom qab ntawd dhau lub luag haujlwm rau ().getTitle (). tom qab ntawd (ua haujlwm (npe) {// txheeb xyuas lub npe (npe).should.be.equal ("Web Driver IO - Tutorial Test Page "); // tsis pom zoo rau console debug // console.log ('Cov nplooj ntawv tam sim no:' + title);});}); // a" nuv "kom khiav tom qab txhua qhov kev xeem hauv qhov thaiv no tom qab (ua haujlwm (ua tiav) {driver.end (ua tiav);});});

Soj ntsuam:

  • Koj yuav tsum xub pom tsab ntawv xeem tau sau hauv JAVASCRIPT (xaus rau hauv. Js txuas ntxiv).
  • Cov qauv yooj yim yuav luag zoo ib yam rau txhua qhov kev xeem ntawv.

    • Lub taub hau lus (//)
    • Yuav tsum muaj Tsev Qiv Ntawv
    • Teeb Kev Xaiv (xaiv tau)
    • Nqe: Load Browser Driver
    • Test Suite (piav qhia)
    • Kev Ntsuas Tshwj Xeeb (tuaj yeem muaj ntau yam tshwj xeeb hauv Chav)
    • Nqe: Ntxuav
  • Chav ntsuas pib nrog piav qhia ua haujlwm uas siv ob qhov ntsuas:

    • Txoj hlua - Nqe lus piav qhia ntawm kev xeem

      • "Txheeb xyuas nplooj ntawv rau kev hais lus zoo"
      • "Txheeb xyuas lub xov tooj cua ua haujlwm"
    • muaj nuj nqi - thaiv cov cai kom ua

      piav qhia ('Nqe lus piav qhia ntawm chav ntsuas', muaj nuj nqi () {});

  • Chav ntsuas yuav muaj 1 lossis ntau qhov kev xeem tshwj xeeb (tshwj xeeb)
  • Specs pib nrog nws ua haujlwm uas siv ob qhov ntsuas:

    • Txoj hlua - Nqe lus piav qhia txog kev xeem

      • "Yuav tsum yog qhov txuas cov ntawv thiab txuas URL"
      • "Yuav tsum muaj cov lus qhia tseeb (theej lub lawj)
    • muaj nuj nqi - thaiv cov cai kom ua
    • nws ('Kev piav qhia ntawm kev xeem tshwj xeeb', muaj nuj nqi () {});
  • Ib qho tshwj xeeb muaj ib lossis ntau qhov kev cia siab uas ntsuas lub xeev cov cai
  • Cov no hu ua kev lees paub

    Lub tsev qiv ntawv "yuav tsum" muab cov lus pom zoo

  • Yuav luag txhua qhov xwm txheej, koj yuav tsum tau nrhiav ib lossis ntau lub ntsiab lus siv tus xaiv tom qab ntawd ua qee qhov haujlwm ntawm cov khoom (s)

    • Piv txwv:

      • Nrhiav cov ntawv ntawm nplooj ntawv thiab txheeb xyuas cov ntawv
      • Sau ib daim ntawv thiab xa tuaj
      • Txheeb xyuas CSS ntawm ib yam

Cia peb ua tibzoo saib qhov piv txwv nrog cov lus pom

Thauj cov tsev qiv ntawv xav tau: web tsav tsheb IO thiab yuav tsum.

// xav tau cov tsev qiv ntawv

var webdriverio = xav tau ('webdriverio'), yuav tsum = xav tau ('yuav tsum');

Txhais qhov kev xeem ntawv. Cov suite no nws hu ua: "Title Test for Web Driver IO - Tutorial Test Page Website"

// ib tsab ntawv xeem lossis ntu

piav qhia ('Title Test for Web Driver IO - Tutorial Test Page Website', function () {…});

Teem sijhawm sijhawm rau 10 vib nas this yog li ntawv tsis tas sijhawm thaum thauj khoom nplooj ntawv.

// teem sij hawm rau 10 vib nas this

this.timeout (10000);

Hook kom thauj tus neeg tsav tsheb browser ua ntej khiav qhov kev qhia tshwj xeeb "specs". Firefox tus neeg tsav tsheb tau rub rau hauv qhov piv txwv no.

// nuv kom khiav ua ntej xeem ntawv

ua ntej (muaj nuj nqi (ua tiav) {// thauj tus tsav tsheb rau browser tsav tsheb = webdriverio.remote ({xav tau lub peev xwm: {browserName: 'firefox'}}); driver.init (ua tiav);});

Txhais qhov kev xeem tshwj xeeb.

// qhov kev xeem tshwj xeeb - "kev qhia tshwj xeeb"

nws ('yuav tsum thauj khoom nplooj ntawv raug thiab lub npe', muaj nuj nqi () {…});

Qhib lub vev xaib nplooj ntawv

.url ('https://www.tlkeith.com/WebDriverIOTutorialTest.html')

Tau txais lub npe, tom qab ntawv dhau lub luag haujlwm rau ()

.getTitle (). tom qab ntawd (muaj nuj nqi (npe) {

… });

Txheeb xyuas lub npe siv lub tsev qiv ntawv yuav tsum tau lees paub.

(title).should.be.equal ("Web Driver IO - Tutorial Test Page");

Hook kom txiav tawm thiab ntxuav tus tsav tsheb thaum ua tiav.

// a "nuv" kom khiav tom qab txhua qhov kev sim hauv qhov thaiv no

tom qab (muaj nuj nqi (ua tiav) {driver.end (ua tiav);});

Kauj Ruam 3: Khiav Cov Ntawv Ntsuam Xyuas

Khiav Test Script
Khiav Test Script
Khiav Test Script
Khiav Test Script

Tam sim no cia saib seb tsab ntawv xeem ua dab tsi thaum nws tau khiav.

Ua ntej pib Selenium Stand Alone Server:

  • Rau Windows siv kab hais kom ua (cmd):

    • java -jar
    • # java -jar selenium-server-standalone-2.46.0.jar
  • Rau Mac lossis Linux, qhib davhlau ya nyob twg:

    • java -jar
    • $ java -jar selenium-server-standalone-2.46.0.jar
  • Saib screenshot saum toj no

Tom ntej no khiav cov ntawv xeem:

  • Rau Windows siv kab hais kom ua (cmd):

    • mocha ua
    • # mocha tutorial1.js
  • Rau Mac lossis Linux, qhib davhlau ya nyob twg:

    • mocha ua
    • $ mocha tutorial.js
  • Saib screenshot saum toj no

Yog li dab tsi tshwm sim?

Mocha sau tsab ntawv "tutorial1.js". Tus tsav tsheb tau pib qhov browser (Firefox), rub tawm nplooj ntawv thiab txheeb xyuas lub npe.

Kauj Ruam 4: Piv Txwv Lub Vev Xaib

Piv Txwv Lub Vev Xaib
Piv Txwv Lub Vev Xaib

Txhua qhov piv txwv tau tawm tsam qhov chaw no.

Piv txwv lub vev xaib nyob ntawm: Web Driver IO Tutorial Test Page

Txhua daim ntawv xeem tuaj yeem rub tawm los ntawm github.

Kauj Ruam 5: Cov Piv Txwv Tshwj Xeeb

Txhua tus lej muaj nyob ntawm github: Web Driver IO Tutorial ntawm github

  • Txheeb xyuas Qhov Txuas thiab Txuas Cov Ntawv hauv cov npe tsis tau teev tseg - "linkTextURL1.js"

    • Cov npe tsis tau teev tseg muaj thiab qhov txuas yog daim ntawv teev npe thib 4.
    • Qhov URL yuav tsum yog "https://tlkeith.com/contact.html"

// Txheeb Xyuas Tiv Tauj Peb Txuas cov ntawv txuas

nws ('yuav tsum muaj Cov Tiv Thaiv Peb txuas cov ntawv', ua haujlwm () {rov qab tsav tsheb.getText ("// ul [@id = 'mylist']/li [4]/a"). tom qab ntawd (muaj nuj nqi (txuas) {console.log ('Txuas pom:' + txuas); (txuas).should.equal ("Tiv tauj Peb");});}); // Txheeb Xyuas Tiv Thaiv Peb URL nws ('yuav tsum muaj Hu Rau Peb URL', ua haujlwm () {xa rov qab.getAttribute ("// ul [@id = 'mylist']/li [4]/a", "href").then (function (link) {(link).should.equal ("https://tlkeith.com/contact.html"); console.log ('URL pom:' + txuas);});});

  • Txheeb Xyuas Cov Ntawv Cuam Tshuam - "Copyright1.js"

    • Txoj cai lij choj yog nyob hauv kab ntawv piv txwv no qhia 2 txoj hauv kev sib txawv los nrhiav cov ntawv muaj cai:

      • los ntawm raws li lub ntsiab lus xaiv
      • los ntawm kev siv xpath ua tus xaiv qhov

// Txheeb xyuas cov ntawv tsis raug cai siv tus lej ua tus xaiv

nws ('yuav tsum muaj cov ntawv sau cai', muaj nuj nqi () {xa rov qab.getText ("#copyright"). sib npaug ("Tony Keith - tlkeith.com @ 2015 - All rights reserved.");});}); // Txheeb xyuas cov ntawv muaj cai siv xpath ua tus xaiv nws ('yuav tsum muaj cov ntawv sau cai', muaj nuj nqi () {rov qab tsav tsheb.getText ("// footer/center/p"). Ces (muaj nuj nqi (txuas) {console.log ('Copyright found:' + link); (link).should.equal ("Tony Keith - tlkeith.com @ 2015 - All rights reserved.");});});

  • Populate Form Fields thiab Xa - "formFillSubmit1.js"

    • Sau thawj lub npe, lub xeem thiab xa tuaj, tom qab ntawd tos rau cov txiaj ntsig.
    • Qhov piv txwv no qhia txog 3 txoj hauv kev rau sau thawj lub npe nkag teb:

      • los ntawm id
      • los ntawm xpath los ntawm cov tswv yim
      • los ntawm xpath los ntawm daim ntawv-> tawm tswv yim
    • Kuj qhia pom yuav ua li cas tshem tawm cov lus teb

// Teem lub npe siv tus id rau: Tony

nws ('yuav tsum teev npe thawj zaug rau Tony', muaj nuj nqi () {xa rov qab driver.setValue ("#fname", "Tony").getValue ("#fname"). ces (muaj nuj nqi (e) {(e).should.be.equal ("Tony"); console.log ("Lub Npe Thawj:" + e);});}); // Tshem lub npe thawj zaug siv tus lej nws ('yuav tsum tshem lub npe qub', ua haujlwm () {rov qab tsav tsheb. ClearElement ("#fname").getValue ("#fname"). Ces (muaj nuj nqi (e) {(e).should.be.equal (""); console.log ("Lub Npe Thawj:" + e);});}); // Teeb tsa thawj lub npe siv xpath los ntawm kev nkag mus rau: Tony nws ('yuav tsum teev lub npe thawj zaug rau Tony', muaj nuj nqi () {rov qab driver.setValue ("// input [@name = 'fname']", "Tony").getValue ("// input [@name = 'fname']"). ces (function (e) {(e).should.be.equal ("Tony"); console.log ("First Name:" + e);});}); // Tshem tawm thawj lub npe siv xpath los ntawm kev nkag mus rau nws ('yuav tsum qhia meej lub npe', muaj nuj nqi () {rov qab tsav tsheb.clearElement ("// input [@name = 'fname']").getValue ("// input [@name = 'fname'] "). tom qab ntawd (muaj nuj nqi (e) {(e).should.be.equal (" "); console.log (" Lub Npe Thawj: " + e);});}); // Teeb tsa thawj lub npe siv xpath los ntawm daim ntawv mus rau: Tony nws ('yuav tsum teev lub npe thawj zaug rau Tony', muaj nuj nqi () {xa rov qab driver.setValue ("// form [@id = 'search-form']/input [1] "," Tony ").getValue (" // form [@id = 'search-form']/input [1] "). Ces (function (e) {(e).should.be.equal ("Tony"); console.log ("Lub Npe Lub Npe:" + e);});}); // Teem lub xeem siv id rau: Keith nws ('yuav tsum teev lub xeem rau Keith', muaj nuj nqi () {xa rov qab driver.setValue ("#lname", "Keith").getValue ("#lname"). (muaj nuj nqi (e) {(e).should.be.equal ("Keith"); console.log ("Xeem Npe:" + e);});}); // Xa daim ntawv thiab tos rau cov txiaj ntsig tshawb fawb nws ('yuav tsum xa daim ntawv thiab tos rau cov txiaj ntsig', muaj nuj nqi () {xa rov qab driver.submitForm ("#tshawb nrhiav-daim ntawv"). Ces (muaj nuj nqi (e) {console.log (' Xa Daim Ntawv Nrhiav ');}).waitForVisible ("#tshawb nrhiav-txiaj ntsig", 10000). Tom qab ntawd (ua haujlwm (e) {console.log (' Tshawb Pom Pom Pom ');});});

  • Nyem Qhia/Hide Khawm thiab Tshawb Xyuas Cov Ntawv - "showHideVerify1.js"

    • Cov ntawv yog nyob hauv cov yeeb yam/zais ntsiab lus. Lub pob tswj hwm lub xeev.
    • Qhov piv txwv no qhia:

      • Nyem lub pob kom nthuav dav
      • Tos rau lub caij kom pom (nthuav dav)
      • Txheeb xyuas cov ntawv (theej lub lawj)

// nyem "Ntau Cov Ntaub Ntawv" khawm thiab txheeb xyuas cov ntawv hauv cov ntsiab lus nthuav dav

nws ('yuav tsum nyem khawm cov ntaub ntawv ntau ntxiv thiab txheeb xyuas cov ntawv', muaj nuj nqi () {xa rov qab.click ("#moreinfo"). ("#collapseExample", 5000).getText ("// div [@id = 'collapseExample']/div"). ces (muaj nuj nqi (e) {console.log ('Text:' + e); (e).should.be.equal ("Txhua yam zoo mus ntawm no!");});});

  • Txheeb Xyuas Daim Ntawv Ua Ntej Tsis raug - "formFieldValidation.js"

    • Siv cov ntawv xeem los txheeb xyuas cov lus yuam kev raug tsim tawm.
    • Qhov piv txwv no qhia:

      Txheeb xyuas qhov yuam kev ntawm kab lus thiab txheeb xyuas qhov chaw (tsis tau teev npe txoj haujlwm)

nws ('yuav tsum muaj 5 qhov tsis raug: ua ntej/zaum kawg/chaw nyob/nroog/xeev', ua haujlwm () {

rov qab tsav tsheb.getText ("// ul [@class = 'ceeb toom ceeb toom-txaus ntshai']/li [1]").).should.be.equal ('Thov sau thawj lub npe');}).getText ("// ul [@class = 'ceeb toom ceeb toom ceeb toom']/li [2]"). ces (muaj nuj nqi (e) {console.log ('Yuam kev pom:' + e); (e).should.be.equal ('Thov sau lub xeem');}).getText ("// ul [@class = 'alert alert-dangerous ']/li [3] "". getText ("// ul [@class = 'alert alert-danger']/li [4]")..be.equal ('Thov nkag rau hauv nroog');}).getText ("// ul [@class = 'ceeb toom ceeb toom ceeb toom']/li [5]"). tom qab ntawd (muaj nuj nqi (e) {console.log ('Yuam kev pom:' + e); (e).should.be.equal ('Thov sau lub xeev');}); });

  • Looping Cov Ntaub Ntawv txhawm rau Txheeb Xyuas URL Txuas/Ntawv/Nplooj Ntawv - "LoopDataExample1.js"

    • Qhov piv txwv no qhia: Siv cov ntaub ntawv JSON los khaws qhov txuas thiab lub npe, tom qab ntawd rov ua dua
      • Txheeb xyuas txhua kab ntawv URL thiab txuas
      • Nyem qhov txuas thiab thauj nplooj ntawv

// Txuas cov ntaub ntawv - txuas thiab ntawv nyeem

var linkArray = [{"txuas": "https://github.com/onewithhammer/WebDriverIOTutorial/blob/master/tutorial1.js", "name": "tutorial1.js"}, {"link": "https://github.com/onewithhammer/WebDriverIOTutorial/blob/master/linkTextURL1.js "," npe ":" linkTextURL1.js "}, {" link ":" https://github.com/onewithhammer/WebDriverIOTutorial/blob/ master/copyright1.js "," name ":" copyright1.js "}, {" link ":" https://github.com/onewithhammer/WebDriverIOTutorial/blob/master/formFillSubmit1.js "," npe ":" formFillSubmit1.js "}, {" link ":" https://github.com/onewithhammer/WebDriverIOTutorial/blob/master/showHideVerify1.js "," name ":" showHideVerify1.js "}, {" link ":" https://github.com/onewithhammer/WebDriverIOTutorial/blob/master/dynamicBrowser.js "," npe ":" dynamicBrowser.js "}, {" link ":" https://github.com/onewithhammer/WebDriverIOTutorial/ blob/master/callbackPromise.js "," name ":" callbackPromise.js "}, {" link ":" https://github.com/onewithhammer/WebDriverIOTutorial/blob/master/debugExample1.js "," npe ": "dab gExample1.js "}, {" link ":" https://github.com/onewithhammer/WebDriverIOTutorial/blob/master/formFieldValidation.js "," name ":" formFieldValidation.js "}, {" link ":" https://github.com/onewithhammer/WebDriverIOTutorial/blob/master/common/commonLib.js "," name ":" commonLib.js "}, {" link ":" https://github.com/onewithhammer/ WebDriverIOTutorial/blob/master/dataLoopExample1.js "," lub npe ":" dataLoopExample1.js "}]; … // voj los ntawm txhua qhov linkArray linkArray.forEach (ua haujlwm (d) {nws ('yuav tsum muaj cov ntawv/txuas tom qab ntawv nkag mus nplooj ntawv -' + d.name, muaj nuj nqi () {rov tsav tsheb // nco ntsoov tias koj nyob ntawm nplooj ntawv pib.url ('https://www.tlkeith.com/WebDriverIOTutorialTest.html').getTitle (). Tutorial Test Page ");}) // nrhiav qhov URL.getAttribute ('a =' + d.name," href "). Ces (muaj nuj nqi (txuas) {(txuas).should.equal (d.link); console.log ('URL pom:' + d.link);}) // mus rau URL nplooj ntawv thiab xyuas nws muaj.click ('a =' + d.name).waitForVisible ("#js-repo-pjax- ntim ", 10000).then (function () {console.log ('Github Page Found');});});});

  • Looping Static Data to Populate Form Fields - "loopDataExample2.js"

    • Qhov piv txwv no qhia: Siv kab ntawv JSON cov ntaub ntawv khaws thawj/lub xeem

      • Loop los ntawm cov ntaub ntawv mus rau daim teb daim ntawv, tom qab ntawv xa daim ntawv
      • Tos rau nplooj ntawv tshwm sim
      • Txheeb xyuas lub npe / xeem lub npe ntawm nplooj ntawv ntsuas

// data array - firstName and lastNamevar dataArray = [{"firstName": "Tony", "lastName": "Keith"}, {"firstName": "John", "lastName": "Doe"}, {"firstName ":" Jane "," lastName ":" Doe "}, {" firstName ":" Don "," lastName ":" Johnson "}]; … // voj los ntawm txhua dataArray dataArray.forEach (muaj nuj nqi (d) {nws ('yuav tsum populate teb, sumbit nplooj ntawv', muaj nuj nqi () {rov tsav tsheb // nco ntsoov tias koj nyob ntawm nplooj ntawv pib.url ('http:/ /www.tlkeith.com/WebDriverIOTutorialTest.html ').getTitle (). tom qab ntawd (ua haujlwm (npe) {// txheeb xyuas lub npe (npe).should.be.equal ("Web Driver IO - Tutorial Test Page");}).setValue ("#fname", d.firstName).getValue ("#fname"). ces (function (e) {(e).should.be.equal (d.firstName); console.log ("Ua ntej Npe: " + e);}).setValue ("#lname ", d.lastName).getValue ("#lname "). Ces (muaj nuj nqi (e) {(e).should.be.equal (d.lastName); console.log ("Xeem Npe:" + e);}).submitForm ("#search-form"). "#search-results", 10000).then (function () {console.log ('Result Page Found');}).getText ("// h1"). ces (function (link) {console.log ('Cov ntawv pom:' + txuas); (txuas).should.equal ("Zoo siab txais tos" + d.firstName + "" + d.lastName + ".");});});});

  • Txheeb xyuas CSS Cov Khoom - "cssValidation1.js"

    • Qhov piv txwv no qhia yuav ua li cas:

      • Txheeb xyuas cov cuab yeej CSS hauv qab no:

        • xim
        • padding (saum, hauv qab, sab xis, sab laug)
        • xim tom qab

nws ('yuav tsum muaj cov xim raug ntawm cov ntawv yuam kev', muaj nuj nqi () {xa rov qab.getCssProperty ("// ul [@class = 'ceeb toom ceeb toom-txaus ntshai']/li [1]", "xim"). muaj nuj nqi (tshwm sim) {console.log ('Xim pom:' + result.parsed.hex + "lossis" + result.value); (result.parsed.hex).should.be.equal ('#a94442'); });});

nws ('yuav tsum muaj cov padding raug hauv cov lus ntawm tes', muaj nuj nqi () {

rov qab tsav tsheb // padding: sab saum toj sab xis hauv qab.getCssProperty ("// table [@id = 'filelist']/thead/tr [1]/td [1]", "padding-top"). ces (muaj nuj nqi (result) {console.log ('padding-top found:' + result.value); (result.value).should.be.equal ('10px');}).getCssProperty ("// rooj [@id = 'filelist']/thead/tr [1]/td [1] "," padding-bottom "). result.value).should.be.equal ('10px');}).getCssProperty ("// table [@id = 'filelist']/thead/tr [1]/td [1]", "padding- txoj cai "). tom qab ntawd (ua haujlwm (tshwm sim) {console.log ('padding-right found:' + result.value); (result.value).should.be.equal ('5px');}).getCssProperty ("// table [@id = 'filelist']/thead/tr [1]/td [1]", "padding-left"). ces (muaj nuj nqi (tshwm sim) {console.log ('padding-laug pom: ' + result.value); (result.value).should.be.equal (' 5px ');}); });

nws ('yuav tsum muaj cov xim keeb kwm yav dhau los raug hauv kab lus header', muaj nuj nqi () {

rov qab tsav tsheb.getCssProperty ("// table [@id = 'filelist']/thead", "background-color"). hex); (result.parsed.hex).should.be.equal ('#eeeeee');}); });

Kauj Ruam 6: Cov Lus Qhia thiab Cov Lus Qhia

Cov Lus Qhia thiab Cov Lus Qhia
Cov Lus Qhia thiab Cov Lus Qhia
  • Kev debugging:

    • Qhib kev nkag mus ntawm tus neeg tsav tsheb kom debug ntau dua thiab tsim cov cav.

      • Teeb logLevel rau 'verbose'
      • Teeb logOutput rau cov npe npe ('cav')

tsav tsheb = webdriverio.remote (loglevel: 'verbose', logOutput: 'cav', {yam xav tau: {browserName: 'firefox'}});

  • Siv console.log (), debug (), getText () kom debug.

    • console.log () - Siv los tso saib cov ntaub ntawv los txiav txim lub xeev.
    • debug () - Siv ncua browser/tsab ntawv kom txog rau thaum nkag mus raug nias ntawm kab hais kom ua.
    • getText () - Siv los txheeb xyuas tias koj tab tom cuam tshuam nrog lub ntsiab lus raug.

      Tshwj xeeb tshaj yog pab nrog kab zauv kab lus

// Nyem rau ntawm Yam 3 los ntawm cov npe

nws ('yuav tsum nyem rau Yam 3 los ntawm cov npe', muaj nuj nqi () {// siv getText () txhawm rau txheeb xyuas xpath yog qhov raug rau lub caij rov qab tsav tsheb.getText ("// ul [@id = 'mylist']/li [3]/div/div/a "). ("Yam khoom 3");}) // siv debug () kom tsis txhob ua qhov pom tshwm sim ntawm qhov browser.debug ().click ("// ul [@id = 'mylist']/li [3] /div/div/a").then (function () {console.log ('Link clicked');}) // tos rau google daim ntawv tshawb fawb kom tshwm.waitForVisible ("#tsf", 20000). (e) {console.log ('Tshawb Pom Kev Pom Zoo');});});

  • Siv Ib puag ncig hloov pauv los hloov Browser Dynamically:

    • Siv ib puag ncig sib txawv SELENIUM_BROWSER txhawm rau ua tus browser sib txawv yam tsis hloov kho cov ntawv xeem txhua lub sijhawm.
    • Yuav tsum tau hloov pauv me ntsis txhawm rau txhawb nqa.

Cov Cai Hloov:

// thauj tus tsav tsheb rau browser

tsav tsheb = webdriverio.remote ({xav tau lub peev xwm: {browserName: process.env. SELENIUM_BROWSER || 'chrome'}});

Txhawb Browser:

  • Internet Explorer - IE 8+ (Windows nkaus xwb)

    SELENIUM_BROWSER = ie mocha

  • Firefox 10+ (Windows/Max/Linux)

    SELENIUM_BROWSER = firefox mocha

  • Chrome 12+ (Windows/Max/Linux)

    SELENIUM_BROWSER = chrome mocha

  • Opera 12+

    SELENIUM_BROWSER = opera mocha

  • Safari

    SELENIUM_BROWSER = safari mocha

Kev Xeem:

  • Rau Windows siv git bash plhaub:

    • SELENIUM_BROWSER = chrome mocha
    • $ SELENIUM_BROWSER = chrome mocha DynamicBrowser.js
  • Rau Mac lossis Linux, qhib davhlau ya nyob twg:

    • SELENIUM_BROWSER = chrome mocha
    • $ SELENIUM_BROWSER = chrome mocha DynamicBrowser.js
  • Xeem Kev Xeem:

    • Txiav txim siab qhov cuam tshuam raws qhov phiaj xwm lossis phiaj xwm (piv txwv li bootstrap).
    • Txheeb xyuas qhov hloov pauv ib puag ncig rau txhua qhov kev cuam tshuam:

      • DESKTOP - 1200 px
      • TABLET - 992 px
      • MOBILE - 768 px
    • Tsim kev hais kom rov siv tau los nyeem ib puag ncig sib txawv thiab teeb tsa qhov browser loj.

      Saib piv txwv hauv qab no

    • Hu rau qhov hais kom rov siv tau hauv koj tsab ntawv xeem.

// rov siv tau code - tsev qiv ntawv // code snippet yog (bp == "DESKTOP") {obj.width = 1200; qhov siab = 600; ob.name = bp; } lwm yog (bp == "TABLET") {obj.width = 992; qhov siab = 600; ob.name = bp; } lwm yog (bp == "MOBILE") {obj.width = 768; qhov siab = 400; ob.name = bp; }

// Test tsab ntawv

ua ntej (muaj nuj nqi (ua tiav) {winsize = common.getWindowSizeParams ();… driver.addCommand ('setWindowSize', common.setWindowSize.bind (tsav tsheb));} // teeb tsa lub qhov rais loj nws ('yuav tsum teeb tsa qhov rai loj', muaj nuj nqi (ua tiav) {// tsuas yog qhov dav qhov teeb meem driver.setWindowSize (winsize.width, winsize.height, muaj nuj nqi () {}). hu (ua tiav);});

  • Reusable Commands (Custom Commands):

    • Web Tsav IO tau yooj yim txuas ntxiv.
    • Kuv nyiam muab txhua lo lus txib rov siv tau rau hauv lub tsev qiv ntawv. (tej zaum qhov no yog tsev kawm qub tab sis nws ua haujlwm!)

common/commonLib.js

// ua pov thawjLastNameCheckError ()

// // Nqe lus piav qhia: // Txheeb xyuas lub xeem lub npe daim ntawv lees paub yuam kev yuam kev // // Kev tawm tswv yim: // tus lej - qhov ntsuas ntawm qhov ua yuam kev (1-5) // Cov Lus Qhia: // tsis muaj // var verifyLastNameCheckError = muaj nuj nqi () { var idx = sib cav [0], callback = sib cav [arguments.length - 1]; this.getText ("// ul [@class = 'alert alert-danger']/li [" + idx + "]", muaj nuj nqi (yuam kev, e) {console.log ('Yuam kev pom:' + e); (e).should.be.equal ('Thov sau lub xeem');}).cov (hu rov qab); }; // export lub luag haujlwm module.exports.verifyLastNameCheckError = txheeb xyuasLastNameCheckError;

Nov yog cov kev hloov pauv tshwj xeeb uas yuav tsum tau hu ua lub luag haujlwm siv tau

Saib formFieldValidation.js kom ua tiav qhov piv txwv ua haujlwm

// xav tau qhov hais kom rov siv tau - CommonLib

common = xav tau ('./ Common/CommonLib'); … // khi cov lus txib driver.addCommand ('verifyFirstNameError', common.verifyFirstNameCheckError.bind (tus tsav tsheb)); driver.addCommand ('verifyLastNameError', common.verifyLastNameCheckError.bind (tus tsav tsheb)); nws ('yuav tsum muaj 2 qhov yuam kev: lub npe/lub xeem', muaj nuj nqi () {// hu tus neeg siv rov ua haujlwm tau.verifyFirstNameError (1);.verifyLastNameError (2);});

  • Cov Ntaub Ntawv Project/Cov Txheej Txheem Txheej Txheem:

    • Nov yog cov phiaj xwm phiaj xwm raug:

      • "Project" - cov phiaj xwm tseem ceeb

        • README.md - nyeem rau txoj haujlwm thoob ntiaj teb
        • "Common" - cov npe rau thoob ntiaj teb kev ua haujlwm zoo rau txhua txoj haujlwm

          • common -lib.js - lub tsev qiv ntawv ua haujlwm thoob ntiaj teb
          • README.md - nyeem rau kev ua haujlwm thoob ntiaj teb
        • "Product1" - cov npe rau khoom 1

          • test-script1.js
          • test-script2.js
          • "Common" - cov npe rau cov haujlwm hauv cheeb tsam rau txoj haujlwm 1

            • prod1 -lib.js - lub tsev qiv ntawv ua haujlwm hauv zej zog rau txoj haujlwm 1
            • README.md - nyeem rau lub luag haujlwm hauv cheeb tsam rau txoj haujlwm 1
        • "Product2"-cov npe rau khoom 2test-script1.jstest-script2.js

          • "Common" - cov npe rau cov haujlwm hauv cheeb tsam rau qhov project 2

            • prod2 -lib.js - lub tsev qiv ntawv ua haujlwm hauv zej zog rau txoj haujlwm 2
            • README.md - nyeem rau lub luag haujlwm hauv cheeb tsam rau txoj haujlwm 2
  • Rhuav cov ntawv sau ua ntau yam ntaub ntawv:

    • Nov yog qhov piv txwv ntawm kev siv ntau cov ntaub ntawv:

      • Sanity Check - cov ntawv xeem yooj yim los txheeb xyuas txhua yam ua haujlwm
      • Static Element thiab Text Validation - txheeb xyuas txhua lub ntsiab lus thiab cov ntawv
      • Form/Page Error Validation - yuam kev siv tau
      • Tshawb Nrhiav - sim cov ntsiab lus zoo
  • Hu rov qab VS. Kev cog lus:

    • Version 3 ntawm Web Driver IO txhawb nqa ob qho kev hu rov qab thiab kev cog lus.

      Cov lus cog tseg yog txoj hauv kev zoo dua vim nws txo qis kev ua yuam kev. Thov saib hauv qab no tib qho piv txwv sau siv callbacks thiab cog lus.

Hu rov qab

// Teeb tsa/txheeb xyuas thawj/lub xeem siv Callback

nws ('yuav tsum teeb tsa/txheeb xyuas thawj/lub xeem siv Callbacks', ua haujlwm (ua tiav) {driver.setValue ("#fname", "Tony", muaj nuj nqi (e) {driver.getValue ("#fname", muaj nuj nqi, e) {(e).should.be.equal ("Tony"); console.log ("First Name:" + e); driver.setValue ("#lname", "Keith", function (e) { driver.getValue ("#lname", function (err, e) {(e).should.be.equal ("Keith"); console.log ("Last Name:" + e); done ();});});});});});));

Kev cog lus

// Teeb tsa/txheeb xyuas thawj/lub xeem siv Kev cog lus

nws ('yuav tsum teeb tsa/txheeb xyuas thawj zaug/lub xeem siv lo lus cog tseg', ua haujlwm () {rov qab tsav tsheb.setValue ("#fname", "Tony").getValue ("#fname"). tom qab ntawd (muaj nuj nqi (e) {(e).should.be.equal ("Tony"); console.log ("Lub Npe Lub Npe:" + e);}).setValue ("#lname", "Keith").getValue ("#lname"). tom qab ntawd (muaj nuj nqi (e) {(e).should.be.equal ("Keith"); console.log ("Xeem Npe:" + e);});});

Kauj Ruam 7: Cov peev txheej ntxiv

Nov yog qee qhov peev txheej ntxiv rau koj siv:

  • Pawg Sib Tham (Gitter)

    • Web Tsav IO Sib Tham Pab Pawg
    • Mocha Sib Tham Pawg
  • Lwm yam haujlwm zoo

    • Supertest - Kev lees paub
    • Chai - kev lees paub

Kauj Ruam 8: Xaus

Kuv siv qee lub sijhawm tshawb fawb cov thev naus laus zis los siv rau kuv txoj haujlwm. Kuv xub pib nrog Selenium Web Driver tab sis hloov mus siv Web Driver IO. Web Tsav IO zoo li siv tau yooj yim dua thiab yooj yim dua rau txuas ntxiv (tsawg kawg yog cov ntaub ntawv rau kev txuas ntxiv - rov siv tau cov lus txib tau zoo dua).

Thaum kuv xub pib saib cov thev naus laus zis nws nyuaj rau pom cov piv txwv zoo uas cuam tshuam nrog txhua yam uas kuv tau sim ua. Qhov no yog qhov laj thawj kuv xav qhia cov ntaub ntawv no thiab kev paub nrog koj.

Cov thev naus laus zis ua haujlwm tau zoo dua li qhov kuv xav tau txawm li cas los xij muaj kev kawm nkhaus koom nrog. Thaum kuv nkag siab tias txhua yam khoom ua haujlwm sib koom ua ke li cas, Kuv muaj peev xwm sau cov ntawv xeem nyuaj hauv lub sijhawm luv luv. Cov ntawv nyuaj tshaj plaws yog JavaScript raws cov khoom xws li tus tuaj tos hnub tim thiab xaiv tus qauv.

Kuv yeej tsis tau sau kuv tus kheej ua tus tsim JavaScript lossis kuv txhua tus xav ua tus kws tshaj lij JavaScript, tab sis kev siv cov thev naus laus zis no tau txhawb kuv kom ntse kuv li txuj ci JS.

Kuv vam tias tsab xov xwm no muaj txiaj ntsig thiab cov piv txwv yog qhov tseeb thiab qhia paub.

Thov qhia rau kuv paub yog tias koj muaj lus nug lossis lus pom.

Ua tsaug, Tony Kev

Pom zoo: