I am trying to use protractor in conjunction with Jenkins. In my jenkins, I need to have URLs dynamically generated.
So while running protractor tests, for example:
describe('angularjs homepage', function() {
it('should greet the named user', function() {
// Load the AngularJS homepage.
browser.get('http://www.angularjs.org');
element(by.model('yourName')).sendKeys('testUser');
});
});
In above example I want to pass a variable dynamically in place of "http://www.angularjs.org".
I could not find any variables that can be specified in the reference config as well.
You can use baseUrl as config parameter inside exports.config and then use browser.get('/path') inside your test spec. So in config you have e.g. baseUrl: 'http://localhost', so browser.get('/path') would call http://localhost/path.
If I understand the question correctly, you are looking for the environmental variable to configure the base url. In that case, since Protractor is built on WebDriver you should be able to set
webdriver.base.url="http://someurl"
Hopefully this is what you are looking for.
It looks like calling browser.baseUrl = "https://test-url.com" does the trick in onPrepare
you must have conf.js or conf.ts file. So, You can set "baseUrl" as part of your conf file under config
// conf.js
exports.config = {
framework: 'jasmine',
specs: ['spec.js'],
baseUrl: 'my-site.com'
}
then call this browser in your test like:
browser.get(browser.baseUrl);
I faced same issue and resolved like this.
Related
I want to execute browser method inside the protractor custom locator like:
import {browser} from 'protractor';
protractor.by.addLocator("demo",(selector: string) => {
browser.executeScript('my script')
});
This throws error like:
protractor_1 not defined.
Any help will be valuable.
Add on: Let me know, if we can use async /await inside custom locator.
The problem is CLEARLY NOT in the code you posted.
Somewhere you're using protractor_1, which is not defined. Look for it in your code
You have imported 'browser' but not the actual 'protractor'
import { browser, protractor } from 'protractor';
browser.executeScript cannot be executed in the context of the browser.
"#param {Function|string} script. A script to be run in the context of the browser.
Follow up the documentation: https://www.protractortest.org/#/api?view=ProtractorBy.prototype.addLocator
I am trying to understand how UIveri5 works and how to apply it to my own projects, but due to the minimal documentation I am unable to create a minimal working example.
I tried to apply the code from https://github.com/SAP/ui5-uiveri5/blob/master/README.md to "my" minimal app ( https://ui5.sap.com/1.62.0/#/sample/sap.m.sample.Button/code/ ), but the IDE VS Code marks errors, since i.e. the commands export or define are not known and I don't see where UIveri5 loads them from. Also if I just execute uiveri5 in my command line as is, I am getting an error ( I guess from selenium ) that my Chrome binary is missing, but don't the drivers get downloaded automatically?
conf.js
exports.config = {
profile: 'integration',
baseUrl: 'localhost:8080/.../sap.m.sample.Button',
};
page.spec.js
describe('Page', function () {
it('should display the page',function() {
element(by.control({
viewName: 'sap.m.sample.Button.Page',
controlType: 'sap.m.Page',
properties: {
title: "Page"
}}));
});
});
It would be awesome if someone already build a minimal example and can share it. It would help me very much in understanding how everything works together.
The minimum example is right in the readme.md. The only problem I see here is with the baseUrl - this should be a valid URL to an existing app. If this is a sample app on your localhost, you need a dev server.
I tried to use CasperJS for headless browser testing using PhantomJS and wanted to have a config file or something to change Website URL, Username passwords etc. So finally I found NuclearJS. Do you guys know any other perfect way to do this? If I wanted to write a one from the scratch would like to know about as well.
I got a solution (not perfect ;) ) that is using multiple configfiles (for selector, execution, desktop, mobile, etc).
I include a in the execution of my casperjs tests a file that offers me all configs i need (i include also global functions there).
Lets guess the test execution looks like that:
casperjs test --includes=loadGlobals.js test_1.js
In the that example loadGlobals.js contains functions like that:
var fs = require('fs');
var config = {},
configFile = fs.read('config.json');
config = JSON.parse(configFile);
Probalby the config.json is looking like that:
{
"url": "http://www.yourTestUrl.com",
"variable_1": "bla",
"variable_2": "blub",
"nextTier": {
"variable_1": "blablub"
}
}
Now you can call in the test_1.js the variables of the config.json:
casper.start(config.url, function() {
casper.then(function() {
casper.echo(config.variable_1);
casper.echo(config.variable_2);
casper.echo(config.nextTier.variable_1);
});
})
casper.run();
You can use like that different configurationfiles, even to override it during tests if nessacary.
The tests should be written in the page object pattern style so they are highly maintable, espacially with a outsourced configuration.
NuclearJS i didn't know, but i will take a look into it, too.
Ive got a weird problem, Ive got a JS library I pull down from a CDN, and before I can use it I need to run an init function on it, and then run my tests. Anyone got any ideas how I can do that?
In my actual project I call the init function first, then call the rest of my code from the inits callback, but I just cant figure out how to do this for a test
You can list CDNs in your karma config:
files : [
'http://theurl.totheserver/thelibrary.js',
Then assuming the library has initialization function that takes a callback function as a parameter:
describe("My tests", function() {
before(function(done) {
myLibrary.initialization(done);
});
describe("My test cases", function() {
However I would consider using Bower to manage your front end dependencies rather than relying on CDNs.
Just wanted to know is it possible to specify cli args to protractor like
--multiCapabilities.0.browserName chrome --multiCapabilities.1.browserName firefox
so that it overrides the multiCapabilities defined in protractor conf file.
A concrete example of Isaac Lyman's first suggestion:
CLI:
protractor ... --params.browsers="chrome,firefox"
conf.js:
var capabilities = {
chrome: {
browserName: 'chrome'
},
firefox: {
browserName: 'firefox'
}
};
...
getMultiCapabilities: function() {
var browsers = this.params.browsers.split(',');
// Using lodash to select the keys in `capabilities` corresponding
// to the browsers param.
return _( capabilities )
.pick(browsers)
.values()
.value();
},
There are a couple of things you could try.
How can I use command line arguments in Angularjs Protractor? explains how to pass in a "params" variable, which if you were totally pro you could reference later in the config file, with the multiCapabilities section (maybe use a helper function or an if statement so you don't have to pass in a complex object from the command line). Not easy to do, but possible.
https://sourcegraph.com/github.com/teerapap/grunt-protractor-runner (see the Options section) is a utility that lets you pass in these things from the command line without any trouble. It's open-source and seems like it would be easy to mod if it doesn't quite meet your needs.
The easiest option, assuming you just need a couple of different options, would just be to use two different config files, "protractor.chrome.conf.js" and "protractor.firefox.conf.js" and run whichever one you need at the moment.
This is a reasonable request. I've created a PR for this here: https://github.com/angular/protractor/pull/1770. For now, you can patch this PR to your local protractor to use this feature.