YuWebdesign



Selenium and Appium Interview Questions

By YuwebDesign


Selectors/Locators/Identifiers

According to Selenium javadoc, there are are 11 (not 8!) Locators or Identifiers in Selenium WebDriver.

Read more about Selenium Locators/Selectors >>>

Out of all identifiers, Id, Classname, and Name are preferred if they are unique and stable (non-dynamic).

LinkText and Partial LinkText are used if a web element is a link.

But if no other identifier is available, we are left with either CSS Selector or XPath.

Dynamic Web Elements change their attributes dynamically:
e.g. Id or Classname, or text associated is changing.

Best possible way to identify a dynamic web element:

  1. First search for another element that is stable and unique within the web page.
  2. Then depending on the situation, we will use various methods, operators, or axes to reach to the desired element.

Methods
  1. text()
    Searches for elements matching the exact text.

    //a[text()=’Mobile & Accessories’]
    
  2. starts-with()
    Can be used to search for web elements matching the start of the text of the attributes passed.

    The text() method can also be used to match the starting of the text.

    //a[starts-with(text(),’Mobile’)]
    
  3. contains()
    often used when the value of any attribute changes dynamically.
    It can search an element with partial information.

    //input[contains(@type=’submit’)]
    
    //input[contains(@name=’email’)] or //input[contains(@name=’ema’)]
    

    text() method is often used as an argument to the contains() method in XPath.

    //a[contains(text(), ‘Mobile & Accessories’)]
    
Operators in xpath
  1. AND operator
    when applied to multiple attributes,
    identifies a web element only when all the attributes are pointing to that element.

    //input[@type=’text’ and @name=’uid’]
    
  2. OR operator
    when applied to multiple attributes,
    defines a web element when any (either) one of the attributes points to that element.

    //input[@type=’text’ or @name=’uid’]
    
Axes

Axes in xpath
points the xpath processor to the direction
in which it should navigate in the hierarchy of the html nodes.

Syntax:

//html_tag[@attribute=’value’]//axes::html_tag

Frequently used axes:

  1. ancestor
    contains ancestor of the context node
  2. parent
    contains the parent of the context node.
  3. child
    includes the child of the context node
  4. siblings
  5. preceding
    gets the elements which precede before the context node.
  6. following
    selects the elements which follow after the context node.
  7. preceding-sibling
    gets the sibling coming before the context node.
  8. following-sibling
    gets the sibling coming after the context node

  1. CSS selectors are often easier to read than XPath. Most front-end developers are more likely to be familiar with CSS selectors already.
  2. Support for CSS selectors is quite consistent across various modern web browsers, which is not the case with their XPath engines.
  3. CSS selectors work faster than XPath.

For various reasons, such as the ones outlined above, Selenium “best practices” advise the use of CSS selectors over XPath.

  1. Absolute XPaths
    1. Start with the root element. First tag of a web page is “html”
    2. “/” is used to access an immediate child of the parent tag.
    3. Example:
      html/body/table/tbody/tr[2]/td/input

      Navigates input within a table in the second row and first cell.

  2. Relative XPaths
    1. Start from anywhere on the page.
    2. “//” is used to access any child of the parent tag.
    3. Syntax:
      //htmlTagname[@attribute=’value’]
    4. Example:
      //input[@type=’text’]
      
      //xpath of a WebElement represented by tagname input 
      //and has an attribute type = ‘text’.
      
Absolute xpath vs. relative xpath
html/head/body/table/tbody/tr/th

//table/tbody/tr/th
  1. Both XPaths refer to the same element on a web page
  2. Absolute xpath is faster.
  3. Absolute xpath is more likely to break with any change made to the page:
    e.g. addition or deletion of some nodes in between.

    E.g., moving the table inside of a div element will stop the absolute XPath from being able to locate the th element.

  4. On the other hand, the relative XPath will still continue to work.

  1. “/”
    1. “/” is used to access the immediate child of a tag
    2. “/” is used in absolute xpath
    3. html/body/table/tbody/tr[2]/td/input

      example of absolute xpath, where we are navigating to input tag by accessing immediate children.

  2. “//”
    1. “//” is used access any child of a tag
    2. “//” is used in writing relative xpath
    3. //input[@type=’text’]

      example of relative xpath that represents first input tag with type=’text’

Immediate child:

  1. An immediate child in XPath is defined indicated using “/”:
    //div/p
  2. while in CSS, it is indicated using “>”:
    div > p

Descendant:

To find paragraph elements that are descendent to any div element
(paragraph element appears in the sub-tree rooted at the div element),
we can use:

  1. “//” in XPath:
    //div//p
  2. and just a whitespace in CSS:
    div p

WebElement sample = driver.findElement(By.xpath("//*[contains(text(), 'data')]"));
  1. It defines a variable “sample” of type WebElement,
  2. and uses an XPath search to initialize it
  3. with a reference to an element that contains the text value “data”.

Commands

command description
getTitle() gets the title of the page
getCurrentUrl() gets the current URL of the page
getPageSource() gets the HTML code of a page
getText() gets the text from a WebElement
getAttribute() gets the attribute value of a WebElement
getWindowHandle() returns a unique session id assigned to the currently active window
getWindowHandles() returns a set of session id’s assigned to all the windows opened

command description
navigate.to(URL) navigates to a particular URL
navigate.back() navigates back to a URL in browsing history
navigate.forward() navigates forward to a URL in browsing history
navigate.refresh() reloads the page

There are two methods to navigate to a particular url using selenium:

  1. get(url)
  2. navigate.to(url)
WebDriver driver = new ChromeDriver();
 
public void invokeBrowser(){
String url1 = "http://qatechhub.com";
String url2 = "http://www.facebook.com";
 
driver.get(url1);
driver.navigate().to(url2);
}

close() quit()
closes currently active window of the browser close all the windows in the session

deleteAllCookies() method is used to delete all the cookies from a browser session.

driver.manage.deleteAllCookies();

Interact with Web Elements

WebElement is an Interface.

  1. findElement() method
    returns a unique WebElement (first that matches the criteria)
  2. findElements() method
    returns a list of all the matching WebElements

In most of the calendars which use date picker, month and year are generally in form of a drop-down and the dates are in a table.

Sometimes directly using Xpath works and sometimes you have to iterate over this table to select a date from the calendar.

Alert Handling

Frequently used alert methods in Selenium:

  1. Accept an alert.
  2. Reject an alert.
  3. Get message from an alert.
public void alertHandling(){
 
Alert alert = driver.switchTo().alert();
 
// To get message from an Alert
String message = alert.getText();
System.out.println("To get the message from an alert : "+ message);
 
//To accept an Alert
alert.accept();
 
//To reject an Alert
alert.dismiss(); 
}

Explain how you will login into any site if it is showing any authentication popup for username and password?

  1. Since there will be popup for logging in, we need to use Explicit wait to verify that alert is actually present.
  2. Only if the alert is present, we will pass the username and password credentials.
WebDriverWait wait = new WebDriverWait(driver, 10); 
Alert alert = wait.until(ExpectedConditions.alertIsPresent()); 
alert.authenticateUsing(new UserAndPassword(**username**, **password**));

Window Handling

  1. getWindowHandle() method
    returns a unique sessionId of the currently active window,
    which selenium maintains to identify a browser window session.
  2. getWindowHandles() method
    returns a set of unique sessionIds of all the opened windows.

When a click operation on link results in opening up of a new window (or new tab), Selenium does not automatically switch to the new window (or new tab).

For changing the WebDriver’s focus/ reference to the new window we need to use the switchTo() command. ‘windowName’ is the name of the window you want to switch your reference to.

driver.switchTo().window();

In case you do not know the name of the window, then you can use the driver.getWindowHandle() command to get the name of all the windows that were initiated by the WebDriver.

//get the parent window
String handle = driver.getWindowHandle();
//get children
for (String handle : driver.getWindowHandles()){
driver.switchTo().window(handle);
}

Note that it will not return the window names of browser windows which are not initiated by your WebDriver.
Once you have the name of the window, then you can use an enhanced for loop to switch to that window.

Scenario:

  1. Save the sessionId or window handle of Parent window.
  2. Click on the click which performs opening up of a new window operation.
  3. Get the sessionId of Child Window.
  4. Switch to the child window.
// Defining variables to store session id of parent window and child window
String sParentWindow, sChildWindow;
 
// Getting session id of parent window
sParentWindow = Driver.getWindowHandle();
System.out.println("Session Id of Parent window "+ sParentWindow);
 
//Clicking on a button or link which results in opening up of a new window
Driver.findElement(By.tagName("button")).click();
 
//Getting session Id of child window
sChildWindow = Driver.getWindowHandles().toArray()[1].toString();
System.out.println("Session id of Child Window "+ sChildWindow);
 
//Switching to Child Window
Driver.switchTo().window(sChildWindow);
System.out.println("Title of Child Window is : "+ Driver.getTitle());
Driver.close();
 
//Switching back to parent window
Driver.switchTo().window(sParentWindow);

  1. setSize(dim) method is used to set the size of a browser window.
    Dimension dim = new Dimension(500, 500);
    driver.manage().window().setSize(dim);
    
  2. window.resizeTo()
    ((JavascriptExecutor)driver).executeScript("window.resizeTo(1024, 768);");
    

When testing a web application with a responsive layout
you’ll want to verify that it renders the page correctly
in the common resolutions
your users use.

Historically this type of verification has been done manually
at the end of a development workflow —
which tends to lead to delays and visual defects
getting released into production.

We can easily sidestep these concerns
by automating responsive layout testing
so we can get feedback fast.

This can be done with a Selenium.

Read more about setting browser window size using Selenium >>>

Frames

Switching to the frame

In order to manipulate frame and its contents you must switch to it first.

This is similar to how you have to switch to a different page before you can interact with it.

driver.switchTo().frame(index);

where index is the zero-based index of the frame.

Switching the frame directs all further interactions through the driver towards the selected frame.

The frame method also works with name, element ID and reference to already located elements.

Switching back from the frame

defaultContent method can be used to switch back from a frame to the default/parent window:

driver.switchTo().defaultContent();

Which technique should you consider using throughout the script “if there is neither frame id nor frame name”?

  1. If neither frame name nor frame id is available, then we can access the frame by index.
  2. Let’s say, that there are 3 frames in a web page and if none of them have frame name or frame id, then we can use frame index attribute (zero-based).
  3. Each frame will have an index number starting with the “0”.
    (The first frame would be at index “0”, the second at index “1” and the third at index “2”.)
  4. Once the frame has been selected, all subsequent calls on the WebDriver interface will be made to that frame.
driver.switchTo().frame(int arg0);

To get the number of frames, we will call a method called findElements which will return all the frames elements in a list, and then find its size.

public void getNumberOfFrames(){
 
int numberOfFrames = driver.findElements(By.tagName("iframe")).size();
 
System.out.println("Number of frames on a page: "+ numberOfFrames);
}

Links

Text

element.getCssProperty(property)

  1. Returns the value of the css property you will pass as an argument.
  2. For color property, pass the property as color.
Driver.findElement(By.id("idName")).getCssValue("color");

getText() method is used to get the text from a WebElement.

WebElement eLogin = driver.findElement(By.name(“Login”);
String LoginText = Login.getText ();

clear() method is used to clear a textbox.

driver.findElement(By.xpath("//input[@type='text']")).clear();

driver.findElement(By.xpath(“xpath_textbox”)).getSize().getWidth();
driver.findElement(By.xpath(“xpath_textbox”)).getSize().getHeight();

Interact with Keyboard Keys

This can be done by simulating key presses on the focused element.

  1. One way is to perform “actions” on the web driver object:
    new Actions(webDriver).sendKeys(“some text”).perform();
  2. An alternative way is to switch to the active element first, and send keys to it directly:
    webDriver.switchTo().activeElement().sendKeys(“some text”);

public void searchitem(){
// Define a textbox as a Web Element with any one of the Identifiers, not necessarily xPath
WebElement textbox = Driver.findElement(By.xpath("//input[@type='text']"));
 
//To Pass tab key
textbox.sendKeys(Keys.TAB);
 
//To Pass Enter key
textbox.sendKeys(Keys.ENTER);
}

When we generally use ALT/SHIFT/CONTROL keys, we hold onto those keys and click other buttons to achieve the special functionality.

So it is not enough just to specify keys.ALT or keys.SHIFT or keys.CONTROL functions.

For the purpose of holding onto these keys while subsequent keys are pressed, we need to define two more methods: keyDown(modifier_key) and keyUp(modifier_key)

  1. keyDown(keys.ALT or Keys.SHIFT or Keys.CONTROL)
    Purpose: Performs a modifier key press and does not release the modifier key. Subsequent interactions may assume it’s kept pressed.
  2. keyUp(keys.ALT or Keys.SHIFT or Keys.CONTROL)
    Purpose: Performs a key release.

With a combination of these two methods, we can capture the special function of a particular key.

public static void main(String[] args) 
{
String baseUrl = “https://www.facebook.com”;
WebDriver driver = new FirefoxDriver();
 
driver.get("baseUrl");
WebElement txtUserName = driver.findElement(By.id(“Email”);
 
Actions builder = new Actions(driver);
Action seriesOfActions = builder
 .moveToElement(txtUerName)
 .click()
 .keyDown(txtUserName, Keys.SHIFT)
 .sendKeys(txtUserName, “hello”)
 .keyUp(txtUserName, Keys.SHIFT)
 .doubleClick(txtUserName);
 .contextClick();
 .build();
seriesOfActions.perform();
}

To upload a file we can simply use the command element.send_keys(file path).

But there is a prerequisite before we upload the file:
We have to use the html tag: ‘input’ and attribute type should be ‘file’.

⟨input type="file" name="uploaded_file" size="50" class="className"⟩
element = driver.find_element_by_id(”uploaded_file")
element.send_keys("C:myfile.txt")

getAttribute()

We can fetch the attribute of an element by using the getAttribute() method.

WebElement eLogin = driver.findElement(By.name(“Login”);
String LoginClassName = eLogin.getAttribute("classname");

Looks for web page’s login button named ‘Login’.
Uses getAttribute() to retrieve a “classname” attribute value of that element
and stores it in string format in LoginClassName.

Checkboxes and Radio Buttons

  1. isSelected() method is used to verify if a checkbox or a radio button is checked.
  2. It’s a boolean operation that returns true if selected or false otherwise.
driver.findElement(locator).isSelected();

To verify the status of one checkbox:

  1. isSelected() method is used to verify if a checkbox or a radio button is checked.
  2. It’s a boolean operation that returns true if selected or false otherwise.

To verify the status of all the checkboxes:

  1. Get all the checkboxes in a list (by using findElements method)
  2. Iterate this list to get the status of each checkbox.
public void getStatus(){
List list = Driver.findElements(By.xpath("//input[@type='checkbox']"));
 
for(WebElement temp : list){
System.out.println(temp.isSelected());
}
}

Dropdown

Questions on dropdown are very common Selenium interview questions because of the technicality involved in writing the code.

To work with a dropdown menu in Selenium, we make a use of html ⟨select⟩ tag.
Each select tag wraps the options it consists of.
Each of these options has a ‘value’ attribute assigned to them.

HTML Dropdown Syntax:

⟨select id="dropdownExample"⟩
⟨option value="option1"⟩Option 1 Value⟨/option⟩
⟨option value="option2"⟩Option 2 Value⟨/option⟩
⟨option value="option3"⟩Option 3 Value⟨/option⟩
⟨/select⟩

Select class from Selenium WebDriver is used to handle Dropdown.

//Identify the ‘select’ html element
WebElement mySelectElement = driver.findElement(By.id("dropdownExample"));
Select dropdown = new Select(dropdownExample);

//Selecting an option by the text that is visible
dropdown.selectByVisibleText(“Option 1 Value”); 
//Selecting, by choosing the Index number of that option
dropdown.selectByIndex(“1”); 
//Selecting, by choosing the value of that option
dropdown.selectByValue(“option1”);

Dropdown object of class Select has the following methods to work with on a Dropdown menu:

  1. selectByValue(“value”);
    Selects an option by value from a dropdown.
  2. selectByVisibleText(“text”);
    Selects an option by visible text from a dropdown.
  3. selectByIndex(index);
    Selects by index from the dropdown.
  4. deselectByValue(“value”);
    Deselects an option by value from a dropdown.
  5. deselectByVisibleText(“text”);
    Deselects an option by visible text from a dropdown.
  6. deselectByIndex(index);
    Deselects an option by index from a dropdown.
  7. deselectAll();
    Deselects All options in a dropdown.
  8. isMultiple();
    Boolean operation which verifies whether a dropdown allows multiple selection or not.
  9. getOptions();
    Returns a list of all options from a dropdown.
  10. getFirstSelectedOption();
    Returns first option selected in a dropdown as a WebElement.
  11. getAllSelectedOptions();
    Returns a list of all selected options in a dropdown.

Element Visibility and Interactivity

isEnabled() method is used to verify whether a WebElement is grayed out or not.

driver.findElement(locator).isEnabled();

isDisplayed() is a boolean method which returns whether a WebElement is visible on a page or not.

driver.findElement().isDisplayed();

Actions

  1. We can handle special keyboard and mouse events by using Advanced User Interactions API.
  2. It contains Actions and Action Classes needed for executing these events.

Most commonly used keyboard and mouse events:

command description
clickAndHold() Clicks (without releasing) the current mouse location.
dragAndDrop() Performs click-and-hold at the location of the source element, moves.
source, target() Moves to the location of the target element, then releases the mouse.

  1. Drag and drop operation means to grab an object and move it to some other location.
  2. Selenium WebDriver provides Actions class to perform drag and drop operation.
Actions action = new Actions(driver);
action.dragAndDrop(sourceElement, destinationElement).build.perform();

  1. Actions class of Selenium WebDriver has method “contextClick()”.
  2. contextClick() method is used to perform right click event of Mouse Operation.
Actions action = new Actions(driver);
action.contextClick(element).build.perform();

Actions class from Selenium WebDriver has a method doubleClick() which is used to perform double click operation.

Actions action = new Actions(driver);
action.doubleClick(element).build.perform();

JavaScript Executor

  1. JavaScriptExecutor is an interface which provides a mechanism to execute Javascript through the Selenium WebDriver.
  2. The script fragment provided will be executed as the body of an anonymous function.
  3. Within the script, use document to refer to the current document.
  4. Note that local variables will not be available once the script has finished executing, though global variables will persist.
Methods

JavaScriptExecutor provides the following methods:

  1. executeScript(java.lang.String script, java.lang.Object… args)

    Executes JavaScript in the context of the currently selected frame or window.

    JavascriptExecutor js = (JavascriptExecutor) driver; 
    js.executeScript(Script,Arguments);
    
  2. executeAsyncScript(java.lang.String script, java.lang.Object… args)
    Executes an asynchronous piece of JavaScript in the context of the currently selected frame or window.

    Example #1: Performing a sleep in the browser under test.

       long start = System.currentTimeMillis();
       ((JavascriptExecutor) driver).executeAsyncScript(
           "window.setTimeout(arguments[arguments.length - 1], 500);");
       System.out.println(
           "Elapsed time: " + System.currentTimeMillis() - start);
    

    Example #2: Synchronizing a test with an AJAX application:

       WebElement composeButton = driver.findElement(By.id("compose-button"));
       composeButton.click();
       ((JavascriptExecutor) driver).executeAsyncScript(
           "var callback = arguments[arguments.length - 1];" +
           "mailClient.getComposeWindowWidget().onload(callback);");
       driver.switchTo().frame("composeWidget");
       driver.findElement(By.id("to")).sendKeys("bog@example.com");
     

    Example #3: Injecting a XMLHttpRequest and waiting for the result:

       Object response = ((JavascriptExecutor) driver).executeAsyncScript(
           "var callback = arguments[arguments.length - 1];" +
           "var xhr = new XMLHttpRequest();" +
           "xhr.open('GET', '/resource/data.json', true);" +
           "xhr.onreadystatechange = function() {" +
           "  if (xhr.readyState == 4) {" +
           "    callback(xhr.responseText);" +
           "  }" +
           "};" +
           "xhr.send();");
       JsonObject json = new JsonParser().parse((String) response);
       assertEquals("cheese", json.get("food").getAsString());
    
Parameters
  1. script
    The JavaScript to execute
  2. args
    The arguments to the script. May be empty
Arguments
  1. Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above.
  2. An exception will be thrown if the arguments do not meet these criteria.
  3. The arguments will be made available to the JavaScript via the “arguments” magic variable, as if the function were called via “Function.apply”.
Return value

If the script has a return value (i.e. if the script contains a return statement), then the following steps will be taken:

  1. WebElement
    For an HTML element, this method returns a WebElement
  2. Double
    For a decimal, a Double is returned
  3. Long
    For a non-decimal number, a Long is returned
  4. Boolean
    For a boolean, a Boolean is returned
  5. String
    For all other cases, a String is returned.
  6. List
    For an array, return a
    List〈Object〉

    Supports nested lists.

  7. Map
    For a map, return a
    Map〈String, Object〉
  8. Null
    Unless the value is null or there is no return value, in which null is returned
Script execution failures
  1. Because of cross domain policies browsers enforce, your script execution may fail unexpectedly and without adequate error messaging.
  2. This is particularly pertinent when creating your own XHR request or when trying to access another frame.
  3. Most times when troubleshooting failure it’s best to view the browser’s console after executing the WebDriver request.

Read more about JavascriptExecutor in Selenium Javadoc >>>

Scroll down operation can be performed with the help of

  1. window.scrollTo() function and JavaScript Executor:
    public static void scrollPage(WebDriver oBrowser, int x, int y){
    String jsCommand;
    JavascriptExecutor oJSEngine;
    
    oJSEngine = (JavascriptExecutor) driver;
    jsCommand = String.format("window.scrollTo(%d, %d)", x,y);
    oJSEngine.executeScript(jsCommand);
    }
    
  2. window.scrollBy() function and JavaScript Executor
    ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
    

To scroll down to a particular element on a web page, we can use the function scrollIntoView().

((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);

public void highLightElement( ){

try {
WebElement oElement;
JavascriptExecutor oJsEngine;
String sJsCommand;
String sOldColour, sHighlightColour;
 
// Element to be highlighted
oElement = oDriver.findElement(By.xpath("//img[@id="add"]"));
 
// To get the previous color
sOldColour = oElement.getCssValue("backgroundColor");
 
oJsEngine = (JavascriptExecutor) oDriver;
 
//JavaScript command to change the color of an element to "yellow"
sJsCommand = String.format("arguments[0].style.backgroundColor=\"%s\";", "yellow");
 
//Executing JavaScript
oJsEngine.executeScript(sJsCommand, oElement);
 
// Wait for 5 seconds
Thread.sleep(5000);
 
//JavaScript command to change the color of an element to old color
sJsCommand = String.format("arguments[0].style.backgroundColor=\"%s\";", sOldColour);
oJsEngine.executeScript(sJsCommand, oElement);
 
} catch (Exception e) {
e.printStackTrace();
}
}

Yes. We can enter/ send text without using sendKeys() method. We can do it using JavaScriptExecutor.

document.getElementById will find the element using DOM (Document Object Model)
then we can send the text by value.

JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("document.getElementById(‘Login').value=Text without sendkeys");

Synchronization and Waits

This question may sound like: “How do you achieve synchronization in WebDriver?”

  1. Thread.Sleep()
    Read more >>>
  2. PageLoadTimeout
    Read more >>>
  3. implicitWait
    Read more >>>
  4. explicitWait
    Read more >>>

An interface for managing timeout behavior for WebDriver instances.

Methods
  1. implicitlyWait(long time, java.util.concurrent.TimeUnit unit)
    Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
    Read more >>>
  2. pageLoadTimeout(long time, java.util.concurrent.TimeUnit unit)
    Sets the amount of time to wait for a page load to complete before throwing an error.
    Read more >>>
  3. setScriptTimeout(long time, java.util.concurrent.TimeUnit unit)
    Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error.
    Read more >>>

Read more about Interface WebDriver.Timeouts in Selenium javadoc >>>

public class FluentWait
extends java.lang.Object
implements Wait
  1. An implementation of the Wait interface that may have its timeout and polling interval configured on the fly.
  2. Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition.
  3. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.
  4. This class makes no thread safety guarantees.
// Waiting 30 seconds for an element to be present on the page, checking
   // for its presence once every 5 seconds.
   Wait wait = new FluentWait(driver)
       .withTimeout(30, SECONDS)
       .pollingEvery(5, SECONDS)
       .ignoring(NoSuchElementException.class);

   WebElement foo = wait.until(new Function() {
     public WebElement apply(WebDriver driver) {
       return driver.findElement(By.id("foo"));
     }
   });

Read more in Selenium javadoc >>>

Class WebDriverWait:
java.lang.Object -> org.openqa.selenium.support.ui.FluentWait -> org.openqa.selenium.support.ui.WebDriverWait

All Implemented Interfaces: Wait⟨WebDriver⟩

public class WebDriverWait extends FluentWait

A specialization of FluentWait that uses WebDriver instances.

Constructor

public WebDriverWait(WebDriver driver,
long timeOutInSeconds,
long sleepInMillis)
Parameters:
driver – The WebDriver instance to pass to the expected conditions
timeOutInSeconds – The timeout in seconds when an expectation is called
sleepInMillis – The duration in milliseconds to sleep between polls.

Wait will ignore instances of NotFoundException that are encountered (thrown) by default in the ‘until’ condition, and immediately propagate all others.

You can add more to the ignore list by calling ignoring(exceptions to add).

Read more in Selenium Javadoc >>>

Syntax:

WebDriver.Timeouts pageLoadTimeout(long time, java.util.concurrent.TimeUnit unit)
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

PageLoadTimeout

  1. Maximum time Selenium waits for a page to get load successfully on a browser.
  2. If the page takes more than this time, it will throw Timeout exception.
  3. If the timeout is negative, page loads can be indefinite.
Parameters
  1. time
    The timeout value.
  2. unit
    The unit of time.
Returns

A Timeouts interface.

Read more in Selenium javadoc >>>

WebDriver.Timeouts setScriptTimeout(long time, java.util.concurrent.TimeUnit unit)
  1. Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error.
  2. If the timeout is negative, then the script will be allowed to run indefinitely.
Parameters
  1. time
    The timeout value.
  2. unit
    The unit of time.
Returns

A self reference.

Read more in Selenium javadoc >>>

Thread.Sleep()

Although Thread.Sleep() is a form of explicit wait
where the thread stalls for a specific duration of time,
it may not be the most reliable way to wait for a page to load.

E.g., rather than using something like:

Thread.Sleep(30000);

where the thread sleeps for 30 seconds (30,000 milliseconds),
the following can be used:

WebDriverWait wait= new WebDriverWait(webDriver,30);
wait.until(ExpectedConditions.visibilityOf(h1));

where Selenium waits until the given element is visible on the page,
or throws TimeoutException after waiting for 30 seconds.

Class ImplicitlyWait:
java.lang.Object -> org.openqa.selenium.remote.server.handler.WebDriverHandler ->
org.openqa.selenium.remote.server.handler.ImplicitlyWait

public class ImplicitlyWait
extends WebDriverHandler
implements JsonParametersAware

Syntax:

WebDriver.Timeouts implicitlyWait(long time, java.util.concurrent.TimeUnit unit)

Example:

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

Implicit Wait

  1. Can be considered as element detection timeout.
    Specifies the amount of time the driver should wait when searching for an element if it is not immediately present.
  2. When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing a NoSuchElementException.
  3. When searching for multiple elements, this wait will be set for all the web elements on a page.
  4. The driver should poll the page until at least one element has been found or this timeout has expired.
  5. Once you have declared implicit wait, it will be available for the entire life of the WebDriver instance.
    By default, the value will be 0.
    If you set a longer default, then the behavior will poll the DOM on a periodic basis depending on the browser/ driver implementation.
  6. This is the maximum time selenium will wait for to interact with that web element, before throwing “Element not found exception”.
  7. Increasing the implicit wait timeout should be used judiciously as it will have an adverse effect on test run time, especially when used with slower location strategies like XPath.
Parameters
  1. time
    The amount of time to wait.
  2. unit
    The unit of measure for time.
Returns

A self reference.

Read more in Selenium Javadoc >>>

Explicit Wait
  1. Applied to a particular Web Element.
  2. Сan be considered as conditional wait
    – it stalls until some specific condition is met.
  3. There are many conditions which can be applied using explicit wait.
    1. elementToBeClickable
    2. elementToBeSelected
    3. presenceOfElementLocated

    Read more about ExpectedConditions in Selenium Javadoc >>>

Explicit Wait vs. Implicit Wait

When a web element takes more than expected time to appear on the page,
instead of increasing Implicit wait for a particular Web Element
we can apply Explicit wait to that element with a condition.

public void waitTillElementVisible(){
 
WebDriverWait wait = new WebDriverWait(driver, 10);
 
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//input[@type='text']")));
}

Explicit wait can be used to apply conditional wait (here, condition is visibility of an element on a page).

public void waitTillElementVisible(){

WebDriverWait wait = new WebDriverWait(Driver, 90);

wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//input[@type='text']")));
}

or

WebDriverWait wait=new WebDriverWait(driver, 20);
Element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath( “

Waiting for an alert to appear on a page can be performed using explicit wait in Selenium WebDriver.

public void waitForAnAlert(){
 
WebDriverWait wait = new WebDriverWait(Driver, 90);
wait.until(ExpectedConditions.alertIsPresent()); 
}

To apply a conditional wait on change of property/attribute (in this case, color) of a WebElement explicit wait can be used.

public void waitTillColourChange(){
 
WebDriverWait wait = new WebDriverWait(Driver, 90);
 
wait.until(ExpectedConditions.attributeContains(By.linkText("advertisement"), "color", "blue")); 
}

If you have ten elements on a page, and you have defined implicit wait as 10 sec.
How much minimum and maximum time will your script take to interact with all the Web Elements?

  1. Minimum time
    will be almost zero seconds – if all these 10 web elements are already available, then selenium will interact with all of them in no time.
  2. Maximum time
    will be (10 * 10 = 100) seconds – if each element appears on the page at the 10th second, then the max time will be 100 seconds (10 sec for each web element).

Browsers and Drivers

  1. FirefoxDriver is a class in Selenium WebDriver.
  2. FirefoxDriver implements an Interface called WebDriver.

Launching Internet Explorer Driver:

System.setProperty("webdriver.ie.driver", "");
InternetExplorerDriver IEDriver = new InternetExplorerDriver();

Launching Chrome Driver:

System.setProperty("webdriver.chrome.driver", "");
ChromeDriver ChDriver = new ChromeDriver();

  1. Headless drivers are typically used in continuous integration (CI) setups.
  2. Headless drivers, such as PhantomJS, provide all standard web browser functionalities, but run in the command-line.
  3. These drivers are based on command-line tools and don’t produce screen output, making them ideal for completely automated setups.
  4. To be able to visually investigate test failures, the developer needs to implement mechanisms to capture screenshots, otherwise rely on command line output.

  1. HTML UnitDriver, PhantomJS are two famous headless browsers.
  2. Chrome also provides a support of headless browser testing.

Selenium provides a PROXY class to redirect browsing from a proxy.

String PROXY = “199.201.125.147:8080”;
 
org.openqa.selenium.Proxy proxy = new.org.openqa.selenium.Proxy();
proxy.setHTTPProxy(Proxy)
 .setFtpProxy(Proxy)
 .setSslProxy(Proxy)
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);

Screenshots

To take a screenshot in Selenium there is a method called getScreenshotAs() from a class called TakesScreenshot.

getScreenshotAs() method allows you to save that screenshot.

File scrFile = ((TakeScreenshot)driver).getScreenshotAs(outputType.FILE);
public void takeSnapshot(String sImageFilename){

try {
TakesScreenshot camera;
File tmpFile, imageFile;

imageFile = new File(sImageFilename);

if( new File(sImageFilename).exists()){
throw new Exception("File already exists..");
}

camera= (TakesScreenshot) driver;

tmpFile = camera.getScreenshotAs(OutputType.FILE);

FileUtils.copyFile(tmpFile, oImageFile);

} catch (Exception e) {
e.printStackTrace();
}
}

Tool Integration with Selenium

Selenium can only automate the Web-based application, but sometimes you may require interacting with Windows-based applications as well.

In a such a scenario we have to use some third-party tool like Sikuli, which is an open source tool.

This tool works with image recognition mechanism. It interacts with web elements through images.

Main reasons for using Excel sheets
  1. Excel sheets can be used as data source for tests.
  2. An excel sheet can also be used to store the data set while performing DataDriven Testing.
Excel sheet as data source can store the following
  1. Application URL for all environments:
    You can specify the URL of the environment in which you want to do the testing, e.g.,

    1. development environment
    2. testing environment
    3. QA environment
    4. staging environment
    5. production/ pre-production environment
  2. User name and password credentials of different environments:
    1. You can store the access credentials of the different applications/ environments in the excel sheet.
    2. You can store them in encoded format and whenever you want to use them, you can decode them instead of leaving it in plain text and unprotected.
  3. Test cases to be executed:
    1. You can list down the entire set of test cases in one column
    2. in the next column, you can specify either Yes or No
      to indicate if you want that particular test case to be executed or ignored.
Excel sheet for Data Driven Test

When you use the excel sheet for DataDriven Test,
you can store the data for different iterations to be performed in the tests.

E.g. while testing a web page, different sets of input data that need to be passed to the test box can be stored in the excel sheet.

  1. POI apache jar file is used to read from an excel sheet.
  2. There are many other open sources and paid tools available in the market like JXL, but Apache POI API is most widely used.

  1. Continuous Integration

    In the application on every code commit by a developer a build is created automatically.
    An event is triggered which builds the code of your application
    (tools like Maven for Java, MSBuild for C Sharp etc) are used

    Unit tests are run,
    code metrics are calculated,
    artifacts are created
    and the application is deployed in the test-like environment.

  2. Continuous delivery

    Continuous delivery does automation and integration testing on every build
    to catch early issues in the build.

    Code is deployed on multiple environments for doing different kind of testing like

    • Integration Testing,
    • Manual Testing,
    • Functional Testing,
    • Performance testing,
    • Security testing
    • and another level testing which your organization support.
  3. Continuous Deployment

    Extends continuous delivery,
    after completion of automated testing the code is deployed to the production.

Benefits
  1. The goal of continuous integration is that software is in working state all the time.
  2. One of the key reasons for using this type of implementation process is that it reduces the time it takes to deliver the product in the market.
  3. Another reason for using continuous integration, delivery and deployment is that it increases quality as inspection happens quickly instead of doing it in the end.
  4. It also shortens the lead time for changes, in other words, it reduces the task in progress and study say a large number of tasks in progress is dangerous for quality.
Types of testing which can be performed
  1. Unit Testing – Unit tests can be performed just after the build is formed.
  2. Code Hygiene – to check the code formatting and banned function checks.
  3. Integration Testing – In this testing test cases are performed in an integrated environment.
  4. TDD (Test Driven Development),
    BDD (Behavior-driven development)
    and Acceptance test driven development
    are some of the functional test cases which can be performed.
  5. Performance Testing
  6. Security Testing
Different tools used at various stages of CD
  1. Version Control – version control tool is used to maintain the versions of a build.
  2. CD pipeline tool – Jenkins, Go CD, VSTS and Bamboo are some important tools in this area.
  3. Build tools – depends on the programming language – maven is famous with Java, TFS is famous with dotNet or C#.
  4. Unit Testing tools – again depending on the programming language, JUnit is used for Java, NUnit is for C# etc.
  5. Integration Testing – Robot framework, Cucumber along with Selenium are famous in this region.
  6. Performance Testing – JMeter and Apache test are famous open source tools.

AutoIT is a tool which is used to automate windows based application.

In some scenarios like downloading and uploading an image, there is a requirement to integrate something which can interact with windows based application.

Logging is one of the essential features of any framework (project).
For logging feature, there are many third-party tools available:

  1. log4j.jar file
    Supports logs at various levels – info, error, debug, exception, etc.
  2. Extent Report
    Provides features like generating HTML report, logs and you can also attach screenshots.

Exceptions

Class SeleniumException:
public class SeleniumException extends java.lang.RuntimeException
Thrown when a Selenium command fails.
Constructor: SeleniumException(java.lang.String message, java.lang.Exception e)
-> java.lang.Object
-> java.lang.Throwable
-> java.lang.Exception
-> java.lang.RuntimeException
-> com.thoughtworks.selenium.SeleniumException

Class WebDriverException:
public class WebDriverException extends java.lang.RuntimeException
Constructor: public WebDriverException(java.lang.String message, java.lang.Throwable cause)
-> java.lang.Object
-> java.lang.Throwable
-> java.lang.Exception
-> java.lang.RuntimeException
-> org.openqa.selenium.WebDriverException

WebDriverException direct known Subclasses

All the Exception classes are inherited by WebDriverException class.

  1. ConnectionClosedException
    Thrown when the connection to the SafariDriver is lost.
  2. ErrorHandler.UnknownServerException
    Used as a place holder if the server returns an error without a stack trace.
  3. ImeActivationFailedException
    Indicates that activating an IME engine has failed.
  4. ImeNotAvailableException
    Indicates that IME support is not available.
    Thrown for every IME-related method call if IME support is not available on the machine.
  5. InvalidArgumentException
  6. InvalidCookieDomainException
    Thrown when attempting to add a cookie under a different domain than the current URL.
  7. InvalidCoordinatesException
    Indicates that the coordinates provided to an interactions operation are invalid.
    This, most likely, means that a move operation was provided with invalid coordinates or that an action that depends on mouse position (like click) was not preceded by a move operation.
  8. InvalidElementStateException
    Indicates that a WebElement is in a state that means actions cannot be performed with it.
    Examples would include an element being obscured by another when clicking, or perhaps not being visible on the DOM.
    Subclasses of this provide more detailed information >>>.
  9. JavascriptException
  10. JsonException
  11. MoveTargetOutOfBoundsException
    Indicates that the target provided to the actions move() method is invalid - outside of the size of the window.
  12. NoSuchSessionException
    Thrown by any command being called after WebDriver.quit().
  13. NotFoundException (Read more >>>)
  14. ScreenshotException
  15. ScriptTimeoutException
    Thrown when an async execute script command does not complete in enough time.
  16. SessionNotCreatedException
    Indicates that a session could not be created.
  17. StaleElementReferenceException (Read more >>>)
    Indicates that a reference to an element is now "stale" --- the element no longer appears on the DOM of the page.
  18. TimeoutException
    Thrown when a command does not complete in enough time.
  19. UnableToCreateProfileException
  20. UnableToSetCookieException
    Thrown when a driver fails to set a cookie.
  21. UnexpectedTagNameException
  22. UnhandledAlertException
  23. UnreachableBrowserException
    Indicates there was a problem communicating with the browser being controlled or the Selenium server. The most common causes for this exception are:

    1. The provided server address to RemoteWebDriver is invalid, so the connection could not be established.
    2. The browser has died mid-test.
  24. UnsupportedCommandException
    Used to indicate that a command used by the remote webdriver is unsupported.

Read more about exceptions on Selenium website >>>

  1. NotFoundException (Read more >>>)
  2. InvalidElementStateException (Read more >>>)

    Includes two subclasses:

    1. ElementNotInteractableException
      • (with subclass ElementNotVisibleException)
    2. ElementNotSelectableException

public InvalidElementStateException(java.lang.String message, java.lang.Throwable cause)

Inheritance of a Class InvalidElementStateException

java.lang.Object
-> java.lang.Throwable
-> java.lang.Exception
-> java.lang.RuntimeException
-> org.openqa.selenium.WebDriverException
-> org.openqa.selenium.InvalidElementStateException

Indicates that a WebElement is in a state that means actions cannot be performed with it.

Examples would include an element being obscured by another when clicking, or perhaps not being visible on the DOM.

Subclasses of this provide more detailed information.

Direct Known Subclasses
  1. ElementNotInteractableException
    This exception is thrown to indicate that although an element is present on the DOM, it is not in a state that can be interacted with.

    1. ElementNotVisibleException
      Subclass of ElementNotInteractableException
      This exception is thrown to indicate that although an element is present on the DOM, it is not visible, and so is not available for interaction.
  2. ElementNotSelectableException
    This exception is thrown to indicate that although an element is present on the DOM, it is not selectable, and so is not able to be interacted with.

public NotFoundException(java.lang.String message, java.lang.Throwable cause)

Inheritance of Class NotFoundException

-> java.lang.Object
-> java.lang.Throwable
-> java.lang.Exception
-> java.lang.RuntimeException
-> org.openqa.selenium.WebDriverException
-> org.openqa.selenium.NotFoundException

Direct Known Subclasses
  1. NoAlertPresentException
  2. NoSuchContextException
  3. NoSuchCookieException
  4. NoSuchElementException
  5. NoSuchFrameException
  6. NoSuchWindowException
Reasons

This exception is thrown when we are trying to interact with a WebElement (e.g., Frame, Window, Alert etc.) that is not found on the page.

  1. Synchronization/Timing issues
    Solution - implement wait
  2. Incorrect locator or type of locator
  3. Element is inside iframe
    Solution - switch to frame before locating the element.

public StaleElementReferenceException(java.lang.String message, java.lang.Throwable cause)

Inheritance of a Class StaleElementReferenceException

java.lang.Object
-> java.lang.Throwable
-> java.lang.Exception
-> java.lang.RuntimeException
-> org.openqa.selenium.WebDriverException
-> org.openqa.selenium.StaleElementReferenceException

Indicates that a reference to an element is now "stale" --- the element no longer appears on the DOM of the page.
The reason for this might be because the page or part of it has reloaded (e.g. due to an asynchronous ajax call) and the referenced element is no longer "fresh".

To avoid stale reference exception, do findBy right before performing action (e.g. click) on the element.

Exception test
exception that you expect will be thrown inside a test class.

Implementation:

  1. Write a test case in a way that it should throw an exception
  2. use the @Test annotation to specify which exception you will be expecting by mentioning it in the parameters.

Syntax: the exception is suffixed with .class

@Test(expectedException = NoSuchElementException.class)

Frameworks

framework description
Data Driven Framework When the entire test data is generated from an external file like Excel, CSV, XML or some database table, then it is called Data Driven framework.
Keyword Driven Framework When only the instructions and operations are written in a different file like an Excel worksheet, it is called Keyword Driven framework.
Hybrid Framework A combination of Data Driven and the Keyword Driven frameworks

Some of the file types of the dataset can be: excel, xml, text, csv, etc.

Here, they want to test that whether you have actually worked on some frameworks or not.
The Answer to this question will be the framework you have designed for your project.

Writing Data Driven, Hybrid Driven or customized framework using design patterns like POM or enhanced POM (Page Factory) is a trend.

The interviewer is expecting to know what kind of directory structure and class structure you are following in your project.
While explaining the answer to this question mention the reason for having the directory structure you are following.

The interviewer wants to test:

  1. whether you have used Object-oriented programming concepts
  2. design patterns
  3. configuration files
  4. Helper classes
  5. Utility classes
  6. How structured your code is?
  7. How modular your code is?
  8. How easily it can be maintained
  9. What logging functionality are you using?
  10. What reporting mechanism you are using in your project?

public static Properties getProperties(String sPropertiesFile){
		
		try {
			InputStream oFileReader;
			Properties oProperty;
			
			oFileReader = new FileInputStream(sPropertiesFile);
			oProperty = new Properties();
			
			oProperty.load(oFileReader);
			
			return oProperty;
			
			
		} catch (Exception e) {
			e.printStackTrace();
		//	log.debug(e.getMessage());
			return null;
		}

Design Patterns

A Design pattern is a generic solution to a common software design/architecture problem.

Implementation of these design patterns leads to inclusion of best practices and best solution, evolved over the time by others while working with similar problems.

  1. Most frequently used design pattern is POM (Page Object Model).
    In this type of design pattern, one class file is created for each page of the application under test. In each class, first, an object repository is created which will have all the WebElements of the respective page then in the constructor, all these WebElements are initialized.
  2. Enhanced POM, also known as Page Factory can be used which helps you in resolving stale element exception, also provides cache management.
  3. Both POM and Page Factory design patterns are inspired through Abstraction (i.e the implementation is hidden from other classes).
  4. After that, all the possible scenarios are created as methods in that class.

Page Object Model (POM)

Page Object Model is a Design Pattern
which has become popular in Selenium Test Automation.

Page object model (POM) can be used in any kind of framework
such as modular, data-driven, keyword driven, hybrid framework
etc.

Page Object model (POM)

  1. POM is a design pattern for creating an Object Repository of UI webElements.
  2. Each web page in the application is required to have it’s own corresponding Page Class.
  3. The Page Class is responsible for finding WebElements in that page and performing operations on those WebElements.

Page Object Model Design Pattern is an elegant way of implementing test routines that are:

  1. readable
  2. reduce code duplication
  3. easy to maintain and extend in the future.

POM Frameworks
Page object model (POM) can be used in any kind of
framework such as modular, data-driven, keyword driven, hybrid
framework etc.

Page Object Model Design Pattern

Page Object
is an object-oriented class that serves as an interface to a
page of Application Under Test(AUT).

POM essentially models the pages/screen of the application as objects called Page Objects.

Object Repository

  1. Each page will be defined as a java class.
  2. All the fields in the page will be defined in an
    interface as members.
  3. The class will then implement the interface.

Page Object Model Design Pattern

Web Pages:

  1. are represented as classes,
  2. and various page elements are defined as
    variables in the class.

    Variables = references to web page elements

  3. Methods implement behaviors.
    All the functions that can be performed in the specific page are encapsulated in the page object of that screen.
    In this way any change made in the UI will only affect that screens page object class thus abstracting the changes from the test classes.
clickLoginButton();
setCredentials(user_name, user_password);
submitLoginForm();

Tests
use Page Object Class methods
to interact with the User Interface (UI) of a web page.

Page Object Model Pattern Architecture

POM advantages
If page UI changes, the tests themselves don’t need to change,
only the code within the page object needs to change.

Subsequently, all changes to support new UI are located in
A single place.

Page Object Model Design Pattern in Selenium

Read more about POM Design Pattern on SeleniumHQ Official Site >>>

  1. Logical Code Hierarchy
    There is a clean separation between

    1. test code
    2. and page specific code (such as locators and layout).

    This allows us to separate operations and flows in the UI from verification.

  2. Easy readability of scripts
    Improved readability due to clean separation between test
    code and page specific code.

    Since the test scripts, functions and locators are in
    different classes it is easy to walk through the code.

  3. Code reusability
    Code can be written once and reused in many tests.

    Since the Object Repository is independent of Test Cases, multiple tests can use the same Object Repository.

  4. Redundancy elimination
    No duplicity of functions or locators.
  5. Easy maintenance
    Code structured this way is very easy to maintain.

    Code changes only in Page Object or Page
    Factory Classes when a UI change occurs.

  6. Reliability

    Better test coverage since the tests are written program
    wise.
    Performance of each test can be known.

Page Factory

  1. inbuilt Page Object Model concept for Selenium Web Driver
    The concept of separating the Page Object Repository and Test Methods is followed here also.
  2. Page Factory Class is an extension to the POM design pattern.

    Simply put, Page Factory Class is nothing but Object Repository.

    For each web page, it has its own Page Object.
    Within Page Object each Web Element should be

    1. uniquely identified
    2. and defined at class level.

    Read more >>>

  3. Page Factory can be used in any kind of framework such as
    1. Data Driven
    2. Modular
    3. Keyword Driven
  4. Page Factory Class is used to
    1. initialize the elements of the Page Object
      initElements method is used to initialize web elements from the Page Factory class
    2. instantiate the Page Objects itself.
Locators

Page Factory is used to initialize every WebElement variable
with a reference to a corresponding element on the actual web page
using configured “locators”.

Annotations

Instead of having to use ‘FindElements’, annotations, such as @FindBy
can be used (and recommended) to define web element to be able to perform actions on it.

@FindBy can accept tagName, partialLinkText, name, linkText, id, css, className & xpath as attributes.

@FindBy(how=How.NAME, using="username")
private WebElement user_name;

Page Object Model with Page Factory Project Structure

  1. Step 1: Creating TestBase.java class
    It goes into the "tests" package.

    Here we create an object of
    WebDriver, maximize browser, implementing waits,
    launching URL
    and etc.

  2. Step 2: Creating classes for each page
    (e.g., FacebookLoginPage.java, FacebookInboxPage.java)
    to hold element locators and their methods.

    It goes into the "pages" package.

    Usually, page objects are created for all available
    pages in the AUT.

    For each page, we create a separate class with a
    constructor.

    Identify all the locators and keep them in one class.

    It allows to

    1. reuse the locators in multiple methods.
    2. do easy maintenance: if there is any change in
      the UI, we can simply change one Page.
  3. Step 3: Creating Test based on above pages

    These go into the "tests" package.

    E.g., for FBLoginTest.java (Test Case 1) scripts run
    as follows:

    1. Launch browser and open facebook.com
    2. Enter user credentials and do signin
    3. Verify the loggedIn user name and do logout
  4. Step 4: Creating testng.xml file

    Page Factory is an enhanced way of achieving POM.
    When we say Page Factory is optimized, it refers to the fact that

  1. memory utilization is very good (through a cashing mechanism)
  2. implementation is done in an object oriented manner.

Page Object Model
is a way of representing an application in a test framework.

For every ‘page’ in the application, we create a Page Object to
reference the ‘page’.

Page Object is a class that

  1. represents a web page
  2. and holds the functionality and members.

Page Factory
is one way of implementing the Page Object Model.

Page Factory is a way to initialize Web Elements
you want to interact with within Page Object
when you create an instance of it.

page object model vs. page factory

Test Class
contains an actual Selenium test script,
where we call Page Action and mentioned actions to be
performed on Web Pages.

For each page, we can write our own test
class for better code readability.

We can write test cases in @Test annotation.

In Page Action Class, we can write all web
pages action per pages and functionality.

Under Page Action component, for each page in the application,
we have corresponding Page class.

Selenium Theoretical Questions

  1. Open Source, no license required.
  2. Customer support: Open source community only
  3. Requires very good coding skills
  4. Environment support: Windows, Linux, Mac
  5. Language support: multiple languages

Selenium v1

Selenium v1 included only three suites of tools:

  1. Selenium IDE
  2. Selenium RC
  3. Selenium Grid
  4. There was no WebDriver in Selenium v1.
Selenium v2
  1. Selenium WebDriver was introduced in Selenium v2.
  2. Selenium RC
    With the onset of WebDriver, Selenium RC got deprecated and is not in use since.
    Older versions of RC is available in the market though, but support for RC is not available.
Selenium v3

Currently, Selenium 3.141 is in use, and it comprises of

  1. IDE
    used for recording and playback of tests
  2. WebDriver
    used for testing dynamic web applications via a programming interface
  3. Grid
    used for deploying tests in remote host machines
Selenium v4

As of October 2019, Selenium 4 is in Alpha 2 and still not stable enough to use in testing.

Selenium Grid

Selenium Grid is the fourth component of Selenium suite.

It is used for:

  1. parallel testing
  2. distributed testing on the same system or on different systems.

Appium

Yes, there is a tool called Appium, a wrapper written over Selenium which can be used to automate Mobile applications.

Experience Interview Questions

Regression and Functional tests are generally automated using any automation tools because these type of tests are more stable and are less prone to change.

Also, as organizations are moving towards the Agile way of working, writing automation as an when functionality is being developed is a new trend.

Integrating selenium with tools like Cucumber or Specflow where we write BDD (Behavior-driven development) gives stakeholders confidence in development.

Synchronization
  1. One of the biggest challenges faced while working with Selenium is Synchronization.
  2. An application under test has its own speed of execution whereas the code written with selenium has its own.
  3. Keeping both in sync is a big task.
  4. Selenium provides us some wait statements (Page load Timeout, Implicit wait, Explicit wait and Fluent wait) which help us to improve synchronization.
Maintainability
  1. Another challenge which generally an automation engineer will face is Maintaining the code.
  2. If your application is more prone to change like html elements of the page changes, some new components are being added and so on, this may affect your code especially locators.
  3. To get rid of these situations we have certain design patterns like POM (Page Object Model), Page Factory.


Leave a Reply or Comment

Your email address will not be published. Required fields are marked *