How to debug in nightwatch - debugging

I am trying to debug in nightwatch.
When I put in a console.log it prints before the test runs - while its doing some sort of construction/compiling of the test.
I tried also visual studio code debugger and same thing - the breakpoint hits before the tests are actually run.
thanks for help - mark

"nightwatch" is built on node.js. Node.js executes statements asynchronously. If you write console.log();, it will be executed asynchronously.
The statements you write using browser (or client) object (e.g. browser.url(); , browser.click(); etc.) will be queued up at Selenium Server. They are also executed asynchronously by node.js but are queued up at selenium server.
To perform console.log() in sync with other statements in nightwatch, use .perform(). You will get output in sync with other statements.
Example
var elementValue;
browser
.getValue('.some-element', function(result) {
elementValue = result.value;
})
// other stuff going on ...
//
// self-completing callback
.perform(function() {
console.log('elementValue', elementValue);
// without any defined parameters, perform
// completes immediately (synchronously)
})
.end();
For debugging purpose, you can stop the execution to find out element values or check the browser related data. Use this command: browser.pause(); (or client.pause();). Do not pass any timer here. It will stop the execution.

As you don't really have an example, or some code that you run, i can only give you the general direction you can try in order to fix the issue.
This happens because your code is ran asynchronously (cause that's how javascript works) and everything that is not browser/html related will run before your browser starts your actual test. I suggest you should try using callbacks in order avoid printing your check msgs first (this way you link the console.log to your code that depends on the browser and it will run only after this finishes. If you do that, the debugging with console log will actually work.
Maybe you can learn here how to do that, as an example.

Related

Global afterEach in cypress

I need a solution. I have a lot of spec files with tests and after each of them I use cy.logout command.
I want to avoid write this logout function each time and want it global.
But when I tried add this function in index.js file it was called at the beginning of each test because index.js works perfect only with beforeEach but not with afterEach.
What should I do?
Thanks
[EDIT]
I just find out that problem was provoke by my mistake in code in second test, so the only problem I have with Cypress is that I dont get any information where I made my mistake because Cypress show me that problem is afterEach hook and nothing else.
If you want your logout runs only once for each of your spec file, and not after each of your test within the file, you must use after instead of afterEach.
after(() => {
cy.logout()
})
Using afterEach() or beforeEach() block in support/index.js. Refer below page to run a "global" hook
https://filiphric.com/cypress-basics-before-beforeeach-after-aftereach

Rerun Postman Test without re-submiting the API Request

Is it possible to rerun the tests created in the post response test without
resubmitting the request?
For example you submit an API request in Postman, then it comes back with
some data.
I want to just re-run the scripts against this data.
Could be really useful in debugging these post-response scripts.
I want to rerun Tests in area 1 without hitting the Send button
(area 2). That way I can test the javascript correctness of my Test scripts without having to wait for server responses.
You can use https://postman-echo.com/post and their API will echo the request you send. So basically you create the actual request once and then use the result in the ECHO call to do the development.
More info here:
https://docs.postman-echo.com/?version=latest
I'm afraid there is no better way at the moment.
I was looking for the same thing and came across this tip on the PostMan Community.
https://community.postman.com/t/re-run-test-script-without-re-sending-request/9160/8
Basically you:
Make your request
Save your response as an example
Create a PostMan mock from that response
Rerun and build up your test against the mock (and any variants, like failure cases)
Run against the original and verify everything is good.
While this workaround helps get the job done, I do wish that they could just make it so that you could hold the original state of the response and the env, run your tests, reset to the original state, rerun your tests and tweak until it all works.
You can write test entire test or the part you want to execute twice in a function and call that function 2 times, that the easy and low effort and maintenance way I am looking

How can I execute code before all tests suite with Cypress?

Basically, I want to login once before all my tests in all files are executed.
Should I call my login command in each test file using the before hook or is there any way to do it once before all tests?
Short answer: You can write your login command in a before hook within the supportFile (the file that is loaded automatically before your other spec files). This before hook will run before any of the code in your other test files.
Recommendations: That being said, this approach leaves little flexibility for variation in your individual test files that you may want in the future like:
What if you want to seed the database differently for one test?
What if you want to log in as a different user with different permissions?
What if you need to do something in onBeforeLoad once?
I would recommend just having the login command in a before hook in each individual spec file.
I would also further recommend having your login command in a beforeEach hook to avoid sharing any state in between tests.
describe('Hooks', function() {
before(function() {
// runs once before all tests in the block
})
})
https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Hooks
I would login before EACH test because there could be things that happen in a previous test that affects the results of the current test. With a fresh login, you're starting with a clean state every time. If you want to test a "chain" of unrelated actions (action A THEN action B), then write that as a separate test, but have basic functionality in individual tests.
describe('/page'), () => {
beforeEach(() => {
cy.login() // custom command that handles login w/o UI
cy.visit('/page') // go to the page you are testing
})
// tests
})
You should include a beforeEach block in every test file. This block should login and navigate to the page in question.
I would wrap the function to execute in a before block, as others already suggested.
Now, looking at the docs, I would make that happen in the cypress/support/index.js file.

Wait until option JMeter

I wanted to know if JMeter has a option where you wait until some element disappears.
Example a loading bar only once that has completed or no longer visible then to carry on. (Also being able to monitor the length of time taken)
I have through about writing it as a webdriver test and then running it as a Junit test in JMeter but wanted to know if there is a simpler solution.
Any ideas welcome :)
First of all you need to realize that JMeter is not a browser
JMeter is not a browser, it works at protocol level. As far as web-services and remote services are concerned, JMeter looks like a browser (or rather, multiple browsers); however JMeter does not perform all the actions supported by browsers. In particular, JMeter does not execute the Javascript found in HTML pages. Nor does it render the HTML pages as a browser does (it's possible to view the response as HTML etc., but the timings are not included in any samples, and only one sample in one thread is ever displayed at a time).
So JMeter doesn't execute any client-side JavaScript, the only way of implementing "wait until" option is using While Controller in order to re-execute the same request again and again until response data will contain (or stop containing) the element you're looking for.
If you need to evaluate client-side JavaScript the only option would be going for Selenium. I would recommend using WebDriver Sampler instead of going for JUnit as this way you won't have to recompile your script for any change, it will be inlined into .jmx
You can use Transaction Controller to monitor the time taken by the whole process and to wait for a change , have a look at this:
http://www.sourcepole.ch/2011/1/4/waiting-for-a-page-change-in-jmeter

Run script directly in 2 various browsers

I have created Ruby test script that use Selenium RC to test my web app directly in 2 browsers(IE, Firefox). My script runs - first on IE then continue on Firefox and then should be continued and finished in already opened IE browser. My problem is: I can't continue(reconnect) to run my script in already opened IE browser. I use:
#browser = RSpecSeleniumHelper.connect_browser("URL")
but it opens with new session (it needs to keep previous session).
Is there a particular reason you need to switch between browsers half way through?
I have no idea how you'd fix the problem, but it seems like it would be best solved by running the tests in one browser at a time.
I'm also unsure why you need to switch back and forth in your browsers.
Regardless, I'm doing something similar, but instead I use a different library. I'm using the "Selenium" gem. (gem install selenium) and here's what I would do in your situation.
#ie_driver = Selenium::SeleniumDriver.new(rc_host, port, "*iexplore", url, 1000)
#ie_driver.start
#ie_driver.whatever //Test code
#ff_driver = Selenium::SeleniumDriver.new(rc_host, port, "*firefox", url, 1000)
#ff_driver.start
#ff_driver.whatever //Test code
#ff_driver.stop
#ie_driver.whatever //Continue test code with IE
#ie_driver.stop
In summary, while I'm not really familiar with your selenium library, typically I would create 2 instances of the R/C driver, that way I won't have to interrupt the session.

Resources