Expect
Nightwatch a new Bdd-style interface to execute assertions, called expect, in version 0.7. It is based on the Chai expect assertion library and is more flexible and efficient than the traditional asset interface.
Language Chains
The following commands are only auxiliary to improve the readability of assertions. Does not have the ability to test itself
- To
- Be
- been
- Is
- That
- which
- and
- Has
- Have
- With
- At
- Does
- Of
. equal (value)/.contain (value)/.match (regex)
Determine if the component
- Value is equal to the specified value
Browser.expect.element (' #main '). Text.to.equal (' The Night Watch ');
- Whether the value contains the specified value
Browser.expect.element (' #main '). Text.to.contain (' The Night Watch ');
- Whether there is a specified style
Browser.expect.element (' #main '). To.have.css (' Display '). Which.equals (' block ');
. not
Take back write in front of Equal,contain,match text.to.not.equal ()
. Before (ms)/.after (MS)
Assert an assertion again at a specified time, which can be used in conjunction with any assertion so that it can be executed again
. A (type)/an
Checks whether the type of the specified component matches the expected. Parameters with: type,message (optional)
- Browser.expect.element (' #q '). to.be.an (' input ');
- Browser.expect.element (' #q '). to.be.an (' input ', ' testing if #q is an input ');
- Browser.expect.element (' #w '). TO.BE.A (' span ');
. Attribute (name)
Checks if an attribute exists for an element and has the expected value (optional)
- Browser.expect.element (' body '). To.have.attribute (' data-attr ');
. css
With attribute
- Browser.expect.element (' #main '). To.have.css (' Display '). Which.matches (/some\ value/);
. Enabled
Checks if an element is now available
- Browser.expect.element (' #weblogin '). to.be.enabled;
- Browser.expect.element (' #main '). to.not.be.enabled;
. Present
Detects if an element appears in the DOM
- Browser.expect.element (' #main '). to.be.present;
. Select
Detect drop-down box, radio box, check box is currently selected
. text
Detects if the component has a specified text/expression that can be used with contains,equals,matches
- . Text.to.match (/the\ night\ watch/)
. value
Same text
- . to.have.value.which.contains/.not.equals/.that.equals
. Visible
is visible
Assert
The classic Assert&verify assertion library is still available in Nightwatch. They work the same, except that the assert skips the assertion after the current assertion fails, ends the test (equivalent to break), and verify skips the current assertion, outputs an error report, Continue to perform other assertions (equivalent to continue).
. Attributecontains ()
Checks whether an element is given a property that has the expected value
Parameters:
- Selector, used to locate elements
- Attribute, property name
- Expected, expected to contain the value
- Message (optional), description of information in test results
eg. Browser.assert.attributeContains (' #someElement ', ' href ', ' google.com ');
. Attributeequals ()
Ibid. (attributecontains)
. ContainsText ()
Parameters:
- Cssselector
- Expectedtext
- MSG (Optional)
eg. Browser.assert.containsText ("#main", "The Night Watch");
. Cssclasspresent ()
Checks whether the element has the specified CSS class name
Parameters:
- Cssselector, used to locate elements
- ClassName, expecting some class names
- Message (optional), description of information in test results
eg. Browser.assert.cssClassPresent ("#main", "container");
. Cssclassnotpresent ()
Upstairs (. cssclasspresent) Take counter
. Cssproperty ()
Checks whether the specified property of an element is the desired state
Parameters:
- Cssselector, used to locate elements
- Cssproperty, properties
- Expected,
- Message (optional), description of information in test results
eg. Browser.assert.cssProperty ("#main", "Display", "block");
. Elementpresent ()
Whether the DOM contains the specified element
- Browser.assert.elementPresent ("#main");
. Elementnotpresent ()
Upstairs take the counter
. Hidden ()
Checks whether the specified element is not visible on the page
- Browser.assert.hidden (". should_not_be_visible");
. Title ()
Checks whether the page title is a specified title
- Browser.assert.title ("Nightwatch.js");
. Urlcontains ()
Checks whether the current URL contains a specified value
- Browser.assert.urlContains (' Google ');
. Urlequals ()
- Browser.assert.urlEquals (' http://www.google.com ');
. Value ()
Checks whether the value of the specified form element is equal to the specified value
- Browser.assert.value ("Form.login input[type=text]", "username");
. Valuecontains ()
- Browser.assert.valueContains ("Form.login input[type=text]", "username");
. Visible ()
- Browser.assert.visible (". should_be_visible");
Commands
Commands can perform different actions on a page, typically containing two or more page-driven protocol behaviors.
callback function
The following methods support callback functions. The callback function executes after the command is completed
. ClearValue ()
Can clean up text field or text input box contents
- Client.clearvalue (' input[type=text] ');
. Click ()
Simulate Click events
- Client.click ("#main ul Li A.first");
Feel more suitable for use with callback functions
Browser.click ("#main ul li A.first", function (response) {
This.assert.ok (Browser = = = This, "Check if the context was right.");
This.assert.ok (typeof response = = "Object", "We got a Response object.");
});
. CloseWindow ()
Close the current window when you use multiple windows
. Deletecookie ()
Delete Cookies
Nightwatch API Learning Expect,assert