Test sorting with Cucumber and Capybara - javascript

Is there a way to test sorting of a list with Cucumber and Capybara. The sorting is done client-side with javascript.
I was thinking something along the lines of:
Then I should see "first element" and then I should see "second element"
Unfortunately I have no idea how to approach building the steps.
Thanks for the help!

It's a good idea to separate out the stories that you're testing (which you want to get close to plain English) and the actual implementation of the testing (which is hidden in the step_definitions).
There are a few ways to tackle this, depending on what you want to test. In the first case, the cuke test is very readable, and it boils down to implementing the step definitions correctly:
Given that I am on page xyz
And I have a list
Then I should see the list in sorted order
In this case, you'll have to define what it means to have a list (can assign it to #list in a step def if you want), and then what it means to see the list in sorted order (here you can pass a regex that ensures you see item 1 before item 2, etc.)
Alternatively, if you like being more verbose in the cuke tests, you can do something like like:
Given that I am on page xyz
Then I should see /item1.*item2.*item3/
which assumes the list is already populated.
Depending on where the list is, you may have to use a within scope param.
Remember that cucumber is great for functional and integration testing, but probably isn't the right tool for unit testing the sort (looking at all edge cases). To test the sorting at a unit test level, I'd highly recommend using QUnit. Since QUnit tests are static pages, try this trick for running the tests as part of capybara:
Given I am on "/test/path/to/qunit/tests"
Then I should see "Whatever Title You have Assigned"
And I should see "0" within "//p[#id='qunit-testresult']/span/[#class='failed']"

Related

How to/ best way to run a test for each element found? Protractor/Jasmine

I am using Protractor and Jasmine.
I'm automating a website that is heavily data-driven with a lot of dynamic elements (elements displayed depend on data available). As a result, I never know exactly how many elements there are, but I need to test each one since data-driven means that just because one works, doesn't mean the rest will.
I'm not sure the best way to go about this - I've done tons of research, but none of the ideas work, or only partially work.
What I've tried:
Throwing an it block into a loop that dynamically grabs the element count
I found that this doesn't work because it appears Jasmine evaluates which / how many tests run at compile. And since I need get to the page before I can grab the count, the count is always 0, so it runs the test 0 times
This only works with static data, but again, my data is dynamic, so this won't work. At least, I couldn't find a way to
Throwing an it block into a loop that loops using a variable and then reassigning the variable in a beforeAll
Same issue as the previous, reassigning doesn't work because Jasmine uses the value that was available on compile
Looping through the elements inside of an 'it' and doing an 'expect' for each element
This works for the most part, but only the first error gets reported. I'd ideally like to see every element that has an issue. Jasmine loops through all elements even when one expect fails, so I'm not sure why it doesn't report them all / or how to report them all
I'd prefer to use solution #3 if I can see all expect failures, but I'm open to any suggestions. If there's a better way, best practice, or a way you handle this instead of what I have listed, I'd like to hear those as well.
Let me know if any more information is needed.

How to check the inheritance relationship of ES6 classes in my project?

I use ES6 extends frequently in projects, sometimes I modify the parent class, but forget to check its child classes (as the increase of developers, things seemed to get worse). Maybe someone couldn't know if the class has been inherited anywhere.
Are there any tools or ways which could help me to check the inheritance relationship of classes?
Unit Tests
Unit test your code! If a superclass changes behavior, it will likely break a unit test, so you know you messed up and you know to correct the offending subclasses.
If it broke functionality in your application, but not one of your unit tests, then your unit test coverage is not good enough, or you've missed some scenarios.
That's the number one thing you should do before any kind of refactoring! Unit test, unit test, and again unit test!
Text Search
If you're using any fancy IDEs you could search through javascript files for something like "extends MyChangedSuperClasss", assuming you colleagues don't use an arbitrary number of spaces between the keyword and the class name.
If you're not using a fancy IDE try to find a file manager that offers text search functions.
(Can't comment yet, so posted as an answer.)
instanceof is as close as you're going to get.
Also, bug-a-lot isn't wrong. Unit testing would go a long way in preventing such regressions.

Should I write unit-tests for 'wrapper' methods

I have function in controller of my directive:
$scope.getPossibleWin = function() {
return betslipService.getPossibleWin()
}
betslipService injected into controller
Now I'm thinking how to test $scope.getPossibleWin:
Test that $scope.getPossibleWin calls betslipService.getPossibleWin
Test that $scope.getPossibleWin return correct value (but this already tested in betslipService!)
Test that $scope.getPossibleWin simply exist
What is best practices in wrappers testing?
Option 2 is the best, option 1 I am not very convinced about. I don't have experience with Javascript so I'm not sure why you should have to verify that a function exists (option 3).
You can find more information on it here but the reason that you should indeed add a test for this method is to prevent yourself from breaking anything in the future. If you only rely on that one method 5 layers deep in your application, it could be that one day you add code in a higher layer which changes the result but it is not being tested. Or at some level some code has a side-effect which disturbs the result that came from the bowels of your codebase.
Therefore I would suggest you to make a test for each (relevant) level. The question what should I test exactly is probably a little bit preference-oriented but I would argue that the very least you should do is testing whether it returns the correct value, as layed out above.
Should you test that it calls that specific inner method? You could, but that isn't exactly something you do in unit-testing because then you would be testing against the unit's internal workings. You don't care how it works inside, you just care that the function gives you the response that you expected. By coupling these two in your unit-test, you'll end up with a broken test for non-broken code when you decide to refactor the internal workings.

Code Organiser Programs

Does anyone know of any tools or easy methods to help re-arrange and organise source code files?
In particular, I am looking for a tool that can take a javascript file, with a number of separate functions, and show me a list of the functions which I can then re-arrange in a more logical order, and have it shuffle the code around to match my new ordering?
Ideally, it would be something that is interactive, rather than a single tool I have to run by command-line, as the order may differ each time.
I'm sure these sorts of tools must exist, but I can never find them whenever I look.
I also suspect that it is something that could be built rather easily by someone with good knowledge of Javascript meta-programming (it might just be a case of 'eval'ing the input, and finding all the functions, then rendering them in a re-orderable list).
I ended up buying WebStorm by JetBrains, which is a commercial product, but it has the ability to see an overview of the file structure, and to reorder statements, including function definitions easily.
This wasn't the reason I bought it, but I am satisfied that it also contains this functionality that I was seeking, so I am going to close this question for now.
There is one, a non-free, editor that I'm aware of so far. I found it when I was duckduckgo'ing months ago.
http://www.yaldex.com/JSFactory_Pro.htm
I've never tried it however.

Selenium IDE: executing a test within a test

I have written a test using selenium IDE (with flow control extensions) that iterates through elements within 2 drop-down lists (using 2 loops) and populates a data entry form according to the elements selected in the drop-down lists.
Esstially the form is different for each iteration, rather than using flow control to handle this in one test (making the test extremely large). Would it possible to cover this functionallty in another test executed from within the loop?
Can this be done in Selenium IDE?
If it can be done, can you point toward any online examples/tutorials?
Thank you for your time
David
The Selenium IDE is a pretty simple tool. You want to encapsulate of your test for reuse in other tests? And you want to use loops and flow control?
It sounds like you're ready to graduate to a real programming language.
Export your tests to java or ruby or whatever language you like. Then you can use the flow control and object orientation of the programming language to solve your problems. That will be easier than trying to figure out how to make it work in the IDE. Plus your tests will be more maintainable.
http://seleniumhq.org/docs/05_selenium_rc.html#from-selenese-to-a-program
http://seleniumhq.org/docs/06_test_design_considerations.html#page-object-design-pattern
Here is an extension that adds full-fledged looping, conditional execution, and callable functions to Selenium IDE: SelBlocks
You setup a script/endScript section in your test, and then call it with parameters. For example:
call|fillform|name="dilbert",phone="555-1212"
call|fillform|name="wally",phone='unlisted"
script|fillform
type|name|${name}
type|phone|${phone}
endScript
(String values are quoted because parameters are regular javascript expressions.)

Categories

Resources