WebDriver Interface

 1. What are the WebDriver methods?

  1. get()
  2. getTitle()
  3. getCurrentURL()
  4. findElement()
  5. findElements()
  6. getWindowHandle()
  7. getWindowHandles()
  8. manage()
  9. close()
  10. quit()
  11. navigate()
  12. switchTo()
  13. 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. 

  1. It can be used for assertions
  2. It is used for data extraction
  3. It is used for security testing
  4. How our page web page is structured
  5. 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?

  1. driver. close(): close the current window. If there are multiple windows are opened it will close only the currently focused window
  2. 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");

// Create a new instance of the FirefoxDriver
WebDriver driver = new FirefoxDriver();
// create a new instance of chrome driver
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?

  1. Yes.
  2. 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?

//WebDriver.Window interface profide setPosition(Point targetPosition)
//above method
//It Set the position of the current window
//It takes argument as object in Point class

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?

  1. It is provided by WebDriver API
  2. It provides static methods to locate elements
  3. 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?

  1. HtmlUniDriver is the fastest web driver. 
  2. Unlike other (Chrome, firefox etc…) drivers this is non-GUI.
  3.  So it executes on headless mode. 
  4. So the browser is not visible during the execution.

18.Why do we use headless drivers?

  1. speed and efficiency
  2. Resources efficiency
  3. Server side execution
  4. Reduce flakiness — graphical rendering of web pages causes issues
  5. It provides more stable and predictable test results

19. How can you visually investigate failures if use a headless driver?

  1. Logging and console output
  2. We can use Log4j or SL4J
  3. Add listeners to take screenshots when the test fails

No comments:

Post a Comment

Cucumber

What is Cucumber? Definition : Cucumber is an open-source tool that supports BDD . Purpose : Bridges the communication gap between business ...