E2E test to select select box with CasperJS, Nightmare and Python + WebDriver + PhantomJS

Purpose

I was writing an E2E test and was addicted to selecting select boxes, so I will try how to write it in multiple libraries after researching. Write some features you care about.

--Select the select box --Take the selected value in the select box --Take the displayed URL --Capture

CasperJS

/*global casper*/
casper.test.begin('Test Sample', 2, function(test) {
    var selectbox = '#lang-chooser';
    var lang = 'fr';

    casper.start('https://www.google.com/doodles');

    casper.then(function() {
        var params = {};
        test.comment('change the value of the select box');

        params[selectbox] = lang;
        this.fillSelectors('#language-menu',params);

        test.assertEvalEquals(
            function(selector) {
                return document.querySelector(selector).value;
            },
            lang,
            'value of the select box has been changed',
            selectbox);
    });

    casper.then(function() {
        var comment = 'is included language type in url';
        var re = new RegExp('\\?hl=(' + lang + ')');
        test.assertMatch(this.getCurrentUrl(), re, comment);
    });

    casper.then(function() {
        this.capture('google.png');
    });

    casper.run(function() {
        test.done();
    });

});

Execution result

$ casperjs test test_google_selector.js
Test file: test_google_selector.js
# Test Sample
PASS Test Sample (2 tests)
# change the value of the select box
PASS value of the select box has been changed
PASS is included language type in url
PASS 2 tests executed in 2.477s, 2 passed, 0 failed, 0 dubious, 0 skipped.

Nightmare + mocha

/*jshint -W024 */
var Nightmare = require('nightmare');
var assert = require("assert");

describe('Test Sample', function(){
    this.timeout(15000);

    it('can change the value of the select box.', function(done){
        var selector = '#lang-chooser';
        var lang = 'fr';
        new Nightmare()
            .goto('https://www.google.com/doodles')
            .select(selector, lang)
            .evaluate(function (selector) {
                return document.querySelector(selector).value;
            }, function (select_value) {
                assert.equal(lang, select_value, 'value of the select box has been changed');
            }, selector)
            .wait(3000)
            .evaluate(function(lang) {
                var re = new RegExp('\\?hl=(' + lang + ')');
                return location.href.match(re);
            }, function(is_included_lang_in_url) {
                assert.ok(is_included_lang_in_url, 'is included language type in url');
            }, lang)

            .screenshot('google.png')
            .run(done);
    });
});

Execution result

$ node_modules/.bin/mocha test_google_selector.js


  Test Sample
    ✓ can select a select box. (3932ms)


  1 passing (4s)

Python + webdriver + phantomjs

from selenium import webdriver
from selenium.webdriver.support.ui import Select
import unittest


class TestSample(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.PhantomJS()
        self.driver.get('https://www.google.com/doodles')

    def tearDown(self):
        self.driver.quit()

    def test_lang_chooser(self):
        """can select a select box"""
        driver = self.driver
        selector = 'select#lang-chooser'
        lang = 'fr'

        # change the value of the select box
        element = driver.find_element_by_css_selector(selector)
        select = Select(element)
        select.select_by_value(lang)

        element = driver.find_element_by_css_selector(selector)
        self.assertEqual(
            lang, element.get_attribute("value"),
            'value of the select box has been changed'
        )

        self.assertTrue(
            '?hl={lang}'.format(lang=lang) in driver.current_url,
            'is included language type in url'
        )

        driver.save_screenshot("google.png ")


if __name__ == "__main__":
    unittest.main()

Execution result

$ python test_google_selector.py -v
test_lang_chooser (__main__.TestSample)
can select a select box ... ok

----------------------------------------------------------------------
Ran 1 test in 5.608s

OK

Impressions

Unintentionally, it became only Phantom JS system .... How do you write an easy-to-read test? Like comments. I wonder if the E2E test should be something that can be written and executed quickly and quickly. There are more Selenium-based ones ...


Casperjs

--If you enclose it in casper.then, it will be less likely to cause an error. If you enclose it too much, the code becomes redundant, so it is difficult to decide where to separate it. --The code description is unique. ――There seems to be a lot of information, but there may be little. I want to see various codes that may be helpful ...

Nightmare

--If you do not put a long wait, an error may occur. Sometimes it's too late and I don't know where to wait ... ――It was extremely slow when I used it on a certain login screen ――When writing a test, I wonder if you can mix mocha or chai as you like. This library also has little information ... ――Evaluation () is a little difficult to understand or use. It may be unavoidable because it is such a thing. --If the speed is improved and there is a lot of information such as test sample code, this may be good for PhantomJS series ...

Python

--Interested ――I thought the code would be redundant, but CasperJS is also quite redundant ... It depends on how you write it. ――It may be surprising if you understand how to write well.


reference

Recommended Posts

E2E test to select select box with CasperJS, Nightmare and Python + WebDriver + PhantomJS
Fractal to make and play with Python
How to do portmanteau test with python
Use selenium phantomjs webdriver with python unittest
Python code to train and test with Custom Vision of Cognitive Service
Scraping tabelog with python and outputting to CSV
MessagePack-Try to link Java and Python with RPC
Memo to create your own Box with Pepper's Python
Procedure to load MNIST with python and output to png
I want to handle optimization with python and cplex
Try to operate DB with Python and visualize with d3
Something to enjoy with Prim Pro (X-Play) and Python
Primality test with Python
Scraping with Python + PhantomJS
Drive WebDriver with python
Primality test with python
Design and test Verilog in Python only with Veriloggen and cocotb.
screen and split screen with python and ssh login to remote server
[Python] How to play with class variables with decorator and metaclass
Test Python with Miniconda on OS X and Linux with travis-ci
Send experiment results (text and images) to slack with Python
Select file in dialog with python → Display file name in message box
Try to bring up a subwindow with PyQt5 and Python
How to do Bulk Update with PyMySQL and notes [Python]
[Let's play with Python] Image processing to monochrome and dots
Convert video to black and white with ffmpeg + python + opencv
I tried to make GUI tic-tac-toe with Python and Tkinter
Get additional data to LDAP with python (Writer and Reader)
How to log in to AtCoder with Python and submit automatically
Module summary that automates and assists WebDriver installation with Python
Programming with Python and Tkinter
Connect to BigQuery with Python
Encryption and decryption with Python
Connect to Wikipedia with Python
Post to slack with Python 3
Switch python to 2.7 with alternatives
Write to csv with Python
Python debug and test module
python with pyenv and venv
Challenge Python3 and Selenium Webdriver
Works with Python and R
Operate Jupyter with REST API to extract and save Python code
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
How to import CSV and TSV files into SQLite with Python
Linking Python and Arduino to display IME On / Off with LED
How to make a surveillance camera (Security Camera) with Opencv and Python
Sample to use after OAuth authentication of BOX API with Python
I tried to make a periodical process with Selenium and Python
How to deal with errors when installing Python and pip with choco
Try to display google map and geospatial information authority map with python
Throw something to Kinesis with python and make sure it's in
I tried to easily detect facial landmarks with python and dlib
Move data to LDAP with python Change / Delete (Writer and Reader)
How to use Python with Jw_cad (Part 2 Command explanation and operation)
How to build Python and Jupyter execution environment with VS Code
[Python] Try to recognize characters from images with OpenCV and pyocr