Collections:
Retrieve Web Page Title with WebDriver in Java
How to Retrieve Web Page Title with Selenium WebDriver in Java?
✍: FYIcenter.com
Once you have connected to the Web browser using the Selenium WebDriver,
you can open any remote Web page and retrieve its page title as shown in this tutorial.
3 main methods from the Selenium WebDriver class will be used:
1. Make sure you have WebDriver loading program, WebDriverLoader.java, compiled in the current directory:
C:\fyicenter> dir WebDriverLoader.*
1,693 WebDriverLoader.class
882 WebDriverLoader.java
2. Enter the following program, GetPageTitle.java, that retrieve Web page title:
C:\fyicenter> type GetPageTitle.java
// GetPageTitle.java
// Copyright (c) FYIcenter.com
import org.openqa.selenium.*;
public class GetPageTitle {
public static void main(String[] args) {
WebDriver driver = WebDriverLoader.load(args[0]);
driver.get("http://sqa.fyicenter.com");
String title = driver.getTitle();
driver.quit();
System.out.println("Test Case - Verify Page Title:");
System.out.println(" Actual Value: "+title);
System.out.println(" Expected Value: FYI Center...");
if (title.startsWith("FYI Center")) {
System.out.println(" Test Result: Passed");
} else {
System.out.println(" Test Result: Failed");
}
}
}
3. Compile and run it with the Selenium Client JAR file. Make sure the current directory is included in the "-classpath".
C:\fyicenter> javac -classpath \
.;\fyicenter\selenium\java\client-combined-3.141.59.jar \
GetPageTitle.java
C:\fyicenter> java -classpath \
.;\fyicenter\selenium\java\client-combined-3.141.59.jar;\
\fyicenter\selenium\java\libs\guava-25.0-jre.jar;\
\fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\
\fyicenter\selenium\java\libs\okio-1.14.0.jar;\
\fyicenter\selenium\java\libs\commons-exec-1.3.jar \
GetPageTitle Chrome
Starting ChromeDriver 75.0.3770.8 (...-refs/branch-heads/3770@{#40}) on port 47740
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
8:02:26 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Test Case - Verify Page Title:
Actual Value: FYI Center for Software QA Testing
Expected Value: FYI Center...
Test Result: Passed
C:\fyicenter> java -classpath \
.;\fyicenter\selenium\java\client-combined-3.141.59.jar;\
\fyicenter\selenium\java\libs\guava-25.0-jre.jar;\
\fyicenter\selenium\java\libs\okhttp-3.11.0.jar;\
\fyicenter\selenium\java\libs\okio-1.14.0.jar;\
\fyicenter\selenium\java\libs\commons-exec-1.3.jar \
WebDriverLoader Edge
[20:02:53.307] - Listening on http://localhost:48540/
8:02:56 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
[20:02:58.828] - Stopping server.
Test Case - Verify Page Title:
Actual Value: FYI Center for Software QA Testing
Expected Value: FYI Center...
Test Result: Passed
As you can see, the getTitle() method works nicely.
⇒ Retrieve Web Form Elements with WebDriver in Java
⇐ Load WebDriver for Different Browsers in Java
2020-01-04, ∼2411🔥, 0💬
Popular Posts:
How to generate currency test values? Currency test values are frequently needed in testing date and...
How to generate user birthday dates? Test user birthday dates are frequently needed in testing appli...
How to valid IP addresses? In order to help your programming or testing tasks, FYIcenter.com has des...
How to access Response Status Code 400 as Success? My sampler failed, because the server returns the...
How to generate test fractional numbers? Test fractional numbers are numbers with fractions that are...