1. What are the WebDriver methods?
- get()
- getTitle()
- getCurrentURL()
- findElement()
- findElements()
- getWindowHandle()
- getWindowHandles()
- manage()
- close()
- quit()
- navigate()
- switchTo()
- getPageSource()
2. What is the use of driver.getPageSource() method?
It is used to get the source code of the page. It returns a String that contains the HTML source code of the page.
- It can be used for assertions
- It is used for data extraction
- It is used for security testing
- How our page web page is structured
- How our web elements are presented
3. How to get the title of the current page?
String title = driver.getTitle()
4. How to close a specific tab?
driver.close();
5. What is the difference between driver.close() and driver.quit() methods?
- driver. close(): close the current window. If there are multiple windows are opened it will close only the currently focused window
- driver.quit(): quit from the driver. Quit from the associated window as well.
6. What is the difference between findElement and findElements options?
findElement: this returns the first element which matches the given selecting mechanism. if it can’t find anything it returns NoSuchElementExecpetion.
findElements: this returns a list of an element that matches the given selecting mechanism. If there is nothing to find it returns an empty list
7. How can we launch different browsers in Selenium WebDriver?
System.setProperty("webdriver.gecko.driver", "path/to/geckodriver");
WebDriver driver = new FirefoxDriver();
Webdriver driver =. new ChromeDriver();
8. We need to test the responsiveness of the UAT in different browser screen sizes. Can this be done in Selenium?
- Yes.
- Use Dimension class in Selenium
int width = 600;
int height = 400;
Dimension dimension = new Dimension(int width, int height);
driver.manage().window().setSize(dimension);
2. window.resizeTo() method and capability commands can also be used to achieve the same result. Use windows.resizeTo() method along with Javascript executor.
((JavascriptExecutor)driver).executeScript(“window.resizeTo(600,400);”);
9. How to set a position?
Point point = new Point(300,200);
driver.manage().window().setPosition(point);
11.Write a code to launch the browser
public static void main(String[] args){
WebDriver driver;
System.setProperty("webdriver.chrome.driver","C:/Users/Driver/chromedriver");
driver = new ChromeDriver();
driver.get("url");
driver.close();
}
12. Write code to launch the browser based on user input
public static void launchBrowser(){
Scanner scanner = new Scanner(System.in);
WebDriver driver = null;
System.out.println("Enter browser name: ");
String browser = scanner.nextLine();
if(browser.equals("chrome")){
System.setProperty("webdriver.chrome.driver","C:/Users/Driver/chromedriver");
driver = new ChromeDriver();
driver.get("url");
}
else if(browser.equals("firefox")){
System.setProperty("webdriver.gecko.driver","C:/Users/Driver/firefoxdriver");
driver = new ChromeDriver();
driver.get("url");
}
else {
System.out.println("Wrong name name should be firefox or chrome");
}
scanner.close();
}
13. What is By class?
- It is provided by WebDriver API
- It provides static methods to locate elements
- id(), className(), name(), tagName(), XPath() etc…
14. How to get the current URL of a page?
driver.getCurrentUrl();
15. Explain the below line
WebDriver driver = new FirefoxDriver();
WebDriver is an interface. We are creating an object from type as WebDriver and instantiating the new object from the FirefoxDriver class. It has access to all the methods in the WebDriver interface. In the future, if the class is changed there is only one place we have to change
16. Why create a driver reference variable from the WebDriver interface?
So we can use it for creating instances from other classes like ChromeDriver, GecoDriver etc…
17. What is HtmlUnitDriver?
- HtmlUniDriver is the fastest web driver.
- Unlike other (Chrome, firefox etc…) drivers this is non-GUI.
- So it executes on headless mode.
- So the browser is not visible during the execution.
18.Why do we use headless drivers?
- speed and efficiency
- Resources efficiency
- Server side execution
- Reduce flakiness — graphical rendering of web pages causes issues
- It provides more stable and predictable test results
19. How can you visually investigate failures if use a headless driver?
- Logging and console output
- We can use Log4j or SL4J
- Add listeners to take screenshots when the test fails