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
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.
Recommended Posts