Is there a way for js code to force Selenium testing to fail? For (a probably bad) example, if I had the following:
return document.getElementById('foo').innerHTML == 'hello'
is there a way I could make the 'runScript' command fail depending on if the js code returned true or false? (I know that example could be used by other Selenium commands, but I wanted a more general solution.)
Do I need to learn how to extend Selenium to add another command?
I'm also relatively new to Selenium so is this something that using Selenium-rc will solve?
assertExpression will give you what you're asking for.
For instance, the following line would cause your test to fail if the JavaScript expression you mention did not evaluate to true.
<tr>
<td>assertExpression</td>
<td>javascript{this.browserbot.getCurrentWindow().document.getElementById('foo').innerHTML == 'hello'}</td>
<td>true</td>
</tr>
It depends how you want to use selenium. In selenese suite there are calls for verifyText, etc (just google for selenese suite and verifyText, waitForCondition). If you are using the selenium remote control in e.g. java than you do the assertions in the java code. With the remote control you send scripts to the page and you get back what the evaluation of the code returns. The comparsion/assertion is done in the java client.
eval('asdfklj;'); //Selenium Hits it. Honestly I've never used it myself. Need to learn it soon, but I'm assuming it can bomb do to javascript errors.
Related
I am an IT instructor and have begun utilizing Nightwatch to test students' homework to see if it is meeting specs.
I can test all the "normal" elements/attributes with no problem. My issue is on some stuff that you would probably not normally test in a production environment.
I want to test that they are using the correct HTML5 doctype, which lies outside of the root, of course, and I believe Nightwatch begins with the HTML node.
I also harp on them about using comments to make their own life and that of their fellow developer easier. So, I would like to test that they are leaving comments. Some parts of the comment are required and consistent, but other parts vary, such as their name within the comment. Here is a sample of a comment...
The Name of the Page
Sample Page for the Widgets
Author: your name
Date: the date
Again, I would probably not be testing for comments and doctype in the real world, but wonder if it is possible with Nightwatch?
I have tried the containsText() and text.to.contain() methods already with no success.
Any thoughts and guidance would be appreciated.
Also, I am not opposed to using another testing tool or any other middleware that might help if you know of any. I have not found any in my searching, nor have I found a solution to my quandary.
I would say that Nightwatch has not been designed to accomplish such a task. It is an amazing framework to perform acceptance testing on a GUI (Graphical User Interface), but it is not a web crawler. Browser automation is a resource-intensive operation, especially when you automate a real web browser via Selenium WebDriver. So if the code structure matters more than the graphical part, I would recommend you to use more "minimalistic" libraries like CasperJS. It is based on PhantomJS, a WebKit headless browser.
Nightwatch
If you want to keep Nightwatch, the best thing you can do is using regular expressions with the .source() method which returns a serialization (string) of the DOM:
browser
.url("http://www.website.com")
.source(function (res) {
if (/<!--/.test(res.value)) {
console.log("Comment detected!");
}
})
This works to detect at least one comment, but you will not be able to check the DOCTYPE because it is not returned by the .source() method. Using XPath, the root element would be /html.
Moreover, here we use a basic console.log for comments, which is not ideal to perform a test. But the problem is that most Nightwatch assertion methods expect a CSS selector...
Casper
This is much easier here...
With Casper, you can use the .getHTML() method to get a serialization of the DOM. But here, the DOCTYPE is returned and you can use assertions (.assertMatch()) to validate the result against regular expressions. Your code could have the following structure:
casper.test.begin('Test website', function (test) {
casper.start('http://www.website.com', function () {
var html = this.getHTML();
test.assertMatch(html, /<!DOCTYPE html>/);
test.assertMatch(html, /<!--.*-->/);
});
casper.then(function () {
// Your code...
});
casper.run(function() {
test.done();
});
});
I have tested this code on a random page that contains a valid HTML5 document type declaration and some comments. It works fine.
I'm writing a test using a LiveServerTestCase, django-casper, and casperjs for a view that includes javascript. Half way through a client side script I have a jQuery.post(url, callback_function(r){}) line.
When callback_function is called during a test r is null. However, when I run the application normally and step through the same javascript when callback_function is called r has the expected value.
This makes me think that there is a detail about LiveServerTestCase I'm missing to get jQuery.post to work with it. Can anybody please shed light on what I should do next to debug this problem?
I'm guessing it's because the static files aren't around. In Django 1.7, LiveServerTestCase no longer supported serving up the static files. It was moved into testing.StaticLiveServerTestCase
Try changing your test classes to subclass StaticLiveServerTestCase.
This could be a stupid question. Jasmine, Qunit, Mocha, Unit.js, etc - as far I as I know are Javascript test frameworks. But what is a Javascript test framework? What it is for actually? Is it any different from these Javascript frameworks below?
ember.js
backbone
require.js
Jasmine,
describe('Hello world', function() {
it('says hello', function() {
expect(helloWorld()).toEqual("Hello world!");
});
});
It seems like that is what node.js does, isn't?
What do you need to test?
(Short overview)
A test framework is a set of tools that allows you to test the functionality of your code (in this case your Javascript code).
It allows you to test certain functions and check if the output/result matches your expectations.
They allow you to test certain conditions and how your code react on that, like missing or unset variables, unexpected data in your variables and so on.
And one of the advantages is the test automation. This allows you to run a bunch of test automatically and it will give you result if every single test. This way you can see which test fails after you made some changes in your code.
Also you should consider reading the link mplungjan provided.
(If I missed something mandatory to say, then leave a comment, I will add that)
I'd like to write a test case (using Selenium, but not the point of this question) to validate that my web application has no script errors\warnings or unhanded exceptions at certain points in time (like after initializing a major library).
This information can easily be seen in the debug consoles of most browsers. Is it possible to execute a javascript statement to get this information programatically?
It's okay if it's different for each browser, I can deal with that.
not so far read about your issue (as far as I understood your problem) here
The idea be the following:
I found, however, that I was often getting JavaScript errors when the page first loaded (because I was working on the JS and was introducing errors), so I was looking for a quick way to add an assert to my test to check whether any JS errors occurred. After some Googling I came to the conclusion that there is nothing built into Selenium to support this, but there are a number of hacks that can be used to accomplish it. I'm going to describe one of them here. Let me state again, for the record, that this is pretty hacky. I'd love to hear from others who may have better solutions.
I simply add a script to my page that will catch any JS errors by intercepting the window.onerror event:
<script type="text/javascript">
window.onerror=function(msg){
$("body").attr("JSError",msg);
}
</script>
This will cause an attribute called JSError with a value corresponding to the JavaScript error message to be added to the body tag of my document if a JavaScript error occurs. Note that I'm using jQuery to do this, so this specific example won't work if jQuery fails to load. Then, in my Selenium test, I just use the command assertElementNotPresent with a target of //body[#JSError]. Now, if any JavaScript errors occur on the page my test will fail and I'll know I have to address them first. If, for some strange reason, I want to check for a particular JavaScript error, I could use the assertElementPresent command with a target of //body[#JSError='the error message'].
Hope this fresh idea helps you :)
try {
//code
} catch(exception) {
//send ajax request: exception.message, exception.stack, etc.
}
More info - MDN Documentation
I am a Selenium newbie ...
As a JavaScript programmer, I think I want to handle JavaScript events in my Selenium-2 tests (JUnit). I am joining a team where all of the existing tests have "waitForSomethingToBeRendered" methods. Is there some way my Selenium tests can handle/listen for DOM (or custom) events?
Also, I've read on SO where developers use FireBug to write/debug Selenium-2 tests. How does this work? I don't see FireBug in the browser launched by Selenium. Similarly, why does the following appear to have no effect? Am I trying something Selenium/JUnit does not support?
selenium().getEval("alert('hello');");
selenium().getEval("debugger;");
You don't see the firebug extension because selenium opens a stripped firefox. I'm guessing the reason is it's much faster. It's possible (and quite easy in selenium-2) to add the plugin.
The alert doesn't work because selenium-1 (I don't know how selenium-2 handles them) swallows the alerts. There's an api for handling alerts.
Plus, in selenium-1, the JS runs in a different window. So the equivalent of document.getElementById() is sel.getEval("selenium.browserbot.getCurrentWindow().document.getElementById()").
Finally, I don't know about events but you can wait for conditions: sel.wait_for_condition().
Firstly if you are getting started with Selenium I suggest using the 2.0 API which is for WebDriver. To evaluate JavaScript in 2.0 simply cast your WebDriver object to a JavascriptExecutor object and use the methods provided by it. 'waitForSomethingToBeRendered' needs to be done in a few steps. First of all you must ensure the DOM object is available on the page. For this you can do something like this:
WebElement e = null;
try {
e = driver.findElement( By.id("asdf") );
} catch {
...
}
Or:
driver.findElements( By.id("asdf") ).size() != 0
After determining whether the DOM object is available you can do:
e.isDisplayed()
Which will return to you whether the element is currently displayed.
In regards to what you have seen about FireBug and Selenium I am guessing you are confusing Selenium IDE which is a Firefox plugin with Selenium RC/WebDriver which is not a plugin.
Use Firebug for Firefox while normally browsing your site to explore your DOM to determine the correct element and class ids to select in your Selenium script. If you are using extjs you are going to have some extra fun in determining which elements to select, as extjs randomizes element ids. The best way is to add an extra css class to find the correct element, then select by that class.