Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning Selenium Testing Tools - Third Edition

You're reading from   Learning Selenium Testing Tools - Third Edition Leverage the power of Selenium to build your own real-time test cases from scratch

Arrow left icon
Product type Paperback
Published in Feb 2015
Publisher
ISBN-13 9781784396497
Length 318 pages
Edition 3rd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Raghavendra Prasad MG Raghavendra Prasad MG
Author Profile Icon Raghavendra Prasad MG
Raghavendra Prasad MG
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting Started with Selenium IDE FREE CHAPTER 2. Locators 3. Overview of the Selenium WebDriver 4. Finding Elements 5. Design Patterns 6. Working with WebDriver 7. Automation Framework Development and Building Utilities 8. Mobile Devices 9. Getting Started with the Selenium Grid 10. Advanced User Interactions 11. Working with HTML5 12. Advanced Topics 13. Migrating from Remote Control to WebDriver A. Automation Prerequisites for Selenium Automation B. Answers for Self-test Questions Index

Validating a test with assert and verify

In the last few steps, we were able to record a workflow that we would expect the user to perform. It will test that the relevant bit of functionality is there, such as buttons and links to work against. Unfortunately, we are not checking whether the other items on the page are there or if they are visible when they should be hidden. We will now work against the same page as before, but we shall make sure that different items are on the page.

There are two mechanisms for validating elements available on the application under test. The first is assert: this allows the test to check whether the element is on the page. If it is not available, then the test will stop on the step that failed. The second is verify: this allows the test to check if the element is on the page, but if it isn't, then the test will carry on execution. To add the assert or verify commands to the tests, we need to use the context menu that Selenium IDE adds to Firefox. All that one needs to do is right-click on the element if on Windows or Linux. If you have a Mac, then you will need to do the two-finger click to show the context menu.

When the context menu appears, it will look roughly like the following screenshot with the normal Firefox functions above it:

Validating a test with assert and verify

We will record the test and learn how to use/verify some commands as follows:

  1. Open the IDE so that we can start recording.
  2. Navigate to http://book.theautomatedtester.co.uk/chapter1.
  3. Select Selenium Grid from the drop-down box.
  4. Change the selection to Selenium Grid.
  5. Verify that the Assert that this text is on the page text is mentioned on the right-hand side of the drop-down box, by right-clicking on the text and selecting verifyText id=diveontheleft Assert that this text is on the page. You can see the command in the previous screenshot.
  6. Verify that the button is on the page. You will need to add a new command for verifyElementPresent with the verifybutton target in Selenium IDE.
  7. Now that you have completed the previous steps, your Selenium IDE should look like the following screenshot:
    Validating a test with assert and verify

If you now run the test, you will see it has verified that what you are expecting to see on the page has appeared. Notice that the verify commands have a darker green color. This is to show that they are more important to the test than moving through the steps. The test has now checked that the text we required is on the page and that the button is there too.

What will happen if the verify command did not find what it was expecting? The IDE would have thrown an error stating what was expected was not there, but then carried on with the rest of the test. We can see an example of this in the following screenshot:

Validating a test with assert and verify

The test would not have carried on if it was using assert as the mechanism for validating that the elements and text were loaded with the page.

We have just seen that we can add asserts or verification to the page. Selenium IDE does not do this when recording, so it will always be a manual step. The assert command will stop the running tests, unlike the verify command in which the tests will continue running even after failure. In both the cases, Selenium IDE will log an error. Each of these have their own merits.

Some of the verify and assert methods are as follows:

Selenium Command

Command Description

verifyElementPresent

This verifies an expected element is present on the page.

assertElementPresent

This asserts an expected element is present on the page.

verifyElementNotPresent

This verifies an expected element is not present on the page.

assertElementNotPresent

This asserts an expected element is not present on the page.

verifyText

This verifies expected text and its corresponding HTML tags are present on the page.

assertText

This asserts expected text and its corresponding HTML tags are present on the page.

verifyAttribute

This verifies the value of an attribute of any HTML tag on the page.

assertAttribute

This asserts the value of an attribute of any HTML tag on the page.

verifyChecked

This verifies whether the condition of the checkbox is checked or not on the page.

assertChecked

This asserts whether the condition of the checkbox is checked or not on the page.

verifyAlert

This verifies the alert present on the page.

assertAlert

This asserts the alert present on the page.

verifyTitle

This verifies an expected page title.

assertTitle

This asserts an expected page title.

You have been reading a chapter from
Learning Selenium Testing Tools - Third Edition - Third Edition
Published in: Feb 2015
Publisher:
ISBN-13: 9781784396497
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime