Using Protractor how to run multiple spec files without closing browser for each spec file - node.js

I'm using page object design pattern in my automation tests. I have spec file per page object. When the tests run, browser restarts in between spec files where it asks to login again. But what I'm trying to achieve is that I want to login at the start of the tests, run tests from multiple specs without closing the browser in between executing the spec files.

In protractor.conf.js, under capabilities, setting shardTestFiles: false will open one browser, run all spec files sequentially in that browser, then exit the browser when all tests have finished.

You can add "Before all" in your first spec file which is needed to load the browser. Once that Spec is run then again next spec is starting to load in same browser window and you won't require login attempt.
beforeAll(() => {
browser.driver.get("https://stackoverflow.com"),
browser.driver.manage().window().maximize();
});

Related

How to run setup code before extension loaded in puppeteer?

I am testing the behavior of a chrome extension using puppeteer.
Upon installation, the extension opens a page. I would like to do initial setup in the browser before the extension is loaded (for example, setting local storage or injecting a jest mock).
The problem I have now is that the extension gets loaded upon browser start, so I don't know of any way to execute setup code before the extension is loaded.
This is how I load the extension currently (which resides in the dist/chrome folder):
const browser = await puppeteer.launch({
args: [
'--disable-extensions-except=dist/chrome',
'--load-extension=dist/chrome',
],
headless: false,
ignoreDefaultArgs: ['--disable-extensions'],
});
How can I do initial setup before the extension is loaded?
Some options I can think of are: 1) load the extension after the browser is launched 2) intercept the extension installation and execute setup code before the extension initializes
I came searching for an answer and I found it reading your question, so I'm leaving it here in case I have to search for it again in the future ...
The trick is to create a "launcher" extension with the sole purpose running the setup code, before the actual extension is ran. It would look like:
'--disable-extensions-except=dist/launcher,dist/chrome',
'--load-extension=dist/launcher,dist/chrome',

How to prevent Mocha from preserving require cache between test files?

I am running my integration test cases in separate files for each API.
Before it begins I start the server along with all services, like databases. When it ends, I close all connections. I use Before and After hooks for that purpose. It is important to know that my application depends on an enterprise framework where most "core work" is written and I install it as a dependency of my application.
I run the tests with Mocha.
When the first file runs, I see no problems. When the second file runs I get a lot of errors related to database connections. I tried to fix it in many different ways, most of them failed because of the limitations the Framework imposed me.
Debugging I found out that Mocha actually loads all files first, that means that all code written before the hooks and the describe calls is executed. So when the second file is loaded, the require.cache is already full of modules. Only after that the suite executes the tests sequentially.
That has a huge impact in this Framework because many objects are actually Singletons, so if in a after hook it closes a connection with a database, it closes the connection inside the Singleton. The way the Framework was built makes it very hard to give a workaround to this problem, like reconnecting to all services in the before hook.
I wrote a very ugly code that helps me before I can refactor the Framework. This goes in each test file I want to invalidate the cache.
function clearRequireCache() {
Object.keys(require.cache).forEach(function (key) {
delete require.cache[key];
});
}
before(() => {
clearRequireCache();
})
It is working, but seems to be very bad practice. And I don`t want this in the code.
As a second idea I was thinking about running Mocha multiple times, one for each "module" (as of my Framework) or file.
"scripts": {
"test-integration" : "./node_modules/mocha/bin/mocha ./api/modules/module1/test/integration/*.integration.js && ./node_modules/mocha/bin/mocha ./api/modules/module2/test/integration/file1.integration.js && ./node_modules/mocha/bin/mocha ./api/modules/module2/test/integration/file2.integration.js"
}
I was wondering if Mocha provides a solution for this problem so I can get rid of that code and delay the code refacting a bit.

How to automate file upload on browserstack using nightwatch node js

I am writing automation using Nightwatch node js. I have a test for uploading a file in my application and testing it locally works perfectly. However, when I test it using BrowserStack, BrowserStack cannot access the file in my local machine.
I have also tried setting FileDetector but it gives error saying setFileDetector is not a function on browser object.
I know this function is available for selenium driver object but I am javascript browser object for writing test scripts.
browser.setFileDetector(new remote.FileDetector());
I see that you want to perform the File Upload Operation for a file available on your local machine. You can review the link: https://www.browserstack.com/automate/node#enhancements-uploads-downloads for more details.

How to inject script using node.js code?

You all know open npm package: https://www.npmjs.com/package/open
Using this package, one can write the following code:
var open = require('./node_modules/open/lib/open.js')
open('http://www.cnn.com')
and activating it by:
$ node app.js
will open a browser window of cnn.com.
I want my script to open this site and inject some code to the console. I mean that the browser will behave like I clicked F12, went to 'console' tab and typed in console the code:
alert('Hello World')
Do you know how to do it?
The open module is used to "Open a file or url in the user's preferred application."
It can open the preferred application (a browser in this case) but it cannot control it. In fact, it doesn't even know what browser will that be (or even if that will be a browser).
What you are asking for can be achieved with tools like PhantomJS ("PhantomJS is a headless WebKit scriptable with a JavaScript API."), Nightmare.js ("A high-level browser automation library.") or CasperJS ("Navigation scripting & testing for PhantomJS and SlimerJS"), see:
http://phantomjs.org/
http://www.nightmarejs.org/
http://casperjs.org/

How to configure Cucumber and Capybara to use the Rack::Test driver?

I am a newbie to Capybara.
Here is my configuration within file env.rb
Capybara.configure do |config|
config.run_server = false
#config.default_driver = :selenium
config.default_driver = :rack_test
config.app_host = 'point to my localhost port 3000'
end
Everything runs just fine if I set default_driver to :selenium. But I need to set the driver to :rack_test, so that when running cucumber command, it will not open the web browser.
Many thanks,
P/S If you are an expert, please show me the learning path, I'm not expecting someone showing them selves.
I presume you want to test against a test server controlled by capybara (which is the normal way to do it), rather than testing against your dev instance (the one at localhost:3000) or a staging server or something.
First, configure capybara to run your Rails app. The usual way to do this is to add the cucumber-rails gem to your Gemfile and require 'cucumber/rails' in your env.rb. You can also set up capybara to run Rails (or any Rack app) manually.
Having done that, capybara will do what you want (use the Rack::Test driver) by default. Remove the configuration that you showed from your env.rb and Cucumber/capybara will work the way you want.
If you also want some scenarios to use Javascript, tag those scenarios with #javascript and add
Capybara.javascript_driver = :selenium
to your env.rb. Capybara will continue to use its Rack::Test driver for scenarios without the tag, and will use its Selenium driver for scenarios with the tag.
Thank you Dave for helping me during the time. Briefly, in order for running "cucumber" without triggering to open a web-browser (which is rack-test), here is the configuration:
1> File env.rb.
require 'cucumber/rails'
Only 1 line above is enough.
2> File .feature
Feature: Post a new Product
Feature: Post a new Product
Scenario: Open new product page
Given I open new product site
When I input new product
Then I should see the product created confirmed
By the way, we don't need "Capybara.javascript_driver = :selenium" within file env.rb.
There's still so many tricky things I need to learn about capybara and cucumber

Resources