Italian Trulli

How To Handle Browser Action in Selenium Using Java

Test Tribe
.

                  Handling Browser Action in Selenium 


  • How to Launch Chrome Browser in Selenium Webdriver Using Java?

Just Follow the Below steps to launch the Chrome browser in Selenium.

  • First, Download the latest ChromeDriver binary from Chromium.org https://chromedriver.chromium.org/downloads  download page and place the executable on your local machine. 
  • Now Set the webdriver.chrome.driver property to the chromeDriver.exe’s location as- System.setProperty(“webdriver.chrome.driver”, “chromeDriver.exe path”);

Selenium Code: Launching Chrome Browser
Selenium code to launch the Chrome browser in selenium is given below. 

System.setProperty("webdriver.chrome.driver""C:\\Software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();

The above selenium code will launch the Chrome browser after successful code execution.


  •  How to Enter URL in Browser Using Selenium Webdriver?

get() - In the Selenium web driver, the get method is used to open a URL.

Selenium Code :
//Enter URL in Browser
driver.get("https://www.facebook.com/");


  • How to Navigate to URL or How to Open a Webpage in Selenium Browser?

navigate().to()- In selenium webdriver navigate to method is used to load a new webpage in the current browser window.

Selenium Code :
//Navigate to URL
 driver.navigat().to("https://www.facebook.com/");


  • How to Maximize Browser Window in Selenium Webdriver?

maximize()- In selenium webdriver maximize method is used to maximize browser window.

Selenium Code:
//Maximize Browser Window 
 driver.manage().window().maximize();


  • How To Refresh Browser in Selenium Webdriver Using Java?

In selenium there are multiple way to refresh the browser Sometimes in certain browser normal refresh command does not work, in that situation we use these command to overcome from the situation.

1- refresh ()- Refresh command in selenium is most frequently used command by the IT professionals . Refresh command is a navigation command in Selenium webdriver.

Selenium Code : 
//Refresh Browser Window
 driver.navigate().refresh();

2-sendkey ()- Sendkeys method in selenium is also used for refreshing a webpage. We use sendkeys method on any text box on webpage.

Selenium Code : 
//Refresh Browser Window
driver.get("https://www.facebook.com/"); 
driver.findElement(By.xpath("//input[@type='text']")).sendkeys(keys.F5);


  • How To Click Browser Back Button in Selenium Webdriver Using Java?

back() - Back method in selenium is used to click back button of an browser. Back command is a navigation command in selenium.

Selenium Code :
//Click browser Back Button
driver.get("https://www.facebook.com/");
 driver.navigate().back();


  • How To Click Browser Forward Button in Selenium Webdriver Using Java?

forward () - Forward method in selenium in used to click forward button of browser. Forward command is a navigation command in selenium webdriver .

Selenium Code :
//Click browser forward Button
driver.get("https://www.facebook.com/");
 driver.navigate().forward();


  • How To Close Browser Window in Selenium Webdriver Using Java?

In selenium webdriver close () and quit () are two method to close the browser session . And knowing and understanding both of them is most important during the test execution .

1- close()- close method in selenium used to close the current browser window.

Selenium Code :
//Close Current browser window
 driver.close();

2-  quit ()- The quit method in Selenium is used to close all the open browser windows and end the web driver session.

Selenium Code :
//Close all browser window
 driver.quit();


  • How To Get Page Title in Selenium Webdriver Using Java?
getTitle() - get title method in selenium is used to get the title of page .

Selenium Code - 

//How to get and verify page title in selenium

String st= driver.getTitle();
System.out.println(st);
if (st.contains("Google")) 
{
System.out.println("Valid Title ");
}
else 
{
System.out.println("Invalid Title");
}

  • How to Get Current URLs in Selenium Using Java?
gerCurrentUrl()- To get the current URL in selenium webdriver use gerCurrentUrl() method.

Selenium Code - 
//How to get and verify current URL
String getU=driver.getCurrentUrl();
System.out.println(getU);
if (getU.contains("https://www.google.com/")) 
{
System.out.println("Valid URL ");
}
else 
{
System.out.println("Invalid URL");
}

  • How To Get Page Source Code in Selenium using Java?
getPageSource() - To get the page source code in selenium use getPageSource() method .

Selenium Code - 
// To get page source Code
String pgs=driver.getPageSource();
System.out.println(pgs);


Selenium Code to Automate Browser Window Using Java.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
 
public class LaunchBrowser {
 
       public static void main(String[] args) {
              // TODO Auto-generated method stub
              System.setProperty("webdriver.chrome.driver""C:\\Software\\chromedriver.exe");
              WebDriver driver=new ChromeDriver();
             
              //Maximize Browser Window
              driver.manage().window().maximize();
             
              //Enter URL in Browser
              driver.get("https://www.facebook.com/");

              
//Navigate to URL

                        driver.navigat().to("https://www.facebook.com/");

              //Refresh Browser Window
              driver.navigate().refresh();
             
              //Click browser Back Button
              driver.navigate().back();
             
              //Click Browser Forword button
              driver.navigate().forward();
             
              //Close Current brwoser window
              driver.close();
             
              //Close All open browser  window
              driver.quit();
      
       }
 }

  • Navigation Command in Selenium Webdriver 
Selenium web driver provides a list of browser command which is listed below -
  • Navigate To Command -driver.navigate.to();
  • Forward Command- driver.navigate.forward();
  • Back Command - driver.navigate.back();
  • Refresh Command -refresh();

Hope !!! The above Tutorial on "how to handle browsers in Selenium using java " is helpful for you ...

Team,
QA acharya

Tags: Handling browser in selenium, Navigation method in selenium, how to open , close , maximize , window in selenium , Navigation command in selenium , browser handling in selenium webdriver , selenium navigation command .

How to handle browser in selenium  


Post a Comment

0 Comments