Selenium-WebDriver - Notes By ShariqSP
WebDriver Methods
WebDriver
WebDriver is a powerful tool for automating web browsers. It provides a programming interface for controlling and interacting with web browsers.
WebDriver is an interface in Selenium WebDriver, which allows you to write test scripts in various programming languages to automate web application testing. It provides methods for performing actions on web elements, navigating between pages, managing browser windows, and more.
WebDriver implementations include ChromeDriver, FirefoxDriver, EdgeDriver, SafariDriver, and others, which allow WebDriver to communicate with specific web browsers.
1. void get(String url)
This method will launch a new browser and opens the given URL in the browser instance.
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.youtube.com/");
2. getTitle()
This method is used to retrieve the title of the webpage the user is currently working on.
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
String title = driver.getTitle();
3. getWindowHandle()
It is used to handle a single window (i.e., the main window). It returns the browser window handle from the focused browser.
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
System.out.println(driver.getWindowHandle());
4. getWindowHandles()
It is used to handle multiple windows. It returns all handles from all opened browsers by Selenium WebDriver.
System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("C:/Users/Dell/Desktop/testing%20docs/Selenium_notes/TestPage.html");
try {
Thread.sleep(10000);
driver.findElement(By.xpath("//Button[@type='button']")).click();
Set<String> list = driver.getWindowHandles();
System.out.println(list);
driver.quit();
} catch (InterruptedException e) {
e.printStackTrace();
}
5. close()
This command is used to close the current browser window which is currently in focus.
driver.close();
6. quit()
This method will close all the browser windows which are currently opened and terminates the WebDriver session.
driver.quit();
7. getCurrentUrl()
This method fetches the string representing the current URL of the current web page.
String currentUrl = driver.getCurrentUrl();
8. getPageSource()
This method returns the source code of the current web page loaded on the current browser.
String pageSource = driver.getPageSource();
9. getSize()
This method gets the size of the current window.
System.out.println(driver.manage().window().getSize());
//get width
int width = driver.manage().window().getSize().getWidth();
//get height
int height = driver.manage().window().getSize().getHeight();
System.out.println(width + " " + height);
//set height and width
Dimension dimension = new Dimension(200, 300);
driver.manage().window().setSize(dimension);
10. getPosition()
This method gets the position of the current window, relative to the upper left corner of the screen.
//get position (point type)
System.out.println(driver.manage().window().getPosition());
//set position
Point point = new Point(20, 10);
driver.manage().window().setPosition(point);
System.out.println(driver.manage().window().getPosition());
//get position x
System.out.println(driver.manage().window().getPosition().getX());
//get position y
System.out.println(driver.manage().window().getPosition().getY());
11. maximize()
This method maximizes the current window if it is not already maximized.
driver.manage().window().maximize();
12. minimize()
This method minimizes the current window if it is not already minimized.
driver.manage().window().minimize();
13. fullscreen()
This method fullscreen the current window if it is not already fullscreen.
driver.manage().window().fullscreen();
14. to()
This method loads a new web page in the current browser window.
//navigate using string
driver.navigate().to("https://www.google.com");
//navigate using URL
URL url = new URL("https://www.yahoo.com");
driver.navigate().to(url);
15. back()
This method moves back one step in the browser's history stack.
driver.navigate().back();
16. forward()
This method moves forward one step in the browser's history stack.
driver.navigate().forward();
17. refresh()
This method reloads the current web page in the browser window.
driver.navigate().refresh();
18. switchTo().window(windowID)
This method switches to the desired window.
System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("C:/Users/Dell/Desktop/testing%20docs/Selenium_notes/TestPage.html");
driver.findElement(By.xpath("//Button[@type='button']")).click();
Thread.sleep(3000);
Set<String> list = driver.getWindowHandles();
for(String handle : list) {
driver.switchTo().window(handle);
System.out.println(driver.getTitle());
}
Interview Questions
- What is WebDriver?
- Explain the difference between findElement() and findElements() in WebDriver.
- What is the WebDriver protocol?
- What are the advantages of using WebDriver over Selenium IDE?
- What is the difference between driver.close() and driver.quit() in WebDriver?
- What is the use of Actions class in WebDriver?
- How do you handle alerts in WebDriver?
- What is the difference between implicit wait and explicit wait in WebDriver?
- What are the different types of locators supported by WebDriver?
- Explain Page Object Model (POM) and how it is implemented in WebDriver.
- How do you simulate keyboard events using WebDriver?
- What is the difference between click() and submit() methods in WebDriver?
- How do you handle frames in WebDriver?
- What is the use of WebElement interface in WebDriver?
- How do you perform drag and drop using WebDriver?
- What is WebDriver Wait in Selenium?
- How do you handle cookies in WebDriver?
- What is WebDriverEventListener in WebDriver?
- What is the use of getScreenshotAs() method in WebDriver?
- Explain the difference between close() and quit() methods of WebDriver.
Multiple Choice Questions (MCQs)
- Which of the following is not a WebDriver implementation?
- Selenium RC
- Selenium WebDriver
- W3C WebDriver
- Selenium IDE
- Which method is used to find a single element in WebDriver?
- findElement()
- findElementById()
- findElements()
- findElementByTag()
- How do you handle alerts in WebDriver?
- alert()
- switchTo().alert()
- handleAlert()
- acceptAlert()
- What is the purpose of implicit wait in WebDriver?
- To wait for a certain condition to occur before executing the next command.
- To specify the time WebDriver should wait when locating elements.
- To handle asynchronous execution of JavaScript.
- To wait for the page to load completely.
- Which interface is used to represent an HTML element in WebDriver?
- WebElement
- WebObject
- WebNode
- WebElementNode
- Which method is used to maximize the browser window in WebDriver?
- maximizeWindow()
- fullscreen()
- maximize()
- maximizeBrowser()
- How do you perform right-click action on an element using WebDriver?
- rightClick()
- contextClick()
- performRightClick()
- rightMouseClick()
- Which WebDriver method is used to clear text from an input field?
- clearText()
- clear()
- delete()
- removeText()
- How do you simulate keyboard events like Enter key press in WebDriver?
- pressKey()
- sendKeys()
- typeKey()
- sendKey()
- What is the purpose of gecko driver in Selenium WebDriver?
- To execute tests on Internet Explorer browser.
- To execute tests on Google Chrome browser.
- To execute tests on Mozilla Firefox browser.
- To execute tests on Safari browser.
- Which WebDriver method is used to switch to a frame by frame index?
- switchToFrameByIndex()
- switchTo().frame()
- selectFrameByIndex()
- frame().switchTo()
- How do you get the current URL of the webpage in WebDriver?
- getCurrentPageURL()
- getPageURL()
- getCurrentUrl()
- getUrl()
- What is the use of Actions class in WebDriver?
- To perform advanced user interactions like drag and drop, key press, etc.
- To perform basic navigation operations.
- To handle alerts and pop-ups.
- To manage cookies in WebDriver.
- How do you perform mouse hover action on an element using WebDriver?
- hoverOver()
- mouseHover()
- moveToElement()
- hover()
- What is the default timeout for implicit wait in WebDriver?
- 5 seconds
- 10 seconds
- 15 seconds
- 20 seconds