How to assert the title of the web page in WebDriver?

Sharad Jaiswal
Sharad Jaiswal

Posted On: Jun 11, 2021

 

    1 Answer Written

  •  Sharad Jaiswal
    Answered by devquora

    To assert the title of a web page using WebDriver, you can use the getTitle() method of the WebDriver class.

    Here is an example code snippet in Java:

    // Import required libraries
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    
    // Create a new instance of the ChromeDriver
    WebDriver driver = new ChromeDriver();
    
    // Navigate to a web page
    driver.get("https://www.example.com");
    
    // Get the title of the web page
    String pageTitle = driver.getTitle();
    
    // Assert that the title of the web page is as expected
    assert pageTitle.equals("Expected Title");
    
    // Close the browser
    driver.quit();
    

    In this example, the getTitle() method is used to retrieve the title of the web page and store it in a variable called pageTitle. The assert statement is then used to compare the value of pageTitle with the expected title of the web page. If the two values are not equal, an assertion error will be thrown. Finally, the quit() method is called to close the browser.

Related Questions

Please Login or Register to leave a response.

Related Questions

Selenium Interview Questions

What Evaluation can Selenium Do?

Selenium Could be Utilized for the purposeful, Regression, load testing of the Internet-based program. The Automation To..

Selenium Interview Questions

Just how much does Selenium License cost per Customer Device?

Selenium is open-source software, released under the Apache 2.0 license and can be downloaded and used without charge...

Selenium Interview Questions

Where to obtain Selenium ?

Selenium Can be Downloaded and Installed for free-form Seleniumhq.org..