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.
- Get webpage title
- Assert the webpage title
- 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.
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
- Create an object of WebDriver(Note: WebDriver is an interface. So, we need to call ChromeDriver or FirefoxDriver or other Driver class).
- Open the website or portal URL in the Browser.
- Get the webpage title by calling the method of WebDriver.
- 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.
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);
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); } }
But what will be the scenarios where assert title will help?
- Provide the correct credentials.
- Click on the login button.
- 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.
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.
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 about – How to find out the Web elements in selenium.
Happy learning, until then.