Interview Questions Selenium
Interview Questions Selenium
Advantages:
2. What is an XPath?
findElement(By.xpath(“Xpath”));
html/body/div[1]/div[2]/div[2]/div[1]/form/div[1]/div/div[1]/div/div/in
put[1]
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/parent node.
2. 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. – Search in a whole page (DOM) for the preceding string.
Syntax: //form/div[1]/div/div[1]/div/div/input[1]
3. Single Attribute:
You could write the syntax in two ways as mentioned below. Including
or excluding HTML Tag. If you want to exclude HTML Tag then you
need to use *
//<HTML tag>[@attribute_name='attribute_value']
Selenium Interview Questions
or
//*[@attribute_name='attribute_value']
4. contains():
It is used to identify an element, when we are familiar with some part
of the attributes value of an element.
//<HTML tag>[contains(@attribute_name,'attribute_value')]
or
//*[contains(@attribute_name,'attribute_value')]
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.
Absolute XPath starts from the root node and ends with desired
descendant element’s node. It starts with top HTML node and ends with
input node. It starts with a single forward slash(/) as shown below.
Syntax: /html/body/div[3]/div[1]/form/table/tbody/tr[1]/td/input
Relative XPath starts from any node in between the HTML page to the
current element’s node(last node of the element). It starts with a double
forward slash(//)
Syntax: //input[@id='email']
Assert:
When an “assert” command fails, the test execution will be aborted. So when
the Assertion fails, all the test steps after that line of code are skipped. The
Selenium Interview Questions
In simple words, if the assert condition is true then the program control will
execute the next test step but if the condition is false, the execution will stop
and further test step will not be executed.
Verify:
When a “verify” command fails, the test will continue executing and logging
the failure. Mostly, the Verify command is used to check non-critical things.
In such cases where we move forward even though the end result of the
check value is failed.
In simple words, there won’t be any halt in the test execution even though
the verify condition is true or false.
if(isElementPresent(By.linkText("login"))){
System.out.println("Login link is present");
} else{
System.out.println("Login link is not present");
}
(OR)
try {
assertTrue(isElementPresent(By.xpath("assert-and-verify")));
} catch (Error e) {
verificationErrors.append(e.toString());
}
package softwareTestingMaterial;
import java.io.File;
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;
import org.testng.annotations.Test;
@Test
driver.manage().window().maximize();
try{
driver.get("https://www.softwaretestingmaterial.com");
driver.navigate().refresh();
//driver.findElement(By.xpath("//*[@id='cse-search-
driver.findElement(By.xpath("//*[@id='cse']")).sendKeys("agile");
}catch(Exception e){
TakesScreenshot ts = (TakesScreenshot)driver;
File source = ts.getScreenshotAs(OutputType.FILE);
Selenium Interview Questions
driver.close();
driver.quit();
a. Implicit Wait:
Implicit waits tell to the WebDriver to wait for a certain amount of time
before it throws an exception. Once we set the time, WebDriver will
wait for the element based on the time we set before it throws an
exception. The default setting is 0 (zero). We need to set some wait
time to make WebDriver to wait for the required time.
Syntax:
driver.manage().timeouts().implicitlyWait(TimeOut,TimeUnit.SECONDS)
b. Explicit Wait:
It is applied on certain element with defined expected condition and time. This wait is
only applied to the specified element. This wait can also throw exception when
element is not found
Selenium Interview Questions
driver.manage().timeouts().Explicity(TimeOut,TimeUnit.SECONDS)
driver.getCurrentUrl();
WebDriver facilitates the user with the following methods to check the visibility of
the web elements. These web elements can be buttons, drop boxes, checkboxes,
radio buttons, labels etc.
a. isDisplayed():
b. isSelected():
c. isEnabled():
actions. MoveToElement(driver.findElement(By.Xpath(“”))).build().Perform();
To handle alerts popups we need to do switch to the alert window and call Selenium
WebDriver Alert API methods.
1. Windows Based
2. Web Based/Browser Based
In order to switch the control to alert pop up, we use the following command :
driver.switchTo().alert();
To Click on OK button:
alert.accept();
alert.dismiss()
alert.getText();
alert.authenticateUsing(Credentials credentials)
By using JavascriptExecutor
Test data can efficiently be read from excel using JXL or POI API
You’d use XSSF if you need to read or write an OOXML Excel file using Java
(XLSX). It has many predefined methods, classes, and interfaces.
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
No, It’s not possible to automate captcha and bar code reader.
driver.switchTo().frame();
25. Read the test data from the Excel cell, in this we are passing parameters as Row num and
Col num
Try{
Cell =excelwsheet.getrow(rowno).getcell(cellNo);
Return celldata;
Catch(Exception e)
Return””;
}
Selenium Interview Questions
26. How to write in the Excel cell, Row num and Col num are the parameters
27. This is to send the PASS value to the Excel sheet in the result column.
ExcelUtils.setCellData("Pass", 1, 3);