First, when it comes to testing, context is everything. You don't have to test the page title every time, but there are cases you do.
People often test the page title as it acts like a smoke test. If it is wrong, you can fail the test and move on. If it's correct, you can proceed with testing other items on the page. Often, I add the page title check to the Page Object's constructor!
In some web apps, the title doesn't change. For example, using StackExchange, maybe they want the title to always say "StackExchange" and nothing else. In this case, checking the title is useless. Luckily, StackExchange doesn't do this.
What does StackExchange do? The title is the same as the question and they use the question in the URI! If I'm doing an e2e test of StackExchange, I'd do:
- Create a new question
- Save question text as a variable in code
- Post question and description
- Assert posted question page title, URI equals the data in the saved variable
In this case, since the page title changes based on user-created content, it's beneficial to test it!
What you are currently doing is not correct here:
> assertEquals(actualTitle, expectedTitle)
> Where actualTitle is copied from HTML title tag by inspecting google chrome browser.
> Where expectedTitle I got is by driver.getTitle()
expectedTitle should be a string variable or just a quoted string, and NOT taken from the DOM.
expectedTitle = 'this is my title'; should be in your code before the
Or, you can do
assertEquals(actualTitle, 'this is my title');
actualTitle should be a variable before the
assertEquals and come from the driver. So,
actualTitle = driver.getTitle().
Or, you can do
assertEquals(driver.getTitle(), 'this is my title'); This is preferred and more readable if you are only using the data once. If you use the title data more than once, yes, use variables!
FYI, "HTML title tag by inspecting google chrome browser" -- checking this in the browser is known as looking at the DOM (document object model)