Selenium WebDriver Interview Questions — part 1

Image for post
Image for post
Photo by Nik MacMillan on Unsplash

Question

Answer

Question

Answer

ResultsPage resultsPage = homePage.search(“java”);

Question

Answer

WebElement element = driver.findElement(locator);
List list = driver.findElements(locator);
WebElement element = list.get(0);
WebDriverWait wait = new WebDriverWait(driver, 30);WebElement list = wait.until(
ExpectedConditions.
visibilityOfElementLocatedBy(loc));
String getElementJS = “function getElement(){ “ +
“var t = document.getElementById(‘title’); return t; }; “ +
“return getElement()”;
WebElement title = (WebElement)jsExec.executeScript(getElementJS);

Question

Answer

String actualUrl = driver.getCurrentUrl();
String actualTitle = driver.getTitle();
assertEquals(actualUrl, expectedUrl, “url is incorrect!”);
assertEquals(actualTitle, expectedTitle, “title is incorrect!”);
WebDriverWait wait = new WebDriverWait(driver, 30);wait.until(ExpectedConditions.urlToBe(expectedUrl));
wait.until(ExpectedConditions.titleIs(expectedTitle));
wait.until(ExpectedConditions.and(
ExpectedConditions.urlToBe(expectedUrl), ExpectedConditions.titleIs(expectedTitle)));

Question

public void selectPage(int i) {
By l = getLc(i);
WebElement p = driver.findElement(l);
p.click();
}

Answer

public void selectPage(int i) {
if (i <= 0)
throw new IllegalArgumentException(“invalid index — “ + i);
By locator = getLocator(i); WebDriverWait wait = new WebDriverWait(driver, 30);

WebElement page = wait.until(
ExpectedConditions
.elementToBeClickable(locator));
page.click();
}

Question

try {
int i = n / 0;
int[] numbers = new int[] {1, 2, 3, 4, 5};
numbers[10] = 9;
}
catch (Exception e) {
console.out(e.getMessage());
}

Answer

try {
int i = n / 0;
int[] numbers = new int[] {1, 2, 3, 4, 5};
numbers[10] = 9;
}
catch (ArithmeticException e) {
throw new RuntimeException(“division to 0!”, e);
catch (IndexOutOfBoundsException e) {
throw new RuntimeException(“elements out of the array”, e);
}

Question

Answer

public class Popup {
private final static By TITLE_ID = By.id(“title”);
private final static By CANCEL_ID = By.id(“cancel”);

private WebDriver driver;
private WebDriverWait wait;

public Popup() {
this.driver = driver;
this.wait = new WebDriverWait(this.driver, 5);
}

public void cancel() {
if (isDisplayed()) {
WebElement cancelButton = this.wait.until(
ExpectedConditions.elementToBeClickable(CANCEL_ID));

cancelButton.click();
this.wait.until(ExpectedConditions.
invisibilityOfElementLocated(TITLE_ID));
}
}

public boolean isDisplayed() {
try {
WebElement title = this.wait.until(
ExpectedConditions.
visibilityOfElementLocatedBy(TITLE_ID));
return title.isDisplayed();
} catch (TimeoutException e) {
return false;
}
}
}
HomePage homePage = new HomePage(driver);
homePage.open();
homePage.popup().cancel();

Written by

Blogs about Selenium and Java at https://seleniumjava.com.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store