Monday, 13 August 2018

WebDriver Browser Interaction Commands

Launching WebBrowser:
------------------------------
WD library consists of individual classes to interact with different browsers

Exp: Firefox:  FirefoxDriver()
        Chrome:   ChromeDriver()
        IE:   InternetExplorerDriver()

We have to create an "object" to the "browser class" to open a web browser

Syntax:
WebDriver obj=new BrowserDriver class();

Exp:
WebDriver driver=new FirefoxDriver();
It will launch Firefox browser in safe mode
Safe mode means, it will open browser with no history, no bookmarks, no add-ons...etc

Browser Interaction Commands:
---------------------------------------
1. get():
Used to open a specifed url
Syntax:
driver.get(url);

2. maximize():
Used to maximize browser
Syntax:
driver.manage().window().maximize();

3. getPageSource():
Used to read page source code
Syntax:
driver.getPageSource():

Ex: Write a script to open FB page, maximize the window,
read page source code and print source code

/*Launch FireFox Browser
          Open Facebook Page
  Maximize the browser window
  Read source code
  Print source code*/

//To launch Firefox Browser
WebDriver driver=new FirefoxDriver();
//To open Facebook Page
driver.get("https://facebook.com");
//To maximize browser window
driver.manage().window().maximize();
//To create a source code variable and Read source code
String pgSource=driver.getPageSource();
//To print source code in console
System.out.println(pgSource);
//Before running the code make sure all your browsers are closed

4. getTitle():
Used to read page title
Syntax:
driver.getTitle();

5. getCurrentUrl():
Used to read current page url
Syntax:
driver.getCurrentUrl();

6. close():
Used to close focused window in browser
Syntax:
driver.close():

Ex: Write a script to open FB, read page title and print, get page url and print,
and to close browser

/*Launch FireFox Browser
  Open Facebook Page
  Maximize the browser window
  Get page title
  Get page url
  Close browser*/

//To launch Firefox Browser
WebDriver driver=new FirefoxDriver();
//To open Facebook Page
driver.get("https://facebook.com");
//To maximize the browser window
driver.manage().window().maximize();
//To get page title
String pgTitle=driver.getTitle();
//To print page title in console
System.out.println(pgTitle);
//To get current url
String pgUrl=driver.getCurrentUrl();
//To print current url in console
System.out.println(pgUrl);
driver.close();


7. navigate().to():
Also used to open specified url
Syntax:
driver.navigate().to(url);

NOTE:   When we use driver.get(): it will wait until page is loaded, once page is loaded then only it will execute the next step
        When we use navigate().to(): it will NOT wait until the page is loaded, it will attempt to execute the next step


8. navigate().back():
Used to move back by one page in browser history
Syntax:
driver.navigate().back();


9. navigate.()forward():
Used to move forward by one page in browser history
Syntax:
driver.navigate().forward();


10. navigate().refresh():
Used to refresh the page
Syntax:
driver.navigate().refresh();

11. quit():
Used to close all the opened windows in a browser
Syntax:
driver.quit();

// to open FF browser
WebDriver driver= new FirefoxDriver();
//to open FB
driver.get("http://facebook.com");
//to maximize window
driver.manage().window().maximize();
//to open Google
driver.navigate().to("http://google.co.in");
//to move back
driver.navigate().back();
//to refresh the page
driver.navigate().refresh();
//to close browser
driver.quit();





No comments:

Post a Comment