⬆️ ⬇️

Testing web projects. jsFUnit

Testing web projects. js f unit



Automated tests play a huge role in professional programming. They replace and the team of professional testers and suggest problems in other parts.

extensive project during development. The work of the web application must be tested on different browser engines: Gecko, Presto, KHTML, WebKit and Trident.



A variety of automated tests are functional tests. Their main feature is the user experience emulation with the application.

through the interface. In web programming, Selenium is used to write functional tests. A huge disadvantage of Seleniuma is its low speed in performing such tests.

')

A faster framework turned out to be jsUnit. True, jsUnit is designed to write unit tests, not functional ones. He has a number of other minor flaws. Like that

appeal to the tests on their way, rather than choosing a test from the list. In the case of at least one error, the entire slider is painted in red, and not divided into green / red areas corresponding to successful / unsuccessful tests.



All this led to the creation of jsFUnit.







The js F Unit consists of an interface for running tests and two libraries: a condition checking library (various assert functions) and a library of functions that emulate a user experience with a web interface. And perl script

(update.pl) registering new tests.



I located js F Unit in Surceforge, from where I propose to download it to everyone: http://sourceforge.net/projects/jsfunit/files/



After you download js F Unit -2.1.zip, unpack it into your web project's public_html. You will have a directory public_html / tests. I hope that all readers are aware of what public_html is. But for those who accidentally began to read the article, I suggest to take the Windows machine, install xampp from http://www.apachefriends.org/en/xampp.html in c: \ xampp and unpack js F Unit -2.1.zip in c: \ xampp \ htdocs.



In the browser, open http: // localhost / tests (or what other address does your host have). You will see the js F Unit interface. It's pretty simple and you can figure it out easily.



To create a test:

1. Add a test to the tests / tests-js directory

2. The test must have the extension .js

3. Run update.pl (it comes with js F Unit and is located in the tests directory. This is a perl script. For beginners, I’ll explain:

Start -> Run ...

cmd

c: \ xampp \ perl \ bin \ perl.exe c: \ xampp \ htdocs \ tests \ update.pl

)

4. Update http: // localhost / tests (F5)



What do the tests look like?



js f unit

similar to jsUnit, jUnit, CUNIT, PHPUnit, NUnit, PyUnit, fUnit, DUnit,

FPCUnit, Test :: Class and Test :: Unit, CPPUnit, FlexUnit, COSUnit, etc.



The test file itself has one or more functions with the test prefix in the name. Which are sequentially launched.



function testNagan () {

...

}



function testBlaster () {

...

}



The functions describe the manipulations with the web application and checks for the correct response of the web application.



function testNagan () {

var nagan = new Nagan ()

assertEquals ( “The name of an instance of Nagan is not of the type 'string'. Condolence,” typeof (nagan.name), “string” )

}



If you need to make any initialization actions at the beginning of each test, you can use setUp.

And at the end - tearDown.



function setUp () {

protiv_loma_net_prijoma = new Lom ()

}



function Nagan () {

assert ( “Where did you learn how to throw scrap?” , protiv_loma_net_prijoma. rasstojanie_v_metrax (20))

}



function Sablja () {

assert ( "Yes, you are very weak, my dear!" , protiv_loma_net_prijoma. rasstojanie_v_metrax (1.5))

}



function tearDown () {

protiv_loma_net_prijoma. polozhit ()

}



But the js F Unit functions for checking



assert ([comment], value) - If the value is 0, "" , undefined, false or null, then assert terminates the test and displays an error in the log with a comment if a comment is specified. The condition must be an expression that returns a boolean value. Example: assert ( “Terrible error !!!” , a == 10 || x <2)

assertTrue ([comment], condition) is a synonym for assert

assertFalse ([comment], condition) is equivalent to assert, but an error is considered when the condition is true

assertEquals ([comment], expected_value, received_value) - error if the received value does not match the expected one

assertNotEquals ([comment], expected_value, received_value) - error if the received value matches the expected one

assertIdentity ([comment], expected_value, obtained_value) is an error if the received value is not identical with the expected one

assertNotIdentity ([comment], expected_value, derived_value ) is an error if the resulting value is identical with the expected

assertNull ([comment], value) - error if the value is not null

assertNotNull ([comment], value) - error if the value is null

assertUndefined ([comment], value) - an error if the value is not undefined

assertNotUndefined ([comment], value) - error if the value is undefined: assertNotUndefined (undefined)

assertNaN ([comment], value) - error if the value is not NaN

assertNotNaN ([comment], value) is an error if the value is NaN: assertNotNaN (Number.NaN)

fail (comment) - error

warn (message, [value]) - Writes a message to the log in red. If a value is specified, then displays it as “message: value”

inform (message, [value]) - Writes to the log in green

info (message, [value]) - Writes to the log in green

debug (message, [value]) - Writes to the log in blue



Interface manipulation functions



These functions are similar to some of the functions of Selenium.



Functions for manipulating windows and elements



Functions ending in Def return false in case of failure, and do not raise an exception (see assert).



function testOpen () {

win = getWindow ( "MAIN" ) // The first thing the user does is open the web application page.

// getWindow waits until the page completely loads

// The page must be in the same domain in which the js F Unit is located

iframe = getFrameWindow (win, "frame_for_tables" ) // Sometimes it is necessary to work with frames

}



function testForm () {

select (win.document. getElementById ( "City" ), "St. Peterburg" ) // select a city from the drop-down list

clickToButton (win, “Apply” ) // click on the [Apply] button

var pwin = openButton (win, “Recalculate” ) // click on the [Recalculate] button and wait for the window with the name Pereschitatq to load

clickToLink (pwin, “Menu 1” , 2) // Click on the second item on the pwin page with innerHTML = “Menu 1”

}



And now more:



getWindowDef (name, [time])



getWindow (name, [time])



Returns a window by its name.





Example:



Window:

<html>

<head>

<title> @@ Admin or as ** </ title>

<script>

window.name = "__ADMIN_ILI_KAK__"

</ script>

</ head>

<body>

</ body>

</ html>



Test:

win = getWindow ( "@@ Admin or as **" )



waitCondition (cond, time) - wait for the execution of cond



if cond does not become true in time milliseconds - exit



cond only works with global variables, if given by a string. For local variables, use the function:

var a = 10, b = 10

waitCondition ( function () {return a == b}, 500)



sleep (time)



sleep time milliseconds. Similar to:

waitCondition ( "false" , 500)



waitLoadWindowDef (win, time)



waitLoadWindow (win, time)



Waits for the window to load win time milliseconds. If the window loads faster than in time, then the wait will complete immediately.





Attention! The getWindow function already uses waitLoadWindow. waitLoadWindow must be used after window.open:

win = window. open ( "localhost / cgi-bin / mypage.pl" , "name_window" )

waitLoadWindow (win, 5000)



getFrameWindowDef (win, frame, [time])



getFrameWindow (win, frame, [time])



Waits for the page to load into the specified frame and returns its window object.





Example:



Page in win:

<html>

<body>

<iframe name = "frame_tab" src = "page_for_this_frame" > </ iframe>

</ body>

</ html>



Test:

var win_in_frame = getFrameWindow (win, "frame_tab" )



selectDef (sel, text)



select (sel, text)



Selects the <select> tag option by its text.

sel is an element. You can get it by name: select (document.all.reason, "That's it" )

<select name = "reason" >

<option value = 1> And no

<option value = 2> Like this

</ select>



The same can be achieved as follows: document.all.reason.value = 2



linkDef (win, text_in_link, [tag], [event], [number])



link (win, text_in_link, [tag], [event], [number])



returns a link to the element with the text text_in_link: <div> text_in_link </ div> or <a> text_in_link </a> or in other elements





Example:



Window:

<html>

<head>

<script>

window.name = "Admin"

</ script>

</ head>

<body>

<div id = Div1> Sex <b> s </ b> PERSON </ div>

<div id = Div2> Paul <b> s </ b> PERSON </ div>

</ body>

</ html>



Test:

win = getWindow ( "Admin" ) // Get the window

div = link (win, “Users” , 2) // get the second element with the text “Users” in it (Div2)

div.style.color = "red" // make it red



clickToLinkDef (win, text_in_link, [number])



clickToLink (win, text_in_link, [number])



clicks on the item specified by content, you can specify which item in the account to click (number)



Example:



Window:

<a href= "/users"> Users </a>



Test:

clickToLink (win, "Users" )



clickToButtonDef (win, text_in_value, [number])



clickToButton (win, text_in_value, [number])



presses a button, you can indicate on which item to click (number)



Example:



Window:

<input type = "button" value = "Users" >



Test:

clickToButton (win, “Users” )



openLink (win, text_in_link, [sleepTime], [time], [number])



clicks on an element and returns a window object of the same name.





Example:



Window:

<script src = "tests / WorkWithHtml.js" >

<script>

window.name = translate ( "Users" )

// window.name = "Polqzovateli"

</ script>



Test:

win = openLink (win, "Users" )



openButton (win, text_in_value, [sleepTime], [time], [number])



presses a button and returns a window object of the same name.





findings





1. Disadvantages of Selenium:

a) server installation required

b) resource demanding

c) a lot of time is spent on starting the server + launching the browser

d) Selenium server restarts the browser for each test



2. Disadvantages of jsUnit:

a) for each test, an html wrapper is created

b) to run several tests, you need to write a new test with the suite () function, in which to list the called

c) the path to the test is entered into the TestRunner (jsUnit interface) manually each time

d) debug information is displayed in a separate window, and not in the general error log



3. Advantages of jsFUnit:

a) does not require a server

b) it is not exacting to resources

c) a test or a group of tests are selected from the drop-down list

g) a group of tests that need to run just put in a separate directory

e) debug information is displayed in the general log

e) you can visually estimate the number of tests performed through the filling of the slider



Thus jsFUnit is fast, easy to use and thus saves your time!

Source: https://habr.com/ru/post/86967/



All Articles