Quick Contact

    Introduction

    The Browser commands are generally the ones which we intuitively feel that a browser should control. Examples are “type url and hit enter”, “getTitle”,”back”,”forward” and so on. Video of the execution and walk through code is explained in the video at the bottom of this page.

    Navigation commands are some which enables the user to navigate to some webpages and also to work with history like back, forth, refresh, to. We can access the these method by accessing the navigate() method

    Selenium Navigation Commands are used to open a web page URL and navigation can be done between different web pages by clicking any element such as back, forward or refresh. Using the navigating interface back and forward can be move in browser history.

    How to Open a URL

    There are few methods that can be used to open an URL. The various methods are:

    • Navigate.To(URL)

    Navigate.To(URL) Syntax:

    driver.navigate().to(URL);

    This methods load a new web page in the current browser window. This is done using an HTTP GET operation, and the method will block until the load is complete. The parameters URL is a fully qualified URL.

    Example:

    public void navigationToURLExample()
    {
    driver= new FirefoxDriver(); driver.navigate().to("http://www.google.com");
    }
    
    • Navigate.To(String)

    Syntax:

    driver.navigate().to(String);

    This methods load a new web page in the current browser window. It is an overloaded version of to (String) that makes it easy to pass in a URL.

    Example:

    public void navigationToStringExample()
     
    
    {
    driver= new FirefoxDriver();
    String URL="http://www.facebook.com"; driver.navigate().to(URL);
    }
    
    Verify Page Title

    Verification of the current page title can be done by checking it with Get Title Command.

    String getTitle(): This method returns the title string of the current page in the browser. It doesn’t allow any parameter and its return type is a String. This method returns a String value. As the title is store in the string and then it use Assert selenium command to return true or false. It can also use If-statement to compare actual and expected web page title. It entirely depends on the discretion of the test engineer and requirement of the project.

    Syntax:

    driver.getTitle();

    To compare a piece of the word(s) in the actual title use contains () method.

    Syntax:

    driver.getTitle().contain(“Character sequence”);

    Here are the steps in the code to verify title of the page:

    1. Use getTitle() and store the value in String
      Example: String actualTitle = driver.getTitle();
    2. Declare expected title in string

      Example: String expectedTitle =”Your expected title”;

    3. Now compare expected and actual
    Method-1 (Using If-Statement)
    if(actualTitle.equalsIgnoreCase(expectedTitle))    System.out.println("Title Matched");
     else
     System.out.println("Title didn't match");
      
      package InviulTest;
    import java.util.concurrent.TimeUnit;
    import org.junit.Assert;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class Test {
    public static void main(String[] args)   {
    System.setProperty("webdriver.chrome.driver", 
    
     "F:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver(); 
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    
    driver.navigate().to("http://www.inviul.com");
    
    String actualTitle = driver.getTitle(); 
    
    driver.manage().window().maximize();
    
    String expectedTitle = "Avinash Mishra Blogger: Learn Selenium   
    
    WebDriver, Automation Framework, QA, SEO & Digital Marketing | 
    
    Inviul Blog | Inviul";
    
    if(actualTitle.equalsIgnoreCase(expectedTitle)) 
    
    System.out.println("Title Matched");
    
    else
    System.out.println("Title didn't match");
    
    driver.close();
    driver.quit();
    }
    }
    
    Method-2 (Using Assert Command)
    Assert.assertEquals(actualTitle, expectedTitle, “Title matched”); 
    package InviulTest;
    import java.util.concurrent.TimeUnit; 
    import org.junit.Assert;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver; 
    import org.openqa.selenium.firefox.FirefoxDriver; 
    public class Test {
    public static void main(String[] args) {
    
    System.setProperty("webdriver.chrome.driver", 
    
    "F:\\chromedriver.exe");
    WebDriver driver = new ChromeDriver(); 
    
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    
    driver.navigate().to("http://www.inviul.com");
    
    String actualTitle = driver.getTitle(); 
    driver.manage().window().maximize();
    
    String expectedTitle = " Learn Selenium WebDriver, Automation Framework";
    
    Assert.assertEquals("Condition true", actualTitle, expectedTitle); 
    
    driver.close();
    driver.quit();
    }
    }
    

    Output

    Example:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
     
    public class GetTitle
     {
          public static void main(String[] args) throws Exception
          {
     
            WebDriver driver=new FirefoxDriver();
    
            driver.get("https://chercher.tech"); 
    
            System.out.println("Page title is : "+driver.getTitle());
    
      }
    
        }
    

    Output

    Page title is Selenium Webdriver

    Example to Open Google.com and verify whether the title is Google or not.

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver; 
    public class Refresh
    {
           
           public static void main(String[] args) throws Exception
         {
             WebDriver driver=new FirefoxDriver(); driver.get("http://google.com");
              if (driver.getTitle().equals("Google"))
              {
    
                System.out.println("Title is Google ");
                }
              else
                {
                	System.out.println("Title is not Google");
    
    
              }
         }
    }
    

    Likewise it can also be compare whether title has Google as a part of title or not

    Strategy to Get the Page Source

    String getPageSource(): This method retrieves the Source Code of a web page. It doesn’t allow any parameter and its return value is a String. This method returns a String value

    Syntax:

    driver.getPageSource();

    While doing test automation using Selenium WebDriver, one might have needs to scan through the HTML source of a webpage. Here it is shown how to get the HTML page source using Selenium WebDriver. One of the most common needs of using page source is to find out any hidden elements or some code snippet of javascript is present as part of the source code.

    Lets first consider the below HTML source code from a page named “selenium-pagesource.html”. Note that the page has not only HTML code, but javascript functions as well.

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function buttonClicked()
    {
    alert("Hello")
    }
    </script>
     
    
    </head>
    <body>
    <div>
    This is an example on how to get html source code using webdiver.</br>
    <button value="Click Me" onclick="buttonClicked();">Click Me</button>
    </div>
    </body>
    </html>
    

    Now let’s see how we can get the entire source code of the above page using Selenium WebDriver and Java.

    Selenium – Get Page Source Code

    //Initialize the webdriver.
    WebDriver driver = new SafariDriver();
    //Invoke the web page. driver.get("http://localhost:8080/selenium-pagesource.html");
    //Get the page source and print the same to the console. System.out.println(driver.getPageSource()); driver.quit();
    

    The above example code will invoke the page and get the page source using getPageSource() function and print it to the console. getPageSource function returns a String which has the entire source code including any client side scripts. This will commonly be used to extract any text or content which can’t directly be accessed using Selenium WebDriver find element methods.

    Difference between Close and Quit

    Close(): This method kills the current window which the WebDriver has the control over. It doesn’t allow any parameter and its return type is void.It’ll terminate the browser if it’s the only window currently open

    Syntax:

    driver.close();

    Example:

    package automationFramework;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class WebDriverCommands_2 {
    public static void main(String[] args) {
    
    WebDriver driver = new FirefoxDriver();
    driver.get("http://demoqa.com/frames-and-windows/");
    driver.findElement(By.xpath(".//*[@id='tabs- 1']/div/p/a")).click();
    driver.close();
    }
    }
    

    Quit (): This method closes all the associated windows opened by the WebDriver. It doesn’t allow any parameter and its return type is void. It’ll automatically close all of the opened windows and terminate the browser.

    Syntax:

    driver.quit();

    Example

    On running the code below, the all windows will be closed i.e. two pop-ups will automatically be closed as well.

    package newproject;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    public class PG3 {
    public static void main(String[] args) {
    
    System.setProperty("webdriver.firefox.marionette","C:\\geckodr iver.exe");
              WebDriver driver = new FirefoxDriver(); 
              driver.get("http://www.popuptest.com/popuptest2.html"); 
              driver.quit(); // using QUIT all windows will close
           }
           }   
    
    Ways to Navigate Back and Forward

    The Navigation commands are very useful and allow to control the navigation flows in the web browser.

    Forward(): This method simulates the browser’s forward button action. It doesn’t allow any parameter and its return type is a void. It moves to the forward by a single page into the browser’s history data.

    Syntax:

    driver.navigate().forward();

    Example

    public void navigationForwardExample()
            {
               driver= new FirefoxDriver();
               String URL="http://www.twitter.com"; driver.navigate().to(URL);
               driver.findElement(By.linkText("Forgot your password?")).click(); 
               driver.navigate().back();
               Thread.sleep(1000); 
               driver.navigate().forward();
             }
    
    • In the above example, to work with navigate forward method, there is need to travel to at least one or multiple pages in the browser history.
    • The first page which has opened is twitter.com page and then traveled to forget password page. Now the browser has two pages in the history. If navigate.back() will use, it will redirect the user to twitter.com page whereas on using navigate.forward(), it will redirect the user to forgot password page.

    Back():This method simulates the browser’s back button action. It doesn’t allow any parameter and its return type is a void. It moves you back by a single page into the browser’s history data.

    Syntax:

    driver.navigate().back();

    Example:

    public void navigationBackExample()
          {
               driver= new FirefoxDriver();
               String URL="http://www.twitter.com"; 
               driver.navigate().to(URL);
               driver.findElement(By.linkText("Forgot your password?")).click(); 
               driver.navigate().back();
              }
    
    • In the above example, to work with navigate back method, there is need to travel to at least one or multiple pages in the browser history.
    • The first page which has opened is twitter.com page and then traveled to forget password page. Now the browser has two pages in the history. If navigate.back() will use, it will redirect the user to twitter.com page.
    How to Refresh a Page

    A Web page can be refresh with the help of the Refresh Command.

    refresh(): This method simulates the browser’s refresh button action. It doesn’t allow any parameter and its return type is a void. It triggers the same action as does the F5 shortcut in the browser.

    There are many ways to refresh the page .Here are some of the ways:

    • Using refresh() method: Selenium webdriver has a method called refresh(). This is widely used command. It refreshes the current page after executing.

      Syntax

      driver.navigate().refresh();
    • Using sendkeys() method: sendkeys() method is used like we do page refresh pressing F5 key through our keyboard. This manual task is executed by code using the sendkeys() command over an element.

      Syntax:

      driver.findElement(By.id(locator)).sendKeys(F5 key);

      Example

      driver.findElement(By.id("gbqfq")).sendKeys(Keys.F5);
    • Using navigate().to() method: Actually browsing the same URL using navigate().to() function. It call getCurrentUrl() function to get the current URL of the page.
    • Syntax:

      driver.navigate().to(driver.getCurrentUrl());
    • Using get() method: If we know the URL then we can load the same URL again to reload the page.

      Syntax:

      driver.get("https://www.google.com.bd/");
    • Using sendkeys() method with ASCII code: ASCII code can be used as argument on sendkeys() method that is equivalent to F5 key command of the keyboard.

      Example:

      driver.findElement(By.id("gbqfq")).sendKeys("\uE035"); 

      Here,

      \uE035 is the ASCII code of F5 key.
    • Using executeScript() method: Using this command it can execute any JavaScript for the need. If it execute location.reload) JavaScript function then current page will be reloaded which meets our purpose.

      Syntax:

      driver.executeScript("location.reload()");

      Example:

      public void testRefreshPage() {
      		driver= new FirefoxDriver(); 
      		driver.navigate().to("http://google.com"); 
      		Actions actions = new Actions(driver);
              actions.keyDown(Keys.CONTROL).sendKeys(Keys.F5).perform();
                } 
    Another Way of Navigating to a Specific Page

    The Get Command is the way to load a Specific page.

    get(String arg0) : This method loads a new web page in the current browser window. But it doesn’t maintain the browser History and cookies so forward and backward button can’t be use and on click page will not get schedule. This method accepts String as a parameter and returns nothing.

    Syntax:

    driver.get(appUrl);

    Where appUrl is the website address to load. It is best to use a fully qualified URL.

    Example:

    driver.get("http://www.google.com");
    //Or can be written as
    String URL = "http://www.google.com"; 
    driver.get(URL);
    

    We can get url of page by using getCurrentUrl(), it returns Url as string

    Example:

    import org.openqa.selenium.WebDriver;
          import org.openqa.selenium.firefox.FirefoxDriver;
    
              public class GetUrl
             {
              public static void main(String[] args) throws Exception
            {
               WebDriver driver=new FirefoxDriver();
                driver.get("https://chercher.tech");
               // below line gets the url of a website driver.getCurrentUrl();
            }
        }
    
    Summary
    • Selenium Navigation Commands are used to open a web page URL
    • Verification of the current page title can be done by checking it with Get Title Command
    • The Navigation commands are very useful and allow to control the navigation flows in the web browser
    • The Browser commands are generally the ones which we intuitively feel that a browser should control
    • Navigation commands are some which enables the user to navigate to webpages
    • A Web page can be refresh with the help of the Refresh Command

    Copyright 1999- Ducat Creative, All rights reserved.