Code/JavaScript

[Node.js] 브라우저 테스팅 툴 NightWatch 사용법

codens 2020. 8. 24. 11:07

 

 

nightwatchjs / nightwatch

https://github.com/nightwatchjs/nightwatch - 10.4k

    - v1.3.8 , 2020/08

 

https://nightwatchjs.org/api



    - 설치

npm i -g nightwatch

 

    - 크롬 드라이버 설치

npm install chromedriver --save-dev



    - nightwatch.conf.js 설정

 

desiredCapabilities: {

        browserName: 'chrome',

        chromeOptions: {

            

            args: [

                //'--no-sandbox',

                //'--ignore-certificate-errors',

                //'--allow-insecure-localhost',

                //'--headless'

                // 확장 추가로 로드

                '--load-extension=C:\\Users\\userID\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 2\\Extensions\\cjpalhdlnbpafiamejdnhcphjbkeiagm\\1.29.2_0',

                '--auto-open-devtools-for-tabs', // 디버깅 툴 열기

            ]

        }

    },

    webdriver: {

        "start_process": true,

        "server_path": "node_modules/.bin/chromedriver",

        "port": 9515

    },

 

//-------------------------

    - 테스트 파일 작성 , test/test1.js

describe('Demo', function () {

    before(browser => browser.url('http://example.com'));

    test('Demo test', function (browser) {

        browser

            .waitForElementVisible('#word1')

            //더블 클릭

            .element('css selector', '#word1', function (ret) {

                this.moveTo(ret.value.ELEMENT, 0, 0, function () {

                    this.doubleClick()

                })

            })

            .perform(function (client, done) {

                console.log('perform', 123);

                client.getText('#word1', function (ret) {

                    console.log(ret);

                })

                done();

            })

            .pause(100000)

    });

    after(browser => browser.end());

 

    - 테스트 실행

nightwatch test/test1.js



//----------------------------

    - 사용자 정의 명령(Custom Command)

https://nightwatchjs.org/guide/extending-nightwatch/#writing-custom-commands

 

        - 설정 (nightwatch.conf.js 파일 수정) 

custom_commands_path: 'test/user',

 

    - test/user/user1.js 파일 생성

module.exports = class user1 extends Events {

    command(ms, cb) {

        console.log('user1', ms, cb);

    }

}



    - 작성한 명령 실행

    browser

            .waitForElementVisible('#word1')

            .user1(123)




반응형