Assert Title - Selenium WebDriver Testing Free Training

Welcome to the Selenium WebDriver Testing Free Training series. In the last tutorial, we tried to write the first Selenium WebDriver code to open www.google.com in the Google Chrome browser.

In this tutorial, we are going to discuss the following things:
  1. Get webpage title
  2. Assert the webpage title
  3. Scenarios, where we can use assert title technique.

To check the last tutorial, please refer to the following link:

Before moving further, let’s take a brief look at what are Assertions in Selenium.

Microsoft

Assertions are the checkpoints that determine the state of the application, whether it is the same as what we are expecting or not. If the assertion fails, then the test case is failed and stops the execution by throwing an error. There is one more concept that works similarly to assertions in Selenium, called Verify. There is the only main difference between Verify and Assertion that if Verify fails it won’t stop the execution.

Important note: we like to tell you that free tutorials are useful to get started but if you are interested in the best online LIVE Master of Automation Testing training program from the experts, please refer to the following link:

For Instructor-led training

Master of Functional Automation Testing

For Self-Driven training

Microsoft

Automation with Selenium WebDriver (Java)

All right!! Let’s see what steps, we need to perform to assert the title of web pages:
  1. Create an object of WebDriver(Note: WebDriver is an interface. So, we need to call ChromeDriver or FirefoxDriver or other Driver class).
  2. Open the website or portal URL in the Browser.
  3. Get the webpage title by calling the method of WebDriver.
  4. And the last step is to assert the title.

Let’s proceed with the first step:

Note: In this example, we are going to assert the google.com title.

Create an object of WebDriver
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe" );
ChromeDriver driver = new ChromeDriver();
Open website
driver.get("https://www.google.com/");
Get the webpage title
String expectedTitle = "Google";
String actualTitle = driver.getTitle();
Assert the title
Assert.assertEquals(actualTitle, expectedTitle);
Here is the full code to assert the title of a webpage:
import org.junit.Assert;
import org.openqa.selenium.chrome.ChromeDriver;

public class AssertTitle {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\chromedriver.exe");
        ChromeDriver driver = new ChromeDriver();
        driver.get("https://www.google.com/");
        String expectedTitle = "Google";
        String actualTitle = driver.getTitle();
        Assert.assertEquals(actualTitle, expectedTitle);
    }
}
That’s how we can assert the webpage title.

But what will be the scenarios where assert title will help?

Let’s say you are trying to test the login process of a portal. To test the login process, here are the following steps to be performed:
  1. Provide the correct credentials.
  2. Click on the login button.
  3. If the login succeeds, then it will go to the dashboard page (assuming).

Now we are at the dashboard page. It means credentials are working, but to check through the code, we need to do asserting the title of the dashboard page. This is how we can verify the login process.

Lemonkind Fat-Burn Latte Cleanse • USDA Certified Organic • 15 ready-to-mix Sachets • Includes Shaker Bottle

You can use this to test  the following flow:

1. Registration flow.

2. Form submission (not Ajax one)

3. Payment flow.

And many more, etc.

Here are some related video tutorials:

Conclusion

All right!! So, in this tutorial, we discussed what is Assertion, how to get the title of the webpage, and in what

scenarios, we can call for assert title technique.

→ The upcoming article in this Selenium WebDriver Testing Free Training series will be aboutHow to find out the Web elements in selenium.

Happy learning, until then.