0
0
Selenium Javatesting~20 mins

Page title and URL retrieval in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Page Title & URL Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium WebDriver code that opens a webpage and retrieves its title and URL. What will be printed to the console?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://example.com");
String title = driver.getTitle();
String url = driver.getCurrentUrl();
System.out.println(title + " - " + url);
driver.quit();
AExample Domain - https://example.com
Bhttps://example.com - Example Domain
Cnull - null
DThrows NoSuchElementException
Attempts:
2 left
💡 Hint
Remember that getTitle() returns the page title and getCurrentUrl() returns the current URL loaded in the browser.
assertion
intermediate
1:30remaining
Which assertion correctly verifies the page title in Selenium Java?
You want to verify that the page title is exactly "Welcome Page" after loading the page. Which assertion is correct?
Selenium Java
WebDriver driver = new ChromeDriver();
driver.get("https://mysite.com/welcome");
String actualTitle = driver.getTitle();
AassertTrue(actualTitle.contains("Welcome Page"));
BassertFalse(actualTitle.isEmpty());
CassertNotNull(actualTitle);
DassertEquals("Welcome Page", actualTitle);
Attempts:
2 left
💡 Hint
You want to check exact equality, not just presence or non-null.
locator
advanced
1:30remaining
Identify the best locator to verify the current URL contains a specific path
You want to write a test that verifies the current URL contains the path "/dashboard" after login. Which Selenium Java code snippet correctly retrieves the URL for this check?
AString url = driver.findElement(By.id("url")).getText();
BString url = driver.getCurrentUrl();
CString url = driver.getTitle();
DString url = driver.getPageSource();
Attempts:
2 left
💡 Hint
The URL is not an element on the page but a property of the browser.
🔧 Debug
advanced
2:00remaining
Why does this Selenium Java test fail to get the page title?
Examine the code below. The test throws a NullPointerException when trying to get the title. What is the most likely cause?
Selenium Java
WebDriver driver;
driver.get("https://example.com");
String title = driver.getTitle();
System.out.println(title);
Adriver was never initialized with a browser instance
BgetTitle() method is deprecated and throws exception
CThe URL https://example.com is unreachable
DMissing driver.quit() causes NullPointerException
Attempts:
2 left
💡 Hint
Check if the WebDriver object is assigned before use.
framework
expert
2:30remaining
In a Selenium Java test framework, which method is best to retrieve and log page title and URL after each test?
You want to automatically log the page title and URL after every test method runs in your Selenium Java test suite using TestNG. Which approach is best?
AUse @BeforeClass annotated method to log title and URL before tests run
BCall driver.getTitle() and driver.getCurrentUrl() inside every @Test method manually
CUse @AfterMethod annotated method to call driver.getTitle() and driver.getCurrentUrl() and log them
DUse a static block to log title and URL once when class loads
Attempts:
2 left
💡 Hint
Think about running code after each test method automatically.