Basic Scripts:
Protractor needs two files to run, the test or spec file, and the configuration file.
How to create scripts:
Spec
files
Protractor tests are written using the syntax of your test framework, for
example Jasmine, and the Protractor API. We are going to
write code with Jasmine framework.
Example
Spec File
This simple script (example_spec.js) tests the 'The
Basics' example on the angularjs.org homepage.
Global
Variables
Protractor exports these global variables to your spec (test) file:
Config
Files
The configuration file tells Protractor how to set up the Selenium Server,
which tests to run, how to set up the browsers, and which test framework to
use. The configuration file can also include one or more global settings.
Example
Config File
A simple configuration (conf.js) is shown below.
Protractor needs two files to run, the test or spec file, and the configuration file.
How to create scripts:
·
Create a folder
·
Create 2 text files name them as
conf.js and spec.js
·
Lets see significance of conf and spec files below with simple
examples
·
Copy paste below codes into spec and conf files as given below and
save them
Spec
files
Example
Spec File
describe('angularjs homepage', function() {
it('should greet the named user', function() {
// Load the AngularJS homepage.
browser.get('http://www.angularjs.org');
expect(browser.getTitle()).toEqual(AngularJS — Superheroic JavaScript MVW Framework');
});
});
Global
Variables
·
browser - A wrapper around an instance of
WebDriver, used for navigation and page-wide information. The browser.get method loads a page. Protractor expects
Angular to be present on a page, so it will throw an error if the page it is
attempting to load does not contain the Angular library. (If you need to
interact with a non-Angular page, you may access the wrapped webdriver instance
directly with browser.driver).
·
element - A helper function for finding and
interacting with DOM elements on the page you are testing. The elementfunction searches for an element
on the page. It requires one parameter, a locator strategy for locating the
element. See Using Locators for more information. See Protractor's
findelements test suite (elements_spec.js) for more
examples.
·
by - A collection of element locator
strategies. For example, elements can be found by CSS selector, by ID, or by
the attribute they are bound to with ng-model. See Using Locators.
·
protractor - The Protractor namespace which wraps
the WebDriver namespace. Contains static variables and classes, such as protractor.Key which enumerates the codes for special
keyboard signals.
Config
Files
Example
Config File
// An example configuration file
exports.config = {
// The address of a running selenium server.
seleniumAddress: 'http://localhost:4444/wd/hub',
// 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: ['example-spec.js'],
// Options to be passed to Jasmine-node.
jasmineNodeOpts: {
showColors: true, // Use colors in the command line report.
}
};
Script execution:
·
Open command prompt
·
Enter webdriver-manager update and hit
enter button , below screen will display
·
·
Enter webdriver-manager start and hit
enter button , below screen will display
·
Open new command prompt
·
Move to the folder where we created
spec.js and conf.js files using cd file location
·
Enter protractor conf.js and press
enter button
·
Browser will be launched and script
will execute on the browser.