Thursday, October 17, 2013

Writing your first script in Webdriver

As we have already created the set up for writing scripts in selenium through the post: How to set up Selenium Webdriver.
Now, we will write our first script in WebDriver that will do the following operations:
1. Opens the Firefox browser window
2. Open the Google page in it.
3. Verify the page title.
4. Close the browser window.

Below is the WebDriver code to achieve above scenario:

package seleniumpkg;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestClass {

 // declaration of objects
 static WebDriver driver;

        //Initialize the String with the url you want to open
 static String baseUrl= "https://www.google.com";  

 public static void main(String[] args) {

  // instantiation of object
  driver= new FirefoxDriver();

  //Open Firefox and direct it to the Base URL
  driver.navigate().to(baseUrl);
  
  //Maximize the browser window
  driver.manage().window().maximize();

  //Get the actual page title
  String title = driver.getTitle(); 
  
  //Verify the actual title with the expected title
  if(title.equals("Google")){
   System.out.println("Verify Title is Passed!");
  }
  else{
   System.out.println("Verify Title is Failed!");
  }

  //Close the browser window
  driver.close();
 }
}

Running the script
In the Eclipse IDE, Click on Run-> Run As-> Java Application


Eclipse Output is displayed in Console and should be "Verify Title is Passed!"


No comments:

Post a Comment