By: Bijan Patel - In:: Selenium Java Interview Questions and Answers - Part 2
By: Bijan Patel - In:: Selenium Java Interview Questions and Answers - Part 2
By: Bijan Patel|In: Automation
– navigate().to();
-navigate().forward();
-navigate().back();
-navigate().refresh();
driver.getCurrentUrl();
3) How can we maximize browser window in Selenium WebDriver?
We can use the maximize() method to maximize browser window.
driver.manage().window().maximize();
4) How to delete cookies in Selenium?
We can use deleteAllCookies() method to delete cookies in selenium.
driver.manage().deleteAllCookies();
5) What are the different ways for refreshing the page using Selenium WebDriver?
Browser refresh operation can be performed using the following ways in Selenium:
– Refresh method
driver.manage().refresh();
– Get method
driver.get(“https://www.google.com”);
driver.get(driver.getCurrentUrl());
-Navigate method
driver.get(“https://www.google.com”);
driver.navigate.to(driver.getCurrentUrl());
-SendKeys method
driver. findElement(By.id("username")).sendKeys(Keys.F5);
6) What is the difference between driver.getWindowHandle() and driver.getWinowHandles() in
Selenium WebDriver and their return type?
driver.getWindowHandle() – To get the window handle of the current window. Returns a string of
alphanumeric window handle
driver.getWinowHandles() – To get the window handle of all current windows. Return a set of window
handles
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('displayed-text').value='text123'");
8) How can you find broken links in a page using Selenium WebDriver?
List<WebElement> elements = driver.findElements(By.tagName(“a”));
List finalList = new ArrayList();
for (WebElement element : elementList){
if(element.getAttribute("href") != null){
finalList.add(element);
}
}
return finalList;
9) How to find more than one web element in the list?
We can find more than one web element by using the findElements() method in Selenium.
In case, when selenium locators do not work you can use JavaScriptExecutor. You can use
JavaScriptExecutor to perform a desired operation on a web element.
-Allow us to separate operations and flows in the UI from verification – improves code readability
-Since the Object Repository is independent of test cases, multiple tests can use the same object
repository
-Reusability of code
@FindBy(id = “userName”)
WebElement txt_UserName;
OR
PageFactory.initElements(driver, Login.class);
17) What is the difference between Page Object Model and Page Factory?
Page Object Model is a design pattern to create an Object Repository for web UI elements. However,
Page Factory is a built-in class in Selenium for maintaining object repository.
-Allow us to separate operations and flows in the UI from verification – improves code readability
-Since the Object Repository is independent of test cases, multiple tests can use the same object
repository
-Re-usability of code
19) How can we use Recovery Scenario in Selenium WebDriver?
We can develop Recovery scenarios using exception handling i.e. By using “Try Catch Block” within your
Selenium WebDriver Java tests
driver.get(baseUrl);
WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));
// enter the file path onto the file-selection input field
uploadElement.sendKeys("C:\\newhtml.html");
21) How to download a file in Selenium WebDriver?
Step 1- Create a firefox Profile.
WebElement fr = driver.findElementById("theIframe");
driver.switchTo().frame(fr);
24) How to connect to a database in Selenium?
Connection con = DriverManager.getConnection(dbUrl,username,password);
25) How to resize browser window using Selenium WebDriver?
driver.manage().window().maximize();