1--> fetch Mercury Tours' homepage
2--> verify its title
3--> print out the result of the comparison
4--> close it before ending the entire program.
PROGRAM
-------------------------------
package newproject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//comment the above line and uncomment below line to use Chrome
//import org.openqa.selenium.chrome.ChromeDriver;
public class PG1 {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.firefox.marionette","C:\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
//comment the above 2 lines and uncomment below 2 lines to use Chrome
//System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
//WebDriver driver = new ChromeDriver();
String baseUrl = "http://demo.guru99.com/test/newtours/";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Fire fox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page with the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Fire fox
driver.close();
}
12 Aug.
}
--------------******--------------*******---------------*********-----
EXPLAINATION
********----*****----*****---
Importing Packages
To get started, you need to import following two packages:
org.openqa.selenium.*- contains the WebDriver class needed to instantiate a new
browser loaded with a specific driver
org.openqa.selenium.firefox.FirefoxDriver - contains the FirefoxDriver class needed
to instantiate a Firefox-specific driver onto the browser instantiated by the
WebDriver class
If your test needs more complicated actions such as accessing another class, taking
browser screenshots, or manipulating external files, definitely you will need to
import more packages.
Instantiating objects and variables
Normally, this is how a driver object is instantiated.
First Selenium Webdriver Script: JAVA Code Example
A FirefoxDriver class with no parameters means that the default Firefox profile
will be launched by our Java program. The default Firefox profile is similar to
launching Firefox in safe mode (no extensions are loaded).
For convenience, we saved the Base URL and the expected title as variables.
Launching a Browser Session
WebDriver's get() method is used to launch a new browser session and directs it to
the URL that you specify as its parameter.
First Selenium Webdriver Script: JAVA Code Example
Get the Actual Page Title
The WebDriver class has the getTitle() method that is always used to obtain the
page title of the currently loaded page.
First Selenium Webdriver Script: JAVA Code Example
Compare the Expected and Actual Values
This portion of the code simply uses a basic Java if-else structure to compare the
actual title with the expected one.
First Selenium Webdriver Script: JAVA Code Example
Terminating a Browser Session
The "close()" method is used to close the browser window.
First Selenium Webdriver Script: JAVA Code Example
Terminating the Entire Program
If you use this command without closing all browser windows first, your whole Java
program will end while leaving the browser window open.
First Selenium Webdriver Script: JAVA Code Example
Running the Test