Automation Testing

Automation testing is a type of testing that involves automated test case execution using an automation tool.

The tester writes test scripts and then run the test scripts either on demand or schedule them for periodic run. This reduces the overall test execution time, thus helping in faster product releases.

  • Automation testing improves efficiency of testing.
  • Reduced testing efforts and costs.
  • Testing can be replicated across different platforms.
  • Gives accurate results.
  • Usually used for large applications with stringent deadlines.

Automation in preferred in following cases

  • Repetitive Tasks
  • Smoke and Sanity Tests
  • Test with multiple data set
  • Regression test cases

Selenium is a robust test automation suite that is used for automating web based applications. It supports multiple browsers, programming languages and platforms.

In the automation process, steps involved are

  • Selecting the Test tool
  • Define scope of automation
  • Planning, design and development
  • Test execution
  • Maintenance

Selenium comes in four forms-

  • Selenium WebDriver – Selenium WebDriver is used to automate web applications using browser’s native methods.
  • Selenium IDE – A firefox plugin that works on record and play back principle.
  • Selenium RC – Selenium Remote Control(RC) is officially deprecated by selenium and it used to work on javascript to automate the web applications.
  • Selenium Grid – Allows selenium tests to run in parallel across multiple machines.

Following are the advantages of Selenium-

  • Selenium is open source and free to use without any licensing cost.
  • It supports multiple languages like Java, ruby, python etc.
  • It supports multi browser testing.
  • It has good amount of resources and helping community over the internet.
  • Using selenium IDE component, non-programmers can also write automation scripts
  • Using selenium grid component, distributed testing can be carried out on remote machines possible.
  • We cannot test desktop application using Selenium.
  • We cannot test web services using Selenium.
  • For creating robust scripts in Selenium Webdriver, programming langauge knowledge is required.
  • We have to rely on external libraries and tools for performing tasks like – logging(log4J), testing framework-(testNG, JUnit), reading from external files(POI for excels) etc.

Besides Selenium, there is Load Runner, Sahi, Silk Test, QTP, Jmeter, WinRunner, etc.

The different locators in Selenium are-

  • Id
  • XPath
  • cssSelector
  • className
  • tagName
  • name
  • linkText
  • partialLinkText

Xpath or XML path is a query language for selecting nodes from XML documents. XPath is one of the locators supported by Selenium Webdriver.

An absolute XPath is a way of locating an element using an XML expression beginning from root node i.e. html node in case of web pages. The main disadvantage of absolute xpath is that even with slightest change in the UI or any element the whole absolute XPath fails.

Example - html/body/div/div[2]/div/div/div/div[1]/div/input

A relative XPath is a way of locating an element using an XML expression beginning from anywhere in the HTML document. There are different ways of creating relative XPaths which are used for creating robust XPaths (unaffected by changes in other UI elements).

Example - //input[@id='username']

There are two ways of navigating to the nth element using XPath-

Using square brackets with index position-

Example - div[2] will find the second div element.

Using position()-

Example - div[position()=3] will find the third div element. 

By .className we can select all the element belonging to a particluar class e.g. ‘.red’ will select all elements having class ‘red’.

By #idValue we can select all the element belonging to a particluar class e.g. ‘#userId’ will select the element having id – userId.

The fundamental difference between XPath and css selector is using XPaths we can traverse up in the document i.e. we can move to parent elements. Whereas using CSS selector we can only move downwards in th

By creating an instance of driver of a particular browser-WebDriver driver = new FirefoxDriver();

The Selenium WebDriver is used for automating tests for websites.

 Feature  Selenium IDE  Selenium RC  WebDriver
Browser CompatibilitySelenium IDE comes as a Firefox plugin, thus it supports only FirefoxSelenium RC supports a varied range of versions of Mozilla Firefox, Google Chrome, Internet Explorer and Opera.WebDriver supports a varied range of versions of Mozilla Firefox, Google Chrome, Internet Explorer and Opera.Also supports HtmlUnitDriver which is a GUI less or headless browser.
Record and PlaybackSelenium IDE supports record and playback featureSelenium RC doesn’t supports record and playback feature.WebDriver doesn’t support record and playback feature
Server RequirementSelenium IDE doesn’t require any server to be started before executing the test scriptsSelenium RC requires server to be started before executing the test scripts.WebDriver doesn’t require any server to be started before executing the test scripts
ArchitectureSelenium IDE is a Javascript based frameworkSelenium RC is a JavaScript based Framework.WebDriver uses the browser’s native compatibility to automation
Object OrientedSelenium IDE is not an object oriented toolSelenium RC is semi object oriented tool.WebDriver is a purely object oriented tool
Dynamic Finders(for locating web elements on a webpage)Selenium IDE doesn’t support dynamic findersSelenium RC doesn’t support dynamic finders.WebDriver supports dynamic finders
Handling Alerts, Navigations, DropdownsSelenium IDE doesn’t explicitly provides aids to handle alerts, navigations, dropdownsSelenium RC doesn’t explicitly provides aids to handle alerts, navigations, dropdowns.WebDriver offers a wide range of utilities and classes that helps in handling alerts, navigations, and dropdowns efficiently and effectively.
WAP (iPhone/Android) TestingSelenium IDE doesn’t support testing of iPhone/Andriod applicationsSelenium RC doesn’t support testing of iPhone/Android applications.WebDriver is designed in a way to efficiently support testing of iPhone/Android applications. The tool comes with a large range of drivers for WAP based testing.For example, AndroidDriver, iPhoneDriver
Listener SupportSelenium IDE doesn’t support listenersSelenium RC doesn’t support listeners.WebDriver supports the implementation of Listeners
SpeedSelenium IDE is fast as it is plugged in with the web-browser that launches the test. Thus, the IDE and browser communicates directlySelenium RC is slower than WebDriver as it doesn’t communicates directly with the browser; rather it sends selenese commands over to Selenium Core which in turn communicates with the browser.WebDriver communicates directly with the web browsers. Thus making it much faster.

Selenium IDE is the simplest and easiest of all the tools within the Selenium Package. Its record and playback feature makes it exceptionally easy to learn with minimal acquaintances to any programming language. Selenium IDE is an ideal tool for a naïve user.

Selenese is the language which is used to write test scripts in Selenium IDE.

Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.

Double Slash “//” – Double slash is used to create Xpath with relative path i.e. the xpath would be created to start selection from anywhere within the document.

Selenium Grid can be used to execute same or different test scripts on multiple platforms and browsers concurrently so as to achieve distributed test execution, testing under different environments and saving execution time remarkably.

The following syntax can be used to launch Browser:

WebDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();WebDriver driver = new InternetExplorerDriver();

The different drivers available in WebDriver are:

  • FirefoxDriver
  • InternetExplorerDriver
  • ChromeDriver
  • SafariDriver
  • OperaDriver
  • AndroidDriver
  • IPhoneDriver
  • HtmlUnitDriver

There are two types of waits available in WebDriver:

  1. Implicit Wait
  2. Explicit Wait

Implicit Wait: Implicit waits are used to provide a default waiting time (say 30 seconds) between each consecutive test step/command across the entire test script. Thus, the subsequent test step would only execute when the 30 seconds have elapsed after executing the previous test step/command.

Explicit Wait: Explicit waits are used to halt the execution till the time a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, explicit waits are applied for a particular instance only.

The user can use sendKeys(“String to be entered”) to enter the string in the textbox.

Syntax:

WebElement username = drv.findElement(By.id(“Email”));
// entering username
username.sendKeys(“sth”);

Get command is used to retrieve the inner text of the specified web element. The command doesn’t require any parameter but returns a string value. It is also one of the extensively used commands for verification of messages, labels, errors etc displayed on the web pages.

Syntax:

String Text = driver.findElement(By.id(“Text”)).getText();

The value in the dropdown can be selected using WebDriver’s Select class.

Syntax:

selectByValue:

Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));

selectByValue.selectByValue(“bluevalue”);

selectByVisibleText:

Select selectByVisibleText = new Select (driver.findElement(By.id(“SelectID_Two”)));

selectByVisibleText.selectByVisibleText(“Cyan”);

selectByIndex:

Select selectByIndex = new Select(driver.findElement(By.id(“SelectID_Three”)));

selectByIndex.selectByIndex(2);

Following are the navigation commands:

navigate().back() – The above command requires no parameters and takes back the user to the previous webpage in the web browser’s history.

Sample code:

driver.navigate().back();

navigate().forward() – This command lets the user to navigate to the next web page with reference to the browser’s history.

Sample code:

driver.navigate().forward();

navigate().refresh() – This command lets the user to refresh the current web page there by reloading all the web elements.

Sample code:

driver.navigate().refresh();

navigate().to() – This command lets the user to launch a new web browser window and navigate to the specified URL.

Sample code:

driver.navigate().to(“https://google.com”);

findElement(): findElement() is used to find the first element in the current web page matching to the specified locator value. Take a note that only first matching element would be fetched.

Syntax:

WebElement element = driver.findElements(By.xpath(“//div[@id='example']//ul//li”));

findElements(): findElements() is used to find all the elements in the current web page matching to the specified locator value. Take a note that all the matching elements would be fetched and stored in the list of WebElements.

Syntax:

List <WebElement> elementList = driver.findElements(By.xpath(“//div[@id='example']//ul//li”));

close(): WebDriver’s close() method closes the web browser window that the user is currently working on or we can also say the window that is being currently accessed by the WebDriver. The command neither requires any parameter nor does it return any value.

quit(): Unlike close() method, quit() method closes down all the windows that the program has opened. Same as close() method, the command neither requires any parameter nor does is return any value.

Junit is a unit testing framework introduced by Apache. Junit is based on Java.

Following are the JUnit Annotations:

  • @TestAnnotation lets the system know that the method annotated as @Test is a test method. There can be multiple test methods in a single test script.
  • @Before: Method annotated as @Before lets the system know that this method shall be executed every time before each of the test methods.
  • @After: Method annotated as @After lets the system know that this method shall be executed every time after each of the test method.
  • @BeforeClass: Method annotated as @BeforeClass lets the system know that this method shall be executed once before any of the test methods.
  • @AfterClass: Method annotated as @AfterClass lets the system know that this method shall be executed once after any of the test methods.
  • @Ignore: Method annotated as @Ignore lets the system know that this method shall not be executed.

A fluent wait is a type of wait in which we can also specify polling interval(intervals after which driver will try to find the element) along with the maximum timeout value.

The different keyboard operations that can be performed in selenium are-

  • .sendKeys(“sequence of characters”) – Used for passing character sequence to an input or textbox element.
  • .pressKey(“non-text keys”) – Used for keys like control, function keys etc that are non-text.
  • .releaseKey(“non-text keys”) – Used in conjuntion with keypress event to simulate releasing a key from keyboard event.

The different mouse events supported in selenium are

  • click(WebElement element)
  • doubleClick(WebElement element)
  • contextClick(WebElement element)
  • mouseDown(WebElement element)
  • mouseUp(WebElement element)
  • mouseMove(WebElement element)
  • mouseMove(WebElement element, long xOffset, long yOffset)

Using driver.getTitle(); we can fetch the page title in selenium. This method returns a string containing the title of the webpage.

Using driver.getPageSource(); we can fetch the page source in selenium. This method returns a string containing the page source.

Some of the commonly seen exception in selenium are-

  • NoSuchElementException – When no element could be located from the locator provided.
  • ElementNotVisibleException – When element is present in the dom but is not visible.
  • NoAlertPresentException – When we try to switch to an alert but the targetted alert is not present.
  • NoSuchFrameException – When we try to switch to a frame but the targetted frame is not present.
  • NoSuchWindowException – When we try to switch to a window but the targetted window is not present.
  • UnexpectedAlertPresentException – When an unexpected alert blocks normal interaction of the driver.
  • TimeoutException – When a command execution gets timeout.
  • InvalidElementStateException – When the state of an element is not appropriate for the desired action.
  • NoSuchAttributeException – When we are trying to fetch an attribute’s value but the attribute is not correct
  • WebDriverException – When there is some issue with driver instance preventing it from getting launched.
Using Select class-
Select countriesDropDown = new Select(driver.findElement(By.id("contient")));
dropdown.selectByVisibleText("Asia");
//or using index of the option starting from 0
dropdown.selectByIndex(1);
//or using its value attribute
dropdown.selectByValue("Asia");

The difference between driver.findElement() and driver.findElements() commands is-

findElement() returns a single WebElement (found first) based on the locator passed as parameter. Whereas findElements() returns a list of WebElements, all satisfying the locator value passed.

Syntax of findElement()-

WebElement textbox = driver.findElement(By.id("textBoxLocator"));

Syntax of findElements()-

List <WebElement> elements = element.findElements(By.id(“value”));

Another difference between the two is- if no element is found then findElement() throws NoSuchElementException whereas findElements() returns a list of 0 elements.

An implicit wait, while finding an element waits for a specified time before throwing NoSuchElementException in case element is not found. The timeout value remains valid throughout the webDriver’s instance and for all the elements.

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

Whereas, Explicit wait is applied to a specified element only-

WebDriverWait wait = new WebDriverWait(driver, 5);  
wait.until(ExpectedConditions.presenceOfElementLocated(ElementLocator));

It is advisable to use explicit waits over implicit waits because higher timeout value of implicit wait set due to an element that takes time to be visible gets applied to all the elements. Thus increasing overall execution time of the script. On the other hand, we can apply different timeouts to different element in case of explicit waits.

Robot API is used for handling Keyboard or mouse events. It is generally used to upload files to the server in selenium automation.

Robot robot = new Robot();
//Simulate enter key action
robot.keyPress(KeyEvent.VK_ENTER);

File upload action can be performed in multiple ways-

Using element.sendKeys(“path of file”) on the webElement of input tag and type file i.e. the elements should be like –

<input type="file" name="fileUpload">
  • Using Robot API.
  • Using AutoIT API.

JavaScript can be executed in selenium using JavaScriptExecuter. Sample code for javascript execution-

WebDriver driver = new FireFoxDriver();
if (driver instanceof JavascriptExecutor) {
 ((JavascriptExecutor)driver).executeScript("{JavaScript Code}");
}

HtmlUnitDriver is the fastest WebDriver. Unlike other drivers (FireFoxDriver, ChromeDriver etc), the HtmlUnitDriver is non-GUI, while running no browser gets launched.

  • Teleric Test Studio, Developed by Teleric.
  • TestingWhiz
  • HPE Unified Functional Testing (HP – UFT formerly QTP)
  • Tosca Testsuite
  • Watir
  • Quick Test Professional, provided by HP.
  • Rational Robot, provided by IBM.
  • Coded UI, provided by Microsoft.
  • Selenium, open source.
  • Auto It, Open Source.
  • Load Runner, provided by Hp.
  • JMeter, provided by Apache.
  • Burp Suite, provided by PortSwigger.
  • Acunetix, provided by Acunetix.

Different types of testing’s that we can achieve through Selenium are.

  • Functional Testing
  • Regression Testing
  • Sanity Testing
  • Smoke Testing
  • Responsive Testing
  • Cross Browser Testing
  • UI testing (black box)
  • Integration Testing

The assertion is used as a verification point. It verifies that the state of the application conforms to what is expected. The types of assertion are “assert”, “verify” and “waitFor”.

JUnit annotations which can be used are:

  • Test
  • Before
  • After
  • Ignore
  • BeforeClass
  • AfterClass
  • RunWith

“type” command is used to type keyboard key values into the text box of software web application. It can also be used for selecting values of combo box whereas “typeAndWait” command is used when your typing is completed and software web page start reloading. This command will wait for software application page to reload. If there is no page reload event on typing, you have to use a simple “type” command.

public class FirefoxBrowserLaunchDemo {  
public static void main(String[] args) {  
//Creating a driver object referencing WebDriver interface  
WebDriver driver;  
//Setting webdriver.gecko.driver property  
System.setProperty("webdriver.gecko.driver", pathToGeckoDriver + "\\geckodriver.exe");  
//Instantiating driver object and launching browser  
driver = newFirefoxDriver();  
//Using get() method to open a webpage  
driver.get("http://kodnest.com");  
//Closing the browser  
driver.quit();  
    }    
}
public class ChromeBrowserLaunchDemo {  
public static void main(String[] args) {  
//Creating a driver object referencing WebDriver interface  
WebDriver driver;  
//Setting the webdriver.chrome.driver property to its executable's location  
System.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromedriver.exe");  
//Instantiating driver object  
driver = newChromeDriver();  
//Using get() method to open a webpage  
driver.get("http://kodnest.com");  
//Closing the browser  
driver.quit();  
    }  
}
public class IEBrowserLaunchDemo {  
public static void main(String[] args) {    
//Creating a driver object referencing WebDriver interface  
WebDriver driver;
//Setting the webdriver.ie.driver property to its executable's location  
System.setProperty("webdriver.ie.driver", "/lib/IEDriverServer/IEDriverServer.exe");  
//Instantiating driver object  
driver = newInternetExplorerDriver();  
//Using get() method to open a webpage  
driver.get("http://kodnest.com");  
//Closing the browser  
driver.quit();  
    }    
}

There are multiple ways of refreshing a page in Webdriver.

1. Using driver.navigate command –

driver.navigate().refresh();  

2. Using driver.getCurrentUrl() with driver.get() command –

driver.get(driver.getCurrentUrl());  

3. Using driver.getCurrentUrl() with driver.navigate() command –

driver.navigate().to(driver.getCurrentUrl());  

4. Pressing an F5 key on any textbox using the sendKeys command –

driver.findElement(By textboxLocator).sendKeys(Keys.F5); 

5. Passing ascii value of the F5 key, i.e., “\uE035” using the sendKeys command –

driver.findElement(By textboxLocator).sendKeys("\uE035");

No, captcha and barcode reader cannot be automated.

HtmlUnitDriver is the fastest WebDriver. Unlike other drivers (FireFoxDriver, ChromeDriver etc), the HtmlUnitDriver is non-GUI, while running no browser gets launched.

Using javaScript executor we can handle hidden elements-

(JavascriptExecutor(driver)) .executeScript("document.getElementsByClassName(ElementLocator).click();")

Start points indicate the point from where the execution should begin. They can be used to run a test script from a breakpoint or the middle of the code.

Breakpoints are used to stop the execution of code. They help you verify that your code is working as expected. 

Page Object Model(POM) is a design pattern in selenium. A design pattern is a solution or a set of standards that are used for solving commonly occuring software problems.

Now coming to POM – POM helps to create a framework for maintaining selenium scripts. In POM for each page of the application a class is created having the web elements belonging to the page and methods handling the events in that page. The test scripts are maintained in seperate files and the methods of the page object files are called from the test scripts file.

The advantages are POM are-

  • Using POM we can create an Object Repository, a set of web elements in seperate files along with their associated functions. Thereby keeping code clean.
  • For any change in UI(or web elements) only page object files are required to be updated leaving test files unchanged.
  • It makes code reusable and maintable.

An object repository is centralized location of all the object or WebElements of the test scripts. In selenium we can create object repository using Page Object Model and Page Factory design patterns.

Page factory is an implementation of Page Object Model in selenium. It provides @FindBy annotation to find web elements and PageFactory.initElements() method to initialize all web elements defined with @FindBy annotation.

public class SamplePage {
WebDriver driver
@FindBy(id="search")
WebElement searchTextBox;
@FindBy(name="searchBtn")
WebElement searchButton;
//Constructor
public samplePage(WebDriver driver){
this.driver = driver;
//initElements method to initialize all elements
PageFactory.initElements(driver, this);
}
//Sample method
public void search(String searchTerm) {
searchTextBox.sendKeys(searchTerm);
searchButtton.click();
}
}

A keyword driven framework is one in which the actions are associated with keywords and kept in external files e.g. an action of launching a browser will be associated with keyword – launchBrowser(), action to write in a textbox with keyword – writeInTextBox(webElement, textToWrite) etc. The code to perform the action based on a keyword specified in external file is implemented in the framework itself.

In this way the test steps can be written in a file by even a person of non-programming background once all the identified actions are implemented.

A data driven framework is one in which the test data is put in external files like csv, excel etc separated from test logic written in test script files. The test data drives the test cases, i.e. the test methods run for each set of test data values. TestNG provides inherent support for data driven testing using @dataProvider annotation.

Selenium grid is a tool that helps in distributed running of test scripts across different machines having different browsers, browser version, platforms etc in parallel. In selenium grid there is hub that is a central server managing all the distributed machines known as nodes.

A hub is server or a central point in selenium grid that controls the test executions on the different machines.

Nodes are the machines which are attached to the selenium grid hub and have selenium instances running the test scripts. Unlike hub there can be multiple nodes in selenium grid.

The advantages of selenium grid are-

  • It allows running test cases in parallel thereby saving test execution time.
  • Multi browser testing is possible using selenium grid by running the test on machines having different browsers.
  • It is allows multi-platform testing by configuring nodes having different operating systems.

TestNG(NG for Next Generation) is a testing framework that can be integrated with selenium or any other automation tool to provide multiple capabilities like assertions, reporting, parallel test execution etc.

testng.xml file is used for configuring the whole test suite. In testng.xml file we can create test suite, create test groups, mark tests for parallel execution, add listeners and pass parameters to test scripts. Later this testng.xml file can be used for triggering the test suite.

Following are the advantages of testNG-

  • TestNG provides different assertions that helps in checking the expected and actual results.
  • It provides parallel execution of test methods.
  • We can define dependency of one test method over other in TestNG.
  • We can assign priority to test methods in selenium.
  • It allows grouping of test methods into test groups.
  • It allows data driven testing using @DataProvider annotation.
  • It has inherent support for reporting.
  • It has support for parameterizing test cases using @Parameters annotation.

Using @DataProvider we can create a data driven framework in which data is passed to the associated test method and multiple iteration of the test runs for the different test data values passed from the @DataProvider method. The method annotated with @DataProvider annotation return a 2D array of object.

//Data provider returning 2D array of 3*2 matrix
@DataProvider(name = "dataProvider1")
public Object[][] dataProviderMethod(){
  return new Object[][]{{"Raj","Mehta"},{"r1","m1"},{"r2","m2"}};
}
//This method is bound to the above data provider returning 2D array of 3*2 matrix
// The test case will run 3 times with different set of values.
@Test(dataProvider = "dataProvider1")

public void sampleTest(String s1, String s2){
   System.out.println(s1+""+s2);
}

Using @Parameter annotation and ‘parameter’ tag in testng.xml we can pass parameters to the test script.

Sample testng.xml –

<suite name="sampleTestSuite">
   <test name="sampleTest">
   <parameter name="sampleParamName" value="sampleParamValue"/>
   <classes>
     <class name="SampleTestFile" />
   </classes>
   </test>
</suite>

Sample test script-

public class SampleTestFile {
@Test
@Parameter("sampleParamName")
public void parameterTest(String paramValue) {
   System.out.println("Value of sampleParamName is - " + sampleParamName);
}

@Factory annotation helps in dynamic execution of test cases. Using @Factory annotation we can pass parameters to the whole test class at run time. The parameters passed can be used by one or more test methods of that class.

Example – there are two classes TestClass and the TestFactory class. Because of the @Factory annotation the test methods in class TestClass will run twice with the data “k1” and “k2”

public class SampleTestClass{
  private String str;
  //Constructor
public TestClass(String str) {
  this.str = str;
}
@Test
public void TestMethod() {
  System.out.println(str);
}
}
public class SampleTestFactory {
  //The test methods in class TestClass will run twice with data "k1" and "k2"
@Factory
public Object[]factoryMethod(){
  return new Object[] {new TestClass("K1"), new TestClass("k2") };
}
}

TestNG provides us different kind of listeners using which we can perform some action in case an event has triggered. Usually testNG listeners are used for configuring reports and logging. One of the most widely used lisetner in testNG is ITestListener interface. It has methods like onTestSuccess, onTestFailure, onTestSkipped etc. We need to implement this interface creating a listener class of our own. After that using the @Listener annotation, we can use specify that for a particular test class, our customized listener class should be used.

@Listeners(PackageName.CustomizedListenerClassName.class)
public class SampleTestClass {
 WebDriver driver= new FirefoxDriver();
 @Test
public void testMethod(){
//testlogic
}
}

@Factory method creates instances of test class and run all the test methods in that class with different set of data.

Whereas, @DataProvider is bound to individual test methods and run the specific methods multiple times.

Using priority parameter in @Test annotation in TestNG we can define priority of test cases. The default priority of test when not specified is integer value 0. Example-

@Test(priority=1)

Using dependsOnMethods parameter inside @Test annotation in testNG we can make one test method run only after successful execution of dependent test method.

@Test(dependsOnMethods = { "preTests" })

Some of the common assertions provided by testNG are-

  • assertEquals(String actual, String expected, String message) – (and other overloaded data type in parameters)
  • assertNotEquals(double data1, double data2, String message) – (and other overloaded data type in parameters)
  • assertFalse(boolean condition, String message)
  • assertTrue(boolean condition, String message)
  • assertNotNull(Object object)
  • fail(boolean condition, String message)
  • true(String message)

The commonly used TestNG annotations are-

  • @Test- @Test annotation marks a method as Test method.
  • @BeforeSuite- The annotated method will run only once before all tests in this suite have run.
  • @AfterSuite-The annotated method will run only once after all tests in this suite have run.
  • @BeforeClass-The annotated method will run only once before the first test method in the current class is invoked.
  • @AfterClass-The annotated method will run only once after all the test methods in the current class have been run.
  • @BeforeTest-The annotated method will run before any test method belonging to the classes inside the <test> tag is run.
  • @AfterTest-The annotated method will run after all the test methods belonging to the classes inside the <test> tag have run.

Log4j is an open source API widely used for logging in Java. It supports multiple levels of logging like – ALL, DEBUG, INFO, WARN, ERROR, TRACE and FATAL.

Apache POI API and JXL(Java Excel API) can be used for reading, writing and updating excel files.

 In order to run the tests in parallel just add these two key value pairs in suite-

  • parallel=”{methods/tests/classes}”
  • thread-count=”{number of thread you want to run simultaneously}”.
<suite name="ArtOfTestingTestSuite" parallel="methods" thread-count="5">

Logging helps in debugging the tests when required and also provides a storage of test’s runtime behaviour.

Using clear() method we can delete the text written in a textbox.

driver.findElement(By.id("elementLocator")).clear();

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 driver.switchTo().window(“{windowHandleName}”) command we can switch control/focus to that particular window.

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

The driver.switchTo() commands can be used for switching to frames.

driver.switchTo().frame("{frameIndex/frameId/frameName}");

For locating a frame we can either use the index (starting from 0), its name or Id.

Desired capabilities are a set of key-value pairs that are used for storing or configuring browser specific properties like its version, platform etc in the browser instances.

All the links are of anchor tag ‘a’. So by locating elements of tagName ‘a’ we can find all the links on a webpage.

List<WebElement> links = driver.findElements(By.tagName("a"));

Using profiles in firefox we can handle accept the SSL untrusted connection certificate. Profiles are basically set of user preferences stored in a file.

FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true); 
profile.setAssumeUntrustedCertificateIssuer(false);
WebDriver driver = new FirefoxDriver(profile);

Sikuli is a tool that uses “Visual Image Match” method to automate graphical user interface. All the web elements in Sikuli should be taken as an image and stored inside the project.

Sikuli is comprised of

  • Sikuli Script
  • Visual Scripting API for Jython
  • Sikuli IDE

Practical uses of Sikuli is that

  • It can be used to automate flash websites or objects
  • It can automate window based application and anything you see on screen without using internal API support
  • It provides simple API
  • It can be easily linked with tools like Selenium
  • Desktop application can be automated
  • Sikuli offers extensive support to automate flash objects
  • To automate desktop, it uses powerful “Visual Match” and Flash objects
  • It can work on any technology-.NET, Java.
SikuliSelenium
It provides extensive support to automate flash objectsIt has simple APIIt uses a visual match to find elements on the screen. So, we can automate anything we see on the screenIt can automate the web as well as windows applicationIt cannot automate flash objects like video player, audio player,It has got complicated APIIt does not have visual matchIt can automate only web applications

The navigation commands are as follows.

navigate().back()

The above command needs no parameters and takes back the user to the previous webpage.

Example

  1. driver.navigate().back();  

navigate().forward()

The above command allows the user to navigate to the next web page with reference to the browser’s history.

Example

  1. driver.navigate().forward();  

navigate().refresh()

The navigate().refresh() command allows the user to refresh the current web page by reloading all the web elements.

Example

  1. driver.navigate().refresh();  

navigate().to()

The navigate().to() command allows the user to launch a new web browser window and navigate to the specified URL.

Example

  1. driver.navigate().to(“https://google.com”);  
import org.junit.After;  
import org.junit.Before;  
import org.junit.Test;  
import java.io.File;  
import java.io.IOException;  
import org.apache.commons.io.FileUtils;  
import org.openqa.selenium.OutputType;  
import org.openqa.selenium.TakesScreenshot;  
import org.openqa.selenium.WebDriver;  
import org.openqa.selenium.firefox.FirefoxDriver;  
public class TakeScreenshot {  
WebDriver drv;  
@Before  
public void setUp() throws Exception {  
driver = new FirefoxDriver();  
drv.get("https://google.com");  
} 
@After  
public void tearDown() throws Exception {  
drv.quit();  
}  
@Test  
public void test() throws IOException {  
//capture the screenshot  
File scrFile = ((TakeScreenshot)drv).getScreenshotAs(OutputType.FILE);  
// paste the screenshot in the desired location  
FileUtils.copyFile(scrFile, new File("C:\\Screenshot\\screen.png"))  
}  
}

Test data can efficiently be read from excel using JXL or POI API. POI API has many advantages than JXL.

1.Bitmap comparison is not possible using Selenium WebDriver

2. Automating Captcha is not possible using Selenium WebDriver

3. We can not read bar code using Selenium WebDriver

Automation Testing

1. What is Automation Testing ?

Automation testing is a type of testing that involves automated test case execution using an automation tool.

The tester writes test scripts and then run the test scripts either on demand or schedule them for periodic run. This reduces the overall test execution time, thus helping in faster product releases.

2. Why Automation Testing ?

  • Automation testing improves efficiency of testing.
  • Reduced testing efforts and costs.
  • Testing can be replicated across different platforms.
  • Gives accurate results.
  • Usually used for large applications with stringent deadlines.

3. When we do Automation Testing ?

Automation in preferred in following cases

  • Repetitive Tasks
  • Smoke and Sanity Tests
  • Test with multiple data set
  • Regression test cases

4. Are You Familiar With Selenium? If Yes, What Is It?

Selenium is a robust test automation suite that is used for automating web based applications. It supports multiple browsers, programming languages and platforms.

5. What are the steps involved in Automation Testing ?

In the automation process, steps involved are

  • Selecting the Test tool
  • Define scope of automation
  • Planning, design and development
  • Test execution
  • Maintenance

6. List down different forms of selenium ?

Selenium comes in four forms-

  • Selenium WebDriver – Selenium WebDriver is used to automate web applications using browser’s native methods.
  • Selenium IDE – A firefox plugin that works on record and play back principle.
  • Selenium RC – Selenium Remote Control(RC) is officially deprecated by selenium and it used to work on javascript to automate the web applications.
  • Selenium Grid – Allows selenium tests to run in parallel across multiple machines.

7. Advantages of selenium.

Following are the advantages of Selenium-

  • Selenium is open source and free to use without any licensing cost.
  • It supports multiple languages like Java, ruby, python etc.
  • It supports multi browser testing.
  • It has good amount of resources and helping community over the internet.
  • Using selenium IDE component, non-programmers can also write automation scripts
  • Using selenium grid component, distributed testing can be carried out on remote machines possible.

8. Disadvantage of selenium

  • We cannot test desktop application using Selenium.
  • We cannot test web services using Selenium.
  • For creating robust scripts in Selenium Webdriver, programming langauge knowledge is required.
  • We have to rely on external libraries and tools for performing tasks like – logging(log4J), testing framework-(testNG, JUnit), reading from external files(POI for excels) etc.

9. List down some of the automatin Testing tools ?

Besides Selenium, there is Load Runner, Sahi, Silk Test, QTP, Jmeter, WinRunner, etc.

10. List down various ways of locating am element in Selenium ?

The different locators in Selenium are-

  • Id
  • XPath
  • cssSelector
  • className
  • tagName
  • name
  • linkText
  • partialLinkText

11. What is Xpath ?

Xpath or XML path is a query language for selecting nodes from XML documents. XPath is one of the locators supported by Selenium Webdriver.

12. What is an absolute Xpath ?

An absolute XPath is a way of locating an element using an XML expression beginning from root node i.e. html node in case of web pages. The main disadvantage of absolute xpath is that even with slightest change in the UI or any element the whole absolute XPath fails.

Example - html/body/div/div[2]/div/div/div/div[1]/div/input

13. What is relative Xpath ?

A relative XPath is a way of locating an element using an XML expression beginning from anywhere in the HTML document. There are different ways of creating relative XPaths which are used for creating robust XPaths (unaffected by changes in other UI elements).

Example - //input[@id='username']

14. How can we move to nth child element using Xpath ?

There are two ways of navigating to the nth element using XPath-

Using square brackets with index position-

Example - div[2] will find the second div element.

Using position()-

Example - div[position()=3] will find the third div element. 

15. Syntax for finding element by class using CSS Selector ?

By .className we can select all the element belonging to a particluar class e.g. ‘.red’ will select all elements having class ‘red’.

16. Syntax for finding element by id using CSS selector ?

By #idValue we can select all the element belonging to a particluar class e.g. ‘#userId’ will select the element having id – userId.

17. What is fundamental difference between XPath and css selector?

The fundamental difference between XPath and css selector is using XPaths we can traverse up in the document i.e. we can move to parent elements. Whereas using CSS selector we can only move downwards in the document.

18. How can we launch different browsers in Selenium webdriver ?

By creating an instance of driver of a particular browser-WebDriver driver = new FirefoxDriver();

19. What is Selenium Webdriver ?

The Selenium WebDriver is used for automating tests for websites.

20. Difference between Selenium IDE, Selenium RC and WebDriver ?

  Feature   Selenium IDE   Selenium RC   WebDriver
Browser Compatibility Selenium IDE comes as a Firefox plugin, thus it supports only Firefox Selenium RC supports a varied range of versions of Mozilla Firefox, Google Chrome, Internet Explorer and Opera. WebDriver supports a varied range of versions of Mozilla Firefox, Google Chrome, Internet Explorer and Opera.Also supports HtmlUnitDriver which is a GUI less or headless browser.
Record and Playback Selenium IDE supports record and playback feature Selenium RC doesn’t supports record and playback feature. WebDriver doesn’t support record and playback feature
Server Requirement Selenium IDE doesn’t require any server to be started before executing the test scripts Selenium RC requires server to be started before executing the test scripts. WebDriver doesn’t require any server to be started before executing the test scripts
Architecture Selenium IDE is a Javascript based framework Selenium RC is a JavaScript based Framework. WebDriver uses the browser’s native compatibility to automation
Object Oriented Selenium IDE is not an object oriented tool Selenium RC is semi object oriented tool. WebDriver is a purely object oriented tool
Dynamic Finders(for locating web elements on a webpage) Selenium IDE doesn’t support dynamic finders Selenium RC doesn’t support dynamic finders. WebDriver supports dynamic finders
Handling Alerts, Navigations, Dropdowns Selenium IDE doesn’t explicitly provides aids to handle alerts, navigations, dropdowns Selenium RC doesn’t explicitly provides aids to handle alerts, navigations, dropdowns. WebDriver offers a wide range of utilities and classes that helps in handling alerts, navigations, and dropdowns efficiently and effectively.
WAP (iPhone/Android) Testing Selenium IDE doesn’t support testing of iPhone/Andriod applications Selenium RC doesn’t support testing of iPhone/Android applications. WebDriver is designed in a way to efficiently support testing of iPhone/Android applications. The tool comes with a large range of drivers for WAP based testing.For example, AndroidDriver, iPhoneDriver
Listener Support Selenium IDE doesn’t support listeners Selenium RC doesn’t support listeners. WebDriver supports the implementation of Listeners
Speed Selenium IDE is fast as it is plugged in with the web-browser that launches the test. Thus, the IDE and browser communicates directly Selenium RC is slower than WebDriver as it doesn’t communicates directly with the browser; rather it sends selenese commands over to Selenium Core which in turn communicates with the browser. WebDriver communicates directly with the web browsers. Thus making it much faster.

21. When should we use Selenium IDE ?

Selenium IDE is the simplest and easiest of all the tools within the Selenium Package. Its record and playback feature makes it exceptionally easy to learn with minimal acquaintances to any programming language. Selenium IDE is an ideal tool for a naïve user.

22. What is Selenese ?

Selenese is the language which is used to write test scripts in Selenium IDE.

23.Difference between “/” and “//” in Xpath ?

Single Slash “/” – Single slash is used to create Xpath with absolute path i.e. the xpath would be created to start selection from the document node/start node.

Double Slash “//” – Double slash is used to create Xpath with relative path i.e. the xpath would be created to start selection from anywhere within the document.

24. When should we use Selenium Grid ?

Selenium Grid can be used to execute same or different test scripts on multiple platforms and browsers concurrently so as to achieve distributed test execution, testing under different environments and saving execution time remarkably.

25. How do I launch the browser using WebDriver ?

The following syntax can be used to launch Browser:

WebDriver driver = new FirefoxDriver();
WebDriver driver = new ChromeDriver();WebDriver driver = new InternetExplorerDriver();

26. What are different types of waits available in Webdriver ?

The different drivers available in WebDriver are:

  • FirefoxDriver
  • InternetExplorerDriver
  • ChromeDriver
  • SafariDriver
  • OperaDriver
  • AndroidDriver
  • IPhoneDriver
  • HtmlUnitDriver

27. What are the different types of waits available in WebDriver ?

There are two types of waits available in WebDriver:

  1. Implicit Wait
  2. Explicit Wait

Implicit Wait: Implicit waits are used to provide a default waiting time (say 30 seconds) between each consecutive test step/command across the entire test script. Thus, the subsequent test step would only execute when the 30 seconds have elapsed after executing the previous test step/command.

Explicit Wait: Explicit waits are used to halt the execution till the time a particular condition is met or the maximum time has elapsed. Unlike Implicit waits, explicit waits are applied for a particular instance only.

28. How to type in a textbox using selenium ?

The user can use sendKeys(“String to be entered”) to enter the string in the textbox.

Syntax:

WebElement username = drv.findElement(By.id(“Email”));
// entering username
username.sendKeys(“sth”);

29.How can we get a text of a web element ?

Get command is used to retrieve the inner text of the specified web element. The command doesn’t require any parameter but returns a string value. It is also one of the extensively used commands for verification of messages, labels, errors etc displayed on the web pages.

Syntax:

String Text = driver.findElement(By.id(“Text”)).getText();

30. How to select value in a dropdown?

The value in the dropdown can be selected using WebDriver’s Select class.

Syntax:

selectByValue:

Select selectByValue = new Select(driver.findElement(By.id(“SelectID_One”)));
selectByValue.selectByValue(“bluevalue”);

selectByVisibleText:

Select selectByVisibleText = new Select (driver.findElement(By.id(“SelectID_Two”)));
selectByVisibleText.selectByVisibleText(“Cyan”);

selectByIndex:

Select selectByIndex = new Select(driver.findElement(By.id(“SelectID_Three”)));
selectByIndex.selectByIndex(2);

31. What are the different types of navigation commands?

Following are the navigation commands:

navigate().back() – The above command requires no parameters and takes back the user to the previous webpage in the web browser’s history.

Sample code:

driver.navigate().back();

navigate().forward() – This command lets the user to navigate to the next web page with reference to the browser’s history.

Sample code:

driver.navigate().forward();

navigate().refresh() – This command lets the user to refresh the current web page there by reloading all the web elements.

Sample code:

driver.navigate().refresh();

navigate().to() – This command lets the user to launch a new web browser window and navigate to the specified URL.

Sample code:

driver.navigate().to(“https://google.com”);

32. When do we use findElement() and findElements()?

findElement(): findElement() is used to find the first element in the current web page matching to the specified locator value. Take a note that only first matching element would be fetched.

Syntax:

WebElement element = driver.findElements(By.xpath(“//div[@id='example']//ul//li”));

findElements(): findElements() is used to find all the elements in the current web page matching to the specified locator value. Take a note that all the matching elements would be fetched and stored in the list of WebElements.

Syntax:

List <WebElement> elementList = driver.findElements(By.xpath(“//div[@id='example']//ul//li”));

33. What is the difference between driver.close() and driver.quit command?

close(): WebDriver’s close() method closes the web browser window that the user is currently working on or we can also say the window that is being currently accessed by the WebDriver. The command neither requires any parameter nor does it return any value.

quit(): Unlike close() method, quit() method closes down all the windows that the program has opened. Same as close() method, the command neither requires any parameter nor does is return any value.

34.What is Junit?

Junit is a unit testing framework introduced by Apache. Junit is based on Java.

35. What are Junit annotations?

Following are the JUnit Annotations:

  • @Test: Annotation lets the system know that the method annotated as @Test is a test method. There can be multiple test methods in a single test script.
  • @Before: Method annotated as @Before lets the system know that this method shall be executed every time before each of the test methods.
  • @After: Method annotated as @After lets the system know that this method shall be executed every time after each of the test method.
  • @BeforeClass: Method annotated as @BeforeClass lets the system know that this method shall be executed once before any of the test methods.
  • @AfterClass: Method annotated as @AfterClass lets the system know that this method shall be executed once after any of the test methods.
  • @Ignore: Method annotated as @Ignore lets the system know that this method shall not be executed.

36. What is fluent wait in selenium?

A fluent wait is a type of wait in which we can also specify polling interval(intervals after which driver will try to find the element) along with the maximum timeout value.

37. What are different keyboard operations that can be performed in selenium ?

The different keyboard operations that can be performed in selenium are-

  • .sendKeys(“sequence of characters”) – Used for passing character sequence to an input or textbox element.
  • .pressKey(“non-text keys”) – Used for keys like control, function keys etc that are non-text.
  • .releaseKey(“non-text keys”) – Used in conjuntion with keypress event to simulate releasing a key from keyboard event.

38. List down mouse actions that can be performed in selenium.

The different mouse events supported in selenium are

  • click(WebElement element)
  • doubleClick(WebElement element)
  • contextClick(WebElement element)
  • mouseDown(WebElement element)
  • mouseUp(WebElement element)
  • mouseMove(WebElement element)
  • mouseMove(WebElement element, long xOffset, long yOffset)

39. How to fetch title of the page in selenium ?

Using driver.getTitle(); we can fetch the page title in selenium. This method returns a string containing the title of the webpage.

40. How to get page source in selenium  ?

Using driver.getPageSource(); we can fetch the page source in selenium. This method returns a string containing the page source.

41. List down some commonly encountered exception in selenium ?

Some of the commonly seen exception in selenium are-

  • NoSuchElementException – When no element could be located from the locator provided.
  • ElementNotVisibleException – When element is present in the dom but is not visible.
  • NoAlertPresentException – When we try to switch to an alert but the targetted alert is not present.
  • NoSuchFrameException – When we try to switch to a frame but the targetted frame is not present.
  • NoSuchWindowException – When we try to switch to a window but the targetted window is not present.
  • UnexpectedAlertPresentException – When an unexpected alert blocks normal interaction of the driver.
  • TimeoutException – When a command execution gets timeout.
  • InvalidElementStateException – When the state of an element is not appropriate for the desired action.
  • NoSuchAttributeException – When we are trying to fetch an attribute’s value but the attribute is not correct
  • WebDriverException – When there is some issue with driver instance preventing it from getting launched.

42. How to handle dropdown in selenium ?

Using Select class-
Select countriesDropDown = new Select(driver.findElement(By.id("contient")));
dropdown.selectByVisibleText("Asia");
//or using index of the option starting from 0
dropdown.selectByIndex(1);
//or using its value attribute
dropdown.selectByValue("Asia");

43. Difference between driver.findElement() and driver.findElements() commands ?

The difference between driver.findElement() and driver.findElements() commands is-

findElement() returns a single WebElement (found first) based on the locator passed as parameter. Whereas findElements() returns a list of WebElements, all satisfying the locator value passed.

Syntax of findElement()-

WebElement textbox = driver.findElement(By.id("textBoxLocator"));

Syntax of findElements()-

List <WebElement> elements = element.findElements(By.id(“value”));

Another difference between the two is- if no element is found then findElement() throws NoSuchElementException whereas findElements() returns a list of 0 elements.

44. Difference between implicit wait and explicit wait ?

An implicit wait, while finding an element waits for a specified time before throwing NoSuchElementException in case element is not found. The timeout value remains valid throughout the webDriver’s instance and for all the elements.

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

Whereas, Explicit wait is applied to a specified element only-

WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(ElementLocator));

It is advisable to use explicit waits over implicit waits because higher timeout value of implicit wait set due to an element that takes time to be visible gets applied to all the elements. Thus increasing overall execution time of the script. On the other hand, we can apply different timeouts to different element in case of explicit waits.

 45. What is Robot API ?

Robot API is used for handling Keyboard or mouse events. It is generally used to upload files to the server in selenium automation.

Robot robot = new Robot();
//Simulate enter key action
robot.keyPress(KeyEvent.VK_ENTER);

46. How can we upload a file in selenium ?

File upload action can be performed in multiple ways-

Using element.sendKeys(“path of file”) on the webElement of input tag and type file i.e. the elements should be like –

<input type="file" name="fileUpload">
  • Using Robot API.
  • Using AutoIT API.

47. How to execute javascript in selenium ?

JavaScript can be executed in selenium using JavaScriptExecuter. Sample code for javascript execution-

WebDriver driver = new FireFoxDriver();
if (driver instanceof JavascriptExecutor) {
 ((JavascriptExecutor)driver).executeScript("{JavaScript Code}");
}

48. What is HTMLUnit Driver ?

HtmlUnitDriver is the fastest WebDriver. Unlike other drivers (FireFoxDriver, ChromeDriver etc), the HtmlUnitDriver is non-GUI, while running no browser gets launched.

49. List down some Automation testing tools that are used for Functional Automation.

  • Teleric Test Studio, Developed by Teleric.
  • TestingWhiz
  • HPE Unified Functional Testing (HP – UFT formerly QTP)
  • Tosca Testsuite
  • Watir
  • Quick Test Professional, provided by HP.
  • Rational Robot, provided by IBM.
  • Coded UI, provided by Microsoft.
  • Selenium, open source.
  • Auto It, Open Source.

50. List down some Automation testing tools that are used for Non- Functional Automation.

  • Load Runner, provided by Hp.
  • JMeter, provided by Apache.
  • Burp Suite, provided by PortSwigger.
  • Acunetix, provided by Acunetix.

51. Which are the test type supported by selenium.

Different types of testing’s that we can achieve through Selenium are.

  • Functional Testing
  • Regression Testing
  • Sanity Testing
  • Smoke Testing
  • Responsive Testing
  • Cross Browser Testing
  • UI testing (black box)
  • Integration Testing

52. What is assertion in Selenium ?

The assertion is used as a verification point. It verifies that the state of the application conforms to what is expected. The types of assertion are “assert”, “verify” and “waitFor”.

53. List down types of annotations used in Selenium ?

JUnit annotations which can be used are:

  • Test
  • Before
  • After
  • Ignore
  • BeforeClass
  • AfterClass
  • RunWith

54. Difference between “type” and “typeAndWait” command.

“type” command is used to type keyboard key values into the text box of software web application. It can also be used for selecting values of combo box whereas “typeAndWait” command is used when your typing is completed and software web page start reloading. This command will wait for software application page to reload. If there is no page reload event on typing, you have to use a simple “type” command.

55. Program to launch Firefox browser in WebDriver.

public class FirefoxBrowserLaunchDemo {
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Setting webdriver.gecko.driver property
System.setProperty("webdriver.gecko.driver", pathToGeckoDriver + "\\geckodriver.exe");
//Instantiating driver object and launching browser
driver = newFirefoxDriver();
//Using get() method to open a webpage
driver.get("https://kodnest.com");
//Closing the browser
driver.quit();
    }
}

56. Program to launch Chrome browser in WebDriver

public class ChromeBrowserLaunchDemo {
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Setting the webdriver.chrome.driver property to its executable's location
System.setProperty("webdriver.chrome.driver", "/lib/chromeDriver/chromedriver.exe");
//Instantiating driver object
driver = newChromeDriver();
//Using get() method to open a webpage
driver.get("https://kodnest.com");
//Closing the browser
driver.quit();
    }
}

57. Program to launch Internet Explorer browser on WebDriver

public class IEBrowserLaunchDemo {
public static void main(String[] args) {
//Creating a driver object referencing WebDriver interface
WebDriver driver;
//Setting the webdriver.ie.driver property to its executable's location
System.setProperty("webdriver.ie.driver", "/lib/IEDriverServer/IEDriverServer.exe");
//Instantiating driver object
driver = newInternetExplorerDriver();
//Using get() method to open a webpage
driver.get("https://kodnest.com");
//Closing the browser
driver.quit();
    }
}

58. List down different ways to refresh a webpage in WebDriver.

There are multiple ways of refreshing a page in Webdriver.

1. Using driver.navigate command –

driver.navigate().refresh();  

2. Using driver.getCurrentUrl() with driver.get() command –

driver.get(driver.getCurrentUrl());  

3. Using driver.getCurrentUrl() with driver.navigate() command –

driver.navigate().to(driver.getCurrentUrl());  

4. Pressing an F5 key on any textbox using the sendKeys command –

driver.findElement(By textboxLocator).sendKeys(Keys.F5); 

5. Passing ascii value of the F5 key, i.e., “\uE035” using the sendKeys command –

driver.findElement(By textboxLocator).sendKeys("\uE035");

59. Can captcha be automated ?

No, captcha and barcode reader cannot be automated.

60. What is HtmlUnitDriver ?

HtmlUnitDriver is the fastest WebDriver. Unlike other drivers (FireFoxDriver, ChromeDriver etc), the HtmlUnitDriver is non-GUI, while running no browser gets launched.

61. How to handle hidden elements in Selenium WebDriver ?

Using javaScript executor we can handle hidden elements-

(JavascriptExecutor(driver)) .executeScript("document.getElementsByClassName(ElementLocator).click();");

62. What are start points and break points in Selenium ?

Start points indicate the point from where the execution should begin. They can be used to run a test script from a breakpoint or the middle of the code.

Breakpoints are used to stop the execution of code. They help you verify that your code is working as expected. 

63. What is Page Object Model or  POM ?

Page Object Model(POM) is a design pattern in selenium. A design pattern is a solution or a set of standards that are used for solving commonly occuring software problems.

Now coming to POM – POM helps to create a framework for maintaining selenium scripts. In POM for each page of the application a class is created having the web elements belonging to the page and methods handling the events in that page. The test scripts are maintained in seperate files and the methods of the page object files are called from the test scripts file.

64. Advantages of POM

The advantages are POM are-

  • Using POM we can create an Object Repository, a set of web elements in seperate files along with their associated functions. Thereby keeping code clean.
  • For any change in UI(or web elements) only page object files are required to be updated leaving test files unchanged.
  • It makes code reusable and maintable.

65. What is Object repository ?

An object repository is centralized location of all the object or WebElements of the test scripts. In selenium we can create object repository using Page Object Model and Page Factory design patterns.

66. What is Page Factory ?

Page factory is an implementation of Page Object Model in selenium. It provides @FindBy annotation to find web elements and PageFactory.initElements() method to initialize all web elements defined with @FindBy annotation.

public class SamplePage {
WebDriver driver
@FindBy(id="search")
WebElement searchTextBox;
@FindBy(name="searchBtn")
WebElement searchButton;
//Constructor
public samplePage(WebDriver driver){
this.driver = driver;
//initElements method to initialize all elements
PageFactory.initElements(driver, this);
}
//Sample method
public void search(String searchTerm) {
searchTextBox.sendKeys(searchTerm);
searchButtton.click();
}
}

67. What is keyword driven framework ?

A keyword driven framework is one in which the actions are associated with keywords and kept in external files e.g. an action of launching a browser will be associated with keyword – launchBrowser(), action to write in a textbox with keyword – writeInTextBox(webElement, textToWrite) etc. The code to perform the action based on a keyword specified in external file is implemented in the framework itself.

In this way the test steps can be written in a file by even a person of non-programming background once all the identified actions are implemented.

68. What is data driven framework ?

A data driven framework is one in which the test data is put in external files like csv, excel etc separated from test logic written in test script files. The test data drives the test cases, i.e. the test methods run for each set of test data values. TestNG provides inherent support for data driven testing using @dataProvider annotation.

69. What is selenium grid ?

Selenium grid is a tool that helps in distributed running of test scripts across different machines having different browsers, browser version, platforms etc in parallel. In selenium grid there is hub that is a central server managing all the distributed machines known as nodes.

70. What do you mean by hub in selenium grid ?

A hub is server or a central point in selenium grid that controls the test executions on the different machines.

71. What do you mean by node in selenium grid ?

Nodes are the machines which are attached to the selenium grid hub and have selenium instances running the test scripts. Unlike hub there can be multiple nodes in selenium grid.

72. List down some of the advantage of selenium grid ?

The advantages of selenium grid are-

  • It allows running test cases in parallel thereby saving test execution time.
  • Multi browser testing is possible using selenium grid by running the test on machines having different browsers.
  • It is allows multi-platform testing by configuring nodes having different operating systems.

73. What is TestNG ?

TestNG(NG for Next Generation) is a testing framework that can be integrated with selenium or any other automation tool to provide multiple capabilities like assertions, reporting, parallel test execution etc.

74. What is the use of testing.xml file ?

testng.xml file is used for configuring the whole test suite. In testng.xml file we can create test suite, create test groups, mark tests for parallel execution, add listeners and pass parameters to test scripts. Later this testng.xml file can be used for triggering the test suite.

75. List down some advantages of testNG ?

Following are the advantages of testNG-

  • TestNG provides different assertions that helps in checking the expected and actual results.
  • It provides parallel execution of test methods.
  • We can define dependency of one test method over other in TestNG.
  • We can assign priority to test methods in selenium.
  • It allows grouping of test methods into test groups.
  • It allows data driven testing using @DataProvider annotation.
  • It has inherent support for reporting.
  • It has support for parameterizing test cases using @Parameters annotation.

76. How to create data driven framework using testNG ?

Using @DataProvider we can create a data driven framework in which data is passed to the associated test method and multiple iteration of the test runs for the different test data values passed from the @DataProvider method. The method annotated with @DataProvider annotation return a 2D array of object.

//Data provider returning 2D array of 3*2 matrix
@DataProvider(name = "dataProvider1")
public Object[][] dataProviderMethod(){
  return new Object[][]{{"Raj","Mehta"},{"r1","m1"},{"r2","m2"}};
}
//This method is bound to the above data provider returning 2D array of 3*2 matrix
// The test case will run 3 times with different set of values.
@Test(dataProvider = "dataProvider1")
public void sampleTest(String s1, String s2){
   System.out.println(s1+""+s2);
}

77. How to pass parameter to rest script using testNG ?

Using @Parameter annotation and ‘parameter’ tag in testng.xml we can pass parameters to the test script.

Sample testng.xml –

<suite name="sampleTestSuite">
   <test name="sampleTest">
   <parameter name="sampleParamName" value="sampleParamValue"/>
   <classes>
     <class name="SampleTestFile" />
   </classes>
   </test>
</suite>

Sample test script-

public class SampleTestFile {
@Test
@Parameter("sampleParamName")
public void parameterTest(String paramValue) {
   System.out.println("Value of sampleParamName is - " + sampleParamName);
}

78. Explain the use of @Factory annotation in TestNG ?

@Factory annotation helps in dynamic execution of test cases. Using @Factory annotation we can pass parameters to the whole test class at run time. The parameters passed can be used by one or more test methods of that class.

Example – there are two classes TestClass and the TestFactory class. Because of the @Factory annotation the test methods in class TestClass will run twice with the data “k1” and “k2”

public class SampleTestClass{
  private String str;
  //Constructor
public TestClass(String str) {
  this.str = str;
}
@Test
public void TestMethod() {
  System.out.println(str);
}
}
public class SampleTestFactory {
  //The test methods in class TestClass will run twice with data "k1" and "k2"
@Factory
public Object[]factoryMethod(){
  return new Object[] {new TestClass("K1"), new TestClass("k2") };
}
}

79. Explain the use of @Listener annotation in TestNG ?

TestNG provides us different kind of listeners using which we can perform some action in case an event has triggered. Usually testNG listeners are used for configuring reports and logging. One of the most widely used lisetner in testNG is ITestListener interface. It has methods like onTestSuccess, onTestFailure, onTestSkipped etc. We need to implement this interface creating a listener class of our own. After that using the @Listener annotation, we can use specify that for a particular test class, our customized listener class should be used.

@Listeners(PackageName.CustomizedListenerClassName.class)
public class SampleTestClass {
 WebDriver driver= new FirefoxDriver();
 @Test
public void testMethod(){
//testlogic
}
}

80. Difference between @Factory and @DataProvide annotation ?

@Factory method creates instances of test class and run all the test methods in that class with different set of data.

Whereas, @DataProvider is bound to individual test methods and run the specific methods multiple times.

81. How to set priority of test case in TestNG ?

Using priority parameter in @Test annotation in TestNG we can define priority of test cases. The default priority of test when not specified is integer value 0. Example-

@Test(priority=1)

82. How to make one test method dependent on other using TestNG ?

Using dependsOnMethods parameter inside @Test annotation in testNG we can make one test method run only after successful execution of dependent test method.

@Test(dependsOnMethods = { "preTests" })

83. List down some assertions provided by testNG ?

Some of the common assertions provided by testNG are-

  • assertEquals(String actual, String expected, String message) – (and other overloaded data type in parameters)
  • assertNotEquals(double data1, double data2, String message) – (and other overloaded data type in parameters)
  • assertFalse(boolean condition, String message)
  • assertTrue(boolean condition, String message)
  • assertNotNull(Object object)
  • fail(boolean condition, String message)
  • true(String message)

84. List down some annotations used in testNG ?

The commonly used TestNG annotations are-

  • @Test@Test annotation marks a method as Test method.
  • @BeforeSuite- The annotated method will run only once before all tests in this suite have run.
  • @AfterSuite-The annotated method will run only once after all tests in this suite have run.
  • @BeforeClass-The annotated method will run only once before the first test method in the current class is invoked.
  • @AfterClass-The annotated method will run only once after all the test methods in the current class have been run.
  • @BeforeTest-The annotated method will run before any test method belonging to the classes inside the <test> tag is run.
  • @AfterTest-The annotated method will run after all the test methods belonging to the classes inside the <test> tag have run.

85. Name an API used for logging in Java.

Log4j is an open source API widely used for logging in Java. It supports multiple levels of logging like – ALL, DEBUG, INFO, WARN, ERROR, TRACE and FATAL.

86.Name an API used for reading and writing data to excel files.

Apache POI API and JXL(Java Excel API) can be used for reading, writing and updating excel files.

87. How to run test cases in parallel using textNG ?

 In order to run the tests in parallel just add these two key value pairs in suite-

  • parallel=”{methods/tests/classes}”
  • thread-count=”{number of thread you want to run simultaneously}”.
<suite name="ArtOfTestingTestSuite" parallel="methods" thread-count="5">

88. What is the use of logging in automation.

Logging helps in debugging the tests when required and also provides a storage of test’s runtime behaviour.

89. How to clear a text written in a textbox ?

Using clear() method we can delete the text written in a textbox.

driver.findElement(By.id("elementLocator")).clear();

90. How to switch between multiple windows in selenium ?

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 driver.switchTo().window(“{windowHandleName}”) command we can switch control/focus to that particular window.

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

91. How to move a particular frame in selenium ?

The driver.switchTo() commands can be used for switching to frames.

driver.switchTo().frame("{frameIndex/frameId/frameName}");

For locating a frame we can either use the index (starting from 0), its name or Id.

92. What are DesiredCapabilities in selenium webdriver ?

Desired capabilities are a set of key-value pairs that are used for storing or configuring browser specific properties like its version, platform etc in the browser instances.

93. How to find all the links on a web page ?

All the links are of anchor tag ‘a’. So by locating elements of tagName ‘a’ we can find all the links on a webpage.

List<WebElement> links = driver.findElements(By.tagName("a"));

94. How to handle HTTPS website in selenium? or How to accept the SSL untrusted connection?

Using profiles in firefox we can handle accept the SSL untrusted connection certificate. Profiles are basically set of user preferences stored in a file.

FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
WebDriver driver = new FirefoxDriver(profile);

95. What is Sikuli ?

Sikuli is a tool that uses “Visual Image Match” method to automate graphical user interface. All the web elements in Sikuli should be taken as an image and stored inside the project.

Sikuli is comprised of

  • Sikuli Script
  • Visual Scripting API for Jython
  • Sikuli IDE

Practical uses of Sikuli is that

  • It can be used to automate flash websites or objects
  • It can automate window based application and anything you see on screen without using internal API support
  • It provides simple API
  • It can be easily linked with tools like Selenium
  • Desktop application can be automated
  • Sikuli offers extensive support to automate flash objects
  • To automate desktop, it uses powerful “Visual Match” and Flash objects
  • It can work on any technology-.NET, Java,

96. Difference between Selenium and Sikuli ?

Sikuli Selenium
It provides extensive support to automate flash objectsIt has simple APIIt uses a visual match to find elements on the screen. So, we can automate anything we see on the screenIt can automate the web as well as windows application It cannot automate flash objects like video player, audio player,It has got complicated APIIt does not have visual matchIt can automate only web applications

97. List down types of navigation commands ?

The navigation commands are as follows.

navigate().back()

The above command needs no parameters and takes back the user to the previous webpage.

Example

  1. driver.navigate().back();  

navigate().forward()

The above command allows the user to navigate to the next web page with reference to the browser’s history.

Example

  1. driver.navigate().forward();  

navigate().refresh()

The navigate().refresh() command allows the user to refresh the current web page by reloading all the web elements.

Example

  1. driver.navigate().refresh();  

navigate().to()

The navigate().to() command allows the user to launch a new web browser window and navigate to the specified URL.

Example

  1. driver.navigate().to(“https://google.com”);  

98. Program to capture screenshot in WebDriver ?

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TakeScreenshot {
WebDriver drv;
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
drv.get("https://google.com");
}
@After
public void tearDown() throws Exception {
drv.quit();
}
@Test
public void test() throws IOException {
//capture the screenshot
File scrFile = ((TakeScreenshot)drv).getScreenshotAs(OutputType.FILE);
// paste the screenshot in the desired location
FileUtils.copyFile(scrFile, new File("C:\\Screenshot\\screen.png"))
}
}

99. How to read test data from excels ?

Test data can efficiently be read from excel using JXL or POI API. POI API has many advantages than JXL.

100. List down some case where we cannot use Selenium WebDriver ?

1.Bitmap comparison is not possible using Selenium WebDriver

2. Automating Captcha is not possible using Selenium WebDriver

3. We can not read bar code using Selenium WebDriver

Responses

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

New Report

Close