Specs not found in using scripts tag in package.json for Protractor - protractor

In pretest command "tsc" i have used so that file should convert to js. This command changes ts code to js. Now in config js file specs extension remains TS example- file "testspec.ts" not js. extension is not changing. Due to this i am facing specs not found. Extension should also change in configuration.js of specs file.How can i resolve this, so that i just have to give command npm run test and my testcase executes.
Code of package.json
{
"name": "protractortypescriptcucumberframework",
"version": "1.0.0",
"description": "cucumberframework",
"main": "index.js",
"scripts": {
"test": "protractor JSFiles/configuration.js",
"pretest": "tsc",
"protractor":"./node_modules/protractor/built/cli.js",
"webdriver-update":"./node_modules/.bin/webdriver-manager update"
},
"author": "ak",
"license": "ISC",
"dependencies": {
"protractor": "^7.0.0",
"typescript": "~3.9.6",
"jasmine": "~3.5.0",
"#types/jasmine": "~3.5.11",
"#types/jasminewd2": "~2.0.8",
"ts-node": "~8.10.2",
"#types/node": "~14.0.23"
}
}
Error i face is below after command > npm run test
> protractortypescriptcucumberframework#1.0.0 pretest C:\Users\Admin\Eclipse Protractor JavaScript\ProtractorTypeScriptCucumberFramework
> tsc
> protractortypescriptcucumberframework#1.0.0 test C:\Users\Admin\Eclipse Protractor JavaScript\ProtractorTypeScriptCucumberFramework
> protractor JSFiles/configuration.js
[13:49:25] I/launcher - Running 1 instances of WebDriver
[13:49:25] I/direct - Using ChromeDriver directly...
DevTools listening on ws://127.0.0.1:49802/devtools/browser/9a9f6b17-da99-4aec-993c-a265017ee716
> protractortypescriptcucumberframework#1.0.0 test C:\Users\Admin\Eclipse Protractor JavaScript\ProtractorTypeScriptCucumberFramework
> protractor JSFiles/configuration.js
[13:49:25] I/launcher - Running 1 instances of WebDriver
[13:49:25] I/direct - Using ChromeDriver directly...
DevTools listening on ws://127.0.0.1:49802/devtools/browser/9a9f6b17-da99-4aec-993c-a265017ee716
Started
No specs found
Finished in 0.218 seconds
[13:49:49] I/launcher - 0 instance(s) of WebDriver still running
[13:49:49] I/launcher - chrome #01 passed
Config file code.
import {Config} from "protractor";
// An example configuration file
export let config: Config = {
// The address of a running selenium server.
//seleniumAddress: 'http://localhost:4444/wd/hub',
directConnect:true,
// Capabilities to be passed to the webdriver instance.
capabilities: {
browserName: 'chrome'
},
// Spec patterns are relative to the configuration file location passed
// to protractor (in this example conf.js).
// They may include glob patterns.
specs: ['testspec.ts'],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true, // Use colors in the command line report.
}
};

I figured out the error.
In config file of ts extension need to pass spec with js extension only. scripts tag will not automatically change spec extension. It has to be passed by user in config ts file.

Related

How to configure VS code for pytest with environment variable

I am trying to debug pytest. I would like to inject an environment variable. Here is how I have set launch.json
{
"type": "python",
"request": "test",
"name": "pytest",
"console": "integratedTerminal",
"env": {
"ENV_VAR":"RandomStuff"
}
},
But it seems when I start debugging. I do not see the env variable injected, as a result my test which expects that env variable fails.
Also I notice error
Could not load unit test config from launch.json
pytest-env
Install
Per https://stackoverflow.com/a/39162893/13697228:
conda install -c conda-forge pytest-env
Or:
pip install pytest-env
pytest.ini
Create pytest.ini file in project directory:
[pytest]
env =
ENV_VAR=RandomStuff
Python
In your code, load environment variables as you normally would:
import os
env_var = os.environ["ENV_VAR"]
pytest / VS Code
Either run:
pytest
(Notice how it says configfile: pytest.ini)
C:\Users\sterg\Documents\GitHub\sparks-baird\mp-time-split> pytest
==================================== test session starts ===================================== platform win32 -- Python 3.9.12, pytest-7.1.1, pluggy-1.0.0 rootdir:
C:\Users\sterg\Documents\GitHub\sparks-baird\mp-time-split,
configfile: pytest.ini plugins: anyio-3.6.1, cov-3.0.0, env-0.6.2
collecting ...
Or:
This only seems to work with breakpoints that have manually been set, I'm guessing some other change is needed to pause on errors.
Python for VS Code Extension
Apparently the Python for VS Code extension recognizes a .env file automatically. E.g.
.env file:
ENV_VAR=RandomStuff
Haven't verified, but I'd assume this has the same behavior as using pytest-env with a pytest.ini file.
When all else fails
When I don't feel like dealing with the strange hackery necessary to get VS Code, Anaconda environments, and pytest playing nicely together (and/or forget how I fixed it before), I call my tests manually and run it like a normal script (see below). This may not work with more advanced pytest trickery using fixtures for example. At the end of your Python script, you can add something like:
if __name__ == "__main__":
my_first_test()
my_second_test()
and run it in debug mode (F5) as normal.
Could not really figure out how to fix "unit" test debugging with Vscode. But with Pytest one can call tests like python -m pytest <test file>(https://docs.pytest.org/en/stable/usage.html#cmdline)
That means Vscode can be configured like a module
"configurations": [
{
"name": "Python: Module",
"type": "python",
"request": "launch",
"module": "pytest",
"args": ["--headful","--capture=no", "--html=report.html"],
}
This is good enough to do debugging of python tests. Also you can then insert environment variables
"env": {
"ENV_VAR":"RandomStuff"
}
This works in v 1.72.2
Create in the workspace folder:
.vscode/launch.json
Similar to the OP, but using "module"
{
"version": "0.2.0",
"configurations": [
{
"name": "Pytest",
"type": "python",
"request": "launch",
"module": "pytest",
"console": "integratedTerminal",
"env": {
"testy": "someval"
}
}
]
}
Try some breakpoints (either in test files or app code)
Start the debugger
Watch the os.environ['testy'] value (must import os where the breakpoint is of course)

how to configure debugging in grunt-protractor-runner v5, protractor v4.0.14, and node v8.13

i have recently transitioned on to a project to re-architect our entire enterprise protractor code base. we use node v8.13, protractor v4.0.14, and grunt-protractor-runner v5. i would be open to debugging in vscode or chrome.
here is how i am configuring...
vscode launch.json
{
"type": "node",
"request": "launch",
"name": "Pro Debug",
"program": "${workspaceRoot}/node_modules/protractor/bin/protractor",
"args": ["${workspaceRoot}/ConvertedJSFiles/tests/config/conf.js"],
}
grunt.js
default: {
options: {
keepAlive: true,
debug: true
}
},
exampleTest.ts
it( '2#Click in Sign in to your account', async function () {
browser.pause();
debugger;
await commonMethods.beforeAll();
landingPage.logIn();
expect( browser.getCurrentUrl() ).toContain( 'login' );
} );
when attempting to debug in chrome, i execute with the command 'grunt'. the test seems to fail at the point where i have entered the 'debugger' statement. i am not positive, but i think this error is caused by protractor as protractor is what spawns a node process.
>> (node:13476) [DEP0022] DeprecationWarning: os.tmpDir() is deprecated. Use os.tmpdir() instead.
>> Debugger listening on ws://127.0.0.1:9229/ff1f9827-a76b-4400-9d0e-b0cec6912508
>> For help see https://nodejs.org/en/docs/inspector
>> (node:13477) [DEP0068] DeprecationWarning: `node debug` is deprecated. Please use `node inspect` instead.
>> Port 9229 is already in use. Please specify another port to debug.
when attempting to debug with vscode i receive the following error. i think this might be due to vscode executing the conf.js without having started protractor.
[16:57:06] E/runner - Unable to start a WebDriver session.
logger.js:170
[16:57:06] E/launcher - Error: UnsupportedOperationError: unknown command: Cannot call non W3C standard command while in W3C mode
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'US8525RT.local', ip: 'fe80:0:0:0:cdc:1e02:9f9f:9858%en0', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.15.4', java.version: '1.8.0_241'
Driver info: driver.version: unknown
at Object.checkLegacyResponse (/Users/jayce.tan/Code/ui-automation-ts/node_modules/protractor/node_modules/selenium-webdriver/lib/error.js:639:15)
any help would be much appreciated. thanks!
Try like this
Note: if you are using outfolder for builded ts file to js, correct the path.
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"stopOnEntry": false,
"program": "${workspaceRoot}/node_modules/protractor/bin/protractor", // path the protractor node modules.
"args": [
"${workspaceRoot}/build/config/config.js" // path to compiled protractor configuration file.
],
"preLaunchTask": null,
"sourceMaps": true,
"outFiles": [ "${workspaceRoot}/build/**/*.js" ]
}
]
}
After that set breakpoints in the visual studio code and debug the program.
If you still have issues with W3C just disable it
Proractor
config.js
Note: adapt it to your needs this is just example how to disable w3c
capabilities: {
browserName: 'chrome',
'goog:chromeOptions': {
w3c: false,
args: [] // args if required.
}
}

Angular Testing - Using Jest with Protractor

I am new to Angular testing, and I want to perform 2 kinds of test for my application:
Unit Test - I choose to use Jest since I can run my test without opening the browser, and it also supports testing for specific cases with --testNamePatern.
End to end test - I want to try out Protractor since it is available in Angular and also has a big Angular community to work with.
My question is, can I use both Jest and Protractor in my application? If yes, do I need to configure anything to use both of them in my application.
You can use both jest and protractor in your application. By default the new angular cli release gives you a karma runner for unit tests and a protractor runner for end to end tests inside the same application. You are just changing Karma with Jest.
Can I run protractor tests (end to end) with jest?
No you cannot.
Can I run unit tests using protractor?
No you cannot.
Can I run protractor for end to end tests and jest for unit tests in the same application?
Yes you can. You will just need to tell jest which files to pick up and the same with protractor.
Can I get both the reports in a single file or a single run?
No you cannot. You will have to configure your jest runner to print reports which will be different from the protractor reports.
You can use both jest and protractor without configuring anything special. Here is a snippet of the package.json I am using for running e2e tests with protractor and lighthouse tests with jest.
{
"name": "performance-tests",
"version": "1.0.0",
"description": "Performance tests and end to end tests.",
"main": "jest.js",
"scripts": {
"debug": "node --inspect-brk ./node_modules/.bin/protractor protractor.conf.js",
"pretest": "npm run tsc && npm run webdriver-update",
"e2e": "npm run tsc && ./node_modules/protractor/bin/protractor protractor/compiled-js-files/protractor.conf.js",
"grid": "sh run-grid.sh && npm run e2e",
"tsc": "./node_modules/typescript/bin/tsc",
"webdriver-update": "./node_modules/protractor/bin/webdriver-manager update --standalone --versions.standalone=3.8.0 --chrome --versions.chrome=78.0.3904.97",
"lighthouse": "./node_modules/jest/bin/jest.js --verbose -t=lighthouse",
"lighthouse-reports": "./node_modules/jest/bin/jest.js --verbose -t=lighthouse && node ./lighthouse/db.js"
},
"repository": {
"type": "",
"url": ""
},
"author": "Sankalan Parajuli",
"license": "ISC",
"bugs": {
"url": ""
},
"homepage": "",
"dependencies": {
"#types/jasmine": "^3.3.12",
"#types/jasminewd2": "^2.0.6",
"#types/node": "^12.12.14",
"jasmine": "^3.3.1",
"lighthouse": "^4.0.0-beta",
"protractor": "5.4.2",
"protractor-beautiful-reporter": "^1.3.3"
},
"devDependencies": {
"#types/request": "^2.48.3",
"#types/selenium-webdriver": "^4.0.0",
"csvtojson": "^2.0.8",
"jest": "^23.4.1",
"moment": "^2.24.0",
"mongodb": "^3.1.13",
"puppeteer": "^1.6.0",
"request-promise": "^4.2.5",
"ts-node": "^8.5.2",
"typescript": "2.8.1"
}
}
Hope it helps.

failed reason or exception is not getting displayed when working with Protractor Cucumber

i am trying to work with protractor-cucumber framework and when i executing the script.. even though the script is fine still script is failing and it is not giving the failure reason or exception
Same thing is working fine with Jasmine but when it comes to protractor-cucumber it is working like this
My Config File
exports.config = {
//seleniumAddress: 'http://localhost:4444/wd/hub',
getPageTimeout: 60000,
allScriptsTimeout: 500000,
directConnect:true,
framework: 'custom',
// path relative to the current config file
frameworkPath: require.resolve('C:\\...\\node_modules\\protractor-cucumber-framework'),
capabilities: {
'browserName': 'chrome'
},
ignoreUncaughtExceptions:true,
// Spec patterns are relative to this directory.
specs: [
'./learnFramework/utility/test.feature'
],
cucumberOpts: {
require: './learnFramework/TestCases/spec.js',
tags: false,
profile: false,
'no-source': true
},
onPrepare: function () {
browser.ignoreSynchronization=true;
}
};
My Spec file
module.exports=function(){
this.Given(/^Open the browser$/,async function(){
browser.ignoreSynchronization=true;
});
this.Given(/^Load the URL$/,async function(){
browser.get("https://google.com");
console.log(await browser.getTitle());
})
}
When i executing the script it is opening the chrome browser and loading the url also but don't know why it is getting failed and it is not even giving the result of the test
Execution Result
I want to use this template for my feature file
Scenario: Title of your scenario
✓ Given Open the browser
✖ And Load the URL
Failures:
[09:15:48] I/launcher - 0 instance(s) of WebDriver still running
[09:15:48] I/launcher - chrome #01 failed 1 test(s)
[09:15:48] I/launcher - overall: 1 failed spec(s)
[09:15:48] E/launcher - Process exited with error code 1
And I'm using the grunt file for execution..
so someone please help me to come out from this issue
await browser.get("https://google.com");
You need to await for the broswer to go to google.

BrowserStack + Protractor getting the name of the tests to show up as session ID

I know that you could use browserstack before protractor 3.0 but they have added official support in 3.0 . I haven't been able to try that out yet so maybe that's the answer to my question? Currently don't have the ability to upgrade so if that's my answer then I will go through the proper channels to make that happen.
The problem I'm having with browser stack is that it names all the sessions random hashes. So I may have a suite of tests and I have no way to tell which session is which test. You can manually change the capabilities.name but then everything will have that name.
I know when I use saucelabs it will name everything after the name of the test file. so for example it will be sometest.js . I don't know if this is something protractor is doing or some node-module we're using that is doing the work behind the scenes and I don't know?
I just want to know how I can run my suite that runs all my smoketests and names each browserstack session after the actual name of the tests in the suite.
here is an example of what things look like on browserstack. I did manually set the capabilities.name to mytest for a few sessions.
here is how they look with sauce labs
Also here is my package.json file in case any of these modules might be making saucelabs name the jobs after the test file name
{
"name": "protractor",
"version": "1.0.0",
"description": "Protractor protype",
"main": "Gruntfile.js",
"dependencies": {
"grunt-sauce-tunnel": "^0.2.1",
"jasmine-reporters": "^2.1.1",
"lodash": "^3.2.0",
"protractor": "2.5.1",
"require-all": "2.0.0"
},
"devDependencies": {
"chalk": "^1.1.1",
"grunt": "^0.4.5",
"grunt-env": "^0.4.2",
"grunt-protractor-runner": "3.0.0",
"grunt-sauce-connect-launcher": "^0.3.0",
"jasmine-spec-reporter": "^2.2.3",
"request-promise": "^1.0.2",
"selenium-webdriver": "2.48.2",
"xlsx-json": "^0.1.0"
},
"scripts": {
"install": "node node_modules/protractor/bin/webdriver-manager update",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git:user/rwolfe/protractor"
},
"author": "",
"license": "ISC"
}
So I don't know if this was due to upgrading to protractor 3.0 or not but apparently in the conf.js I saw that for sauce labs we were setting
exports.config.capabilities.name = " ";
and when I did that for browserstack now the names of the test show up how I would expect as below.