0

I am a new to use selenium to do web automation test. Why do we need to verify the page title by assertEquals(actualTitle, expectedTitle) to do the test? The actualTitle value I copied from HTML title tag by inspecting google chrome browser. The expectedTitle I got is by driver.getTitle(), sure WebDriver driver = new ChromeDriver();

Are there any cases where the expected value I got from html title tag doesn't match the actual value which was obtained by method "driver.getTitle()"? If I got failed test result, what do I need to do next, report to developer to fix the bug?

4 Answers 4

1

You're right that the test, as you described it, is useless.

What is the purpose of this test? For example, you have a link on a page that says, "Navigate to Foo." You then expect the page you arrive at to be titled "Foo." But if the page is titled "Bar," you know you went to the wrong place.

Your "expected" value should not be coming from the same place that the actual value does.

So thinking of (in the context you wish to test) what the expected value should truly be—not something you extract from the page itself—where can you get a value of what you truly expect, that you can check against what is actually on the page?

3
  • So, the expected value is coming from the person or team who requires me test it? Oct 29, 2023 at 4:48
  • Typically, yes.
    – Roger Cook
    Oct 30, 2023 at 5:14
  • If you manually got the title from Chrome, and then hard coded that to your test, that wouldn't be crazy. i.e. it would catch regressions where someone changes the title. Not uncommon for the expected value, especially for something that's being automated later, to be the currently observed value.
    – ernie
    Nov 2, 2023 at 20:36
1

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:

  • Login
  • 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()

Specifically, 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 assertEquals

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)

2
  • Thank you so much! Nov 1, 2023 at 3:00
  • You're welcome, @user3559982. Be sure to vote and accept the answer you feel is best!
    – Lee Jensen
    Nov 1, 2023 at 17:55
0

As data quality testing need for automation, we have to ensure that correct testdata is provided to the automation scripts.

Validation of page title usually plays a major role in verifying the navigation between the pages.

The "actualTitle" should be fetched at runtime, and "expectedTitle" should be stored as string under a variable in the testdata file. In the Assertion, the "actualtitle" should be compared with that "Variable"

To Exemplify: Clicking on button1 on page1 should navigate to page2. If button1 does not navigate to the required page2, then script will instantly fail which will save the efforts of finding locators and executing tests on an inaccurate page.

0

I verify the title in order to assert that the login process worked correctly and I was able to reach the expected app home page.

Since the login process is managed and tasted by another team, and that team is full of humans who sometimes make mistakes, the title verification test is the precondition test - if it fails, no other tests are run, I don't get a giant pile of useless failed test results, and the login team gets to hear about it faster b/c my work is blocked.

But I could as easily use other methods to verify the page - the url for instance, or anything else that I might expect to be on the page.

Be careful though - the method you described is a pitfall that I can only summarize as 'there was stuff on the page, and it was stuff!' You want to work with cold hard user requirements when available, and hard code expected strings when applicable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.