module.exports = {
'test' : function (browser) {
browser
.url("someurl");
browser.getTitle((title) => {
browser.verify.ok(title.trim().length !== 0, "Title is not empty");
});
browser.expect.element('h1').text.to.not.equal('');
}
};
If browser.verify.ok fails testcase stops browser.expect.element('h1').text.to.not.equal(''); is not running.
How can I fix this?
In order to continue running tests after an assertion fails, set abortOnAssertionFailure: false in globals.js file. Reference: https://github.com/nightwatchjs/nightwatch/blob/master/lib/settings/defaults.js#L19
Related
When running Cypress headlessly, I can see console.log output from the frontend code under test by using the DEBUG environment variable, like:
DEBUG='cypress:launcher' npx cypress run --browser chrome
However, I haven't found any similar way to see the output of cy.log from the Cypress test code when running headlessly. Even with DEBUG='cypress:*' I don't see them - they only seem to be visible in the interactive interface. It feels like there must be some way to see the cy.log output headlessly - can someone help with that?
The first step is to add a new task in your Cypress config file so that you can run console.log from Node:
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
on("task", {
log(args) {
console.log(...args);
return null;
}
});
},
},
});
Then, you can override cy.log so that it calls this task whenever you run the command in headless mode, and console.log when you're running in headed mode. You can do this by adding the following to your commands file:
Cypress.Commands.overwrite("log", function(log, ...args) {
if (Cypress.browser.isHeadless) {
return cy.task("log", args, { log: false }).then(() => {
return log(...args);
});
} else {
console.log(...args);
return log(...args);
}
});
I'm wanting to test our tests that we have written. For example, if a developer creates a test with the type command, for example:
cy.get('input').type('Hello, World')
I want to test if this type() has the option of log:false. If there is no option of log:false then fail the test.
Code should look like this:
cy.get('input').type('Hello, World', { log: false })
Has anyone using Cypress accomplished this? Running tests to test our tests.
Not quite what you asked, but
/cypress/support/index.js
Cypress.Commands.overwrite('type', (originalFn, text, options) => {
// enforce no-log policy
return originalFn(text, { ...options, log: false })
})
Or if you like to finger-wag
Cypress.Commands.overwrite('type', (originalFn, text, options) => {
if (!options.log || options.log) {
throw " 👎 No logging allowed on .type()"
}
return originalFn(text, options)
})
I am writing the end-to-end tests using Cypress for my web application. In my tests, I am trying to create a task, https://docs.cypress.io/api/commands/task. But it is throwing an error. Here is what I did.
I declared a task in the plugins/index.js file as follow.
module.exports = (on) => {
on("task", {
setTestId(id) {
testId = id;
return null;
},
getTestId() {
return testId;
}
});
};
Then I use the task in the test as follow.
cy.task('setTestId', 7654321);
When I run the tests, I am getting the following error.
The 'task' event has not been registered in the plugins file. You must register it before using cy.task()
As you can see, I tried this solution as well, Cypress task fails and complains that task event has not been registered in the plugins file. It did not work either. What is wrong with my code and how can I fix it?
You're missing some formatting (":" and "=>" and such). Try this:
module.exports = (on) => {
on("task", {
setTestId: (id) => {
testId = id;
return null;
},
getTestId: () => {
return testId;
}
});
};
Set the testId as you did:
cy.task('setTestId', 7654321);
And when you want to retrieve the testId, use:
cy.task('getTestId').then((testId) => {
cy.log(testId)
});
I'm trying to redirect after executing some code with flash message then apache hang and sent error
Connot modify header information - headers already sent
public function convert_to_invoice($id)
{
if (!has_permission('invoices', '', 'create')) {
access_denied('invoices');
}
if (!$id) {
die('No estimate found');
}
$draft_invoice = false;
if ($this->input->get('save_as_draft')) {
$draft_invoice = true;
}
$invoiceid = $this->estimates_model->convert_to_invoice($id, false, $draft_invoice);
if ($invoiceid) {
$this->session->set_flashdata('message-success','estimate_convert_to_invoice_successfully');
redirect('invoices/list_invoices/' . $invoiceid);
} else {
if ($this->session->has_userdata('estimate_pipeline') && $this->session->userdata('estimate_pipeline') == 'true') {
$this->session->set_flashdata('estimateid', $id);
}
if ($this->set_estimate_pipeline_autoload($id)) {
redirect($_SERVER['HTTP_REFERER']);
} else {
redirect(admin_url('estimates/list_estimates/' . $id));
}
}
}
As redirect uses headers to perform its task nothing can be outputted before it is called. I don't see anything that would cause output except:
if (!has_permission('invoices', '', 'create')) {
access_denied('invoices'); // exits?
}
if (!$id) {
die('No estimate found');
}
However both seem to exit so the redirect stage shouldn't be reached.
I would suggest removing the redirects temporarily, running the script, and seeing what is outputting. You can then remove/silence those offending pieces of code and re-institute the redirects. It might be as simple as an error outputting.
When I try to run JSCover with PhantomJS, I see below ERROR:
Steps followed:
1) Run the JSCover Server:
java -jar ~/JSCover/target/dist/JSCover-all.jar -ws --report-dir=report
2) Run the PhantomJS runner with JSCover:
*phantomjs --debug=true ~/JSCover/src/test/javascript/lib/PhantomJS/run-jscover-jasmine.js
localhost8080/<app>/module/framework/test/SpecRunner.html
TypeError: 'null' is not an object(evaluating''document.body.querySelector('.description').innerText')`
phantomjs://webpage.evaluate():3
phantomjs://webpage.evaluate():22
phantomjs://webpage.evaluate():22
2013-09-19T16:36:07 [DEBUG] WebPage - evaluateJavaScript result QVariant(, )
2013-09-19T16:36:07 [DEBUG] WebPage - evaluateJavaScript "(function() { return (function () {
jscoverage_report('phantom');
})(); })()"
2013-09-19T16:36:07 [DEBUG] WebPage - evaluateJavaScript result QVariant(, )
2013-09-19T16:36:07 [DEBUG] Network - Resource request error: 5 ( "Operation canceled" ) URL: localhost8080/<app_home>/lib/backbone/1.0.0/backbone.js?cb=0.5381254460662603
This was an issue that I ran into yesterday. It turns out that the example script does not work for newer versions, so I built a new Phantom Script that works for Jasmine 2.X which fixes it. You can locate the working script here in my repository:
https://github.com/tkaplan/PhantomJS-Jasmine
I faced with the same issue when I try running Jasmine with PhantomJS.
I realized that the latest version of Jasmine-html.js (jasmine-2.0.0-rc2)
does not go along with PhantomJS's run-jasmine.js (phantomjs-1.9.2-windows).
In the jasmine-2.0.0-rc2 version of Jasmine-html.js,
The '.description' class is not available if all tests passed.
This 'description' class is created only if any test failed.
Thus, when I run the phantomjs with all tests passed, I get the above error message.
I modified run-jasmine.js to adapt to Jasmine-html.js (jasmine-2.0.0-rc2) to
resolve this issue.
Are you loading your tests asynchronously? I use requirejs for modular javascript. It is also used to load the test specs:
<script data-main='SpecRunner' src='/test/scripts/libs/require.js'></script>
When using JSCover, the run-jscover-jasmine.js script does not account for this async behaviour, so the DOM nodes referenced in the query do not exist (yet). I modified the script to delay the waitFor call by 1 second:
page.open(system.args[1], function(status){
if (status !== "success") {
console.log("Unable to access network");
phantom.exit();
} else {
// Added 1s delay here
window.setTimeout(function() {
waitFor(function(){
return page.evaluate(function(){
return document.body.querySelector('.symbolSummary .pending') === null
});
}, function(){
var exitCode = page.evaluate(function(){
console.log('');
console.log(document.body.querySelector('.description').innerText);
var list = document.body.querySelectorAll('.results > #details > .specDetail.failed');
if (list && list.length > 0) {
console.log('');
console.log(list.length + ' test(s) FAILED:');
for (i = 0; i < list.length; ++i) {
var el = list[i],
desc = el.querySelector('.description'),
msg = el.querySelector('.resultMessage.fail');
console.log('');
console.log(desc.innerText);
console.log(msg.innerText);
console.log('');
}
return 1;
} else {
console.log(document.body.querySelector('.alert > .passingAlert.bar').innerText);
return 0;
}
});
page.evaluate(function(){
jscoverage_report('phantom');
});
phantom.exit(exitCode);
});
}, 1000);
}
});
Depending on the amount of code loaded, you may have to increase the delay.