selenium interview questions and answers code example

Example 1: selenium interview questions 2019

// Set up the JavaScript object
JavascriptExecutor jscript = (JavascriptExecutor) webdriver;
// Read the site title
String strTitle = (String)jscript.executeScript("return document.title");
System.out.println("Webpage Title: " + strTitle);

Example 2: selenium interview questions 2019

public class LogInPage
{
    private WebElement user;
    private WebElement pass;

    public LogInPage() {
    }

    public void findObjects() {
        user = browser.findElement(By.id("userName"));
        pass = browser.findElement(By.id("password"));
    }

    public void processLogIn() {
        user.sendKeys("john");
        pass.sendKeys("password");
    }
}

Example 3: selenium interview questions 2019

<suite name="SuperSuite">
  <suite-files>
    <suite-file path="subSuite1.xml" />
    <suite-file path="subSuite2.xml" />
    ...
  </suite-files>
</suite>

Example 4: selenium interview questions 2019

public class LogInPage
{
    @FindBy(id="userName")
    private WebElement user;

    @FindBy(id="password")
    private WebElement pass;

    public LogInPage() {
        PageFactory.initElements(browser, this); // Setup the members as browser.findElement()
    }

    public void processLogIn() {
        user.sendKeys("john");
        pass.sendKeys("password");
    }
}

Example 5: selenium interview questions

How to switch between multiple windows in Selenium?
Ans. Selenium has driver.getWindowHandles() and driver.switchTo().window({windowHandleName}) commands to work with multiple windows.

The getWindowHandles() command returns a list of ids corresponding to each window and on passing a particular window handle to the driver.switchTo().window({windowHandleName}) command, we can switch control/focus to that particular window.

for (String windowHandle : driver.getWindowHandles()) {
     driver.switchTo().window(handle);
}

Example 6: selenium interview questions 2019

Import org.apache.commons.io.FileUtils;
WebDriver ins = new ChromeDriver();
ins.get("http://www.google.com/");

File screen = ((TakesScreenshot)ins).getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere

FileUtils.copyFile(screen, new File("c:\tmp\myscreen.png"));