Prerequisites for writing Selenium WebDriver Script:

 

Scenario’s:

  • Launch firefox browser and open “stqatools.com”.
  • Wait until full page load.
  • Get page title and store into string.
  • Store Expected page title into string.
  • Verify Actual and Expected page title then print the verification result.
  • Close the web browser.

 

Example: First Selenium WebDriver script:

Output :

 

Import packages:

  • import org.openqa.selenium.WebDriver:- 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 into the browser instantiated by the WebDriver class.

 

To instantiate driver object: 

  • Setting GeckoDriver path on windows for Selenium WebDriver script.

 

Launch a browser:

  • We will use webdriver’s get() method to launch a new browser session and redirects it to the specified URL.

  

Wait Until Page load: 

  • Using implicitlywait wait until page loading

  

Get the actual page title:

  • The webdriver class has the getTitle() method that is used to get the page title of the current page.

 

Store Expected page title into string: 

 

Compare the expected and actual values: 

  • The following code is use to compare the actual title with the expected one.

 

Terminating a browser session:

  • The “close()” method is used to close the browser window.

 

Running the test in eclipse:

  • On Eclipse’s menu bar, click Run –> Run

OR

  • Press Ctrl+F11 to run the entire code.

 

Locating GUI WebElements:

  • Locating WebElements in WebDriver is done by using the “findElement(By.locator())” method. Using different locators  for Selenium WebDriver script.