Handling Browser in Selenium

Handling Browser in Selenium WebDriver

• Selenium automates browsers. Primarily, it is for automating web applications for testing purposes, but not limited to just that, Web-based administration tasks can also be automated as well.

• Selenium Supports all popular browsers (Google Chrome, IE, Mozilla Firefox, Opera, Safar etc...) to create and execute Test cases.

• Test Cases / Test Scripts are same for all browsers, but Browser driver varies from one browser to another,

• If you want to create and execute a Test case using Firefox browser then create Frefox browser, if want to use Google Chrome browser then create Chrome browser.

• You create Test cases using any one the browsers(ex: Firefox) and execute them using all browsers(Ex: IE, Chrome, Firefox etc...).

Note: Mozilla Firefox is default browser in Selenium,

• By default, Firefox driver comes with selenium-serverstanalone.jar library added.

• For Chrome, IE, Safari, Opera, there are libraries that need to be instantiated externally.

Creating Mozill Firefox Browser Object
WebDriver driver = new FirefoxDriver();

Create Google Chrome Browser
System.setproperty("webdriver.chrome.driver", "driver .exe file path");
WebDriver driverName = new ChromeDriver();

Create Internet Explorer Browser
System.setproperty("webdriver.ie.driver", "driver .exe file path");
WebDriver driverName = new InternetExplorerDriver();

Handling Browser

Operations on Browser

•  Launch Browser
•  Navigate to specified URL
•  Return Current URL
•  Return Page Title
•  Return Window Handle
•  Close focused Browser
•  Close all browsers that opened by WebDriver during execution
-----------------------
•  Navigate to another URL
•  Navigate back to previous URL
•  Navigate forward
•  Refresh the Browser
•  Maximize the Browser window
--------------
•  minimize the Browser Window

Selenium WebDrivers Test Steps:
 

// Launch Browser
WebDriver driver = new FirefoxDriver();

// Navigate to specified URL

driver.get("https://www.google.co.in");

//Return Current Page Title 

String PageTitle = driver.getTitle();
System.out.println(PageTitle);

//Return Current URL 

String URL = driver.getCurrentUrl();
System.out.println(URL);

// Return Page Source 

String PageSource = driver.getPageSource();
System.out.println(PageSource);

//Return Browser Window Handle

String WindowHandle = driver.getWindowHandle();
System.out.println(WindowHandle);

// Navigate to specified URL

driver.navigate().to("https://login.yahoo.com/");

// Navigate back to previous URL

driver.navigate().back();

// Navigate one item forward in the Browser History 

driver.navigate().forward();

//Miximize the Browser Window

driver.manage().window().maximize();

// Browser Full Screen 

driver.manage().window().fullscreen();

// Refresh the Browser

driver.navigate().refresh();

// Close the Focused Browser

driver.close();

// Close all Browsers that opened by Selenium WebDriver during Execution 

driver.quit();

// Working with multiple Browsers 


WebDriver driver = new FirefoxDriver();
driver.get("file:///E:/HTMLExamples/LoginPage.html");
String parent = driver.getWindowHandle();
//System.out.println(parent);

driver.findElement(By.linkText("Sign In")).click();

Set <String> Handles = driver.getWindowHandles();
int BrowserCount = Handles.size();
System.out.println(BrowserCount);

for (String s1:Handles){
if (! s1.equals(parent)){
driver.switchTo().window(s1);
System.out.println(driver.getCurrentUrl());
}
}
driver.switchTo().window(parent);
System.out.println(driver.getCurrentUrl());
}
}
-----------------------------------------------------------------

0 comments:

Post a Comment